| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/nfs4/filehandle.cc |
| Date: | 2026-10-18 02:40:11 |
| 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 | 1494 | uint32_t Fnv1a(const unsigned char *data, size_t len) { | |
| 16 | 1494 | uint32_t h = 2166136261u; | |
| 17 |
2/2✓ Branch 0 taken 39099 times.
✓ Branch 1 taken 1494 times.
|
40593 | for (size_t i = 0; i < len; ++i) { |
| 18 | 39099 | h ^= data[i]; | |
| 19 | 39099 | h *= 16777619u; | |
| 20 | } | ||
| 21 | 1494 | return h; | |
| 22 | } | ||
| 23 | } // anonymous namespace | ||
| 24 | |||
| 25 | 575 | std::string FileHandle::Encode() const { | |
| 26 | 575 | XdrWriter w; | |
| 27 |
1/2✓ Branch 1 taken 575 times.
✗ Branch 2 not taken.
|
575 | w.PutRaw(kMagic, sizeof(kMagic)); |
| 28 | 575 | const unsigned char header[4] = {kVersion, static_cast<unsigned char>(kind), | |
| 29 | 575 | 0, 0}; | |
| 30 |
1/2✓ Branch 1 taken 575 times.
✗ Branch 2 not taken.
|
575 | w.PutRaw(header, sizeof(header)); |
| 31 |
1/2✓ Branch 1 taken 575 times.
✗ Branch 2 not taken.
|
575 | w.PutU64(fsid); |
| 32 |
1/2✓ Branch 1 taken 575 times.
✗ Branch 2 not taken.
|
575 | w.PutU64(ino); |
| 33 |
1/2✓ Branch 1 taken 575 times.
✗ Branch 2 not taken.
|
575 | w.PutU32(aux); |
| 34 | 575 | const std::string &buf = w.buffer(); | |
| 35 |
1/2✓ Branch 3 taken 575 times.
✗ Branch 4 not taken.
|
1150 | w.PutU32( |
| 36 | 575 | Fnv1a(reinterpret_cast<const unsigned char *>(buf.data()), buf.size())); | |
| 37 |
1/2✓ Branch 2 taken 575 times.
✗ Branch 3 not taken.
|
1150 | return w.buffer(); |
| 38 | 575 | } | |
| 39 | |||
| 40 | 794 | bool FileHandle::Decode(const std::string &raw, FileHandle *fh) { | |
| 41 |
2/2✓ Branch 1 taken 19 times.
✓ Branch 2 taken 775 times.
|
794 | if (raw.size() != kSize) |
| 42 | 19 | return false; | |
| 43 | 775 | const unsigned char *p = reinterpret_cast<const unsigned char *>(raw.data()); | |
| 44 |
2/2✓ Branch 0 taken 3082 times.
✓ Branch 1 taken 763 times.
|
3845 | for (unsigned i = 0; i < sizeof(kMagic); ++i) { |
| 45 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 3070 times.
|
3082 | if (p[i] != kMagic[i]) |
| 46 | 12 | return false; | |
| 47 | } | ||
| 48 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 760 times.
|
763 | if (p[4] != kVersion) |
| 49 | 3 | return false; | |
| 50 | 760 | const unsigned char kind = p[5]; | |
| 51 |
2/4✓ Branch 0 taken 760 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 760 times.
|
760 | if (kind < kObject || kind > kNamedAttr) |
| 52 | ✗ | return false; | |
| 53 | |||
| 54 | 760 | XdrReader r(p + 8, kSize - 8); | |
| 55 | uint64_t fsid, ino; | ||
| 56 | uint32_t aux, check; | ||
| 57 |
1/2✓ Branch 1 taken 760 times.
✗ Branch 2 not taken.
|
760 | r.GetU64(&fsid); |
| 58 |
1/2✓ Branch 1 taken 760 times.
✗ Branch 2 not taken.
|
760 | r.GetU64(&ino); |
| 59 |
1/2✓ Branch 1 taken 760 times.
✗ Branch 2 not taken.
|
760 | r.GetU32(&aux); |
| 60 |
1/2✓ Branch 1 taken 760 times.
✗ Branch 2 not taken.
|
760 | r.GetU32(&check); |
| 61 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 760 times.
|
760 | if (!r.ok()) |
| 62 | ✗ | return false; | |
| 63 |
2/2✓ Branch 1 taken 81 times.
✓ Branch 2 taken 679 times.
|
760 | if (check != Fnv1a(p, kSize - 4)) |
| 64 | 81 | return false; | |
| 65 | |||
| 66 | 679 | fh->kind = static_cast<Kind>(kind); | |
| 67 | 679 | fh->fsid = fsid; | |
| 68 | 679 | fh->ino = ino; | |
| 69 | 679 | fh->aux = aux; | |
| 70 | 679 | return true; | |
| 71 | } | ||
| 72 | |||
| 73 | 159 | uint32_t HashName(const std::string &name) { | |
| 74 | 159 | return Fnv1a(reinterpret_cast<const unsigned char *>(name.data()), | |
| 75 | 159 | name.size()); | |
| 76 | } | ||
| 77 | |||
| 78 | } // namespace nfs4 | ||
| 79 |