CernVM-FS  2.13.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
data_dir_mgmt.cc
Go to the documentation of this file.
1 
4 #include "data_dir_mgmt.h"
5 
6 #include <errno.h>
7 #include <stdio.h>
8 
9 #include "helpers.h"
11 #include "util/posix.h"
12 
16 void PosixCheckDirStructure(std::string cur_path,
17  mode_t mode,
18  unsigned depth = 1) {
19  std::string max_dir_name = std::string(kDigitsPerDirLevel, 'f');
20  // Build current base path
21  if (depth == 1) {
22  bool res1 = MkdirDeep(cur_path.c_str(), mode);
23  assert(res1);
24  } else {
25  int res = mkdir(cur_path.c_str(), mode);
26  assert(res == 0 || errno == EEXIST);
27  }
28  // Build template for directory names:
30  char hex[kDigitsPerDirLevel + 1];
31  char dir_name_template[5];
32  snprintf(dir_name_template,
33  sizeof(dir_name_template),
34  "%%%02ux",
36  // Go through all levels:
37  for (; depth <= kDirLevels; depth++) {
38  if (!DirectoryExists(cur_path + "/" + max_dir_name)) {
39  // Directories in this level not yet fully created...
40  for (unsigned int i = 0;
41  i < (((unsigned int)1) << 4 * kDigitsPerDirLevel);
42  i++) {
43  // Go through directories 0^kDigitsPerDirLevel to f^kDigitsPerDirLevel
44  snprintf(hex, sizeof(hex), dir_name_template, i);
45  std::string this_path = cur_path + "/" + std::string(hex);
46  int res = mkdir(this_path.c_str(), mode);
47  assert(res == 0 || errno == EEXIST);
48  // Once directory created: Prepare substructures
49  PosixCheckDirStructure(this_path, mode, depth + 1);
50  }
51  break;
52  } else {
53  // Directories on this level fully created; check ./
54  PosixCheckDirStructure(cur_path + "/" + max_dir_name, mode, depth + 1);
55  }
56  }
57 }
58 
60  // NOTE(steuber): Can we do this in parallel?
61  PosixCheckDirStructure(ctx->data, 0700);
62 }
struct cvmcache_context * ctx
assert((mem||(size==0))&&"Out Of Memory")
void InitializeDataDirectory(struct fs_traversal_context *ctx)
void PosixCheckDirStructure(std::string cur_path, mode_t mode, unsigned depth=1)
const unsigned kDigitsPerDirLevel
Definition: helpers.h:22
bool MkdirDeep(const std::string &path, const mode_t mode, bool verify_writable)
Definition: posix.cc:855
const unsigned kDirLevels
Definition: helpers.h:21
bool DirectoryExists(const std::string &path)
Definition: posix.cc:824