| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/swissknife_lease_json.cc |
| Date: | 2026-04-05 02:35:23 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 52 | 90 | 57.8% |
| Branches: | 67 | 208 | 32.2% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * This file is part of the CernVM File System. | ||
| 3 | */ | ||
| 4 | |||
| 5 | #include "swissknife_lease_json.h" | ||
| 6 | |||
| 7 | #include "json_document.h" | ||
| 8 | #include "util/logging.h" | ||
| 9 | #include "util/pointer.h" | ||
| 10 | |||
| 11 | // TODO(@vvolkl): refactor | ||
| 12 | ✗ | LeaseReply ParseAcquireReplyWithRevision(const CurlBuffer &buffer, | |
| 13 | std::string *session_token, | ||
| 14 | uint64_t *current_revision, | ||
| 15 | std::string ¤t_root_hash) { | ||
| 16 | ✗ | if (buffer.data.size() == 0 || session_token == NULL) { | |
| 17 | ✗ | return kLeaseReplyFailure; | |
| 18 | } | ||
| 19 | |||
| 20 | ✗ | const UniquePtr<JsonDocument> reply(JsonDocument::Create(buffer.data)); | |
| 21 | ✗ | if (!reply.IsValid() || !reply->IsValid()) { | |
| 22 | ✗ | return kLeaseReplyFailure; | |
| 23 | } | ||
| 24 | |||
| 25 | ✗ | const JSON *result = JsonDocument::SearchInObject(reply->root(), "status", | |
| 26 | JSON_STRING); | ||
| 27 | ✗ | if (result != NULL) { | |
| 28 | ✗ | const std::string status = result->get<std::string>(); | |
| 29 | ✗ | if (status == "ok") { | |
| 30 | ✗ | LogCvmfs(kLogCvmfs, kLogStdout, "Gateway reply: ok"); | |
| 31 | ✗ | const JSON *token = JsonDocument::SearchInObject( | |
| 32 | reply->root(), "session_token", JSON_STRING); | ||
| 33 | ✗ | if (token != NULL) { | |
| 34 | ✗ | const JSON *rev = JsonDocument::SearchInObject( | |
| 35 | reply->root(), "revision", | ||
| 36 | JSON_INT); // TODO FIXME: make the json lib uint64 aware | ||
| 37 | ✗ | if (rev != NULL) { | |
| 38 | ✗ | *current_revision = (uint64_t)rev->get<int>(); | |
| 39 | } | ||
| 40 | ✗ | const JSON *hash = JsonDocument::SearchInObject( | |
| 41 | reply->root(), "root_hash", JSON_STRING); | ||
| 42 | ✗ | if (hash != NULL) { | |
| 43 | ✗ | current_root_hash = hash->get<std::string>(); | |
| 44 | } | ||
| 45 | ✗ | LogCvmfs(kLogCvmfs, kLogDebug, "Session token: %s", | |
| 46 | ✗ | token->get<std::string>().c_str()); | |
| 47 | ✗ | *session_token = token->get<std::string>(); | |
| 48 | ✗ | return kLeaseReplySuccess; | |
| 49 | } | ||
| 50 | ✗ | } else if (status == "path_busy") { | |
| 51 | ✗ | const JSON *time_remaining = JsonDocument::SearchInObject( | |
| 52 | reply->root(), "time_remaining", JSON_STRING); | ||
| 53 | ✗ | if (time_remaining != NULL) { | |
| 54 | ✗ | LogCvmfs(kLogCvmfs, kLogStdout, "Path busy. Time remaining = %s", | |
| 55 | ✗ | time_remaining->get<std::string>().c_str()); | |
| 56 | ✗ | return kLeaseReplyBusy; | |
| 57 | } | ||
| 58 | ✗ | } else if (status == "error") { | |
| 59 | ✗ | const JSON *reason = JsonDocument::SearchInObject(reply->root(), "reason", | |
| 60 | JSON_STRING); | ||
| 61 | ✗ | if (reason != NULL) { | |
| 62 | ✗ | LogCvmfs(kLogCvmfs, kLogStdout, "Error: %s", | |
| 63 | ✗ | reason->get<std::string>().c_str()); | |
| 64 | } | ||
| 65 | } else { | ||
| 66 | ✗ | LogCvmfs(kLogCvmfs, kLogStdout, "Unknown reply. Status: %s", | |
| 67 | status.c_str()); | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | ✗ | return kLeaseReplyFailure; | |
| 72 | } | ||
| 73 | 294 | LeaseReply ParseAcquireReply(const CurlBuffer &buffer, | |
| 74 | std::string *session_token) { | ||
| 75 |
5/6✓ Branch 1 taken 196 times.
✓ Branch 2 taken 98 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 196 times.
✓ Branch 5 taken 98 times.
✓ Branch 6 taken 196 times.
|
294 | if (buffer.data.size() == 0 || session_token == NULL) { |
| 76 | 98 | return kLeaseReplyFailure; | |
| 77 | } | ||
| 78 | |||
| 79 |
2/4✓ Branch 1 taken 196 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 196 times.
✗ Branch 5 not taken.
|
196 | const UniquePtr<JsonDocument> reply(JsonDocument::Create(buffer.data)); |
| 80 |
5/6✓ Branch 1 taken 147 times.
✓ Branch 2 taken 49 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 147 times.
✓ Branch 7 taken 49 times.
✓ Branch 8 taken 147 times.
|
196 | if (!reply.IsValid() || !reply->IsValid()) { |
| 81 | 49 | return kLeaseReplyFailure; | |
| 82 | } | ||
| 83 | |||
| 84 |
2/4✓ Branch 2 taken 147 times.
✗ Branch 3 not taken.
✓ Branch 7 taken 147 times.
✗ Branch 8 not taken.
|
147 | const JSON *result = JsonDocument::SearchInObject(reply->root(), "status", |
| 85 | JSON_STRING); | ||
| 86 |
1/2✓ Branch 0 taken 147 times.
✗ Branch 1 not taken.
|
147 | if (result != NULL) { |
| 87 |
1/2✓ Branch 1 taken 147 times.
✗ Branch 2 not taken.
|
147 | const std::string status = result->get<std::string>(); |
| 88 |
2/2✓ Branch 1 taken 49 times.
✓ Branch 2 taken 98 times.
|
147 | if (status == "ok") { |
| 89 |
1/2✓ Branch 1 taken 49 times.
✗ Branch 2 not taken.
|
49 | LogCvmfs(kLogCvmfs, kLogStdout, "Gateway reply: ok"); |
| 90 |
2/4✓ Branch 2 taken 49 times.
✗ Branch 3 not taken.
✓ Branch 7 taken 49 times.
✗ Branch 8 not taken.
|
49 | const JSON *token = JsonDocument::SearchInObject( |
| 91 | reply->root(), "session_token", JSON_STRING); | ||
| 92 |
1/2✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
|
49 | if (token != NULL) { |
| 93 |
1/2✓ Branch 2 taken 49 times.
✗ Branch 3 not taken.
|
49 | LogCvmfs(kLogCvmfs, kLogDebug, "Session token: %s", |
| 94 |
1/2✓ Branch 1 taken 49 times.
✗ Branch 2 not taken.
|
98 | token->get<std::string>().c_str()); |
| 95 |
1/2✓ Branch 1 taken 49 times.
✗ Branch 2 not taken.
|
49 | *session_token = token->get<std::string>(); |
| 96 | 49 | return kLeaseReplySuccess; | |
| 97 | } | ||
| 98 |
2/2✓ Branch 1 taken 49 times.
✓ Branch 2 taken 49 times.
|
98 | } else if (status == "path_busy") { |
| 99 |
2/4✓ Branch 2 taken 49 times.
✗ Branch 3 not taken.
✓ Branch 7 taken 49 times.
✗ Branch 8 not taken.
|
49 | const JSON *time_remaining = JsonDocument::SearchInObject( |
| 100 | reply->root(), "time_remaining", JSON_INT); | ||
| 101 |
1/2✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
|
49 | if (time_remaining != NULL) { |
| 102 |
2/4✓ Branch 1 taken 49 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 49 times.
✗ Branch 5 not taken.
|
49 | LogCvmfs(kLogCvmfs, kLogStdout, "Path busy. Time remaining = %d s", |
| 103 | time_remaining->get<int>()); | ||
| 104 | 49 | return kLeaseReplyBusy; | |
| 105 | } | ||
| 106 |
1/2✓ Branch 1 taken 49 times.
✗ Branch 2 not taken.
|
49 | } else if (status == "error") { |
| 107 |
2/4✓ Branch 2 taken 49 times.
✗ Branch 3 not taken.
✓ Branch 7 taken 49 times.
✗ Branch 8 not taken.
|
49 | const JSON *reason = JsonDocument::SearchInObject(reply->root(), "reason", |
| 108 | JSON_STRING); | ||
| 109 |
1/2✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
|
49 | if (reason != NULL) { |
| 110 |
1/5✗ Branch 1 not taken.
✓ Branch 2 taken 49 times.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
49 | LogCvmfs(kLogCvmfs, kLogStdout, "Error: %s", |
| 111 |
1/2✓ Branch 1 taken 49 times.
✗ Branch 2 not taken.
|
98 | reason->get<std::string>().c_str()); |
| 112 | } | ||
| 113 | } else { | ||
| 114 | ✗ | LogCvmfs(kLogCvmfs, kLogStdout, "Unknown reply. Status: %s", | |
| 115 | status.c_str()); | ||
| 116 | } | ||
| 117 |
2/2✓ Branch 1 taken 49 times.
✓ Branch 2 taken 98 times.
|
147 | } |
| 118 | |||
| 119 | 49 | return kLeaseReplyFailure; | |
| 120 | 196 | } | |
| 121 | |||
| 122 | |||
| 123 | 245 | LeaseReply ParseDropReply(const CurlBuffer &buffer) { | |
| 124 |
2/2✓ Branch 1 taken 49 times.
✓ Branch 2 taken 196 times.
|
245 | if (buffer.data.size() == 0) { |
| 125 | 49 | return kLeaseReplyFailure; | |
| 126 | } | ||
| 127 | |||
| 128 |
2/4✓ Branch 1 taken 196 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 196 times.
✗ Branch 5 not taken.
|
196 | const UniquePtr<const JsonDocument> reply(JsonDocument::Create(buffer.data)); |
| 129 |
5/6✓ Branch 1 taken 147 times.
✓ Branch 2 taken 49 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 147 times.
✓ Branch 7 taken 49 times.
✓ Branch 8 taken 147 times.
|
196 | if (!reply.IsValid() || !reply->IsValid()) { |
| 130 | 49 | return kLeaseReplyFailure; | |
| 131 | } | ||
| 132 | |||
| 133 |
2/4✓ Branch 2 taken 147 times.
✗ Branch 3 not taken.
✓ Branch 7 taken 147 times.
✗ Branch 8 not taken.
|
147 | const JSON *result = JsonDocument::SearchInObject(reply->root(), "status", |
| 134 | JSON_STRING); | ||
| 135 |
1/2✓ Branch 0 taken 147 times.
✗ Branch 1 not taken.
|
147 | if (result != NULL) { |
| 136 |
1/2✓ Branch 1 taken 147 times.
✗ Branch 2 not taken.
|
147 | const std::string status = result->get<std::string>(); |
| 137 |
2/2✓ Branch 1 taken 49 times.
✓ Branch 2 taken 98 times.
|
147 | if (status == "ok") { |
| 138 |
1/2✓ Branch 1 taken 49 times.
✗ Branch 2 not taken.
|
49 | LogCvmfs(kLogCvmfs, kLogStdout, "Gateway reply: ok"); |
| 139 | 49 | return kLeaseReplySuccess; | |
| 140 |
2/2✓ Branch 1 taken 49 times.
✓ Branch 2 taken 49 times.
|
98 | } else if (status == "invalid_token") { |
| 141 |
1/2✓ Branch 1 taken 49 times.
✗ Branch 2 not taken.
|
49 | LogCvmfs(kLogCvmfs, kLogStdout, "Error: invalid session token"); |
| 142 |
1/2✓ Branch 1 taken 49 times.
✗ Branch 2 not taken.
|
49 | } else if (status == "error") { |
| 143 |
2/4✓ Branch 2 taken 49 times.
✗ Branch 3 not taken.
✓ Branch 7 taken 49 times.
✗ Branch 8 not taken.
|
49 | const JSON *reason = JsonDocument::SearchInObject(reply->root(), "reason", |
| 144 | JSON_STRING); | ||
| 145 |
1/2✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
|
49 | if (reason != NULL) { |
| 146 |
1/5✗ Branch 1 not taken.
✓ Branch 2 taken 49 times.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
49 | LogCvmfs(kLogCvmfs, kLogStdout, "Error: %s", |
| 147 |
1/2✓ Branch 1 taken 49 times.
✗ Branch 2 not taken.
|
98 | reason->get<std::string>().c_str()); |
| 148 | } | ||
| 149 | } else { | ||
| 150 | ✗ | LogCvmfs(kLogCvmfs, kLogStdout, "Unknown reply. Status: %s", | |
| 151 | status.c_str()); | ||
| 152 | } | ||
| 153 |
2/2✓ Branch 1 taken 98 times.
✓ Branch 2 taken 49 times.
|
147 | } |
| 154 | |||
| 155 | 98 | return kLeaseReplyFailure; | |
| 156 | 196 | } | |
| 157 |