File: main.cpp

package info (click to toggle)
ccache 4.13.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,420 kB
  • sloc: cpp: 50,251; asm: 28,570; sh: 9,632; ansic: 5,357; python: 834; perl: 68; makefile: 24
file content (313 lines) | stat: -rw-r--r-- 8,205 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Copyright (C) 2025-2026 Joel Rosdahl and other contributors
//
// See doc/authors.adoc for a complete list of contributors.
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with
// this program; if not, write to the Free Software Foundation, Inc., 51
// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

#include <ccache/storage/remote/client.hpp>
#include <ccache/util/bytes.hpp>
#include <ccache/util/conversion.hpp>
#include <ccache/util/file.hpp>
#include <ccache/util/format.hpp>
#include <ccache/util/string.hpp>

#include <chrono>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>

#ifdef HAVE_UNISTD_H
#  include <unistd.h>
#endif

using namespace std::chrono_literals;

namespace {

const auto k_data_timeout = 1000ms;    // default for data-timeout property
const auto k_request_timeout = 5000ms; // default for request-timeout property

constexpr const char USAGE_TEXT[] =
  R"(Usage: {0} IPC_ENDPOINT COMMAND [args...]

This is a CLI tool for testing ccache storage helper implementations.

Commands:
    ping                            check if helper is reachable
    get KEY -o FILE                 get a value and output to file
    get KEY -o -                    get a value and output to stdout
    put [--overwrite] KEY -i FILE   put a value from file
    put [--overwrite] KEY -i -      put a value from stdin
    put [--overwrite] KEY -v VALUE  put a literal value
    remove KEY                      remove a value from storage
    stop                            tell the helper to stop

Notes:
    KEY must be a hexadecimal string (0-9, a-f, A-F).
    IPC_ENDPOINT is a Unix socket path or Windows named pipe name.
)";

void
print_usage(FILE* stream, const char* program_name)
{
  PRINT(stream, USAGE_TEXT, program_name);
}

int
cmd_get(storage::remote::Client& client, const std::vector<std::string>& args)
{
  if (args.size() != 3 || args[1] != "-o") {
    PRINT(stderr, "Error: get requires: KEY -o OUTPUT\n");
    PRINT(stderr, "  where OUTPUT is a file path or - for stdout\n");
    return 1;
  }

  auto key_result = util::parse_base16(args[0]);
  if (!key_result) {
    PRINT(stderr, "Error: Invalid hex key: {}\n", key_result.error());
    return 1;
  }
  const auto& key = *key_result;
  const auto& output = args[2];

  auto result = client.get(key);

  if (!result) {
    PRINT(stderr, "Error: {}\n", result.error().message);
    return 1;
  }

  if (!*result) {
    PRINT(stderr, "Key not found: {}\n", util::format_base16(key));
    return 2;
  }

  const auto& value = **result;

  if (output == "-") {
    std::fwrite(value.data(), 1, value.size(), stdout);
  } else {
    if (auto r = util::write_file(output, value); !r) {
      PRINT(stderr, "Error writing to {}: {}", output, r.error());
      return 1;
    }
  }

  return 0;
}

int
cmd_put(storage::remote::Client& client, const std::vector<std::string>& args)
{
  storage::remote::Client::PutFlags flags;
  size_t start_idx = 0;

  if (!args.empty() && args[0] == "--overwrite") {
    flags.overwrite = true;
    start_idx = 1;
  }

  if (args.size() - start_idx != 3) {
    PRINT(stderr,
          "Error: put requires: [--overwrite] KEY -i INPUT\n"
          "                 or: [--overwrite] KEY -v VALUE\n"
          "  where INPUT is a file path or - for stdin\n");
    return 1;
  }

  auto key_result = util::parse_base16(args[start_idx]);
  if (!key_result) {
    PRINT(stderr, "Error: Invalid hex key: {}\n", key_result.error());
    return 1;
  }
  const auto& key = *key_result;
  const auto& mode = args[start_idx + 1];
  const auto& input = args[start_idx + 2];

  util::Bytes value;

  if (mode == "-v") {
    value = input;
  } else if (mode == "-i") {
    if (input == "-") {
      auto r = util::read_fd(STDIN_FILENO);
      if (!r) {
        PRINT(stderr, "Error reading from stdin: {}", r.error());
        return 1;
      }
      value = std::move(*r);
    } else {
      auto r = util::read_file<util::Bytes>(input);
      if (!r) {
        PRINT(stderr, "Error reading from {}: {}", input, r.error());
        return 1;
      }
      value = std::move(*r);
    }
  } else {
    PRINT(stderr, "Error: Unknown mode \"{}\". Use -v or -i\n", mode);
    return 1;
  }

  auto result = client.put(key, value, flags);

  if (!result) {
    PRINT(stderr, "Error: {}\n", result.error().message);
    return 1;
  }

  if (*result) {
    PRINT(stdout, "Stored key: {}\n", util::format_base16(key));
    return 0;
  } else {
    PRINT(stderr, "Not stored: {}\n", util::format_base16(key));
    return 2;
  }
}

