| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/shortstring.cc |
| Date: | 2025-12-21 02:39:23 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 29 | 29 | 100.0% |
| Branches: | 24 | 26 | 92.3% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * This file is part of the CernVM File System. | ||
| 3 | * | ||
| 4 | * Some common functions. | ||
| 5 | */ | ||
| 6 | |||
| 7 | |||
| 8 | #include "shortstring.h" | ||
| 9 | |||
| 10 | #ifdef CVMFS_NAMESPACE_GUARD | ||
| 11 | namespace CVMFS_NAMESPACE_GUARD { | ||
| 12 | #endif | ||
| 13 | |||
| 14 | 22698 | PathString GetParentPath(const PathString &path) { | |
| 15 | 22698 | const int length = static_cast<int>(path.GetLength()); | |
| 16 |
2/2✓ Branch 0 taken 23 times.
✓ Branch 1 taken 22675 times.
|
22698 | if (length == 0) |
| 17 | 23 | return path; | |
| 18 | 22675 | const char *chars = path.GetChars(); | |
| 19 | |||
| 20 |
2/2✓ Branch 0 taken 89248 times.
✓ Branch 1 taken 23 times.
|
89271 | for (int i = length - 1; i >= 0; --i) { |
| 21 |
2/2✓ Branch 0 taken 22652 times.
✓ Branch 1 taken 66596 times.
|
89248 | if (chars[i] == '/') |
| 22 | 22652 | return PathString(chars, i); | |
| 23 | } | ||
| 24 | |||
| 25 | 23 | return path; | |
| 26 | } | ||
| 27 | |||
| 28 | 99 | NameString GetFileName(const PathString &path) { | |
| 29 | 99 | NameString name; | |
| 30 | 99 | const int length = static_cast<int>(path.GetLength()); | |
| 31 | 99 | const char *chars = path.GetChars(); | |
| 32 | |||
| 33 | int i; | ||
| 34 |
2/2✓ Branch 0 taken 396 times.
✓ Branch 1 taken 32 times.
|
428 | for (i = length - 1; i >= 0; --i) { |
| 35 |
2/2✓ Branch 0 taken 67 times.
✓ Branch 1 taken 329 times.
|
396 | if (chars[i] == '/') |
| 36 | 67 | break; | |
| 37 | } | ||
| 38 | 99 | i++; | |
| 39 |
2/2✓ Branch 0 taken 67 times.
✓ Branch 1 taken 32 times.
|
99 | if (i < length) { |
| 40 |
1/2✓ Branch 1 taken 67 times.
✗ Branch 2 not taken.
|
67 | name.Append(chars + i, length - i); |
| 41 | } | ||
| 42 | |||
| 43 | 99 | return name; | |
| 44 | } | ||
| 45 | |||
| 46 | |||
| 47 | 1184 | bool IsSubPath(const PathString &parent, const PathString &path) { | |
| 48 | // If parent is "", then any path is a subpath | ||
| 49 |
2/2✓ Branch 1 taken 908 times.
✓ Branch 2 taken 276 times.
|
1184 | if (parent.GetLength() == 0) { |
| 50 | 908 | return true; | |
| 51 | } | ||
| 52 | |||
| 53 | // If the parent string is the prefix of the path string and either | ||
| 54 | // the strings are identical or the separator character is a "/", | ||
| 55 | // then the path is a subpath | ||
| 56 | 276 | if (path.StartsWith(parent) | |
| 57 |
6/6✓ Branch 0 taken 138 times.
✓ Branch 1 taken 138 times.
✓ Branch 4 taken 92 times.
✓ Branch 5 taken 46 times.
✓ Branch 6 taken 92 times.
✓ Branch 7 taken 184 times.
|
368 | && ((path.GetLength() == parent.GetLength()) |
| 58 |
2/2✓ Branch 2 taken 46 times.
✓ Branch 3 taken 46 times.
|
92 | || (path.GetChars()[parent.GetLength()] == '/') |
| 59 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 46 times.
|
46 | || (path.GetChars()[parent.GetLength() - 1] == '/'))) { |
| 60 | 92 | return true; | |
| 61 | } | ||
| 62 | |||
| 63 | 184 | return false; | |
| 64 | } | ||
| 65 | |||
| 66 | #ifdef CVMFS_NAMESPACE_GUARD | ||
| 67 | } // namespace CVMFS_NAMESPACE_GUARD | ||
| 68 | #endif | ||
| 69 |