]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Reduced code duplication when creating rock db file.
authorAlex Rousskov <rousskov@measurement-factory.com>
Tue, 4 Dec 2012 22:47:36 +0000 (15:47 -0700)
committerAlex Rousskov <rousskov@measurement-factory.com>
Tue, 4 Dec 2012 22:47:36 +0000 (15:47 -0700)
src/fs/rock/RockSwapDir.cc
src/fs/rock/RockSwapDir.h

index aa4dd966df532684f718a4a56268a9b6d4c41ea1..7f5de22ffc8ec712a8bb8ec189f35f729d772996 100644 (file)
@@ -164,50 +164,42 @@ Rock::SwapDir::create()
     if (::stat(path, &swap_sb) < 0) {
         debugs (47, DBG_IMPORTANT, "Creating Rock db directory: " << path);
         const int res = mkdir(path, 0700);
-        if (res != 0) {
-            debugs(47, DBG_CRITICAL, "Failed to create Rock db dir " << path <<
-                   ": " << xstrerror());
-            fatal("Rock Store db creation error");
-        }
+        if (res != 0)
+            createError("mkdir");
     }
 
+    const int swap = open(filePath, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0600);
+    if (swap < 0)
+        createError("create");
+
 #if SLOWLY_FILL_WITH_ZEROS
     char block[1024];
     Must(maxSize() % sizeof(block) == 0);
     memset(block, '\0', sizeof(block));
 
-    const int swap = open(filePath, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0600);
     for (off_t offset = 0; offset < maxSize(); offset += sizeof(block)) {
-        if (write(swap, block, sizeof(block)) != sizeof(block)) {
-            debugs(47, DBG_CRITICAL, "ERROR: Failed to create Rock Store db in " << filePath <<
-                   ": " << xstrerror());
-            fatal("Rock Store db creation error");
-        }
+        if (write(swap, block, sizeof(block)) != sizeof(block))
+            createError("write");
     }
-    close(swap);
 #else
-    const int swap = open(filePath, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0600);
-    if (swap < 0) {
-        debugs(47, DBG_CRITICAL, "ERROR: Failed to initialize Rock Store db in " << filePath <<
-               "; create error: " << xstrerror());
-        fatal("Rock Store db creation error");
-    }
-
-    if (ftruncate(swap, maxSize()) != 0) {
-        debugs(47, DBG_CRITICAL, "ERROR: Failed to initialize Rock Store db in " << filePath <<
-               "; truncate error: " << xstrerror());
-        fatal("Rock Store db creation error");
-    }
+    if (ftruncate(swap, maxSize()) != 0)
+        createError("truncate");
 
     char header[HeaderSize];
     memset(header, '\0', sizeof(header));
-    if (write(swap, header, sizeof(header)) != sizeof(header)) {
-        debugs(47, DBG_CRITICAL, "ERROR: Failed to initialize Rock Store db in " << filePath <<
-               "; write error: " << xstrerror());
-        fatal("Rock Store db initialization error");
-    }
-    close(swap);
+    if (write(swap, header, sizeof(header)) != sizeof(header))
+        createError("write");
 #endif
+
+    close(swap);
+}
+
+// report Rock DB creation error and exit
+void
+Rock::SwapDir::createError(const char *const msg) {
+    debugs(47, DBG_CRITICAL, "ERROR: Failed to initialize Rock Store db in " <<
+           filePath << "; " << msg << " error: " << xstrerror());
+    fatal("Rock Store db creation error");
 }
 
 void
index 165fa2cfc14f03745ca265249356464eec04b4f1..29576ed43e7d87eab568c64147e378bafe85c15e 100644 (file)
@@ -87,6 +87,8 @@ protected:
     const char *filePath; ///< location of cache storage file inside path/
 
 private:
+    void createError(const char *const msg);
+
     DiskIOStrategy *io;
     RefCount<DiskFile> theFile; ///< cache storage for this cache_dir
     DirMap *map;