int
cmd_remove(storage::remote::Client& client,
           const std::vector<std::string>& args)
{
  if (args.size() != 1) {
    PRINT(stderr, "Error: remove requires exactly 1 argument: KEY\n");
    return 1;
  }

  auto key_result = util::parse_base16(args[0]);
  if (!key_result) {
    PRINT(stderr, "Error: Invalid hex key: {}\n", key_result.error());
    return 1;
  }
  const auto& key = *key_result;

  auto result = client.remove(key);

  if (!result) {
    PRINT(stderr, "Error: {}\n", result.error().message);
    return 1;
  }

  if (*result) {
    PRINT(stdout, "Removed key: {}\n", util::format_base16(key));
    return 0;
  } else {
    PRINT(stderr, "Not removed: {}\n", util::format_base16(key));
    return 2;
  }
}

int
cmd_stop(storage::remote::Client& client, const std::vector<std::string>& args)
{
  if (!args.empty()) {
    PRINT(stderr, "Error: stop takes no arguments\n");
    return 1;
  }

  auto result = client.stop();

  if (!result) {
    PRINT(stderr, "Error: {}\n", result.error().message);
    return 1;
  }

  PRINT(stdout, "Helper stopped\n");
  return 0;
}

int
cmd_ping(storage::remote::Client& client, const std::vector<std::string>& args)
{
  if (!args.empty()) {
    PRINT(stderr, "Error: ping takes no arguments\n");
    return 1;
  }

  // Connection and protocol verification already done in main.
  PRINT(stdout, "Helper is reachable\n");
  return 0;
}

} // namespace

int
main(int argc, char* argv[])
{
  if (argc >= 2
      && (std::string_view(argv[1]) == "-h"
          || std::string_view(argv[1]) == "--help")) {
    print_usage(stdout, argv[0]);
    return 0;
  }
  if (argc < 3) {
    print_usage(stderr, argv[0]);
    return 1;
  }

  const std::string ipc_endpoint =
#ifdef _WIN32
    FMT("\\\\.\\pipe\\{}", argv[1]);
#else
    argv[1];
#endif
  const std::string command = argv[2];

  std::vector<std::string> cmd_args;
  for (int i = 3; i < argc; ++i) {
    cmd_args.push_back(argv[i]);
  }

  storage::remote::Client client(k_data_timeout, k_request_timeout);
  auto connect_result = client.connect(ipc_endpoint);

  if (!connect_result) {
    PRINT(stderr,
          "Failed to connect to {}: {}\n",
          ipc_endpoint,
          connect_result.error().message);
    return 1;
  }

  if (client.protocol_version()
      != storage::remote::Client::k_protocol_version) {
    PRINT(
      stderr, "Unsupported protocol version: {}\n", client.protocol_version());
    return 1;
  }

  if (!client.has_capability(
        storage::remote::Client::Capability::get_put_remove_stop)) {
    PRINT(stderr, "Helper does not support get/put/remove/stop operations\n");
    return 1;
  }

  if (command == "ping") {
    return cmd_ping(client, cmd_args);
  } else if (command == "get") {
    return cmd_get(client, cmd_args);
  } else if (command == "put") {
    return cmd_put(client, cmd_args);
  } else if (command == "remove") {
    return cmd_remove(client, cmd_args);
  } else if (command == "stop") {
    return cmd_stop(client, cmd_args);
  } else {
    PRINT(stderr, "Unknown command: {}\n\n", command);
    print_usage(stderr, argv[0]);
    return 1;
  }
}