11 #include <memcached.h>
12 #include "client_options.h"
13 #include "utilities.h"
15 #define PROGRAM_NAME "memcp"
16 #define PROGRAM_DESCRIPTION "Copy a set of files to a memcached cluster."
19 void options_parse(int argc
, char *argv
[]);
21 static int opt_verbose
= 0;
22 static char *opt_servers
= NULL
;
23 static int opt_method
= OPT_SET
;
24 static uint16_t opt_flags
= 0;
25 static time_t opt_expires
= 0;
27 int main(int argc
, char *argv
[])
31 memcached_server_st
*servers
;
33 options_parse(argc
, argv
);
35 memc
= memcached_create(NULL
);
41 servers
= parse_opt_servers(opt_servers
);
43 servers
= parse_opt_servers(argv
[--argc
]);
45 memcached_server_push(memc
, servers
);
46 memcached_server_list_free(servers
);
54 char *file_buffer_ptr
;
56 fd
= open(argv
[optind
], O_RDONLY
);
59 fprintf(stderr
, "memcp: %s: %s\n", argv
[optind
], strerror(errno
));
64 (void)fstat(fd
, &sbuf
);
66 ptr
= rindex(argv
[optind
], '/');
74 static char *opstr
[] = { "set", "add", "replace" };
75 printf("op: %s\nsource file: %s\nlength: %zu\n"
76 "key: %s\nflags: %x\nexpires: %llu\n",
77 opstr
[opt_method
- OPT_SET
], argv
[optind
], (size_t)sbuf
.st_size
,
78 ptr
, opt_flags
, (unsigned long long)opt_expires
);
81 if ((file_buffer_ptr
= (char *)malloc(sizeof(char) * sbuf
.st_size
)) == NULL
)
83 fprintf(stderr
, "malloc: %s\n", strerror(errno
));
87 if ((read_length
= read(fd
, file_buffer_ptr
, sbuf
.st_size
)) == -1)
89 fprintf(stderr
, "read: %s\n", strerror(errno
));
92 assert(read_length
== sbuf
.st_size
);
94 if (opt_method
== OPT_ADD
)
95 rc
= memcached_add(memc
, ptr
, strlen(ptr
),
96 file_buffer_ptr
, sbuf
.st_size
,
97 opt_expires
, opt_flags
);
98 else if (opt_method
== OPT_REPLACE
)
99 rc
= memcached_replace(memc
, ptr
, strlen(ptr
),
100 file_buffer_ptr
, sbuf
.st_size
,
101 opt_expires
, opt_flags
);
103 rc
= memcached_set(memc
, ptr
, strlen(ptr
),
104 file_buffer_ptr
, sbuf
.st_size
,
105 opt_expires
, opt_flags
);
107 if (rc
!= MEMCACHED_SUCCESS
)
108 fprintf(stderr
, "memcp: %s: memcache error %s\n",
109 ptr
, memcached_strerror(memc
, rc
));
111 free(file_buffer_ptr
);
116 memcached_free(memc
);
123 void options_parse(int argc
, char *argv
[])
128 memcached_programs_help_st help_options
[]=
133 static struct option long_options
[]=
135 {"version", no_argument
, NULL
, OPT_VERSION
},
136 {"help", no_argument
, NULL
, OPT_HELP
},
137 {"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
138 {"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
139 {"servers", required_argument
, NULL
, OPT_SERVERS
},
140 {"flag", required_argument
, NULL
, OPT_FLAG
},
141 {"expire", required_argument
, NULL
, OPT_EXPIRE
},
142 {"set", no_argument
, NULL
, OPT_SET
},
143 {"add", no_argument
, NULL
, OPT_ADD
},
144 {"replace", no_argument
, NULL
, OPT_REPLACE
},
150 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
152 if (option_rv
== -1) break;
158 case OPT_VERBOSE
: /* --verbose or -v */
159 opt_verbose
= OPT_VERBOSE
;
161 case OPT_DEBUG
: /* --debug or -d */
162 opt_verbose
= OPT_DEBUG
;
164 case OPT_VERSION
: /* --version or -V */
165 version_command(PROGRAM_NAME
);
167 case OPT_HELP
: /* --help or -h */
168 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
170 case OPT_SERVERS
: /* --servers or -s */
171 opt_servers
= strdup(optarg
);
173 case OPT_FLAG
: /* --flag */
174 opt_flags
= (uint16_t)strtol(optarg
, (char **)NULL
, 16);
176 case OPT_EXPIRE
: /* --expire */
177 opt_expires
= (time_t)strtoll(optarg
, (char **)NULL
, 10);
183 opt_method
= OPT_REPLACE
;
189 /* getopt_long already printed an error message. */