| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/nfs4/filehandle.cc |
| Date: | 2026-11-01 02:41:19 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 47 | 49 | 95.9% |
| Branches: | 26 | 40 | 65.0% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * This file is part of the CernVM File System. | ||
| 3 | */ | ||
| 4 | |||
| 5 | #include "nfs4/filehandle.h" | ||
| 6 | |||
| 7 | #include "nfs4/xdr.h" | ||
| 8 | |||
| 9 | namespace nfs4 { | ||
| 10 | |||
| 11 | namespace { | ||
| 12 | const unsigned char kMagic[4] = {'C', 'V', 'N', '4'}; | ||
| 13 | const unsigned char kVersion = 1; | ||
| 14 | |||
| 15 | 3950 | uint32_t Fnv1a(const unsigned char *data, size_t len) { | |
| 16 | 3950 | uint32_t h = 2166136261u; | |
| 17 |
2/2✓ Branch 0 taken 103790 times.
✓ Branch 1 taken 3950 times.
|
107740 | for (size_t i = 0; i < len; ++i) { |
| 18 | 103790 | h ^= data[i]; | |
| 19 | 103790 | h *= 16777619u; | |
| 20 | } | ||
| 21 | 3950 | return h; | |
| 22 | } | ||
| 23 | } // anonymous namespace | ||
| 24 | |||
| 25 | 1413 | std::string FileHandle::Encode() const { | |
| 26 | 1413 | XdrWriter w; | |
| 27 |
1/2✓ Branch 1 taken 1413 times.
✗ Branch 2 not taken.
|
1413 | w.PutRaw(kMagic, sizeof(kMagic)); |
| 28 | 1413 | const unsigned char header[4] = {kVersion, static_cast<unsigned char>(kind), | |
| 29 | 1413 | 0, 0}; | |
| 30 |
1/2✓ Branch 1 taken 1413 times.
✗ Branch 2 not taken.
|
1413 | w.PutRaw(header, sizeof(header)); |
| 31 |
1/2✓ Branch 1 taken 1413 times.
✗ Branch 2 not taken.
|
1413 | w.PutU64(fsid); |
| 32 |
1/2✓ Branch 1 taken 1413 times.
✗ Branch 2 not taken.
|
1413 | w.PutU64(ino); |
| 33 |
1/2✓ Branch 1 taken 1413 times.
✗ Branch 2 not taken.
|
1413 | w.PutU32(aux); |
| 34 | 1413 | const std::string &buf = w.buffer(); | |
| 35 |
1/2✓ Branch 3 taken 1413 times.
✗ Branch 4 not taken.
|
2826 | w.PutU32( |
| 36 | 1413 | Fnv1a(reinterpret_cast<const unsigned char *>(buf.data()), buf.size())); | |
| 37 |
1/2✓ Branch 2 taken 1413 times.
✗ Branch 3 not taken.
|
2826 | return w.buffer(); |
| 38 | 1413 | } | |
| 39 | |||
| 40 | 2304 | bool FileHandle::Decode(const std::string &raw, FileHandle *fh) { | |
| 41 |
2/2✓ Branch 1 taken 67 times.
✓ Branch 2 taken 2237 times.
|
2304 | if (raw.size() != kSize) |
| 42 | 67 | return false; | |
| 43 | 2237 | const unsigned char *p = reinterpret_cast<const unsigned char *>(raw.data()); | |
| 44 |
2/2✓ Branch 0 taken 8840 times.
✓ Branch 1 taken 2165 times.
|
11005 | for (unsigned i = 0; i < sizeof(kMagic); ++i) { |
| 45 |
2/2✓ Branch 0 taken 72 times.
✓ Branch 1 taken 8768 times.
|
8840 | if (p[i] != kMagic[i]) |
| 46 | 72 | return false; | |
| 47 | } | ||
| 48 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 2147 times.
|
2165 | if (p[4] != kVersion) |
| 49 | 18 | return false; | |
| 50 | 2147 | const unsigned char kind = p[5]; | |
| 51 |
2/4✓ Branch 0 taken 2147 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2147 times.
|
2147 | if (kind < kObject || kind > kNamedAttr) |
| 52 | ✗ | return false; | |
| 53 | |||
| 54 | 2147 | XdrReader r(p + 8, kSize - 8); | |
| 55 | uint64_t fsid, ino; | ||
| 56 | uint32_t aux, check; | ||
| 57 |
1/2✓ Branch 1 taken 2147 times.
✗ Branch 2 not taken.
|
2147 | r.GetU64(&fsid); |
| 58 |
1/2✓ Branch 1 taken 2147 times.
✗ Branch 2 not taken.
|
2147 | r.GetU64(&ino); |
| 59 |
1/2✓ Branch 1 taken 2147 times.
✗ Branch 2 not taken.
|
2147 | r.GetU32(&aux); |
| 60 |
1/2✓ Branch 1 taken 2147 times.
✗ Branch 2 not taken.
|
2147 | r.GetU32(&check); |
| 61 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2147 times.
|
2147 | if (!r.ok()) |
| 62 | ✗ | return false; | |
| 63 |
2/2✓ Branch 1 taken 486 times.
✓ Branch 2 taken 1661 times.
|
2147 | if (check != Fnv1a(p, kSize - 4)) |
| 64 | 486 | return false; | |
| 65 | |||
| 66 | 1661 | fh->kind = static_cast<Kind>(kind); | |
| 67 | 1661 | fh->fsid = fsid; | |
| 68 | 1661 | fh->ino = ino; | |
| 69 | 1661 | fh->aux = aux; | |
| 70 | 1661 | return true; | |
| 71 | } | ||
| 72 | |||
| 73 | 390 | uint32_t HashName(const std::string &name) { | |
| 74 | 390 | return Fnv1a(reinterpret_cast<const unsigned char *>(name.data()), | |
| 75 | 390 | name.size()); | |
| 76 | } | ||
| 77 | |||
| 78 | } // namespace nfs4 | ||
| 79 |