15 #include <memcached.h>
16 #include "client_options.h"
17 #include "utilities.h"
19 #define PROGRAM_NAME "memcp"
20 #define PROGRAM_DESCRIPTION "Copy a set of files to a memcached cluster."
23 void options_parse(int argc
, char *argv
[]);
25 static int opt_verbose
= 0;
26 static char *opt_servers
= NULL
;
27 static char *opt_hash
= NULL
;
28 static int opt_method
= OPT_SET
;
29 static uint32_t opt_flags
= 0;
30 static time_t opt_expires
= 0;
32 int main(int argc
, char *argv
[])
36 memcached_server_st
*servers
;
38 options_parse(argc
, argv
);
40 memc
= memcached_create(NULL
);
41 process_hash_option(memc
, opt_hash
);
47 if ((temp
= getenv("MEMCACHED_SERVERS")))
48 opt_servers
= strdup(temp
);
54 servers
= memcached_servers_parse(opt_servers
);
56 servers
= memcached_servers_parse(argv
[--argc
]);
58 memcached_server_push(memc
, servers
);
59 memcached_server_list_free(servers
);
67 char *file_buffer_ptr
;
69 fd
= open(argv
[optind
], O_RDONLY
);
72 fprintf(stderr
, "memcp: %s: %s\n", argv
[optind
], strerror(errno
));
77 (void)fstat(fd
, &sbuf
);
79 ptr
= rindex(argv
[optind
], '/');
87 static char *opstr
[] = { "set", "add", "replace" };
88 printf("op: %s\nsource file: %s\nlength: %zu\n"
89 "key: %s\nflags: %x\nexpires: %llu\n",
90 opstr
[opt_method
- OPT_SET
], argv
[optind
], (size_t)sbuf
.st_size
,
91 ptr
, opt_flags
, (unsigned long long)opt_expires
);
94 if ((file_buffer_ptr
= (char *)malloc(sizeof(char) * sbuf
.st_size
)) == NULL
)
96 fprintf(stderr
, "malloc: %s\n", strerror(errno
));
100 if ((read_length
= read(fd
, file_buffer_ptr
, sbuf
.st_size
)) == -1)
102 fprintf(stderr
, "read: %s\n", strerror(errno
));
105 assert(read_length
== sbuf
.st_size
);
107 if (opt_method
== OPT_ADD
)
108 rc
= memcached_add(memc
, ptr
, strlen(ptr
),
109 file_buffer_ptr
, sbuf
.st_size
,
110 opt_expires
, opt_flags
);
111 else if (opt_method
== OPT_REPLACE
)
112 rc
= memcached_replace(memc
, ptr
, strlen(ptr
),
113 file_buffer_ptr
, sbuf
.st_size
,
114 opt_expires
, opt_flags
);
116 rc
= memcached_set(memc
, ptr
, strlen(ptr
),
117 file_buffer_ptr
, sbuf
.st_size
,
118 opt_expires
, opt_flags
);
120 if (rc
!= MEMCACHED_SUCCESS
)
122 fprintf(stderr
, "memcp: %s: memcache error %s",
123 ptr
, memcached_strerror(memc
, rc
));
124 if (memc
->cached_errno
)
125 fprintf(stderr
, " system error %s", strerror(memc
->cached_errno
));
126 fprintf(stderr
, "\n");
129 free(file_buffer_ptr
);
134 memcached_free(memc
);
144 void options_parse(int argc
, char *argv
[])
149 memcached_programs_help_st help_options
[]=
154 static struct option long_options
[]=
156 {"version", no_argument
, NULL
, OPT_VERSION
},
157 {"help", no_argument
, NULL
, OPT_HELP
},
158 {"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
159 {"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
160 {"servers", required_argument
, NULL
, OPT_SERVERS
},
161 {"flag", required_argument
, NULL
, OPT_FLAG
},
162 {"expire", required_argument
, NULL
, OPT_EXPIRE
},
163 {"set", no_argument
, NULL
, OPT_SET
},
164 {"add", no_argument
, NULL
, OPT_ADD
},
165 {"replace", no_argument
, NULL
, OPT_REPLACE
},
166 {"hash", required_argument
, NULL
, OPT_HASH
},
172 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
174 if (option_rv
== -1) break;
180 case OPT_VERBOSE
: /* --verbose or -v */
181 opt_verbose
= OPT_VERBOSE
;
183 case OPT_DEBUG
: /* --debug or -d */
184 opt_verbose
= OPT_DEBUG
;
186 case OPT_VERSION
: /* --version or -V */
187 version_command(PROGRAM_NAME
);
189 case OPT_HELP
: /* --help or -h */
190 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
192 case OPT_SERVERS
: /* --servers or -s */
193 opt_servers
= strdup(optarg
);
195 case OPT_FLAG
: /* --flag */
196 opt_flags
= (uint32_t)strtol(optarg
, (char **)NULL
, 16);
198 case OPT_EXPIRE
: /* --expire */
199 opt_expires
= (time_t)strtoll(optarg
, (char **)NULL
, 10);
205 opt_method
= OPT_REPLACE
;
210 opt_hash
= strdup(optarg
);
213 /* getopt_long already printed an error message. */