Re: Remove sort files - Mailing list pgsql-patches

From Bruce Momjian
Subject Re: Remove sort files
Date
Msg-id 200105240015.f4O0F0F29409@candle.pha.pa.us
Whole thread Raw
In response to Re: Remove sort files  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: Remove sort files  (Ian Lance Taylor <ian@airs.com>)
Re: Remove sort files  (Peter Eisentraut <peter_e@gmx.net>)
List pgsql-patches
> Bruce Momjian <pgman@candle.pha.pa.us> writes:
> > OK, here is the new code:
>
> >     snprintf(clear_pg_sorttemp, sizeof(clear_pg_sorttemp),
> >             "sh -c '\
> >             cd \"%s\"/base && \
> >             ls | while read DIR; \
> >             do \
> >                 export DIR; \
> >                 (cd \"$DIR\"/pg_sorttemp/ 2>/dev/null && rm -f *); \
> >             done'",
> >             DataDir);
>
> A readdir() loop would be hardly any longer, and it'd be faster and more
> secure.  Among other problems with the above code, we do not prohibit
> double-quote in database paths anymore ...

OK, I did the readdir() thing.  I hope it is safe to unlink a file while
in a readdir() loop.

Patch attached.

--
  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: 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/24 00:11:55
***************
*** 58,63 ****
--- 58,64 ----
  #include <ctype.h>
  #include <sys/types.h>
  #include <sys/stat.h>
+ #include <dirent.h>
  #include <sys/time.h>
  #include <sys/socket.h>
  #include <errno.h>
***************
*** 243,248 ****
--- 244,250 ----
  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 ****
--- 597,605 ----
      if (!CreateDataDirLockFile(DataDir, true))
          ExitPostmaster(1);

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

      fclose(fp);
      return true;
+ }
+
+
+ /*
+  * Remove old sort files
+  */
+ static void
+ RemovePgSorttemp(void)
+ {
+     char         db_path[MAXPGPATH];
+     char         temp_path[MAXPGPATH];
+     char         rm_path[MAXPGPATH];
+     DIR           *db_dir;
+     DIR           *temp_dir;
+     struct dirent  *db_de;
+     struct dirent  *temp_de;
+
+     /*
+      * Cycle through pg_tempsort for all databases and
+      * and remove old sort files.
+      */
+     /* trailing slash forces symlink following */
+     snprintf(db_path, sizeof(db_path), "%s/base/",    DataDir);
+     if ((db_dir = opendir(db_path)) != NULL)
+         while ((db_de = readdir(db_dir)) != NULL)
+         {
+             snprintf(temp_path, sizeof(temp_path),
+                 "%s/%s/%s/",    db_path, db_de->d_name, SORT_TEMP_DIR);
+             if ((temp_dir = opendir(temp_path)) != NULL)
+                 while ((temp_de = readdir(temp_dir)) != NULL)
+                 {
+                     if (temp_de->d_type == DT_REG)
+                     {
+                         snprintf(rm_path, sizeof(temp_path),
+                             "%s/%s/%s/%s",
+                             db_path, db_de->d_name,
+                             SORT_TEMP_DIR, temp_de->d_name);
+                         unlink(rm_path);
+                     }
+                 }
+         }
  }
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/24 00:11:55
***************
*** 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;
--- 742,770 ----
  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", SORT_TEMP_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(SORT_TEMP_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;
Index: src/include/storage/fd.h
===================================================================
RCS file: /home/projects/pgsql/cvsroot/pgsql/src/include/storage/fd.h,v
retrieving revision 1.27
diff -c -r1.27 fd.h
*** src/include/storage/fd.h    2001/02/18 04:39:42    1.27
--- src/include/storage/fd.h    2001/05/24 00:12:01
***************
*** 39,44 ****
--- 39,46 ----
   * FileSeek uses the standard UNIX lseek(2) flags.
   */

+ #define SORT_TEMP_DIR "pg_sorttemp"
+
  typedef char *FileName;

  typedef int File;

pgsql-patches by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: [JDBC] ANT fix for jar creation
Next
From: Ian Lance Taylor
Date:
Subject: Re: Remove sort files