11 #include <memcached.h>
12 #include "client_options.h"
13 #include "utilities.h"
15 #define PROGRAM_NAME "memcp"
16 #define PROGRAM_DESCRIPTION "Copy a set of files to a memcached cluster."
19 void options_parse(int argc
, char *argv
[]);
21 static int opt_verbose
= 0;
22 static char *opt_servers
= NULL
;
23 static int opt_method
= OPT_SET
;
24 static uint16_t opt_flags
= 0;
25 static time_t opt_expires
= 0;
27 int main(int argc
, char *argv
[])
31 memcached_server_st
*servers
;
33 options_parse(argc
, argv
);
35 memc
= memcached_create(NULL
);
41 if ((temp
= getenv("MEMCACHED_SERVERS")))
42 opt_servers
= strdup(temp
);
48 servers
= memcached_servers_parse(opt_servers
);
50 servers
= memcached_servers_parse(argv
[--argc
]);
52 memcached_server_push(memc
, servers
);
53 memcached_server_list_free(servers
);
61 char *file_buffer_ptr
;
63 fd
= open(argv
[optind
], O_RDONLY
);
66 fprintf(stderr
, "memcp: %s: %s\n", argv
[optind
], strerror(errno
));
71 (void)fstat(fd
, &sbuf
);
73 ptr
= rindex(argv
[optind
], '/');
81 static char *opstr
[] = { "set", "add", "replace" };
82 printf("op: %s\nsource file: %s\nlength: %zu\n"
83 "key: %s\nflags: %x\nexpires: %llu\n",
84 opstr
[opt_method
- OPT_SET
], argv
[optind
], (size_t)sbuf
.st_size
,
85 ptr
, opt_flags
, (unsigned long long)opt_expires
);
88 if ((file_buffer_ptr
= (char *)malloc(sizeof(char) * sbuf
.st_size
)) == NULL
)
90 fprintf(stderr
, "malloc: %s\n", strerror(errno
));
94 if ((read_length
= read(fd
, file_buffer_ptr
, sbuf
.st_size
)) == -1)
96 fprintf(stderr
, "read: %s\n", strerror(errno
));
99 assert(read_length
== sbuf
.st_size
);
101 if (opt_method
== OPT_ADD
)
102 rc
= memcached_add(memc
, ptr
, strlen(ptr
),
103 file_buffer_ptr
, sbuf
.st_size
,
104 opt_expires
, opt_flags
);
105 else if (opt_method
== OPT_REPLACE
)
106 rc
= memcached_replace(memc
, ptr
, strlen(ptr
),
107 file_buffer_ptr
, sbuf
.st_size
,
108 opt_expires
, opt_flags
);
110 rc
= memcached_set(memc
, ptr
, strlen(ptr
),
111 file_buffer_ptr
, sbuf
.st_size
,
112 opt_expires
, opt_flags
);
114 if (rc
!= MEMCACHED_SUCCESS
)
115 fprintf(stderr
, "memcp: %s: memcache error %s\n",
116 ptr
, memcached_strerror(memc
, rc
));
118 free(file_buffer_ptr
);
123 memcached_free(memc
);
130 void options_parse(int argc
, char *argv
[])
135 memcached_programs_help_st help_options
[]=
140 static struct option long_options
[]=
142 {"version", no_argument
, NULL
, OPT_VERSION
},
143 {"help", no_argument
, NULL
, OPT_HELP
},
144 {"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
145 {"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
146 {"servers", required_argument
, NULL
, OPT_SERVERS
},
147 {"flag", required_argument
, NULL
, OPT_FLAG
},
148 {"expire", required_argument
, NULL
, OPT_EXPIRE
},
149 {"set", no_argument
, NULL
, OPT_SET
},
150 {"add", no_argument
, NULL
, OPT_ADD
},
151 {"replace", no_argument
, NULL
, OPT_REPLACE
},
157 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
159 if (option_rv
== -1) break;
165 case OPT_VERBOSE
: /* --verbose or -v */
166 opt_verbose
= OPT_VERBOSE
;
168 case OPT_DEBUG
: /* --debug or -d */
169 opt_verbose
= OPT_DEBUG
;
171 case OPT_VERSION
: /* --version or -V */
172 version_command(PROGRAM_NAME
);
174 case OPT_HELP
: /* --help or -h */
175 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
177 case OPT_SERVERS
: /* --servers or -s */
178 opt_servers
= strdup(optarg
);
180 case OPT_FLAG
: /* --flag */
181 opt_flags
= (uint16_t)strtol(optarg
, (char **)NULL
, 16);
183 case OPT_EXPIRE
: /* --expire */
184 opt_expires
= (time_t)strtoll(optarg
, (char **)NULL
, 10);
190 opt_method
= OPT_REPLACE
;
196 /* getopt_long already printed an error message. */