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_verbose
= 0;
27 static char *opt_servers
= NULL
;
28 static char *opt_hash
= NULL
;
29 static int opt_method
= OPT_SET
;
30 static uint32_t opt_flags
= 0;
31 static time_t opt_expires
= 0;
33 int main(int argc
, char *argv
[])
37 memcached_server_st
*servers
;
39 options_parse(argc
, argv
);
41 memc
= memcached_create(NULL
);
42 process_hash_option(memc
, opt_hash
);
48 if ((temp
= getenv("MEMCACHED_SERVERS")))
49 opt_servers
= strdup(temp
);
52 fprintf(stderr
, "No Servers provided\n");
58 servers
= memcached_servers_parse(opt_servers
);
60 servers
= memcached_servers_parse(argv
[--argc
]);
62 memcached_server_push(memc
, servers
);
63 memcached_server_list_free(servers
);
71 char *file_buffer_ptr
;
73 fd
= open(argv
[optind
], O_RDONLY
);
76 fprintf(stderr
, "memcp: %s: %s\n", argv
[optind
], strerror(errno
));
81 (void)fstat(fd
, &sbuf
);
83 ptr
= rindex(argv
[optind
], '/');
91 static char *opstr
[] = { "set", "add", "replace" };
92 printf("op: %s\nsource file: %s\nlength: %zu\n"
93 "key: %s\nflags: %x\nexpires: %llu\n",
94 opstr
[opt_method
- OPT_SET
], argv
[optind
], (size_t)sbuf
.st_size
,
95 ptr
, opt_flags
, (unsigned long long)opt_expires
);
98 if ((file_buffer_ptr
= (char *)malloc(sizeof(char) * sbuf
.st_size
)) == NULL
)
100 fprintf(stderr
, "malloc: %s\n", strerror(errno
));
104 if ((read_length
= read(fd
, file_buffer_ptr
, sbuf
.st_size
)) == -1)
106 fprintf(stderr
, "read: %s\n", strerror(errno
));
109 assert(read_length
== sbuf
.st_size
);
111 if (opt_method
== OPT_ADD
)
112 rc
= memcached_add(memc
, ptr
, strlen(ptr
),
113 file_buffer_ptr
, sbuf
.st_size
,
114 opt_expires
, opt_flags
);
115 else if (opt_method
== OPT_REPLACE
)
116 rc
= memcached_replace(memc
, ptr
, strlen(ptr
),
117 file_buffer_ptr
, sbuf
.st_size
,
118 opt_expires
, opt_flags
);
120 rc
= memcached_set(memc
, ptr
, strlen(ptr
),
121 file_buffer_ptr
, sbuf
.st_size
,
122 opt_expires
, opt_flags
);
124 if (rc
!= MEMCACHED_SUCCESS
)
126 fprintf(stderr
, "memcp: %s: memcache error %s",
127 ptr
, memcached_strerror(memc
, rc
));
128 if (memc
->cached_errno
)
129 fprintf(stderr
, " system error %s", strerror(memc
->cached_errno
));
130 fprintf(stderr
, "\n");
133 free(file_buffer_ptr
);
138 memcached_free(memc
);
148 void options_parse(int argc
, char *argv
[])
153 memcached_programs_help_st help_options
[]=
158 static struct option long_options
[]=
160 {"version", no_argument
, NULL
, OPT_VERSION
},
161 {"help", no_argument
, NULL
, OPT_HELP
},
162 {"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
163 {"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
164 {"servers", required_argument
, NULL
, OPT_SERVERS
},
165 {"flag", required_argument
, NULL
, OPT_FLAG
},
166 {"expire", required_argument
, NULL
, OPT_EXPIRE
},
167 {"set", no_argument
, NULL
, OPT_SET
},
168 {"add", no_argument
, NULL
, OPT_ADD
},
169 {"replace", no_argument
, NULL
, OPT_REPLACE
},
170 {"hash", required_argument
, NULL
, OPT_HASH
},
176 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
178 if (option_rv
== -1) break;
184 case OPT_VERBOSE
: /* --verbose or -v */
185 opt_verbose
= OPT_VERBOSE
;
187 case OPT_DEBUG
: /* --debug or -d */
188 opt_verbose
= OPT_DEBUG
;
190 case OPT_VERSION
: /* --version or -V */
191 version_command(PROGRAM_NAME
);
193 case OPT_HELP
: /* --help or -h */
194 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
196 case OPT_SERVERS
: /* --servers or -s */
197 opt_servers
= strdup(optarg
);
199 case OPT_FLAG
: /* --flag */
200 opt_flags
= (uint32_t)strtol(optarg
, (char **)NULL
, 16);
202 case OPT_EXPIRE
: /* --expire */
203 opt_expires
= (time_t)strtoll(optarg
, (char **)NULL
, 10);
209 opt_method
= OPT_REPLACE
;
214 opt_hash
= strdup(optarg
);
217 /* getopt_long already printed an error message. */