]> granicus.if.org Git - php/commitdiff
implement single directory garbage collector
authorSascha Schumann <sas@php.net>
Sat, 4 Sep 1999 08:20:43 +0000 (08:20 +0000)
committerSascha Schumann <sas@php.net>
Sat, 4 Sep 1999 08:20:43 +0000 (08:20 +0000)
ext/session/mod_files.c

index 32bd760f88642f0e43bfcdb69ca50c6cab9e0e6b..f137873667ada1e34ae5539a0fb37652b7e213af 100644 (file)
 #include <sys/file.h>
 #endif
 
+#if HAVE_DIRENT_H
+#include <dirent.h>
+#endif
+
 #include <fcntl.h>
 
 #include "php_session.h"
 #include "mod_files.h"
 #include "ext/standard/flock_compat.h"
 
+#define FILE_PREFIX "sess_"
+
 typedef struct {
        int fd;
        char *lastkey;
@@ -102,7 +108,7 @@ static void _ps_files_open(ps_files *data, const char *key)
 
                keylen = strlen(key);
                if(keylen <= data->dirdepth || MAXPATHLEN < 
-                               (strlen(data->basedir) + 2 * data->dirdepth + keylen + 5)) 
+                               (strlen(data->basedir) + 2 * data->dirdepth + keylen + 5 + sizeof(FILE_PREFIX))) 
                        return;
                p = key;
                n = sprintf(buf, "%s/", data->basedir);
@@ -110,6 +116,7 @@ static void _ps_files_open(ps_files *data, const char *key)
                        buf[n++] = *p++;
                        buf[n++] = DIR_DELIMITER;
                }
+               strcat(buf, FILE_PREFIX);
                strcat(buf, p);
                
                data->lastkey = estrdup(key);
@@ -186,8 +193,49 @@ PS_DESTROY_FUNC(files)
        return SUCCESS;
 }
 
+static void ps_files_cleanup_dir(const char *dirname, int maxlifetime)
+{
+       DIR *dir;
+       struct dirent *entry;
+       struct stat sbuf;
+       char buf[MAXPATHLEN];
+       time_t now;
+
+       dir = opendir(dirname);
+       if(!dir) return;
+
+       time(&now);
+
+       while((entry = readdir(dir))) {
+               /* does the file start with our prefix? */
+               if(!strncmp(entry->d_name, FILE_PREFIX, sizeof(FILE_PREFIX) - 1) &&
+                               /* create full path */
+                               snprintf(buf, MAXPATHLEN, "%s%c%s", dirname, DIR_DELIMITER,
+                                       entry->d_name) > 0 &&
+                               /* stat the directory entry */
+                               stat(buf, &sbuf) == 0 &&
+                               /* is it expired? */
+                               (now - sbuf.st_atime) > maxlifetime) {
+                       unlink(buf);
+               }
+       }
+
+       closedir(dir);
+}
+
 PS_GC_FUNC(files) 
 {
+       PS_FILES_DATA;
+       
+       /* we don't perform any cleanup, if dirdepth is larger than 0.
+          we return SUCCESS, since all cleanup should be handled by
+          an external entity (i.e. find -ctime x | xargs rm) */
+          
+       if(data->dirdepth > 0) {
+               return SUCCESS;
+       }
+
+       ps_files_cleanup_dir(data->basedir, maxlifetime);
        
        return SUCCESS;
 }