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"
36 # define PATH_MAX MAX_PATH
58 static inline std::string
stream2string(const std::istream
&istream
) {
59 return dynamic_cast<std::ostringstream
&&>(std::ostringstream
{} << istream
.rdbuf()).str();
62 static memcached_return_t
memcp(const client_options
&opt
, memcached_st
&memc
, const char *key
,
63 const memcp_file
&file
) {
64 std::ifstream fstream
{};
65 std::istream
*istream
= check_istream(opt
, file
.path
, fstream
);
68 return MEMCACHED_ERROR
;
72 memcached_return_t rc
;
73 auto data
= stream2string(*istream
);
74 if (file
.op
== memcp_file::mode::REPLACE
) {
76 rc
= memcached_replace(&memc
, key
, strlen(key
), data
.c_str(), data
.length(),
77 file
.expire
, file
.flags
);
78 } else if (file
.op
== memcp_file::mode::ADD
) {
80 rc
= memcached_add(&memc
, key
, strlen(key
), data
.c_str(), data
.length(),
81 file
.expire
, file
.flags
);
84 rc
= memcached_set(&memc
, key
, strlen(key
), data
.c_str(), data
.length(),
85 file
.expire
, file
.flags
);
88 if (!memcached_success(rc
)) {
89 auto error
= memcached_last_error(&memc
)
90 ? memcached_last_error_message(&memc
)
91 : memcached_strerror(&memc
, rc
);
92 std::cerr
<< "Error occurred during memcached_" << mode
<<"('" << key
<< "'): " << error
<< "\n";
97 static void add_file(std::vector
<memcp_file
> &files
, const client_options
&opt
, char *file
) {
98 memcp_file::type type
= memcp_file::type::basename
;
99 memcp_file::mode mode
= memcp_file::mode::SET
;
103 if (opt
.isset("absolute")) {
104 type
= memcp_file::type::absolute
;
105 } else if (opt
.isset("relative")) {
106 type
= memcp_file::type::relative
;
109 if (opt
.isset("replace")) {
110 mode
= memcp_file::mode::REPLACE
;
111 } else if (opt
.isset("add")) {
112 mode
= memcp_file::mode::ADD
;
115 if (auto flags_str
= opt
.argof("flags")) {
116 flags
= std::stoul(flags_str
);
118 if (auto expire_str
= opt
.argof("expire")) {
119 expire
= std::stoul(expire_str
);
122 if (opt
.isset("debug")) {
123 auto mode_str
= mode
== memcp_file::mode::REPLACE
? "REPLACE" : mode
== memcp_file::mode::ADD
? "ADD" : "SET";
124 std::cerr
<< "Scheduling " << mode_str
<< " '" << file
<< "' (expire=" << expire
<< ", flags=" << flags
<< ").\n";
127 files
.emplace_back(memcp_file
{type
, mode
, file
, flags
, expire
});
130 static bool path2key(const client_options
&opt
, memcp_file
&file
, char **path
) {
131 static char rpath
[PATH_MAX
+ 1];
132 if (file
.key
== memcp_file::type::absolute
) {
133 *path
= realpath(file
.path
, rpath
);
135 if (!opt
.isset("quiet")) {
140 } else if (file
.key
== memcp_file::type::relative
) {
143 *path
= basename(file
.path
);
148 int main(int argc
, char *argv
[]) {
149 std::vector
<memcp_file
> files
{};
150 client_options opt
{PROGRAM_NAME
, PROGRAM_VERSION
, PROGRAM_DESCRIPTION
,
152 "\n\t\t\t# NOTE: order of flags and positional"
153 "\n\t\t\t# arguments matters on GNU systems)"};
155 opt
.add(nullptr, '-', no_argument
, "GNU argv extension")
156 .parse
= [&files
](client_options
&opt_
, client_options::extended_option
&ext
) {
157 add_file(files
, opt_
, ext
.arg
);
161 for (const auto &def
: opt
.defaults
) {
165 opt
.add("set", 'S', no_argument
, "Perform SET operations.")
166 .parse
= [](client_options
&opt_
, client_options::extended_option
&) {
168 opt_
.unset("replace");
171 opt
.add("add", 'A', no_argument
, "Perform ADD operations.")
172 .parse
= [](client_options
&opt_
, client_options::extended_option
&) {
174 opt_
.unset("replace");
177 opt
.add("replace", 'R', no_argument
, "Perform REPLACE operations.")
178 .parse
= [](client_options
&opt_
, client_options::extended_option
&) {
184 opt
.add("udp", 'U', no_argument
, "Use UDP.")
185 .apply
= [](const client_options
&opt_
, const client_options::extended_option
&ext
, memcached_st
*memc
) {
186 if (MEMCACHED_SUCCESS
!= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, ext
.set
)) {
187 if (!opt_
.isset("quiet")) {
188 std::cerr
<< memcached_last_error_message(memc
) << "\n";
194 opt
.add("flags", 'F', required_argument
, "Set key flags, too.");
195 opt
.add("expire", 'e', required_argument
, "Set expire time, too.");
197 opt
.add("basename", '.', no_argument
, "Use basename of path as key (default).");
198 opt
.add("relative", '+', no_argument
, "Use relative path (as passed), instead of basename only.");
199 opt
.add("absolute", '/', no_argument
, "Use absolute path (real path), instead of basename only.");
205 char **argp
= nullptr;
206 if (!opt
.parse(argc
, argv
, &argp
)) {
211 if (!check_memcached(opt
, memc
)) {
215 if (!opt
.apply(&memc
)) {
220 if (!check_argp(opt
, argp
, "No file(s) provided.")) {
221 memcached_free(&memc
);
224 for (auto arg
= argp
; *arg
; ++arg
) {
225 add_file(files
, opt
, *arg
);
229 auto exit_code
= EXIT_SUCCESS
;
230 for (auto &file
: files
) {
231 char *path
= nullptr;
232 if (!path2key(opt
, file
, &path
)) {
233 exit_code
= EXIT_FAILURE
;
237 auto rc
= memcp(opt
, memc
, path
, file
);
238 if (memcached_success(rc
)) {
239 if (opt
.isset("verbose")) {
240 std::cout
<< path
<< "\n";
243 exit_code
= EXIT_FAILURE
;
247 if (!check_buffering(opt
, memc
)) {
248 exit_code
= EXIT_FAILURE
;
251 memcached_free(&memc
);