]> git.saurik.com Git - redis.git/blobdiff - src/diskstore.c
diskstore more fixes
[redis.git] / src / diskstore.c
index 6e59bc679cf704678945830b0d69b91ddef98fc1..ce58dffba8121d215670d753cdb3b7b2ff5134ee 100644 (file)
@@ -74,6 +74,7 @@
 
 #include <fcntl.h>
 #include <sys/stat.h>
+#include <dirent.h>
 
 int create256dir(char *prefix) {
     char buf[1024];
@@ -239,6 +240,8 @@ robj *dsGet(redisDb *db, robj *key, time_t *expire) {
     return val;
 
 readerr:
+    redisLog(REDIS_WARNING,"Read error reading reading %s. Corrupted key?",
+        buf);
     redisPanic("Unrecoverable error reading from disk store");
     return NULL; /* unreached */
 }
@@ -268,5 +271,56 @@ int dsExists(redisDb *db, robj *key) {
     return access(buf,R_OK) == 0;
 }
 
-int dsFlushDb(int dbid) {
+void dsFlushOneDir(char *path, int dbid) {
+    DIR *dir;
+    struct dirent *dp, de;
+
+    dir = opendir(path);
+    if (dir == NULL) {
+        redisLog(REDIS_WARNING,"Disk store can't open dir %s: %s",
+            path, strerror(errno));
+        redisPanic("Unrecoverable Disk store errore. Existing.");
+    }
+    while(1) {
+        char buf[1024];
+
+        readdir_r(dir,&de,&dp);
+        if (dp == NULL) break;
+        if (dp->d_name[0] == '.') continue;
+
+        /* Check if we need to remove this entry accordingly to the
+         * DB number */
+        if (dbid != -1) {
+            char id[64];
+            char *p = strchr(dp->d_name,'_');
+            int len = (p - dp->d_name);
+
+            redisAssert(p != NULL && len < 64);
+            memcpy(id,dp->d_name,len);
+            id[len] = '\0';
+            if (atoi(id) != dbid) continue; /* skip this file */
+        }
+        
+        /* Finally unlink the file */
+        snprintf(buf,1024,"%s/%s",path,dp->d_name);
+        if (unlink(buf) == -1) {
+            redisLog(REDIS_WARNING,
+                "Can't unlink %s: %s", buf, strerror(errno));
+            redisPanic("Unrecoverable Disk store errore. Existing.");
+        }
+    }
+    closedir(dir);
+}
+
+void dsFlushDb(int dbid) {
+    char buf[1024];
+    int j, i;
+
+    redisLog(REDIS_NOTICE,"Flushing diskstore DB (%d)",dbid);
+    for (j = 0; j < 256; j++) {
+        for (i = 0; i < 256; i++) {
+            snprintf(buf,1024,"%s/%02x/%02x",server.ds_path,j,i);
+            dsFlushOneDir(buf,dbid);
+        }
+    }
 }