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