0001713d5ecda853e233fa949737620d31de4a42
10 #include <memcached.h>
11 #include "client_options.h"
12 #include "utilities.h"
15 void options_parse(int argc
, char *argv
[]);
17 static int opt_verbose
= 0;
18 static char *opt_servers
= NULL
;
19 static int opt_method
= OPT_SET
;
20 static uint16_t opt_flags
= 0;
21 static time_t opt_expires
= 0;
23 int main(int argc
, char *argv
[])
28 options_parse(argc
, argv
);
30 memc
= memcached_init(NULL
);
33 parse_opt_servers(memc
, opt_servers
);
35 parse_opt_servers(memc
, argv
[--argc
]);
43 char *file_buffer_ptr
;
45 fd
= open(argv
[optind
], O_RDONLY
);
48 fprintf(stderr
, "memcp: %s: %s\n", argv
[optind
], strerror(errno
));
53 (void)fstat(fd
, &sbuf
);
55 ptr
= rindex(argv
[optind
], '/');
63 static char *opstr
[] = { "set", "add", "replace" };
64 printf("op: %s\nsource file: %s\nlength: %zu\n"
65 "key: %s\nflags: %x\nexpires: %llu\n",
66 opstr
[opt_method
- OPT_SET
], argv
[optind
], (size_t)sbuf
.st_size
,
67 ptr
, opt_flags
, (unsigned long long)opt_expires
);
70 if ((file_buffer_ptr
= (char *)malloc(sizeof(char) * sbuf
.st_size
)) == NULL
)
72 fprintf(stderr
, "malloc: %s\n", strerror(errno
));
76 if ((read_length
= read(fd
, file_buffer_ptr
, sbuf
.st_size
)) == -1)
78 fprintf(stderr
, "read: %s\n", strerror(errno
));
81 assert(read_length
== sbuf
.st_size
);
83 if (opt_method
== OPT_ADD
)
84 rc
= memcached_add(memc
, ptr
, strlen(ptr
),
85 file_buffer_ptr
, sbuf
.st_size
,
86 opt_expires
, opt_flags
);
87 else if (opt_method
== OPT_REPLACE
)
88 rc
= memcached_replace(memc
, ptr
, strlen(ptr
),
89 file_buffer_ptr
, sbuf
.st_size
,
90 opt_expires
, opt_flags
);
92 rc
= memcached_set(memc
, ptr
, strlen(ptr
),
93 file_buffer_ptr
, sbuf
.st_size
,
94 opt_expires
, opt_flags
);
96 if (rc
!= MEMCACHED_SUCCESS
)
97 fprintf(stderr
, "memcp: %s: memcache error %s\n",
98 ptr
, memcached_strerror(memc
, rc
));
100 free(file_buffer_ptr
);
105 memcached_deinit(memc
);
112 void options_parse(int argc
, char *argv
[])
117 static struct option long_options
[]=
119 {"version", no_argument
, NULL
, OPT_VERSION
},
120 {"help", no_argument
, NULL
, OPT_HELP
},
121 {"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
122 {"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
123 {"servers", required_argument
, NULL
, OPT_SERVERS
},
124 {"flag", required_argument
, NULL
, OPT_FLAG
},
125 {"expire", required_argument
, NULL
, OPT_EXPIRE
},
126 {"set", no_argument
, NULL
, OPT_SET
},
127 {"add", no_argument
, NULL
, OPT_ADD
},
128 {"replace", no_argument
, NULL
, OPT_REPLACE
},
134 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
136 if (option_rv
== -1) break;
142 case OPT_VERBOSE
: /* --verbose or -v */
143 opt_verbose
= OPT_VERBOSE
;
145 case OPT_DEBUG
: /* --debug or -d */
146 opt_verbose
= OPT_DEBUG
;
148 case OPT_VERSION
: /* --version or -V */
149 printf("memcache tools, memcp, v1.0\n");
151 case OPT_HELP
: /* --help or -h */
152 printf("useful help messages go here\n");
154 case OPT_SERVERS
: /* --servers or -s */
155 opt_servers
= strdup(optarg
);
157 case OPT_FLAG
: /* --flag */
158 opt_flags
= (uint16_t)strtol(optarg
, (char **)NULL
, 16);
160 case OPT_EXPIRE
: /* --expire */
161 opt_expires
= (time_t)strtoll(optarg
, (char **)NULL
, 10);
167 opt_method
= OPT_REPLACE
;
173 /* getopt_long already printed an error message. */