| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/swissknife_lease_json.cc |
| Date: | 2025-11-09 02:35:23 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 51 | 88 | 58.0% |
| Branches: | 63 | 184 | 34.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.h" | ||
| 8 | #include "json_document.h" | ||
| 9 | #include "util/logging.h" | ||
| 10 | #include "util/pointer.h" | ||
| 11 | |||
| 12 | // TODO(@vvolkl): refactor | ||
| 13 | ✗ | LeaseReply ParseAcquireReplyWithRevision(const CurlBuffer &buffer, | |
| 14 | std::string *session_token, | ||
| 15 | uint64_t *current_revision, | ||
| 16 | std::string ¤t_root_hash) { | ||
| 17 | ✗ | if (buffer.data.size() == 0 || session_token == NULL) { | |
| 18 | ✗ | return kLeaseReplyFailure; | |
| 19 | } | ||
| 20 | |||
| 21 | ✗ | const UniquePtr<JsonDocument> reply(JsonDocument::Create(buffer.data)); | |
| 22 | ✗ | if (!reply.IsValid() || !reply->IsValid()) { | |
| 23 | ✗ | return kLeaseReplyFailure; | |
| 24 | } | ||
| 25 | |||
| 26 | ✗ | const JSON *result = JsonDocument::SearchInObject(reply->root(), "status", | |
| 27 | JSON_STRING); | ||
| 28 | ✗ | if (result != NULL) { | |
| 29 | ✗ | const std::string status = result->string_value; | |
| 30 | ✗ | if (status == "ok") { | |
| 31 | ✗ | LogCvmfs(kLogCvmfs, kLogStdout, "Gateway reply: ok"); | |
| 32 | ✗ | const JSON *token = JsonDocument::SearchInObject( | |
| 33 | reply->root(), "session_token", JSON_STRING); | ||
| 34 | ✗ | if (token != NULL) { | |
| 35 | ✗ | const JSON *rev = JsonDocument::SearchInObject( | |
| 36 | reply->root(), "revision", | ||
| 37 | JSON_INT); // TODO FIXME: make the json lib uint64 aware | ||
| 38 | ✗ | if (rev != NULL) { | |
| 39 | ✗ | *current_revision = (uint64_t)rev->int_value; | |
| 40 | } | ||
| 41 | ✗ | const JSON *hash = JsonDocument::SearchInObject( | |
| 42 | reply->root(), "root_hash", JSON_STRING); | ||
| 43 | ✗ | if (hash != NULL) { | |
| 44 | ✗ | current_root_hash = hash->string_value; | |
| 45 | } | ||
| 46 | ✗ | LogCvmfs(kLogCvmfs, kLogDebug, "Session token: %s", | |
| 47 | ✗ | token->string_value); | |
| 48 | ✗ | *session_token = token->string_value; | |
| 49 | ✗ | return kLeaseReplySuccess; | |
| 50 | } | ||
| 51 | ✗ | } else if (status == "path_busy") { | |
| 52 | ✗ | const JSON *time_remaining = JsonDocument::SearchInObject( | |
| 53 | reply->root(), "time_remaining", JSON_STRING); | ||
| 54 | ✗ | if (time_remaining != NULL) { | |
| 55 | ✗ | LogCvmfs(kLogCvmfs, kLogStdout, "Path busy. Time remaining = %s", | |
| 56 | ✗ | time_remaining->string_value); | |
| 57 | ✗ | return kLeaseReplyBusy; | |
| 58 | } | ||
| 59 | ✗ | } else if (status == "error") { | |
| 60 | ✗ | const JSON *reason = JsonDocument::SearchInObject(reply->root(), "reason", | |
| 61 | JSON_STRING); | ||
| 62 | ✗ | if (reason != NULL) { | |
| 63 | ✗ | LogCvmfs(kLogCvmfs, kLogStdout, "Error: %s", reason->string_value); | |
| 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 2 taken 147 times.
✗ Branch 3 not taken.
|
147 | const std::string status = result->string_value; |
| 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 | 49 | LogCvmfs(kLogCvmfs, kLogDebug, "Session token: %s", | |
| 94 |
1/2✓ Branch 1 taken 49 times.
✗ Branch 2 not taken.
|
49 | token->string_value); |
| 95 |
1/2✓ Branch 1 taken 49 times.
✗ Branch 2 not taken.
|
49 | *session_token = token->string_value; |
| 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 |
0/2✗ Branch 1 not taken.
✗ Branch 2 not taken.
|
49 | LogCvmfs(kLogCvmfs, kLogStdout, "Path busy. Time remaining = %d s", |
| 103 |
1/2✓ Branch 1 taken 49 times.
✗ Branch 2 not taken.
|
49 | time_remaining->int_value); |
| 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/2✓ Branch 1 taken 49 times.
✗ Branch 2 not taken.
|
49 | LogCvmfs(kLogCvmfs, kLogStdout, "Error: %s", reason->string_value); |
| 111 | } | ||
| 112 | } else { | ||
| 113 | ✗ | LogCvmfs(kLogCvmfs, kLogStdout, "Unknown reply. Status: %s", | |
| 114 | status.c_str()); | ||
| 115 | } | ||
| 116 |
2/2✓ Branch 1 taken 49 times.
✓ Branch 2 taken 98 times.
|
147 | } |
| 117 | |||
| 118 | 49 | return kLeaseReplyFailure; | |
| 119 | 196 | } | |
| 120 | |||
| 121 | |||
| 122 | 245 | LeaseReply ParseDropReply(const CurlBuffer &buffer) { | |
| 123 |
2/2✓ Branch 1 taken 49 times.
✓ Branch 2 taken 196 times.
|
245 | if (buffer.data.size() == 0) { |
| 124 | 49 | return kLeaseReplyFailure; | |
| 125 | } | ||
| 126 | |||
| 127 |
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)); |
| 128 |
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()) { |
| 129 | 49 | return kLeaseReplyFailure; | |
| 130 | } | ||
| 131 | |||
| 132 |
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", |
| 133 | JSON_STRING); | ||
| 134 |
1/2✓ Branch 0 taken 147 times.
✗ Branch 1 not taken.
|
147 | if (result != NULL) { |
| 135 |
1/2✓ Branch 2 taken 147 times.
✗ Branch 3 not taken.
|
147 | const std::string status = result->string_value; |
| 136 |
2/2✓ Branch 1 taken 49 times.
✓ Branch 2 taken 98 times.
|
147 | if (status == "ok") { |
| 137 |
1/2✓ Branch 1 taken 49 times.
✗ Branch 2 not taken.
|
49 | LogCvmfs(kLogCvmfs, kLogStdout, "Gateway reply: ok"); |
| 138 | 49 | return kLeaseReplySuccess; | |
| 139 |
2/2✓ Branch 1 taken 49 times.
✓ Branch 2 taken 49 times.
|
98 | } else if (status == "invalid_token") { |
| 140 |
1/2✓ Branch 1 taken 49 times.
✗ Branch 2 not taken.
|
49 | LogCvmfs(kLogCvmfs, kLogStdout, "Error: invalid session token"); |
| 141 |
1/2✓ Branch 1 taken 49 times.
✗ Branch 2 not taken.
|
49 | } else if (status == "error") { |
| 142 |
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", |
| 143 | JSON_STRING); | ||
| 144 |
1/2✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
|
49 | if (reason != NULL) { |
| 145 |
1/2✓ Branch 1 taken 49 times.
✗ Branch 2 not taken.
|
49 | LogCvmfs(kLogCvmfs, kLogStdout, "Error: %s", reason->string_value); |
| 146 | } | ||
| 147 | } else { | ||
| 148 | ✗ | LogCvmfs(kLogCvmfs, kLogStdout, "Unknown reply. Status: %s", | |
| 149 | status.c_str()); | ||
| 150 | } | ||
| 151 |
2/2✓ Branch 1 taken 98 times.
✓ Branch 2 taken 49 times.
|
147 | } |
| 152 | |||
| 153 | 98 | return kLeaseReplyFailure; | |
| 154 | 196 | } | |
| 155 |