CernVM-FS  2.12.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
helpers.cc
Go to the documentation of this file.
1 
4 #include "helpers.h"
5 
6 #ifdef __GLIBC__
7 #include <gnu/libc-version.h>
8 #endif
9 #include <stdio.h>
10 #include <sys/time.h>
11 #include <sys/types.h>
12 #include <utime.h>
13 
14 // #include <attr/xattr.h> // NOLINT
15 // Necessary because xattr.h does not import sys/types.h
16 
17 #include <string>
18 #include <vector>
19 
20 #include "data_dir_mgmt.h"
21 #include "garbage_collector.h"
23 #include "util/logging.h"
24 #include "util/platform.h"
25 #include "util/posix.h"
26 #include "xattr.h"
27 
28 #ifdef __GLIBC__
29  #if __GLIBC_MINOR__ < 6
30  #warning No lutimes support, glibc >= 2.6 required
31  #else
32  #define CVMFS_HAS_LUTIMES 1
33  #endif
34 #else
35  #define CVMFS_HAS_LUTIMES 1
36 #endif
37 
38 
43 }
44 
47 }
48 
50  const char *warning = WARNING_FILE_NAME;
51  FILE *f = fopen(BuildPath(ctx, "/" WARNING_FILE_NAME).c_str(), "w");
52  if (f != NULL) {
53  fwrite(warning, sizeof(char), strlen(warning), f);
54  fclose(f);
55  } else {
57  "Could not write warning file for posix file system!");
58  }
59 }
60 
61 std::string BuildPath(struct fs_traversal_context *ctx,
62  const char *dir) {
63  std::string result = ctx->base;
64  result += ctx->repo;
65  if (dir[0] != '/') {
66  result += "/";
67  }
68  result += dir;
69  return result;
70 }
71 
73  const char *ident) {
74  std::string cur_path = ctx->data;
75  cur_path += ident;
76  return cur_path;
77 }
78 
79 int PosixSetMeta(const char *path,
80  const struct cvmfs_attr *stat_info, bool set_permissions/* = true*/) {
81  int res = 0;
82  if (set_permissions) {
83  res = chmod(path, stat_info->st_mode);
84  if (res != 0) return -1;
85  res = chown(path, stat_info->st_uid, stat_info->st_gid);
86  if (res != 0) return -1;
87  }
88  std::string path_str = std::string(path);
89  if (stat_info->cvm_xattrs != NULL) {
90  XattrList *xlist = reinterpret_cast<XattrList *>(stat_info->cvm_xattrs);
91  if (xlist) {
92  std::vector<std::string> v = xlist->ListKeys();
93  std::string val;
94  if (set_permissions) {
95  for (std::vector<std::string>::iterator it = v.begin();
96  it != v.end();
97  ++it) {
98  xlist->Get(*it, &val);
99  bool res = platform_lsetxattr(path_str, *it, val);
100  if (!res) return -1;
101  }
102  }
103  }
104  }
105 #ifdef CVMFS_HAS_LUTIMES
106  const struct timeval times[2] = {
107  {stat_info->mtime, 0},
108  {stat_info->mtime, 0}
109  };
110  res = lutimes(path, times);
111  if (res != 0) return -1;
112 #endif
113  return 0;
114 }
115 
116 bool BackupMtimes(std::string path, struct utimbuf *mtimes) {
117  // According to valgrind this is necessary due to struct paddings here...
118  struct stat stat_buf;
119  int res = stat(path.c_str(), &stat_buf);
120  if (res != 0) return false;
121  mtimes->actime = stat_buf.st_mtime;
122  mtimes->modtime = stat_buf.st_mtime;
123  return true;
124 }
struct cvmcache_context * ctx
void InitialFsOperations(struct fs_traversal_context *ctx)
Definition: helpers.cc:39
int PosixSetMeta(const char *path, const struct cvmfs_attr *stat_info, bool set_permissions)
Definition: helpers.cc:79
void FinalizeGarbageCollection(struct fs_traversal_context *ctx)
void FinalizeFsOperations(struct fs_traversal_context *ctx)
Definition: helpers.cc:45
void InitializeDataDirectory(struct fs_traversal_context *ctx)
#define WARNING_FILE_NAME
Definition: helpers.h:15
void InitializeGarbageCollection(struct fs_traversal_context *ctx)
bool Get(const std::string &key, std::string *value) const
Definition: xattr.cc:108
void * cvm_xattrs
Definition: libcvmfs.h:175
void InitializeWarningFile(struct fs_traversal_context *ctx)
Definition: helpers.cc:49
time_t mtime
Definition: libcvmfs.h:164
gid_t st_gid
Definition: libcvmfs.h:161
std::string BuildPath(struct fs_traversal_context *ctx, const char *dir)
Definition: helpers.cc:61
bool BackupMtimes(std::string path, struct utimbuf *mtimes)
Definition: helpers.cc:116
std::string BuildHiddenPath(struct fs_traversal_context *ctx, const char *ident)
Definition: helpers.cc:72
uid_t st_uid
Definition: libcvmfs.h:160
std::vector< std::string > ListKeys() const
Definition: xattr.cc:119
bool platform_lsetxattr(const std::string &path, const std::string &name, const std::string &value)
mode_t st_mode
Definition: libcvmfs.h:158
CVMFS_EXPORT void LogCvmfs(const LogSource source, const int mask, const char *format,...)
Definition: logging.cc:528