Huge refactoring of directory structure.
[awesomized/libmemcached] / clients / memcp.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include <unistd.h>
5 #include <getopt.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <sys/types.h>
9 #include <fcntl.h>
10 #include <errno.h>
11 #include <strings.h>
12 #include <string.h>
13 #include <assert.h>
14
15 #include <libmemcached/memcached.h>
16
17 #include "client_options.h"
18 #include "utilities.h"
19
20 #define PROGRAM_NAME "memcp"
21 #define PROGRAM_DESCRIPTION "Copy a set of files to a memcached cluster."
22
23 /* Prototypes */
24 void options_parse(int argc, char *argv[]);
25
26 static int opt_verbose= 0;
27 static char *opt_servers= NULL;
28 static char *opt_hash= NULL;
29 static int opt_method= OPT_SET;
30 static uint32_t opt_flags= 0;
31 static time_t opt_expires= 0;
32
33 int main(int argc, char *argv[])
34 {
35 memcached_st *memc;
36 memcached_return rc;
37 memcached_server_st *servers;
38
39 options_parse(argc, argv);
40
41 memc= memcached_create(NULL);
42 process_hash_option(memc, opt_hash);
43
44 if (!opt_servers)
45 {
46 char *temp;
47
48 if ((temp= getenv("MEMCACHED_SERVERS")))
49 opt_servers= strdup(temp);
50 else
51 {
52 fprintf(stderr, "No Servers provided\n");
53 exit(1);
54 }
55 }
56
57 if (opt_servers)
58 servers= memcached_servers_parse(opt_servers);
59 else
60 servers= memcached_servers_parse(argv[--argc]);
61
62 memcached_server_push(memc, servers);
63 memcached_server_list_free(servers);
64
65 while (optind < argc)
66 {
67 struct stat sbuf;
68 int fd;
69 char *ptr;
70 ssize_t read_length;
71 char *file_buffer_ptr;
72
73 fd= open(argv[optind], O_RDONLY);
74 if (fd < 0)
75 {
76 fprintf(stderr, "memcp: %s: %s\n", argv[optind], strerror(errno));
77 optind++;
78 continue;
79 }
80
81 (void)fstat(fd, &sbuf);
82
83 ptr= rindex(argv[optind], '/');
84 if (ptr)
85 ptr++;
86 else
87 ptr= argv[optind];
88
89 if (opt_verbose)
90 {
91 static char *opstr[] = { "set", "add", "replace" };
92 printf("op: %s\nsource file: %s\nlength: %zu\n"
93 "key: %s\nflags: %x\nexpires: %llu\n",
94 opstr[opt_method - OPT_SET], argv[optind], (size_t)sbuf.st_size,
95 ptr, opt_flags, (unsigned long long)opt_expires);
96 }
97
98 if ((file_buffer_ptr= (char *)malloc(sizeof(char) * sbuf.st_size)) == NULL)
99 {
100 fprintf(stderr, "malloc: %s\n", strerror(errno));
101 exit(1);
102 }
103
104 if ((read_length= read(fd, file_buffer_ptr, sbuf.st_size)) == -1)
105 {
106 fprintf(stderr, "read: %s\n", strerror(errno));
107 exit(1);
108 }
109 assert(read_length == sbuf.st_size);
110
111 if (opt_method == OPT_ADD)
112 rc= memcached_add(memc, ptr, strlen(ptr),
113 file_buffer_ptr, sbuf.st_size,
114 opt_expires, opt_flags);
115 else if (opt_method == OPT_REPLACE)
116 rc= memcached_replace(memc, ptr, strlen(ptr),
117 file_buffer_ptr, sbuf.st_size,
118 opt_expires, opt_flags);
119 else
120 rc= memcached_set(memc, ptr, strlen(ptr),
121 file_buffer_ptr, sbuf.st_size,
122 opt_expires, opt_flags);
123
124 if (rc != MEMCACHED_SUCCESS)
125 {
126 fprintf(stderr, "memcp: %s: memcache error %s",
127 ptr, memcached_strerror(memc, rc));
128 if (memc->cached_errno)
129 fprintf(stderr, " system error %s", strerror(memc->cached_errno));
130 fprintf(stderr, "\n");
131 }
132
133 free(file_buffer_ptr);
134 close(fd);
135 optind++;
136 }
137
138 memcached_free(memc);
139
140 if (opt_servers)
141 free(opt_servers);
142 if (opt_hash)
143 free(opt_hash);
144
145 return 0;
146 }
147
148 void options_parse(int argc, char *argv[])
149 {
150 int option_index= 0;
151 int option_rv;
152
153 memcached_programs_help_st help_options[]=
154 {
155 {0},
156 };
157
158 static struct option long_options[]=
159 {
160 {"version", no_argument, NULL, OPT_VERSION},
161 {"help", no_argument, NULL, OPT_HELP},
162 {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
163 {"debug", no_argument, &opt_verbose, OPT_DEBUG},
164 {"servers", required_argument, NULL, OPT_SERVERS},
165 {"flag", required_argument, NULL, OPT_FLAG},
166 {"expire", required_argument, NULL, OPT_EXPIRE},
167 {"set", no_argument, NULL, OPT_SET},
168 {"add", no_argument, NULL, OPT_ADD},
169 {"replace", no_argument, NULL, OPT_REPLACE},
170 {"hash", required_argument, NULL, OPT_HASH},
171 {0, 0, 0, 0},
172 };
173
174 while (1)
175 {
176 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
177
178 if (option_rv == -1) break;
179
180 switch (option_rv)
181 {
182 case 0:
183 break;
184 case OPT_VERBOSE: /* --verbose or -v */
185 opt_verbose = OPT_VERBOSE;
186 break;
187 case OPT_DEBUG: /* --debug or -d */
188 opt_verbose = OPT_DEBUG;
189 break;
190 case OPT_VERSION: /* --version or -V */
191 version_command(PROGRAM_NAME);
192 break;
193 case OPT_HELP: /* --help or -h */
194 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
195 break;
196 case OPT_SERVERS: /* --servers or -s */
197 opt_servers= strdup(optarg);
198 break;
199 case OPT_FLAG: /* --flag */
200 opt_flags= (uint32_t)strtol(optarg, (char **)NULL, 16);
201 break;
202 case OPT_EXPIRE: /* --expire */
203 opt_expires= (time_t)strtoll(optarg, (char **)NULL, 10);
204 break;
205 case OPT_SET:
206 opt_method= OPT_SET;
207 break;
208 case OPT_REPLACE:
209 opt_method= OPT_REPLACE;
210 break;
211 case OPT_ADD:
212 opt_method= OPT_ADD;
213 case OPT_HASH:
214 opt_hash= strdup(optarg);
215 break;
216 case '?':
217 /* getopt_long already printed an error message. */
218 exit(1);
219 default:
220 abort();
221 }
222 }
223 }