Readjusted a number function names.
[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
10 #include <memcached.h>
11 #include "client_options.h"
12 #include "utilities.h"
13
14 /* Prototypes */
15 void options_parse(int argc, char *argv[]);
16
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;
22
23 int main(int argc, char *argv[])
24 {
25 memcached_st *memc;
26 memcached_return rc;
27
28 options_parse(argc, argv);
29
30 memc= memcached_init(NULL);
31
32 if (opt_servers)
33 parse_opt_servers(memc, opt_servers);
34 else
35 parse_opt_servers(memc, argv[--argc]);
36
37 while (optind < argc)
38 {
39 struct stat sbuf;
40 int fd;
41 char *ptr;
42 ssize_t read_length;
43 char *file_buffer_ptr;
44
45 fd= open(argv[optind], O_RDONLY);
46 if (fd < 0)
47 {
48 fprintf(stderr, "memcp: %s: %s\n", argv[optind], strerror(errno));
49 optind++;
50 continue;
51 }
52
53 (void)fstat(fd, &sbuf);
54
55 ptr= rindex(argv[optind], '/');
56 if (ptr)
57 ptr++;
58 else
59 ptr= argv[optind];
60
61 if (opt_verbose)
62 {
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);
68 }
69
70 if ((file_buffer_ptr= (char *)malloc(sizeof(char) * sbuf.st_size)) == NULL)
71 {
72 fprintf(stderr, "malloc: %s\n", strerror(errno));
73 exit(1);
74 }
75
76 if ((read_length= read(fd, file_buffer_ptr, sbuf.st_size)) == -1)
77 {
78 fprintf(stderr, "read: %s\n", strerror(errno));
79 exit(1);
80 }
81 assert(read_length == sbuf.st_size);
82
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);
91 else
92 rc= memcached_set(memc, ptr, strlen(ptr),
93 file_buffer_ptr, sbuf.st_size,
94 opt_expires, opt_flags);
95
96 if (rc != MEMCACHED_SUCCESS)
97 fprintf(stderr, "memcp: %s: memcache error %s\n",
98 ptr, memcached_strerror(memc, rc));
99
100 free(file_buffer_ptr);
101 close(fd);
102 optind++;
103 }
104
105 memcached_deinit(memc);
106
107 free(opt_servers);
108
109 return 0;
110 }
111
112 void options_parse(int argc, char *argv[])
113 {
114 int option_index= 0;
115 int option_rv;
116
117 static struct option long_options[]=
118 {
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},
129 {0, 0, 0, 0},
130 };
131
132 while (1)
133 {
134 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
135
136 if (option_rv == -1) break;
137
138 switch (option_rv)
139 {
140 case 0:
141 break;
142 case OPT_VERBOSE: /* --verbose or -v */
143 opt_verbose = OPT_VERBOSE;
144 break;
145 case OPT_DEBUG: /* --debug or -d */
146 opt_verbose = OPT_DEBUG;
147 break;
148 case OPT_VERSION: /* --version or -V */
149 printf("memcache tools, memcp, v1.0\n");
150 exit(0);
151 case OPT_HELP: /* --help or -h */
152 printf("useful help messages go here\n");
153 exit(0);
154 case OPT_SERVERS: /* --servers or -s */
155 opt_servers= strdup(optarg);
156 break;
157 case OPT_FLAG: /* --flag */
158 opt_flags= (uint16_t)strtol(optarg, (char **)NULL, 16);
159 break;
160 case OPT_EXPIRE: /* --expire */
161 opt_expires= (time_t)strtoll(optarg, (char **)NULL, 10);
162 break;
163 case OPT_SET:
164 opt_method= OPT_SET;
165 break;
166 case OPT_REPLACE:
167 opt_method= OPT_REPLACE;
168 break;
169 case OPT_ADD:
170 opt_method= OPT_ADD;
171 break;
172 case '?':
173 /* getopt_long already printed an error message. */
174 exit(1);
175 default:
176 abort();
177 }
178 }
179 }