24d3869fa15ce84bd2f08385f48e0982fbe23b91
[m6w6/libmemcached] / src / bin / memtouch.cc
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
14 */
15
16 #include "mem_config.h"
17
18 #define PROGRAM_NAME "memtouch"
19 #define PROGRAM_DESCRIPTION "Update the expiration time of an already existing key."
20 #define PROGRAM_VERSION "1.1"
21
22 #include "common/options.hpp"
23 #include "common/checks.hpp"
24
25 int main(int argc, char *argv[]) {
26 client_options opt{PROGRAM_NAME, PROGRAM_VERSION, PROGRAM_DESCRIPTION, "key [key ...]"};
27
28 for (const auto &def : opt.defaults) {
29 opt.add(def);
30 }
31
32 opt.add("expire", 'e', required_argument, "Expiration time in seconds or unix timestamp.");
33
34 char **argp = nullptr;
35 if (!opt.parse(argc, argv, &argp)) {
36 exit(EXIT_FAILURE);
37 }
38
39 memcached_st memc;
40 if (!check_memcached(opt, memc)) {
41 exit(EXIT_FAILURE);
42 }
43
44 if (!opt.apply(&memc)) {
45 memcached_free(&memc);
46 exit(EXIT_FAILURE);
47 }
48
49 if (!check_argp(opt, argp, "No key(s) provided.")) {
50 memcached_free(&memc);
51 exit(EXIT_FAILURE);
52 }
53
54 time_t expire = 0;
55 if (auto exp_str = opt.argof("expire")) {
56 expire = std::stoul(exp_str);
57 }
58
59 auto exit_code = EXIT_SUCCESS;
60 for (auto arg = argp; *arg; ++arg) {
61 auto key = *arg;
62 if (*key) {
63 if (!check_return(opt, memc, key, memcached_touch(&memc, key, strlen(key), expire))) {
64 exit_code = EXIT_FAILURE;
65 }
66 }
67 }
68
69 if (!check_buffering(opt, memc)) {
70 exit_code = EXIT_FAILURE;
71 }
72
73 memcached_free(&memc);
74 exit(exit_code);
75 }