CernVM-FS  2.13.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, const char *dir) {
62  std::string result = ctx->base;
63  result += ctx->repo;
64  if (dir[0] != '/') {
65  result += "/";
66  }
67  result += dir;
68  return result;
69 }
70 
72  const char *ident) {
73  std::string cur_path = ctx->data;
74  cur_path += ident;
75  return cur_path;
76 }
77 
78 int PosixSetMeta(const char *path, const struct cvmfs_attr *stat_info,
79  bool set_permissions /* = true*/) {
80  int res = 0;
81  if (set_permissions) {
82  res = chmod(path, stat_info->st_mode);
83  if (res != 0)
84  return -1;
85  res = chown(path, stat_info->st_uid, stat_info->st_gid);
86  if (res != 0)
87  return -1;
88  }
89  std::string path_str = std::string(path);
90  if (stat_info->cvm_xattrs != NULL) {
91  XattrList *xlist = reinterpret_cast<XattrList *>(stat_info->cvm_xattrs);
92  if (xlist) {
93  std::vector<std::string> v = xlist->ListKeys();
94  std::string val;
95  if (set_permissions) {
96  for (std::vector<std::string>::iterator it = v.begin(); it != v.end();
97  ++it) {
98  xlist->Get(*it, &val);
99  bool res = platform_lsetxattr(path_str, *it, val);
100  if (!res)
101  return -1;
102  }
103  }
104  }
105  }
106 #ifdef CVMFS_HAS_LUTIMES
107  const struct timeval times[2] = {{stat_info->mtime, 0},
108  {stat_info->mtime, 0}};
109  res = lutimes(path, times);
110  if (res != 0)
111  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)
121  return false;
122  mtimes->actime = stat_buf.st_mtime;
123  mtimes->modtime = stat_buf.st_mtime;
124  return true;
125 }
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:78
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:71
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:545