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 int opt_method
= OPT_SET
;
28 static uint32_t opt_flags
= 0;
29 static time_t opt_expires
= 0;
31 int main(int argc
, char *argv
[])
35 memcached_server_st
*servers
;
37 options_parse(argc
, argv
);
39 memc
= memcached_create(NULL
);
45 if ((temp
= getenv("MEMCACHED_SERVERS")))
46 opt_servers
= strdup(temp
);
52 servers
= memcached_servers_parse(opt_servers
);
54 servers
= memcached_servers_parse(argv
[--argc
]);
56 memcached_server_push(memc
, servers
);
57 memcached_server_list_free(servers
);
65 char *file_buffer_ptr
;
67 fd
= open(argv
[optind
], O_RDONLY
);
70 fprintf(stderr
, "memcp: %s: %s\n", argv
[optind
], strerror(errno
));
75 (void)fstat(fd
, &sbuf
);
77 ptr
= rindex(argv
[optind
], '/');
85 static char *opstr
[] = { "set", "add", "replace" };
86 printf("op: %s\nsource file: %s\nlength: %zu\n"
87 "key: %s\nflags: %x\nexpires: %llu\n",
88 opstr
[opt_method
- OPT_SET
], argv
[optind
], (size_t)sbuf
.st_size
,
89 ptr
, opt_flags
, (unsigned long long)opt_expires
);
92 if ((file_buffer_ptr
= (char *)malloc(sizeof(char) * sbuf
.st_size
)) == NULL
)
94 fprintf(stderr
, "malloc: %s\n", strerror(errno
));
98 if ((read_length
= read(fd
, file_buffer_ptr
, sbuf
.st_size
)) == -1)
100 fprintf(stderr
, "read: %s\n", strerror(errno
));
103 assert(read_length
== sbuf
.st_size
);
105 if (opt_method
== OPT_ADD
)
106 rc
= memcached_add(memc
, ptr
, strlen(ptr
),
107 file_buffer_ptr
, sbuf
.st_size
,
108 opt_expires
, opt_flags
);
109 else if (opt_method
== OPT_REPLACE
)
110 rc
= memcached_replace(memc
, ptr
, strlen(ptr
),
111 file_buffer_ptr
, sbuf
.st_size
,
112 opt_expires
, opt_flags
);
114 rc
= memcached_set(memc
, ptr
, strlen(ptr
),
115 file_buffer_ptr
, sbuf
.st_size
,
116 opt_expires
, opt_flags
);
118 if (rc
!= MEMCACHED_SUCCESS
)
120 fprintf(stderr
, "memcp: %s: memcache error %s",
121 ptr
, memcached_strerror(memc
, rc
));
122 if (memc
->cached_errno
)
123 fprintf(stderr
, " system error %s", strerror(memc
->cached_errno
));
124 fprintf(stderr
, "\n");
127 free(file_buffer_ptr
);
132 memcached_free(memc
);
139 void options_parse(int argc
, char *argv
[])
144 memcached_programs_help_st help_options
[]=
149 static struct option long_options
[]=
151 {"version", no_argument
, NULL
, OPT_VERSION
},
152 {"help", no_argument
, NULL
, OPT_HELP
},
153 {"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
154 {"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
155 {"servers", required_argument
, NULL
, OPT_SERVERS
},
156 {"flag", required_argument
, NULL
, OPT_FLAG
},
157 {"expire", required_argument
, NULL
, OPT_EXPIRE
},
158 {"set", no_argument
, NULL
, OPT_SET
},
159 {"add", no_argument
, NULL
, OPT_ADD
},
160 {"replace", no_argument
, NULL
, OPT_REPLACE
},
166 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
168 if (option_rv
== -1) break;
174 case OPT_VERBOSE
: /* --verbose or -v */
175 opt_verbose
= OPT_VERBOSE
;
177 case OPT_DEBUG
: /* --debug or -d */
178 opt_verbose
= OPT_DEBUG
;
180 case OPT_VERSION
: /* --version or -V */
181 version_command(PROGRAM_NAME
);
183 case OPT_HELP
: /* --help or -h */
184 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
186 case OPT_SERVERS
: /* --servers or -s */
187 opt_servers
= strdup(optarg
);
189 case OPT_FLAG
: /* --flag */
190 opt_flags
= (uint32_t)strtol(optarg
, (char **)NULL
, 16);
192 case OPT_EXPIRE
: /* --expire */
193 opt_expires
= (time_t)strtoll(optarg
, (char **)NULL
, 10);
199 opt_method
= OPT_REPLACE
;
205 /* getopt_long already printed an error message. */