PS_MOD(files)
};
-#define PS_FILES_DATA ps_files *data = PS_GET_MOD_DATA()
-
-PS_OPEN_FUNC(files)
-{
- ps_files *data;
- char *p;
-
- data = ecalloc(sizeof(*data), 1);
- PS_SET_MOD_DATA(data);
-
- data->fd = -1;
- if((p = strchr(save_path, ':'))) {
- data->dirdepth = strtol(save_path, NULL, 10);
- save_path = p + 1;
- }
- data->basedir = estrdup(save_path);
-
- return SUCCESS;
-}
-
-PS_CLOSE_FUNC(files)
-{
- PS_FILES_DATA;
-
- if(data->fd > -1) close(data->fd);
- if(data->lastkey) efree(data->lastkey);
- efree(data->basedir);
- efree(data);
- *mod_data = NULL;
-
- return SUCCESS;
-}
-
#if WIN32|WINNT
#define DIR_DELIMITER '\\'
#else
#define DIR_DELIMITER '/'
#endif
-static void _ps_files_open(ps_files *data, const char *key)
+static char *_ps_files_path_create(char *buf, size_t buflen, ps_files *data, const char *key)
{
- char buf[MAXPATHLEN];
+ int keylen;
const char *p;
int i;
int n;
- int keylen;
+
+ keylen = strlen(key);
+ if(keylen <= data->dirdepth || buflen <
+ (strlen(data->basedir) + 2 * data->dirdepth + keylen + 5 + sizeof(FILE_PREFIX)))
+ return NULL;
+ p = key;
+ n = sprintf(buf, "%s/", data->basedir);
+ for(i = 0; i < data->dirdepth; i++) {
+ buf[n++] = *p++;
+ buf[n++] = DIR_DELIMITER;
+ }
+ strcat(buf, FILE_PREFIX);
+ strcat(buf, p);
+
+ return buf;
+}
+
+static void _ps_files_open(ps_files *data, const char *key)
+{
+ char buf[MAXPATHLEN];
if(data->fd < 0 || !data->lastkey || strcmp(key, data->lastkey)) {
if(data->lastkey) {
close(data->fd);
data->fd = -1;
}
-
- keylen = strlen(key);
- if(keylen <= data->dirdepth || MAXPATHLEN <
- (strlen(data->basedir) + 2 * data->dirdepth + keylen + 5 + sizeof(FILE_PREFIX)))
+
+ if(!_ps_files_path_create(buf, sizeof(buf), data, key))
return;
- p = key;
- n = sprintf(buf, "%s/", data->basedir);
- for(i = 0; i < data->dirdepth; i++) {
- buf[n++] = *p++;
- buf[n++] = DIR_DELIMITER;
- }
- strcat(buf, FILE_PREFIX);
- strcat(buf, p);
data->lastkey = estrdup(key);
}
}
+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);
+}
+
+#define PS_FILES_DATA ps_files *data = PS_GET_MOD_DATA()
+
+PS_OPEN_FUNC(files)
+{
+ ps_files *data;
+ char *p;
+
+ data = ecalloc(sizeof(*data), 1);
+ PS_SET_MOD_DATA(data);
+
+ data->fd = -1;
+ if((p = strchr(save_path, ':'))) {
+ data->dirdepth = strtol(save_path, NULL, 10);
+ save_path = p + 1;
+ }
+ data->basedir = estrdup(save_path);
+
+ return SUCCESS;
+}
+
+PS_CLOSE_FUNC(files)
+{
+ PS_FILES_DATA;
+
+ if(data->fd > -1) close(data->fd);
+ if(data->lastkey) efree(data->lastkey);
+ efree(data->basedir);
+ efree(data);
+ *mod_data = NULL;
+
+ return SUCCESS;
+}
+
PS_READ_FUNC(files)
{
int n;
char buf[MAXPATHLEN];
PS_FILES_DATA;
- snprintf(buf, MAXPATHLEN, "%s/%s", data->basedir, key);
+ if(!_ps_files_path_create(buf, sizeof(buf), data, key))
+ return FAILURE;
+
unlink(buf);
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;
return SUCCESS;
}
- ps_files_cleanup_dir(data->basedir, maxlifetime);
+ _ps_files_cleanup_dir(data->basedir, maxlifetime);
return SUCCESS;
}