Removed dumb bug concerning set (was still using a buffer).
[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
13 /* Prototypes */
14 void options_parse(int argc, char *argv[]);
15
16 static int opt_verbose= 0;
17 static char *opt_servers= NULL;
18 static int opt_method= 0;
19 uint16_t opt_flags= 0;
20 time_t opt_expires= 0;
21
22 int main(int argc, char *argv[])
23 {
24 memcached_st *memc;
25 char *string;
26 size_t string_length;
27 memcached_return rc;
28 options_parse(argc, argv);
29
30 memc= memcached_init(NULL);
31 if (opt_servers)
32 parse_opt_servers(memc, opt_servers);
33 else
34 parse_opt_servers(memc, argv[--argc]);
35
36 while (optind < argc)
37 {
38 struct stat sbuf;
39 int fd;
40 char *ptr;
41 ssize_t read_length;
42 char *file_buffer_ptr;
43
44 fd= open(argv[optind], O_RDONLY);
45 if (fd < 0)
46 {
47 fprintf(stderr, "memcp: %s: %s\n", argv[optind], strerror(errno));
48 optind++;
49 continue;
50 }
51
52 (void)fstat(fd, &sbuf);
53
54 ptr= rindex(argv[optind], '/');
55 if (ptr)
56 ptr++;
57 else
58 ptr= argv[optind];
59
60 if (opt_verbose)
61 {
62 static char *opstr[] = { "set", "add", "replace" };
63 printf("op: %s\nsource file: %s\nlength: %zu\n"
64 "key: %s\nflags: %u\n expires: %llu\n",
65 opstr[opt_method], argv[optind], sbuf.st_size,
66 ptr, opt_flags, opt_expires);
67 }
68
69 if ((file_buffer_ptr= (char *)malloc(sizeof(char) * sbuf.st_size)) == NULL)
70 {
71 fprintf(stderr, "malloc: %s\n", strerror(errno));
72 exit(1);
73 }
74
75 if ((read_length= read(fd, file_buffer_ptr, sbuf.st_size)) == -1)
76 {
77 fprintf(stderr, "read: %s\n", strerror(errno));
78 exit(1);
79 }
80 assert(read_length == sbuf.st_size);
81
82 if (opt_method == OPT_ADD)
83 rc= memcached_add(memc, ptr, strlen(ptr),
84 file_buffer_ptr, sbuf.st_size,
85 opt_expires, opt_flags);
86 else if (opt_method == OPT_REPLACE)
87 rc= memcached_replace(memc, ptr, strlen(ptr),
88 file_buffer_ptr, sbuf.st_size,
89 opt_expires, opt_flags);
90 else
91 rc= memcached_set(memc, ptr, strlen(ptr),
92 file_buffer_ptr, sbuf.st_size,
93 opt_expires, opt_flags);
94
95 if (rc != MEMCACHED_SUCCESS)
96 fprintf(stderr, "memcp: %s: memcache error %s\n",
97 ptr, memcached_strerror(memc, rc));
98
99 WATCHPOINT;
100 free(file_buffer_ptr);
101 close(fd);
102 optind++;
103 }
104
105 memcached_deinit(memc);
106
107 cleanup();
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 case 0:
140 break;
141 case OPT_VERSION: /* --version or -V */
142 printf("memcache tools, memcp, v1.0\n");
143 exit(0);
144 case OPT_HELP: /* --help or -h */
145 printf("useful help messages go here\n");
146 exit(0);
147 case OPT_SERVERS: /* --servers or -s */
148 opt_servers= strdup_cleanup(optarg);
149 break;
150 case OPT_FLAG: /* --flag */
151 opt_flags= (uint16_t)strtol(optarg, (char **)NULL, 10);
152 break;
153 case OPT_EXPIRE: /* --expire */
154 opt_expires= (time_t)strtol(optarg, (char **)NULL, 10);
155 break;
156 case OPT_SET:
157 opt_method= OPT_SET;
158 break;
159 case OPT_REPLACE:
160 opt_method= OPT_REPLACE;
161 break;
162 case OPT_ADD:
163 opt_method= OPT_ADD;
164 break;
165 case '?':
166 /* getopt_long already printed an error message. */
167 exit(1);
168 default:
169 abort();
170 }
171 }
172 }