GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/xattr.h
Date: 2025-12-28 02:35:52
Exec Total Coverage
Lines: 6 6 100.0%
Branches: 6 6 100.0%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 */
4
5 #ifndef CVMFS_XATTR_H_
6 #define CVMFS_XATTR_H_
7
8 #include <inttypes.h>
9
10 #include <map>
11 #include <string>
12 #include <vector>
13
14 /**
15 * Represents extended attributes that are maintained by the system or the user
16 * and just blindly stored and returned by cvmfs. Note that cvmfs' magic
17 * extended attributes (e.g. user.pid) will hide the ones maintained by the user
18 * but they are still present in the file catalogs.
19 *
20 * First application of the extended attributes is security.capability in order
21 * to support POSIX file capabilities. Cvmfs' support for custom extended
22 * attributes is limited to 256 attributes, with names <= 255 characters and
23 * values <= 64k bytes. Note that values > 255 characters require cvmfs
24 * version >= 2.14. Earlier versions will ignore all xattrs for a file
25 * if a big one is set. Attribute names must not be the empty string and
26 * must not contain the zero character. There are no restrictions on the content.
27 */
28 class XattrList {
29 public:
30 static const uint8_t kVersionSmall; ///< Version 1, key and value < 255 chars
31 static const uint8_t kVersionBig; ///< Version 2, value up to 64k chars
32
33 117 static bool IsSupportedVersion(uint8_t v) {
34
4/4
✓ Branch 0 taken 104 times.
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 91 times.
✓ Branch 3 taken 13 times.
117 return v == kVersionSmall || v == kVersionBig;
35 }
36
37 static XattrList *CreateFromFile(const std::string &path);
38
39 std::vector<std::string> ListKeys() const;
40 std::string ListKeysPosix(const std::string &merge_with) const;
41 bool Has(const std::string &key) const;
42 bool Get(const std::string &key, std::string *value) const;
43 bool Set(const std::string &key, const std::string &value);
44 bool Remove(const std::string &key);
45 24632 bool IsEmpty() const { return xattrs_.empty(); }
46 110 void Clear() { xattrs_.clear(); }
47
48 void Serialize(unsigned char **outbuf, unsigned *size,
49 const std::vector<std::string> *blacklist = NULL) const;
50 static XattrList *Deserialize(const unsigned char *inbuf,
51 const unsigned size);
52
53 private:
54 struct XattrHeader {
55 182 XattrHeader() : version(kVersionSmall), num_xattrs(0) { }
56 explicit XattrHeader(const uint8_t num_xattrs)
57 : version(kVersionSmall), num_xattrs(num_xattrs) { }
58 uint8_t version;
59 uint8_t num_xattrs;
60 };
61
62 class XattrEntrySerializer {
63 public:
64 explicit XattrEntrySerializer(uint8_t version);
65
2/2
✓ Branch 0 taken 689 times.
✓ Branch 1 taken 65 times.
754 uint32_t GetHeaderSize() const { return version_ == kVersionBig ? 3 : 2; }
66 uint32_t Serialize(const std::string &key, const std::string &value,
67 unsigned char *to);
68 uint32_t Deserialize(const unsigned char *from, uint32_t bufsize,
69 std::string *key, std::string *value);
70
71 private:
72 uint8_t version_;
73 };
74
75 struct XattrEntry {
76 XattrEntry(const std::string &key, const std::string &value);
77 XattrEntry() : len_key(0), len_value(0) { }
78 uint16_t GetSize() const;
79 std::string GetKey() const;
80 std::string GetValue() const;
81 uint8_t len_key;
82 uint8_t len_value;
83 // Concatenate the key the value. When written out or read in, data is cut
84 // off at len_key+len_value
85 char data[512];
86 };
87
88 std::map<std::string, std::string> xattrs_;
89 };
90
91 #endif // CVMFS_XATTR_H_
92