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
);
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
));
108 assert(read_length
== sbuf
.st_size
);
110 if (opt_method
== OPT_ADD
)
111 rc
= memcached_add(memc
, ptr
, strlen(ptr
),
112 file_buffer_ptr
, sbuf
.st_size
,
113 opt_expires
, opt_flags
);
114 else if (opt_method
== OPT_REPLACE
)
115 rc
= memcached_replace(memc
, ptr
, strlen(ptr
),
116 file_buffer_ptr
, sbuf
.st_size
,
117 opt_expires
, opt_flags
);
119 rc
= memcached_set(memc
, ptr
, strlen(ptr
),
120 file_buffer_ptr
, sbuf
.st_size
,
121 opt_expires
, opt_flags
);
123 if (rc
!= MEMCACHED_SUCCESS
)
125 fprintf(stderr
, "memcp: %s: memcache error %s",
126 ptr
, memcached_strerror(memc
, rc
));
127 if (memc
->cached_errno
)
128 fprintf(stderr
, " system error %s", strerror(memc
->cached_errno
));
129 fprintf(stderr
, "\n");
132 free(file_buffer_ptr
);
137 memcached_free(memc
);
147 void options_parse(int argc
, char *argv
[])
152 memcached_programs_help_st help_options
[]=
157 static struct option long_options
[]=
159 {"version", no_argument
, NULL
, OPT_VERSION
},
160 {"help", no_argument
, NULL
, OPT_HELP
},
161 {"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
162 {"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
163 {"servers", required_argument
, NULL
, OPT_SERVERS
},
164 {"flag", required_argument
, NULL
, OPT_FLAG
},
165 {"expire", required_argument
, NULL
, OPT_EXPIRE
},
166 {"set", no_argument
, NULL
, OPT_SET
},
167 {"add", no_argument
, NULL
, OPT_ADD
},
168 {"replace", no_argument
, NULL
, OPT_REPLACE
},
169 {"hash", required_argument
, NULL
, OPT_HASH
},
175 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
177 if (option_rv
== -1) break;
183 case OPT_VERBOSE
: /* --verbose or -v */
184 opt_verbose
= OPT_VERBOSE
;
186 case OPT_DEBUG
: /* --debug or -d */
187 opt_verbose
= OPT_DEBUG
;
189 case OPT_VERSION
: /* --version or -V */
190 version_command(PROGRAM_NAME
);
192 case OPT_HELP
: /* --help or -h */
193 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
195 case OPT_SERVERS
: /* --servers or -s */
196 opt_servers
= strdup(optarg
);
198 case OPT_FLAG
: /* --flag */
199 opt_flags
= (uint32_t)strtol(optarg
, (char **)NULL
, 16);
201 case OPT_EXPIRE
: /* --expire */
202 opt_expires
= (time_t)strtoll(optarg
, (char **)NULL
, 10);
208 opt_method
= OPT_REPLACE
;
213 opt_hash
= strdup(optarg
);
216 /* getopt_long already printed an error message. */