1 #include "libmemcached/common.h"
15 #include <libmemcached/memcached.h>
17 #include "client_options.h"
18 #include "utilities.h"
20 #define PROGRAM_NAME "memcp"
21 #define PROGRAM_DESCRIPTION "Copy a set of files to a memcached cluster."
24 void options_parse(int argc
, char *argv
[]);
26 static int opt_binary
=0;
27 static int opt_verbose
= 0;
28 static char *opt_servers
= NULL
;
29 static char *opt_hash
= NULL
;
30 static int opt_method
= OPT_SET
;
31 static uint32_t opt_flags
= 0;
32 static time_t opt_expires
= 0;
34 int main(int argc
, char *argv
[])
38 memcached_server_st
*servers
;
40 options_parse(argc
, argv
);
42 memc
= memcached_create(NULL
);
43 process_hash_option(memc
, opt_hash
);
49 if ((temp
= getenv("MEMCACHED_SERVERS")))
50 opt_servers
= strdup(temp
);
53 fprintf(stderr
, "No Servers provided\n");
59 servers
= memcached_servers_parse(opt_servers
);
61 servers
= memcached_servers_parse(argv
[--argc
]);
63 memcached_server_push(memc
, servers
);
64 memcached_server_list_free(servers
);
65 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, opt_binary
);
73 char *file_buffer_ptr
;
75 fd
= open(argv
[optind
], O_RDONLY
);
78 fprintf(stderr
, "memcp: %s: %s\n", argv
[optind
], strerror(errno
));
83 (void)fstat(fd
, &sbuf
);
85 ptr
= rindex(argv
[optind
], '/');
93 static char *opstr
[] = { "set", "add", "replace" };
94 printf("op: %s\nsource file: %s\nlength: %zu\n"
95 "key: %s\nflags: %x\nexpires: %llu\n",
96 opstr
[opt_method
- OPT_SET
], argv
[optind
], (size_t)sbuf
.st_size
,
97 ptr
, opt_flags
, (unsigned long long)opt_expires
);
100 if ((file_buffer_ptr
= (char *)malloc(sizeof(char) * sbuf
.st_size
)) == NULL
)
102 fprintf(stderr
, "malloc: %s\n", strerror(errno
));
106 if ((read_length
= read(fd
, file_buffer_ptr
, sbuf
.st_size
)) == -1)
108 fprintf(stderr
, "read: %s\n", strerror(errno
));
112 if (read_length
!= sbuf
.st_size
)
114 fprintf(stderr
, "Failure reading from file\n");
118 if (opt_method
== OPT_ADD
)
119 rc
= memcached_add(memc
, ptr
, strlen(ptr
),
120 file_buffer_ptr
, sbuf
.st_size
,
121 opt_expires
, opt_flags
);
122 else if (opt_method
== OPT_REPLACE
)
123 rc
= memcached_replace(memc
, ptr
, strlen(ptr
),
124 file_buffer_ptr
, sbuf
.st_size
,
125 opt_expires
, opt_flags
);
127 rc
= memcached_set(memc
, ptr
, strlen(ptr
),
128 file_buffer_ptr
, sbuf
.st_size
,
129 opt_expires
, opt_flags
);
131 if (rc
!= MEMCACHED_SUCCESS
)
133 fprintf(stderr
, "memcp: %s: memcache error %s",
134 ptr
, memcached_strerror(memc
, rc
));
135 if (memc
->cached_errno
)
136 fprintf(stderr
, " system error %s", strerror(memc
->cached_errno
));
137 fprintf(stderr
, "\n");
140 free(file_buffer_ptr
);
145 memcached_free(memc
);
155 void options_parse(int argc
, char *argv
[])
160 memcached_programs_help_st help_options
[]=
165 static struct option long_options
[]=
167 {"version", no_argument
, NULL
, OPT_VERSION
},
168 {"help", no_argument
, NULL
, OPT_HELP
},
169 {"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
170 {"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
171 {"servers", required_argument
, NULL
, OPT_SERVERS
},
172 {"flag", required_argument
, NULL
, OPT_FLAG
},
173 {"expire", required_argument
, NULL
, OPT_EXPIRE
},
174 {"set", no_argument
, NULL
, OPT_SET
},
175 {"add", no_argument
, NULL
, OPT_ADD
},
176 {"replace", no_argument
, NULL
, OPT_REPLACE
},
177 {"hash", required_argument
, NULL
, OPT_HASH
},
178 {"binary", no_argument
, NULL
, OPT_BINARY
},
184 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
186 if (option_rv
== -1) break;
195 case OPT_VERBOSE
: /* --verbose or -v */
196 opt_verbose
= OPT_VERBOSE
;
198 case OPT_DEBUG
: /* --debug or -d */
199 opt_verbose
= OPT_DEBUG
;
201 case OPT_VERSION
: /* --version or -V */
202 version_command(PROGRAM_NAME
);
204 case OPT_HELP
: /* --help or -h */
205 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
207 case OPT_SERVERS
: /* --servers or -s */
208 opt_servers
= strdup(optarg
);
210 case OPT_FLAG
: /* --flag */
211 opt_flags
= (uint32_t)strtol(optarg
, (char **)NULL
, 16);
213 case OPT_EXPIRE
: /* --expire */
214 opt_expires
= (time_t)strtoll(optarg
, (char **)NULL
, 10);
220 opt_method
= OPT_REPLACE
;
225 opt_hash
= strdup(optarg
);
228 /* getopt_long already printed an error message. */