11 #include <memcached.h>
12 #include "client_options.h"
13 #include "utilities.h"
16 void options_parse(int argc
, char *argv
[]);
18 static int opt_verbose
= 0;
19 static char *opt_servers
= NULL
;
20 static int opt_method
= OPT_SET
;
21 static uint16_t opt_flags
= 0;
22 static time_t opt_expires
= 0;
24 int main(int argc
, char *argv
[])
28 memcached_server_st
*servers
;
30 options_parse(argc
, argv
);
32 memc
= memcached_init(NULL
);
38 servers
= parse_opt_servers(opt_servers
);
40 servers
= parse_opt_servers(argv
[--argc
]);
42 memcached_server_push(memc
, servers
);
43 memcached_server_list_free(servers
);
51 char *file_buffer_ptr
;
53 fd
= open(argv
[optind
], O_RDONLY
);
56 fprintf(stderr
, "memcp: %s: %s\n", argv
[optind
], strerror(errno
));
61 (void)fstat(fd
, &sbuf
);
63 ptr
= rindex(argv
[optind
], '/');
71 static char *opstr
[] = { "set", "add", "replace" };
72 printf("op: %s\nsource file: %s\nlength: %zu\n"
73 "key: %s\nflags: %x\nexpires: %llu\n",
74 opstr
[opt_method
- OPT_SET
], argv
[optind
], (size_t)sbuf
.st_size
,
75 ptr
, opt_flags
, (unsigned long long)opt_expires
);
78 if ((file_buffer_ptr
= (char *)malloc(sizeof(char) * sbuf
.st_size
)) == NULL
)
80 fprintf(stderr
, "malloc: %s\n", strerror(errno
));
84 if ((read_length
= read(fd
, file_buffer_ptr
, sbuf
.st_size
)) == -1)
86 fprintf(stderr
, "read: %s\n", strerror(errno
));
89 assert(read_length
== sbuf
.st_size
);
91 if (opt_method
== OPT_ADD
)
92 rc
= memcached_add(memc
, ptr
, strlen(ptr
),
93 file_buffer_ptr
, sbuf
.st_size
,
94 opt_expires
, opt_flags
);
95 else if (opt_method
== OPT_REPLACE
)
96 rc
= memcached_replace(memc
, ptr
, strlen(ptr
),
97 file_buffer_ptr
, sbuf
.st_size
,
98 opt_expires
, opt_flags
);
100 rc
= memcached_set(memc
, ptr
, strlen(ptr
),
101 file_buffer_ptr
, sbuf
.st_size
,
102 opt_expires
, opt_flags
);
104 if (rc
!= MEMCACHED_SUCCESS
)
105 fprintf(stderr
, "memcp: %s: memcache error %s\n",
106 ptr
, memcached_strerror(memc
, rc
));
108 free(file_buffer_ptr
);
113 memcached_deinit(memc
);
120 void options_parse(int argc
, char *argv
[])
125 static struct option long_options
[]=
127 {"version", no_argument
, NULL
, OPT_VERSION
},
128 {"help", no_argument
, NULL
, OPT_HELP
},
129 {"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
130 {"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
131 {"servers", required_argument
, NULL
, OPT_SERVERS
},
132 {"flag", required_argument
, NULL
, OPT_FLAG
},
133 {"expire", required_argument
, NULL
, OPT_EXPIRE
},
134 {"set", no_argument
, NULL
, OPT_SET
},
135 {"add", no_argument
, NULL
, OPT_ADD
},
136 {"replace", no_argument
, NULL
, OPT_REPLACE
},
142 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
144 if (option_rv
== -1) break;
150 case OPT_VERBOSE
: /* --verbose or -v */
151 opt_verbose
= OPT_VERBOSE
;
153 case OPT_DEBUG
: /* --debug or -d */
154 opt_verbose
= OPT_DEBUG
;
156 case OPT_VERSION
: /* --version or -V */
157 printf("memcache tools, memcp, v1.0\n");
159 case OPT_HELP
: /* --help or -h */
160 printf("useful help messages go here\n");
162 case OPT_SERVERS
: /* --servers or -s */
163 opt_servers
= strdup(optarg
);
165 case OPT_FLAG
: /* --flag */
166 opt_flags
= (uint16_t)strtol(optarg
, (char **)NULL
, 16);
168 case OPT_EXPIRE
: /* --expire */
169 opt_expires
= (time_t)strtoll(optarg
, (char **)NULL
, 10);
175 opt_method
= OPT_REPLACE
;
181 /* getopt_long already printed an error message. */