| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/swissknife_lease_json.cc |
| Date: | 2026-08-30 02:40:36 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 55 | 93 | 59.1% |
| Branches: | 71 | 212 | 33.5% |
| 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 <memory> | ||
| 8 | |||
| 9 | #include "json_document.h" | ||
| 10 | #include "util/logging.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 std::unique_ptr<JsonDocument> reply(JsonDocument::Create(buffer.data)); | |
| 22 | ✗ | if (reply.get() == nullptr || !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->get<std::string>(); | |
| 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->get<int>(); | |
| 40 | } | ||
| 41 | ✗ | const JSON *hash = JsonDocument::SearchInObject( | |
| 42 | reply->root(), "root_hash", JSON_STRING); | ||
| 43 | ✗ | if (hash != NULL) { | |
| 44 | ✗ | current_root_hash = hash->get<std::string>(); | |
| 45 | } | ||
| 46 | ✗ | LogCvmfs(kLogCvmfs, kLogDebug, "Session token: %s", | |
| 47 | ✗ | token->get<std::string>().c_str()); | |
| 48 | ✗ | *session_token = token->get<std::string>(); | |
| 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->get<std::string>().c_str()); | |
| 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", | |
| 64 | ✗ | reason->get<std::string>().c_str()); | |
| 65 | } | ||
| 66 | } else { | ||
| 67 | ✗ | LogCvmfs(kLogCvmfs, kLogStdout, "Unknown reply. Status: %s", | |
| 68 | status.c_str()); | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | ✗ | return kLeaseReplyFailure; | |
| 73 | } | ||
| 74 | 210 | LeaseReply ParseAcquireReply(const CurlBuffer &buffer, | |
| 75 | std::string *session_token, | ||
| 76 | int *max_api_version) { | ||
| 77 |
5/6✓ Branch 1 taken 150 times.
✓ Branch 2 taken 60 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 150 times.
✓ Branch 5 taken 60 times.
✓ Branch 6 taken 150 times.
|
210 | if (buffer.data.size() == 0 || session_token == NULL) { |
| 78 | 60 | return kLeaseReplyFailure; | |
| 79 | } | ||
| 80 | |||
| 81 |
1/2✓ Branch 1 taken 150 times.
✗ Branch 2 not taken.
|
150 | const std::unique_ptr<JsonDocument> reply(JsonDocument::Create(buffer.data)); |
| 82 |
5/6✓ Branch 1 taken 120 times.
✓ Branch 2 taken 30 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 120 times.
✓ Branch 7 taken 30 times.
✓ Branch 8 taken 120 times.
|
150 | if (reply.get() == nullptr || !reply->IsValid()) { |
| 83 | 30 | return kLeaseReplyFailure; | |
| 84 | } | ||
| 85 | |||
| 86 |
2/4✓ Branch 2 taken 120 times.
✗ Branch 3 not taken.
✓ Branch 7 taken 120 times.
✗ Branch 8 not taken.
|
120 | const JSON *result = JsonDocument::SearchInObject(reply->root(), "status", |
| 87 | JSON_STRING); | ||
| 88 |
1/2✓ Branch 0 taken 120 times.
✗ Branch 1 not taken.
|
120 | if (result != NULL) { |
| 89 |
1/2✓ Branch 1 taken 120 times.
✗ Branch 2 not taken.
|
120 | const std::string status = result->get<std::string>(); |
| 90 |
2/2✓ Branch 1 taken 60 times.
✓ Branch 2 taken 60 times.
|
120 | if (status == "ok") { |
| 91 |
1/2✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
|
60 | LogCvmfs(kLogCvmfs, kLogStdout, "Gateway reply: ok"); |
| 92 |
2/4✓ Branch 2 taken 60 times.
✗ Branch 3 not taken.
✓ Branch 7 taken 60 times.
✗ Branch 8 not taken.
|
60 | const JSON *token = JsonDocument::SearchInObject( |
| 93 | reply->root(), "session_token", JSON_STRING); | ||
| 94 |
1/2✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
|
60 | if (token != NULL) { |
| 95 |
1/2✓ Branch 2 taken 60 times.
✗ Branch 3 not taken.
|
60 | LogCvmfs(kLogCvmfs, kLogDebug, "Session token: %s", |
| 96 |
1/2✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
|
120 | token->get<std::string>().c_str()); |
| 97 |
1/2✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
|
60 | *session_token = token->get<std::string>(); |
| 98 |
2/4✓ Branch 2 taken 60 times.
✗ Branch 3 not taken.
✓ Branch 7 taken 60 times.
✗ Branch 8 not taken.
|
60 | const JSON *api_version = JsonDocument::SearchInObject( |
| 99 | reply->root(), "max_api_version", JSON_INT); | ||
| 100 |
3/4✓ Branch 0 taken 30 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
|
60 | if (api_version != NULL && max_api_version != NULL) { |
| 101 |
1/2✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
|
30 | *max_api_version = api_version->get<int>(); |
| 102 | } | ||
| 103 | 60 | return kLeaseReplySuccess; | |
| 104 | } | ||
| 105 |
2/2✓ Branch 1 taken 30 times.
✓ Branch 2 taken 30 times.
|
60 | } else if (status == "path_busy") { |
| 106 |
2/4✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
✓ Branch 7 taken 30 times.
✗ Branch 8 not taken.
|
30 | const JSON *time_remaining = JsonDocument::SearchInObject( |
| 107 | reply->root(), "time_remaining", JSON_INT); | ||
| 108 |
1/2✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
|
30 | if (time_remaining != NULL) { |
| 109 |
2/4✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 30 times.
✗ Branch 5 not taken.
|
30 | LogCvmfs(kLogCvmfs, kLogStdout, "Path busy. Time remaining = %d s", |
| 110 | time_remaining->get<int>()); | ||
| 111 | 30 | return kLeaseReplyBusy; | |
| 112 | } | ||
| 113 |
1/2✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
|
30 | } else if (status == "error") { |
| 114 |
2/4✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
✓ Branch 7 taken 30 times.
✗ Branch 8 not taken.
|
30 | const JSON *reason = JsonDocument::SearchInObject(reply->root(), "reason", |
| 115 | JSON_STRING); | ||
| 116 |
1/2✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
|
30 | if (reason != NULL) { |
| 117 |
1/5✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
30 | LogCvmfs(kLogCvmfs, kLogStdout, "Error: %s", |
| 118 |
1/2✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
|
60 | reason->get<std::string>().c_str()); |
| 119 | } | ||
| 120 | } else { | ||
| 121 | ✗ | LogCvmfs(kLogCvmfs, kLogStdout, "Unknown reply. Status: %s", | |
| 122 | status.c_str()); | ||
| 123 | } | ||
| 124 |
2/2✓ Branch 1 taken 30 times.
✓ Branch 2 taken 90 times.
|
120 | } |
| 125 | |||
| 126 | 30 | return kLeaseReplyFailure; | |
| 127 | 150 | } | |
| 128 | |||
| 129 | |||
| 130 | 150 | LeaseReply ParseDropReply(const CurlBuffer &buffer) { | |
| 131 |
2/2✓ Branch 1 taken 30 times.
✓ Branch 2 taken 120 times.
|
150 | if (buffer.data.size() == 0) { |
| 132 | 30 | return kLeaseReplyFailure; | |
| 133 | } | ||
| 134 | |||
| 135 | const std::unique_ptr<const JsonDocument> reply( | ||
| 136 |
1/2✓ Branch 1 taken 120 times.
✗ Branch 2 not taken.
|
120 | JsonDocument::Create(buffer.data)); |
| 137 |
5/6✓ Branch 1 taken 90 times.
✓ Branch 2 taken 30 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 90 times.
✓ Branch 7 taken 30 times.
✓ Branch 8 taken 90 times.
|
120 | if (reply.get() == nullptr || !reply->IsValid()) { |
| 138 | 30 | return kLeaseReplyFailure; | |
| 139 | } | ||
| 140 | |||
| 141 |
2/4✓ Branch 2 taken 90 times.
✗ Branch 3 not taken.
✓ Branch 7 taken 90 times.
✗ Branch 8 not taken.
|
90 | const JSON *result = JsonDocument::SearchInObject(reply->root(), "status", |
| 142 | JSON_STRING); | ||
| 143 |
1/2✓ Branch 0 taken 90 times.
✗ Branch 1 not taken.
|
90 | if (result != NULL) { |
| 144 |
1/2✓ Branch 1 taken 90 times.
✗ Branch 2 not taken.
|
90 | const std::string status = result->get<std::string>(); |
| 145 |
2/2✓ Branch 1 taken 30 times.
✓ Branch 2 taken 60 times.
|
90 | if (status == "ok") { |
| 146 |
1/2✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
|
30 | LogCvmfs(kLogCvmfs, kLogStdout, "Gateway reply: ok"); |
| 147 | 30 | return kLeaseReplySuccess; | |
| 148 |
2/2✓ Branch 1 taken 30 times.
✓ Branch 2 taken 30 times.
|
60 | } else if (status == "invalid_token") { |
| 149 |
1/2✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
|
30 | LogCvmfs(kLogCvmfs, kLogStdout, "Error: invalid session token"); |
| 150 |
1/2✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
|
30 | } else if (status == "error") { |
| 151 |
2/4✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
✓ Branch 7 taken 30 times.
✗ Branch 8 not taken.
|
30 | const JSON *reason = JsonDocument::SearchInObject(reply->root(), "reason", |
| 152 | JSON_STRING); | ||
| 153 |
1/2✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
|
30 | if (reason != NULL) { |
| 154 |
1/5✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
30 | LogCvmfs(kLogCvmfs, kLogStdout, "Error: %s", |
| 155 |
1/2✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
|
60 | reason->get<std::string>().c_str()); |
| 156 | } | ||
| 157 | } else { | ||
| 158 | ✗ | LogCvmfs(kLogCvmfs, kLogStdout, "Unknown reply. Status: %s", | |
| 159 | status.c_str()); | ||
| 160 | } | ||
| 161 |
2/2✓ Branch 1 taken 60 times.
✓ Branch 2 taken 30 times.
|
90 | } |
| 162 | |||
| 163 | 60 | return kLeaseReplyFailure; | |
| 164 | 120 | } | |
| 165 |