10 #include <memcached.h>
11 #include "client_options.h"
14 void options_parse(int argc
, char *argv
[]);
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;
22 int main(int argc
, char *argv
[])
28 options_parse(argc
, argv
);
30 memc
= memcached_init(NULL
);
32 parse_opt_servers(memc
, opt_servers
);
34 parse_opt_servers(memc
, argv
[--argc
]);
42 char *file_buffer_ptr
;
44 fd
= open(argv
[optind
], O_RDONLY
);
47 fprintf(stderr
, "memcp: %s: %s\n", argv
[optind
], strerror(errno
));
52 (void)fstat(fd
, &sbuf
);
54 ptr
= rindex(argv
[optind
], '/');
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
);
69 if ((file_buffer_ptr
= (char *)malloc(sizeof(char) * sbuf
.st_size
)) == NULL
)
71 fprintf(stderr
, "malloc: %s\n", strerror(errno
));
75 if ((read_length
= read(fd
, file_buffer_ptr
, sbuf
.st_size
)) == -1)
77 fprintf(stderr
, "read: %s\n", strerror(errno
));
80 assert(read_length
== sbuf
.st_size
);
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
);
91 rc
= memcached_set(memc
, ptr
, strlen(ptr
),
92 file_buffer_ptr
, sbuf
.st_size
,
93 opt_expires
, opt_flags
);
95 if (rc
!= MEMCACHED_SUCCESS
)
96 fprintf(stderr
, "memcp: %s: memcache error %s\n",
97 ptr
, memcached_strerror(memc
, rc
));
100 free(file_buffer_ptr
);
105 memcached_deinit(memc
);
112 void options_parse(int argc
, char *argv
[])
117 static struct option long_options
[] =
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
},
134 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
136 if (option_rv
== -1) break;
141 case OPT_VERSION
: /* --version or -V */
142 printf("memcache tools, memcp, v1.0\n");
144 case OPT_HELP
: /* --help or -h */
145 printf("useful help messages go here\n");
147 case OPT_SERVERS
: /* --servers or -s */
148 opt_servers
= strdup_cleanup(optarg
);
150 case OPT_FLAG
: /* --flag */
151 opt_flags
= (uint16_t)strtol(optarg
, (char **)NULL
, 10);
153 case OPT_EXPIRE
: /* --expire */
154 opt_expires
= (time_t)strtol(optarg
, (char **)NULL
, 10);
160 opt_method
= OPT_REPLACE
;
166 /* getopt_long already printed an error message. */