memcap works, though it does nothing useful at the moment :)
[awesomized/libmemcached] / src / memcp.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <getopt.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <sys/types.h>
7 #include <fcntl.h>
8 #include <errno.h>
9 #include <strings.h>
10
11 #include <memcached.h>
12 #include "client_options.h"
13 #include "utilities.h"
14
15 /* Prototypes */
16 void options_parse(int argc, char *argv[]);
17
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;
23
24 int main(int argc, char *argv[])
25 {
26 memcached_st *memc;
27 memcached_return rc;
28 memcached_server_st *servers;
29
30 options_parse(argc, argv);
31
32 memc= memcached_init(NULL);
33
34 if (!opt_servers)
35 return 0;
36
37 if (opt_servers)
38 servers= parse_opt_servers(opt_servers);
39 else
40 servers= parse_opt_servers(argv[--argc]);
41
42 memcached_server_push(memc, servers);
43 memcached_server_list_free(servers);
44
45 while (optind < argc)
46 {
47 struct stat sbuf;
48 int fd;
49 char *ptr;
50 ssize_t read_length;
51 char *file_buffer_ptr;
52
53 fd= open(argv[optind], O_RDONLY);
54 if (fd < 0)
55 {
56 fprintf(stderr, "memcp: %s: %s\n", argv[optind], strerror(errno));
57 optind++;
58 continue;
59 }
60
61 (void)fstat(fd, &sbuf);
62
63 ptr= rindex(argv[optind], '/');
64 if (ptr)
65 ptr++;
66 else
67 ptr= argv[optind];
68
69 if (opt_verbose)
70 {
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);
76 }
77
78 if ((file_buffer_ptr= (char *)malloc(sizeof(char) * sbuf.st_size)) == NULL)
79 {
80 fprintf(stderr, "malloc: %s\n", strerror(errno));
81 exit(1);
82 }
83
84 if ((read_length= read(fd, file_buffer_ptr, sbuf.st_size)) == -1)
85 {
86 fprintf(stderr, "read: %s\n", strerror(errno));
87 exit(1);
88 }
89 assert(read_length == sbuf.st_size);
90
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);
99 else
100 rc= memcached_set(memc, ptr, strlen(ptr),
101 file_buffer_ptr, sbuf.st_size,
102 opt_expires, opt_flags);
103
104 if (rc != MEMCACHED_SUCCESS)
105 fprintf(stderr, "memcp: %s: memcache error %s\n",
106 ptr, memcached_strerror(memc, rc));
107
108 free(file_buffer_ptr);
109 close(fd);
110 optind++;
111 }
112
113 memcached_deinit(memc);
114
115 free(opt_servers);
116
117 return 0;
118 }
119
120 void options_parse(int argc, char *argv[])
121 {
122 int option_index= 0;
123 int option_rv;
124
125 static struct option long_options[]=
126 {
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},
137 {0, 0, 0, 0},
138 };
139
140 while (1)
141 {
142 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
143
144 if (option_rv == -1) break;
145
146 switch (option_rv)
147 {
148 case 0:
149 break;
150 case OPT_VERBOSE: /* --verbose or -v */
151 opt_verbose = OPT_VERBOSE;
152 break;
153 case OPT_DEBUG: /* --debug or -d */
154 opt_verbose = OPT_DEBUG;
155 break;
156 case OPT_VERSION: /* --version or -V */
157 printf("memcache tools, memcp, v1.0\n");
158 exit(0);
159 case OPT_HELP: /* --help or -h */
160 printf("useful help messages go here\n");
161 exit(0);
162 case OPT_SERVERS: /* --servers or -s */
163 opt_servers= strdup(optarg);
164 break;
165 case OPT_FLAG: /* --flag */
166 opt_flags= (uint16_t)strtol(optarg, (char **)NULL, 16);
167 break;
168 case OPT_EXPIRE: /* --expire */
169 opt_expires= (time_t)strtoll(optarg, (char **)NULL, 10);
170 break;
171 case OPT_SET:
172 opt_method= OPT_SET;
173 break;
174 case OPT_REPLACE:
175 opt_method= OPT_REPLACE;
176 break;
177 case OPT_ADD:
178 opt_method= OPT_ADD;
179 break;
180 case '?':
181 /* getopt_long already printed an error message. */
182 exit(1);
183 default:
184 abort();
185 }
186 }
187 }