Re: Remove sort files - Mailing list pgsql-patches

From Bruce Momjian
Subject Re: Remove sort files
Date
Msg-id 200105230111.f4N1BwN24369@candle.pha.pa.us
Whole thread Raw
In response to Re: Remove sort files  ("Michael Richards" <michael@fastmail.ca>)
Responses Re: Remove sort files
List pgsql-patches
> > The only unusual part is the use of system("find...rm...") to
> > remove the files in pg_sorttemp.  Seemed cleaner than doing fancy
> > directory walking code in C.
>
> Should this figure out and use a path to the find command? Unless
> postgres sets CWD, is it possible as-is to coerce it into rming some
> files you did not intend to have rm'd? I didn't have/spend time
> looking at it with a fine toothcomb, but usually calling find and rm
> without an explicit path is a Bad Thing(tm).

I do use a full path for find:

    find DataDir -...

Here is an updated patch which forces find and rm to come from /bin or
/usr/bin.  Seems safer that way.

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026
Index: configure.in
===================================================================
RCS file: /home/projects/pgsql/cvsroot/pgsql/configure.in,v
retrieving revision 1.127
diff -c -r1.127 configure.in
*** configure.in    2001/05/16 17:24:10    1.127
--- configure.in    2001/05/23 01:09:21
***************
*** 622,627 ****
--- 622,629 ----
  PGAC_PATH_FLEX
  AC_PROG_LN_S
  AC_PROG_LD
+ AC_PROG_FIND
+ AC_PROG_RM
  AC_SUBST(LD)
  AC_SUBST(with_gnu_ld)
  case $host_os in sysv5uw*)
Index: src/backend/postmaster/postmaster.c
===================================================================
RCS file: /home/projects/pgsql/cvsroot/pgsql/src/backend/postmaster/postmaster.c,v
retrieving revision 1.212
diff -c -r1.212 postmaster.c
*** src/backend/postmaster/postmaster.c    2001/04/19 19:09:23    1.212
--- src/backend/postmaster/postmaster.c    2001/05/23 01:09:36
***************
*** 243,248 ****
--- 243,249 ----
  static void SignalChildren(int signal);
  static int    CountChildren(void);
  static bool CreateOptsFile(int argc, char *argv[]);
+ static void RemovePgSorttemp(void);

  static pid_t SSDataBase(int xlop);

***************
*** 595,600 ****
--- 596,604 ----
      if (!CreateDataDirLockFile(DataDir, true))
          ExitPostmaster(1);

+     /* Remove old sort files */
+     RemovePgSorttemp();
+
      /*
       * Establish input sockets.
       */
***************
*** 2449,2452 ****
--- 2453,2477 ----

      fclose(fp);
      return true;
+ }
+
+
+ /*
+  * Remove old sort files
+  */
+ static void
+ RemovePgSorttemp(void)
+ {
+     char clear_pg_sorttemp[1024];
+
+     /* Don't remove directory in case it is a symlink */
+     snprintf(clear_pg_sorttemp, sizeof(clear_pg_sorttemp),
+             "/bin/sh -c \
+             'PATH=/bin:/usr/bin; \
+             export PATH; \
+             find \"%s\"/base -name pg_sorttemp -type d -exec sh -c \"rm -f {}/*\" \\;'",
+             DataDir);
+     /* Make sure we have the full 'find' command */
+     if (strlen(clear_pg_sorttemp)+1 < 1024)
+         system(clear_pg_sorttemp);
  }
Index: src/backend/storage/file/fd.c
===================================================================
RCS file: /home/projects/pgsql/cvsroot/pgsql/src/backend/storage/file/fd.c,v
retrieving revision 1.76
diff -c -r1.76 fd.c
*** src/backend/storage/file/fd.c    2001/04/03 04:07:02    1.76
--- src/backend/storage/file/fd.c    2001/05/23 01:09:37
***************
*** 90,95 ****
--- 90,97 ----

  #define VFD_CLOSED (-1)

+ #define TEMP_SORT_DIR "pg_sorttemp"
+
  #define FileIsValid(file) \
      ((file) > 0 && (file) < (int) SizeVfdCache && VfdCache[file].fileName != NULL)

***************
*** 742,762 ****
  File
  OpenTemporaryFile(void)
  {
!     char        tempfilename[64];
      File        file;

      /*
       * Generate a tempfile name that's unique within the current
       * transaction
       */
!     snprintf(tempfilename, sizeof(tempfilename),
!              "pg_sorttemp%d.%ld", MyProcPid, tempFileCounter++);

      /* Open the file */
!     file = FileNameOpenFile(tempfilename,
                              O_RDWR | O_CREAT | O_TRUNC | PG_BINARY, 0600);
      if (file <= 0)
!         elog(ERROR, "Failed to create temporary file %s", tempfilename);

      /* Mark it for deletion at close or EOXact */
      VfdCache[file].fdstate |= FD_TEMPORARY;
--- 744,772 ----
  File
  OpenTemporaryFile(void)
  {
!     char        tempfilepath[128];
      File        file;

      /*
       * Generate a tempfile name that's unique within the current
       * transaction
       */
!     snprintf(tempfilepath, sizeof(tempfilepath),
!              "%s%c%d.%ld", TEMP_SORT_DIR, SEP_CHAR, MyProcPid,
!              tempFileCounter++);

      /* Open the file */
!     file = FileNameOpenFile(tempfilepath,
                              O_RDWR | O_CREAT | O_TRUNC | PG_BINARY, 0600);
      if (file <= 0)
!     {
!         /* mkdir could fail if some one else already created it */
!         mkdir(TEMP_SORT_DIR, S_IRWXU);
!         file = FileNameOpenFile(tempfilepath,
!                             O_RDWR | O_CREAT | O_TRUNC | PG_BINARY, 0600);
!         if (file <= 0)
!             elog(ERROR, "Failed to create temporary file %s", tempfilepath);
!     }

      /* Mark it for deletion at close or EOXact */
      VfdCache[file].fdstate |= FD_TEMPORARY;

pgsql-patches by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: Remove sort files
Next
From: Peter Eisentraut
Date:
Subject: Re: Remove sort files