14 #include <libmemcached/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
);
51 fprintf(stderr
, "No Servers provided\n");
57 servers
= memcached_servers_parse(opt_servers
);
59 servers
= memcached_servers_parse(argv
[--argc
]);
61 memcached_server_push(memc
, servers
);
62 memcached_server_list_free(servers
);
70 char *file_buffer_ptr
;
72 fd
= open(argv
[optind
], O_RDONLY
);
75 fprintf(stderr
, "memcp: %s: %s\n", argv
[optind
], strerror(errno
));
80 (void)fstat(fd
, &sbuf
);
82 ptr
= rindex(argv
[optind
], '/');
90 static char *opstr
[] = { "set", "add", "replace" };
91 printf("op: %s\nsource file: %s\nlength: %zu\n"
92 "key: %s\nflags: %x\nexpires: %llu\n",
93 opstr
[opt_method
- OPT_SET
], argv
[optind
], (size_t)sbuf
.st_size
,
94 ptr
, opt_flags
, (unsigned long long)opt_expires
);
97 if ((file_buffer_ptr
= (char *)malloc(sizeof(char) * sbuf
.st_size
)) == NULL
)
99 fprintf(stderr
, "malloc: %s\n", strerror(errno
));
103 if ((read_length
= read(fd
, file_buffer_ptr
, sbuf
.st_size
)) == -1)
105 fprintf(stderr
, "read: %s\n", strerror(errno
));
109 if (read_length
!= sbuf
.st_size
)
111 fprintf(stderr
, "Failure reading from file\n");
115 if (opt_method
== OPT_ADD
)
116 rc
= memcached_add(memc
, ptr
, strlen(ptr
),
117 file_buffer_ptr
, sbuf
.st_size
,
118 opt_expires
, opt_flags
);
119 else if (opt_method
== OPT_REPLACE
)
120 rc
= memcached_replace(memc
, ptr
, strlen(ptr
),
121 file_buffer_ptr
, sbuf
.st_size
,
122 opt_expires
, opt_flags
);
124 rc
= memcached_set(memc
, ptr
, strlen(ptr
),
125 file_buffer_ptr
, sbuf
.st_size
,
126 opt_expires
, opt_flags
);
128 if (rc
!= MEMCACHED_SUCCESS
)
130 fprintf(stderr
, "memcp: %s: memcache error %s",
131 ptr
, memcached_strerror(memc
, rc
));
132 if (memc
->cached_errno
)
133 fprintf(stderr
, " system error %s", strerror(memc
->cached_errno
));
134 fprintf(stderr
, "\n");
137 free(file_buffer_ptr
);
142 memcached_free(memc
);
152 void options_parse(int argc
, char *argv
[])
157 memcached_programs_help_st help_options
[]=
162 static struct option long_options
[]=
164 {"version", no_argument
, NULL
, OPT_VERSION
},
165 {"help", no_argument
, NULL
, OPT_HELP
},
166 {"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
167 {"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
168 {"servers", required_argument
, NULL
, OPT_SERVERS
},
169 {"flag", required_argument
, NULL
, OPT_FLAG
},
170 {"expire", required_argument
, NULL
, OPT_EXPIRE
},
171 {"set", no_argument
, NULL
, OPT_SET
},
172 {"add", no_argument
, NULL
, OPT_ADD
},
173 {"replace", no_argument
, NULL
, OPT_REPLACE
},
174 {"hash", required_argument
, NULL
, OPT_HASH
},
180 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
182 if (option_rv
== -1) break;
188 case OPT_VERBOSE
: /* --verbose or -v */
189 opt_verbose
= OPT_VERBOSE
;
191 case OPT_DEBUG
: /* --debug or -d */
192 opt_verbose
= OPT_DEBUG
;
194 case OPT_VERSION
: /* --version or -V */
195 version_command(PROGRAM_NAME
);
197 case OPT_HELP
: /* --help or -h */
198 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
200 case OPT_SERVERS
: /* --servers or -s */
201 opt_servers
= strdup(optarg
);
203 case OPT_FLAG
: /* --flag */
204 opt_flags
= (uint32_t)strtol(optarg
, (char **)NULL
, 16);
206 case OPT_EXPIRE
: /* --expire */
207 opt_expires
= (time_t)strtoll(optarg
, (char **)NULL
, 10);
213 opt_method
= OPT_REPLACE
;
218 opt_hash
= strdup(optarg
);
221 /* getopt_long already printed an error message. */