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 +--------------------------------------------------------------------+
16 #include "mem_config.h"
18 #define PROGRAM_NAME "memcp"
19 #define PROGRAM_DESCRIPTION "Copy a set of files to a memcached cluster."
20 #define PROGRAM_VERSION "1.1"
22 #include "common/options.hpp"
23 #include "common/checks.hpp"
47 static void add_file(std::vector
<memcp_file
> &files
, const client_options
&opt
, const char *file
) {
48 memcp_file::type type
= memcp_file::type::basename
;
49 memcp_file::mode mode
= memcp_file::mode::SET
;
53 if (opt
.isset("absolute")) {
54 type
= memcp_file::type::absolute
;
55 } else if (opt
.isset("relative")) {
56 type
= memcp_file::type::relative
;
59 if (opt
.isset("replace")) {
60 mode
= memcp_file::mode::REPLACE
;
61 } else if (opt
.isset("add")) {
62 mode
= memcp_file::mode::ADD
;
65 if (auto flags_str
= opt
.argof("flags")) {
66 flags
= std::stoul(flags_str
);
68 if (auto expire_str
= opt
.argof("expire")) {
69 expire
= std::stoul(expire_str
);
72 if (opt
.isset("debug")) {
73 auto mode_str
= mode
== memcp_file::mode::REPLACE
? "REPLACE" : mode
== memcp_file::mode::ADD
? "ADD" : "SET";
74 std::cerr
<< "Scheduling " << mode_str
<< " '" << file
<< "' (expire=" << expire
<< ", flags=" << flags
<< ").\n";
77 files
.emplace_back(memcp_file
{type
, mode
, file
, flags
, expire
});
80 int main(int argc
, char *argv
[]) {
81 std::vector
<memcp_file
> files
{};
82 client_options opt
{PROGRAM_NAME
, PROGRAM_VERSION
, PROGRAM_DESCRIPTION
, "file [file ...]"};
84 opt
.add(nullptr, '-', no_argument
, "GNU argv extension")
85 .parse
= [&files
](client_options
&opt_
, client_options::extended_option
&ext
) {
86 add_file(files
, opt_
, ext
.arg
);
90 for (const auto &def
: opt
.defaults
) {
94 opt
.add("set", 'S', no_argument
, "Perform SET operations.")
95 .parse
= [](client_options
&opt_
, client_options::extended_option
&) {
97 opt_
.unset("replace");
100 opt
.add("add", 'A', no_argument
, "Perform ADD operations.")
101 .parse
= [](client_options
&opt_
, client_options::extended_option
&) {
103 opt_
.unset("replace");
106 opt
.add("replace", 'R', no_argument
, "Perform REPLACE operations.")
107 .parse
= [](client_options
&opt_
, client_options::extended_option
&) {
113 opt
.add("udp", 'U', no_argument
, "Use UDP.")
114 .apply
= [](const client_options
&opt
, const client_options::extended_option
&ext
, memcached_st
*memc
) {
115 if (MEMCACHED_SUCCESS
!= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, ext
.set
)) {
116 if (!opt
.isset("quiet")) {
117 std::cerr
<< memcached_last_error_message(memc
) << "\n";
123 opt
.add("flags", 'F', required_argument
, "Set key flags, too.");
124 opt
.add("expire", 'e', required_argument
, "Set expire time, too.");
126 opt
.add("basename", '.', no_argument
, "Use basename of path as key (default).");
127 opt
.add("relative", '+', no_argument
, "Use relative path (as passed), instead of basename only.");
128 opt
.add("absolute", '/', no_argument
, "Use absolute path (real path), instead of basename only.");
134 char **argp
= nullptr;
135 if (!opt
.parse(argc
, argv
, &argp
)) {
140 if (!check_memcached(opt
, memc
)) {
144 if (!opt
.apply(&memc
)) {
149 if (!check_argp(opt
, argp
, "No file(s) provided.")) {
150 memcached_free(&memc
);
153 for (auto arg
= argp
; *arg
; ++arg
) {
154 add_file(files
, opt
, *arg
);
158 auto exit_code
= EXIT_SUCCESS
;
159 for (const auto &file
: files
) {
160 auto filename
= file
.path
;
161 std::ifstream filestream
{filename
, std::ios::in
|std::ios::binary
};
164 if (!opt
.isset("quiet")) {
165 std::cerr
<< "Could not open file '" << filename
<< "'.\n";
167 exit_code
= EXIT_FAILURE
;
171 char rpath
[PATH_MAX
+1];
173 if (file
.key
== memcp_file::type::relative
) {
175 } else if (file
.key
== memcp_file::type::absolute
) {
176 path
= realpath(filename
, rpath
);
178 if (!opt
.isset("quiet")) {
181 exit_code
= EXIT_FAILURE
;
185 path
= basename(const_cast<char *>(filename
));
188 std::ostringstream data
{};
189 data
<< filestream
.rdbuf();
191 memcached_return_t rc
;
193 if (file
.op
== memcp_file::mode::REPLACE
) {
195 rc
= memcached_replace(&memc
, path
, strlen(path
), data
.str().c_str(), data
.str().length(),
196 file
.expire
, file
.flags
);
197 } else if (file
.op
== memcp_file::mode::ADD
) {
199 rc
= memcached_add(&memc
, path
, strlen(path
), data
.str().c_str(), data
.str().length(),
200 file
.expire
, file
.flags
);
203 rc
= memcached_set(&memc
, path
, strlen(path
), data
.str().c_str(), data
.str().length(),
204 file
.expire
, file
.flags
);
207 if (!memcached_success(rc
)) {
208 exit_code
= EXIT_FAILURE
;
210 auto error
= memcached_last_error(&memc
)
211 ? memcached_last_error_message(&memc
)
212 : memcached_strerror(&memc
, rc
);
213 std::cerr
<< "Error occurred during memcached_" << mode
<<"('" << path
<< "'): " << error
<< "\n";
217 if (opt
.isset("verbose")) {
218 std::cout
<< path
<< "\n";
223 if (!check_buffering(opt
, memc
)) {
224 exit_code
= EXIT_FAILURE
;
227 memcached_free(&memc
);