From: Alex Rousskov Date: Tue, 4 Dec 2012 22:47:36 +0000 (-0700) Subject: Reduced code duplication when creating rock db file. X-Git-Tag: SQUID_3_5_0_1~444^2~98 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=413f00bd58c921e83ebd56edb9a30ab8fb304d0b;p=thirdparty%2Fsquid.git Reduced code duplication when creating rock db file. --- diff --git a/src/fs/rock/RockSwapDir.cc b/src/fs/rock/RockSwapDir.cc index aa4dd966df..7f5de22ffc 100644 --- a/src/fs/rock/RockSwapDir.cc +++ b/src/fs/rock/RockSwapDir.cc @@ -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 diff --git a/src/fs/rock/RockSwapDir.h b/src/fs/rock/RockSwapDir.h index 165fa2cfc1..29576ed43e 100644 --- a/src/fs/rock/RockSwapDir.h +++ b/src/fs/rock/RockSwapDir.h @@ -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 theFile; ///< cache storage for this cache_dir DirMap *map;