6ae8e3b4905154b3de3115e23408a44e442c064a
[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 <sys/mman.h>
8 #include <fcntl.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;
17 static char *opt_servers;
18 static int opt_replace;
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
29 options_parse(argc, argv);
30
31 memc= memcached_init(NULL);
32 parse_opt_servers(memc, opt_servers);
33
34 while (optind <= argc)
35 {
36 char *mptr;
37 struct stat sbuf;
38 int fd;
39 char *ptr;
40
41 fd= open(argv[optind], O_RDONLY);
42
43 if (fd == -1)
44 {
45 fprintf(stderr, "Failed opening %s\n", argv[optind]);
46 continue;
47 }
48
49 (void)fstat(fd, &sbuf);
50 mptr= mmap(NULL, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
51
52 ptr= rindex(argv[optind], '/');
53 if (ptr)
54 ptr++;
55 else
56 ptr= argv[optind];
57
58 if (opt_verbose) {
59 static char *opstr[] = { "set", "add", "replace" };
60 printf("op: %s\nsource file: %s\nlength: %d\n"
61 "key: %s\nflags: %d\n expires: %ld\n",
62 opstr[opt_replace], argv[optind], sbuf.st_size,
63 ptr, opt_flags, opt_expires);
64 }
65
66 if (opt_replace == 0)
67 rc= memcached_set(memc, ptr, strlen(ptr),
68 mptr, sbuf.st_size,
69 opt_expires, opt_flags);
70 else if (opt_replace == 1)
71 rc= memcached_add(memc, ptr, strlen(ptr),
72 mptr, sbuf.st_size,
73 opt_expires, opt_flags);
74 else if (opt_replace == 2)
75 rc= memcached_replace(memc, ptr, strlen(ptr),
76 mptr, sbuf.st_size,
77 opt_expires, opt_flags);
78 else
79 abort();
80
81 munmap(mptr, sbuf.st_size);
82 close(fd);
83 optind++;
84 }
85
86 memcached_deinit(memc);
87
88 return 0;
89 };
90
91 void options_parse(int argc, char *argv[])
92 {
93 int option_index= 0;
94 int option_rv;
95
96 static struct option long_options[] =
97 {
98 {"version", no_argument, NULL, OPT_VERSION},
99 {"help", no_argument, NULL, OPT_HELP},
100 {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
101 {"debug", no_argument, &opt_verbose, OPT_DEBUG},
102 {"servers", required_argument, NULL, OPT_SERVERS},
103 {"flag", required_argument, NULL, OPT_FLAG},
104 {"expire", required_argument, NULL, OPT_EXPIRE},
105 {"set", no_argument, &opt_replace, OPT_SET},
106 {"add", no_argument, &opt_replace, OPT_ADD},
107 {"replace", no_argument, &opt_replace, OPT_REPLACE},
108 {0, 0, 0, 0},
109 };
110
111 while (1)
112 {
113 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
114
115 if (option_rv == -1) break;
116
117 switch (option_rv) {
118 case 0:
119 break;
120 case OPT_VERSION: /* --version or -V */
121 printf("memcache tools, memcp, v1.0\n");
122 exit(0);
123 case OPT_HELP: /* --help or -h */
124 printf("useful help messages go here\n");
125 exit(0);
126 case OPT_SERVERS: /* --servers or -s */
127 opt_servers= optarg;
128 break;
129 case OPT_FLAG: /* --flag */
130 opt_flags= (uint16_t)strtol(optarg, (char **)NULL, 10);
131 break;
132 case OPT_EXPIRE: /* --expire */
133 opt_expires= (time_t)strtol(optarg, (char **)NULL, 10);
134 break;
135 case '?':
136 /* getopt_long already printed an error message. */
137 exit(1);
138 default:
139 abort();
140 }
141 }
142 }