11 #include <memcached.h>
12 #include "client_options.h"
13 #include "utilities.h"
16 void options_parse(int argc
, char *argv
[]);
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;
24 int main(int argc
, char *argv
[])
29 options_parse(argc
, argv
);
31 memc
= memcached_init(NULL
);
34 parse_opt_servers(memc
, opt_servers
);
36 parse_opt_servers(memc
, argv
[--argc
]);
44 char *file_buffer_ptr
;
46 fd
= open(argv
[optind
], O_RDONLY
);
49 fprintf(stderr
, "memcp: %s: %s\n", argv
[optind
], strerror(errno
));
54 (void)fstat(fd
, &sbuf
);
56 ptr
= rindex(argv
[optind
], '/');
64 static char *opstr
[] = { "set", "add", "replace" };
65 printf("op: %s\nsource file: %s\nlength: %zu\n"
66 "key: %s\nflags: %x\nexpires: %llu\n",
67 opstr
[opt_method
- OPT_SET
], argv
[optind
], (size_t)sbuf
.st_size
,
68 ptr
, opt_flags
, (unsigned long long)opt_expires
);
71 if ((file_buffer_ptr
= (char *)malloc(sizeof(char) * sbuf
.st_size
)) == NULL
)
73 fprintf(stderr
, "malloc: %s\n", strerror(errno
));
77 if ((read_length
= read(fd
, file_buffer_ptr
, sbuf
.st_size
)) == -1)
79 fprintf(stderr
, "read: %s\n", strerror(errno
));
82 assert(read_length
== sbuf
.st_size
);
84 if (opt_method
== OPT_ADD
)
85 rc
= memcached_add(memc
, ptr
, strlen(ptr
),
86 file_buffer_ptr
, sbuf
.st_size
,
87 opt_expires
, opt_flags
);
88 else if (opt_method
== OPT_REPLACE
)
89 rc
= memcached_replace(memc
, ptr
, strlen(ptr
),
90 file_buffer_ptr
, sbuf
.st_size
,
91 opt_expires
, opt_flags
);
93 rc
= memcached_set(memc
, ptr
, strlen(ptr
),
94 file_buffer_ptr
, sbuf
.st_size
,
95 opt_expires
, opt_flags
);
97 if (rc
!= MEMCACHED_SUCCESS
)
98 fprintf(stderr
, "memcp: %s: memcache error %s\n",
99 ptr
, memcached_strerror(memc
, rc
));
101 free(file_buffer_ptr
);
106 memcached_deinit(memc
);
113 void options_parse(int argc
, char *argv
[])
118 static struct option long_options
[]=
120 {"version", no_argument
, NULL
, OPT_VERSION
},
121 {"help", no_argument
, NULL
, OPT_HELP
},
122 {"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
123 {"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
124 {"servers", required_argument
, NULL
, OPT_SERVERS
},
125 {"flag", required_argument
, NULL
, OPT_FLAG
},
126 {"expire", required_argument
, NULL
, OPT_EXPIRE
},
127 {"set", no_argument
, NULL
, OPT_SET
},
128 {"add", no_argument
, NULL
, OPT_ADD
},
129 {"replace", no_argument
, NULL
, OPT_REPLACE
},
135 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
137 if (option_rv
== -1) break;
143 case OPT_VERBOSE
: /* --verbose or -v */
144 opt_verbose
= OPT_VERBOSE
;
146 case OPT_DEBUG
: /* --debug or -d */
147 opt_verbose
= OPT_DEBUG
;
149 case OPT_VERSION
: /* --version or -V */
150 printf("memcache tools, memcp, v1.0\n");
152 case OPT_HELP
: /* --help or -h */
153 printf("useful help messages go here\n");
155 case OPT_SERVERS
: /* --servers or -s */
156 opt_servers
= strdup(optarg
);
158 case OPT_FLAG
: /* --flag */
159 opt_flags
= (uint16_t)strtol(optarg
, (char **)NULL
, 16);
161 case OPT_EXPIRE
: /* --expire */
162 opt_expires
= (time_t)strtoll(optarg
, (char **)NULL
, 10);
168 opt_method
= OPT_REPLACE
;
174 /* getopt_long already printed an error message. */