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