CernVM-FS  2.13.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 "util.h"
6 
7 #include <stdio.h>
8 
9 #include <string>
10 #include <vector>
11 
12 #include "crypto/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) + 1
20  + sizeof(uid_t) / sizeof(unsigned char) + 1
21  + sizeof(gid_t) / sizeof(unsigned char) + 1;
22  XattrList *xlist = reinterpret_cast<XattrList *>(stat_info->cvm_xattrs);
23  unsigned xlist_buffer_size = 0;
24  unsigned char *xlist_buffer;
25  std::vector<std::string> serialize_blacklist;
26  serialize_blacklist.push_back("security.selinux");
27  if (xlist) {
28  xlist->Serialize(&xlist_buffer, &xlist_buffer_size, &serialize_blacklist);
29  }
30  unsigned char buffer[min_buffer_size + xlist_buffer_size];
31  for (unsigned i = 0; i < (min_buffer_size + xlist_buffer_size); i++) {
32  buffer[i] = 255;
33  }
34  unsigned offset = 0;
35  // Add mode
36  mode_t hash_mode = stat_info->st_mode & 0777;
37  memcpy(buffer + offset, &hash_mode, sizeof(mode_t));
38  offset += sizeof(mode_t) / sizeof(unsigned char);
39  *(buffer + offset) = 0;
40  offset += 1;
41  // Add uid
42  memcpy(buffer + offset, &(stat_info->st_uid), sizeof(uid_t));
43  offset += sizeof(uid_t) / sizeof(unsigned char);
44  *(buffer + offset) = 0;
45  offset += 1;
46  // Add gid
47  memcpy(buffer + offset, &(stat_info->st_gid), sizeof(gid_t));
48  offset += sizeof(gid_t) / sizeof(unsigned char);
49  *(buffer + offset) = 0;
50  offset += 1;
51  // Add xlist
52  if (xlist) {
53  memcpy(buffer + offset, xlist_buffer, xlist_buffer_size);
54  free(xlist_buffer);
55  }
56  // Hash
57  shash::HashMem(buffer, min_buffer_size + xlist_buffer_size, &meta_hash);
58  return meta_hash;
59 }
60 
61 void AppendStringToList(char const *str,
62  char ***buf,
63  size_t *listlen,
64  size_t *buflen) {
65  if (*listlen + 1 >= *buflen) {
66  size_t newbuflen = (*listlen) * 2 + 5;
67  *buf = reinterpret_cast<char **>(realloc(*buf, sizeof(char *) * newbuflen));
68  assert(*buf);
69  *buflen = newbuflen;
70  assert(*listlen < *buflen);
71  }
72  if (str) {
73  (*buf)[(*listlen)] = strdup(str);
74  // null-terminate the list
75  (*buf)[++(*listlen)] = NULL;
76  } else {
77  (*buf)[(*listlen)] = NULL;
78  }
79 }
assert((mem||(size==0))&&"Out Of Memory")
void * cvm_xattrs
Definition: libcvmfs.h:175
void Serialize(unsigned char **outbuf, unsigned *size, const std::vector< std::string > *blacklist=NULL) const
Definition: xattr.cc:198
gid_t st_gid
Definition: libcvmfs.h:161
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:257
uid_t st_uid
Definition: libcvmfs.h:160
void AppendStringToList(char const *str, char ***buf, size_t *listlen, size_t *buflen)
Definition: util.cc:61
mode_t st_mode
Definition: libcvmfs.h:158