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"
48 static inline std::string
stream2string(const std::istream
&istream
) {
49 return dynamic_cast<std::ostringstream
&>(std::ostringstream
{} << istream
.rdbuf()).str();
52 static memcached_return_t
memcp(const client_options
&opt
, memcached_st
&memc
, const char *key
,
53 const memcp_file
&file
) {
54 std::ifstream fstream
{};
55 std::istream
*istream
= check_istream(opt
, file
.path
, fstream
);
58 return MEMCACHED_ERROR
;
62 memcached_return_t rc
;
63 auto data
= stream2string(*istream
);
64 if (file
.op
== memcp_file::mode::REPLACE
) {
66 rc
= memcached_replace(&memc
, key
, strlen(key
), data
.c_str(), data
.length(),
67 file
.expire
, file
.flags
);
68 } else if (file
.op
== memcp_file::mode::ADD
) {
70 rc
= memcached_add(&memc
, key
, strlen(key
), data
.c_str(), data
.length(),
71 file
.expire
, file
.flags
);
74 rc
= memcached_set(&memc
, key
, strlen(key
), data
.c_str(), data
.length(),
75 file
.expire
, file
.flags
);
78 if (!memcached_success(rc
)) {
79 auto error
= memcached_last_error(&memc
)
80 ? memcached_last_error_message(&memc
)
81 : memcached_strerror(&memc
, rc
);
82 std::cerr
<< "Error occurred during memcached_" << mode
<<"('" << key
<< "'): " << error
<< "\n";
87 static void add_file(std::vector
<memcp_file
> &files
, const client_options
&opt
, char *file
) {
88 memcp_file::type type
= memcp_file::type::basename
;
89 memcp_file::mode mode
= memcp_file::mode::SET
;
93 if (opt
.isset("absolute")) {
94 type
= memcp_file::type::absolute
;
95 } else if (opt
.isset("relative")) {
96 type
= memcp_file::type::relative
;
99 if (opt
.isset("replace")) {
100 mode
= memcp_file::mode::REPLACE
;
101 } else if (opt
.isset("add")) {
102 mode
= memcp_file::mode::ADD
;
105 if (auto flags_str
= opt
.argof("flags")) {
106 flags
= std::stoul(flags_str
);
108 if (auto expire_str
= opt
.argof("expire")) {
109 expire
= std::stoul(expire_str
);
112 if (opt
.isset("debug")) {
113 auto mode_str
= mode
== memcp_file::mode::REPLACE
? "REPLACE" : mode
== memcp_file::mode::ADD
? "ADD" : "SET";
114 std::cerr
<< "Scheduling " << mode_str
<< " '" << file
<< "' (expire=" << expire
<< ", flags=" << flags
<< ").\n";
117 files
.emplace_back(memcp_file
{type
, mode
, file
, flags
, expire
});
120 static bool path2key(const client_options
&opt
, memcp_file
&file
, char **path
) {
121 static char rpath
[PATH_MAX
+ 1];
122 if (file
.key
== memcp_file::type::absolute
) {
123 *path
= realpath(file
.path
, rpath
);
125 if (!opt
.isset("quiet")) {
130 } else if (file
.key
== memcp_file::type::relative
) {
133 *path
= basename((file
.path
));
138 int main(int argc
, char *argv
[]) {
139 std::vector
<memcp_file
> files
{};
140 client_options opt
{PROGRAM_NAME
, PROGRAM_VERSION
, PROGRAM_DESCRIPTION
,
142 "\n\t\t\t# NOTE: order of flags and positional"
143 "\n\t\t\t# arguments matters on GNU systems)"};
145 opt
.add(nullptr, '-', no_argument
, "GNU argv extension")
146 .parse
= [&files
](client_options
&opt_
, client_options::extended_option
&ext
) {
147 add_file(files
, opt_
, ext
.arg
);
151 for (const auto &def
: opt
.defaults
) {
155 opt
.add("set", 'S', no_argument
, "Perform SET operations.")
156 .parse
= [](client_options
&opt_
, client_options::extended_option
&) {
158 opt_
.unset("replace");
161 opt
.add("add", 'A', no_argument
, "Perform ADD operations.")
162 .parse
= [](client_options
&opt_
, client_options::extended_option
&) {
164 opt_
.unset("replace");
167 opt
.add("replace", 'R', no_argument
, "Perform REPLACE operations.")
168 .parse
= [](client_options
&opt_
, client_options::extended_option
&) {
174 opt
.add("udp", 'U', no_argument
, "Use UDP.")
175 .apply
= [](const client_options
&opt_
, const client_options::extended_option
&ext
, memcached_st
*memc
) {
176 if (MEMCACHED_SUCCESS
!= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, ext
.set
)) {
177 if (!opt_
.isset("quiet")) {
178 std::cerr
<< memcached_last_error_message(memc
) << "\n";
184 opt
.add("flags", 'F', required_argument
, "Set key flags, too.");
185 opt
.add("expire", 'e', required_argument
, "Set expire time, too.");
187 opt
.add("basename", '.', no_argument
, "Use basename of path as key (default).");
188 opt
.add("relative", '+', no_argument
, "Use relative path (as passed), instead of basename only.");
189 opt
.add("absolute", '/', no_argument
, "Use absolute path (real path), instead of basename only.");
195 char **argp
= nullptr;
196 if (!opt
.parse(argc
, argv
, &argp
)) {
201 if (!check_memcached(opt
, memc
)) {
205 if (!opt
.apply(&memc
)) {
210 if (!check_argp(opt
, argp
, "No file(s) provided.")) {
211 memcached_free(&memc
);
214 for (auto arg
= argp
; *arg
; ++arg
) {
215 add_file(files
, opt
, *arg
);
219 auto exit_code
= EXIT_SUCCESS
;
220 for (auto &file
: files
) {
221 char *path
= nullptr;
222 if (!path2key(opt
, file
, &path
)) {
223 exit_code
= EXIT_FAILURE
;
227 auto rc
= memcp(opt
, memc
, path
, file
);
228 if (memcached_success(rc
)) {
229 if (opt
.isset("verbose")) {
230 std::cout
<< path
<< "\n";
233 exit_code
= EXIT_FAILURE
;
237 if (!check_buffering(opt
, memc
)) {
238 exit_code
= EXIT_FAILURE
;
241 memcached_free(&memc
);