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 uint16_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
)
119 fprintf(stderr
, "memcp: %s: memcache error %s\n",
120 ptr
, memcached_strerror(memc
, rc
));
122 free(file_buffer_ptr
);
127 memcached_free(memc
);
134 void options_parse(int argc
, char *argv
[])
139 memcached_programs_help_st help_options
[]=
144 static struct option long_options
[]=
146 {"version", no_argument
, NULL
, OPT_VERSION
},
147 {"help", no_argument
, NULL
, OPT_HELP
},
148 {"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
149 {"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
150 {"servers", required_argument
, NULL
, OPT_SERVERS
},
151 {"flag", required_argument
, NULL
, OPT_FLAG
},
152 {"expire", required_argument
, NULL
, OPT_EXPIRE
},
153 {"set", no_argument
, NULL
, OPT_SET
},
154 {"add", no_argument
, NULL
, OPT_ADD
},
155 {"replace", no_argument
, NULL
, OPT_REPLACE
},
161 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
163 if (option_rv
== -1) break;
169 case OPT_VERBOSE
: /* --verbose or -v */
170 opt_verbose
= OPT_VERBOSE
;
172 case OPT_DEBUG
: /* --debug or -d */
173 opt_verbose
= OPT_DEBUG
;
175 case OPT_VERSION
: /* --version or -V */
176 version_command(PROGRAM_NAME
);
178 case OPT_HELP
: /* --help or -h */
179 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
181 case OPT_SERVERS
: /* --servers or -s */
182 opt_servers
= strdup(optarg
);
184 case OPT_FLAG
: /* --flag */
185 opt_flags
= (uint16_t)strtol(optarg
, (char **)NULL
, 16);
187 case OPT_EXPIRE
: /* --expire */
188 opt_expires
= (time_t)strtoll(optarg
, (char **)NULL
, 10);
194 opt_method
= OPT_REPLACE
;
200 /* getopt_long already printed an error message. */