CernVM-FS  2.10.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
util.cc
Go to the documentation of this file.
1 
5 #include <stdio.h>
6 
7 #include <string>
8 #include <vector>
9 
10 #include "util.h"
11 
12 #include "hash.h"
13 #include "libcvmfs.h"
14 #include "xattr.h"
15 
16 shash::Any HashMeta(const struct cvmfs_attr *stat_info) {
17  // TODO(steuber): Can we do any better here?
18  shash::Any meta_hash(shash::kMd5);
19  unsigned min_buffer_size = sizeof(mode_t)/sizeof(unsigned char)
20  + 1
21  + sizeof(uid_t)/sizeof(unsigned char)
22  + 1
23  + sizeof(gid_t)/sizeof(unsigned char)
24  + 1;
25  XattrList *xlist = reinterpret_cast<XattrList *>(stat_info->cvm_xattrs);
26  unsigned xlist_buffer_size = 0;
27  unsigned char *xlist_buffer;
28  std::vector<std::string> serialize_blacklist;
29  serialize_blacklist.push_back("security.selinux");
30  if (xlist) {
31  xlist->Serialize(&xlist_buffer, &xlist_buffer_size, &serialize_blacklist);
32  }
33  unsigned char buffer[min_buffer_size+xlist_buffer_size];
34  for (unsigned i = 0; i < (min_buffer_size+xlist_buffer_size); i++) {
35  buffer[i] = 255;
36  }
37  unsigned offset = 0;
38  // Add mode
39  mode_t hash_mode = stat_info->st_mode & 0777;
40  memcpy(buffer+offset, &hash_mode, sizeof(mode_t));
41  offset+=sizeof(mode_t)/sizeof(unsigned char);
42  *(buffer+offset) = 0;
43  offset+=1;
44  // Add uid
45  memcpy(buffer+offset, &(stat_info->st_uid), sizeof(uid_t));
46  offset+=sizeof(uid_t)/sizeof(unsigned char);
47  *(buffer+offset) = 0;
48  offset+=1;
49  // Add gid
50  memcpy(buffer+offset, &(stat_info->st_gid), sizeof(gid_t));
51  offset+=sizeof(gid_t)/sizeof(unsigned char);
52  *(buffer+offset) = 0;
53  offset+=1;
54  // Add xlist
55  if (xlist) {
56  memcpy(buffer+offset, xlist_buffer, xlist_buffer_size);
57  free(xlist_buffer);
58  }
59  // Hash
60  shash::HashMem(buffer, min_buffer_size+xlist_buffer_size, &meta_hash);
61  return meta_hash;
62 }
63 
64 void AppendStringToList(char const *str,
65  char ***buf,
66  size_t *listlen,
67  size_t *buflen) {
68  if (*listlen + 1 >= *buflen) {
69  size_t newbuflen = (*listlen)*2 + 5;
70  *buf = reinterpret_cast<char **>(
71  realloc(*buf, sizeof(char *) * newbuflen));
72  assert(*buf);
73  *buflen = newbuflen;
74  assert(*listlen < *buflen);
75  }
76  if (str) {
77  (*buf)[(*listlen)] = strdup(str);
78  // null-terminate the list
79  (*buf)[++(*listlen)] = NULL;
80  } else {
81  (*buf)[(*listlen)] = NULL;
82  }
83 }
assert((mem||(size==0))&&"Out Of Memory")
void * cvm_xattrs
Definition: libcvmfs.h:173
void Serialize(unsigned char **outbuf, unsigned *size, const std::vector< std::string > *blacklist=NULL) const
Definition: xattr.cc:196
gid_t st_gid
Definition: libcvmfs.h:159
shash::Any HashMeta(const struct cvmfs_attr *stat_info)
Definition: util.cc:16
void HashMem(const unsigned char *buffer, const unsigned buffer_size, Any *any_digest)
Definition: hash.cc:255
uid_t st_uid
Definition: libcvmfs.h:158
void AppendStringToList(char const *str, char ***buf, size_t *listlen, size_t *buflen)
Definition: util.cc:64
mode_t st_mode
Definition: libcvmfs.h:156