3 Main program, argument handling and block copying.
5 Copyright (C) James Peach 2005-2006
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "system/filesys.h"
23 #include "auth/gensec/gensec.h"
24 #include "lib/cmdline/popt_common.h"
25 #include "libcli/resolve/resolve.h"
26 #include "libcli/raw/libcliraw.h"
27 #include "lib/events/events.h"
30 #include "param/param.h"
32 const char * const PROGNAME = "cifsdd";
34 #define SYNTAX_EXIT_CODE 1 /* Invokation syntax or logic error. */
35 #define EOM_EXIT_CODE 9 /* Out of memory error. */
36 #define FILESYS_EXIT_CODE 10 /* File manipulation error. */
37 #define IOERROR_EXIT_CODE 11 /* Error during IO phase. */
39 struct dd_stats_record dd_stats;
42 static int dd_sigusr1;
44 static void dd_handle_signal(int sig)
59 /* ------------------------------------------------------------------------- */
60 /* Argument handling. */
61 /* ------------------------------------------------------------------------- */
63 static const char * argtype_str(enum argtype arg_type)
66 enum argtype arg_type;
67 const char * arg_name;
70 { ARG_NUMERIC, "COUNT" },
72 { ARG_PATHNAME, "FILE" },
73 { ARG_BOOL, "BOOLEAN" },
78 for (i = 0; i < ARRAY_SIZE(names); ++i) {
79 if (arg_type == names[i].arg_type) {
80 return(names[i].arg_name);
87 static struct argdef args[] =
89 { "bs", ARG_SIZE, "force ibs and obs to SIZE bytes" },
90 { "ibs", ARG_SIZE, "read SIZE bytes at a time" },
91 { "obs", ARG_SIZE, "write SIZE bytes at a time" },
93 { "count", ARG_NUMERIC, "copy COUNT input blocks" },
94 { "seek",ARG_NUMERIC, "skip COUNT blocks at start of output" },
95 { "skip",ARG_NUMERIC, "skip COUNT blocks at start of input" },
97 { "if", ARG_PATHNAME, "read input from FILE" },
98 { "of", ARG_PATHNAME, "write output to FILE" },
100 { "direct", ARG_BOOL, "use direct I/O if non-zero" },
101 { "sync", ARG_BOOL, "use synchronous writes if non-zero" },
102 { "oplock", ARG_BOOL, "take oplocks on the input and output files" },
104 /* FIXME: We should support using iflags and oflags for setting oplock and I/O
105 * options. This would make us compatible with GNU dd.
109 static struct argdef * find_named_arg(const char * arg)
113 for (i = 0; i < ARRAY_SIZE(args); ++i) {
114 if (strwicmp(arg, args[i].arg_name) == 0) {
122 int set_arg_argv(const char * argv)
129 if ((name = strdup(argv)) == NULL) {
133 if ((val = strchr(name, '=')) == NULL) {
134 fprintf(stderr, "%s: malformed argument \"%s\"\n",
142 if ((arg = find_named_arg(name)) == NULL) {
143 fprintf(stderr, "%s: ignoring unknown argument \"%s\"\n",
148 /* Found a matching name; convert the variable argument. */
149 switch (arg->arg_type) {
151 if (!conv_str_u64(val, &arg->arg_val.nval)) {
156 if (!conv_str_size(val, &arg->arg_val.nval)) {
161 if (!conv_str_bool(val, &arg->arg_val.bval)) {
166 if (!(arg->arg_val.pval = strdup(val))) {
171 fprintf(stderr, "%s: argument \"%s\" is of "
172 "unknown type\n", PROGNAME, name);
184 void set_arg_val(const char * name, ...)
190 if ((arg = find_named_arg(name)) == NULL) {
194 /* Found a matching name; convert the variable argument. */
195 switch (arg->arg_type) {
198 arg->arg_val.nval = va_arg(ap, uint64_t);
201 arg->arg_val.bval = va_arg(ap, int);
204 arg->arg_val.pval = va_arg(ap, char *);
205 if (arg->arg_val.pval) {
206 arg->arg_val.pval = strdup(arg->arg_val.pval);
210 fprintf(stderr, "%s: argument \"%s\" is of "
211 "unknown type\n", PROGNAME, name);
219 fprintf(stderr, "%s: ignoring unknown argument \"%s\"\n",
225 bool check_arg_bool(const char * name)
229 if ((arg = find_named_arg(name)) &&
230 (arg->arg_type == ARG_BOOL)) {
231 return(arg->arg_val.bval);
234 DEBUG(0, ("invalid argument name: %s", name));
239 uint64_t check_arg_numeric(const char * name)
243 if ((arg = find_named_arg(name)) &&
244 (arg->arg_type == ARG_NUMERIC || arg->arg_type == ARG_SIZE)) {
245 return(arg->arg_val.nval);
248 DEBUG(0, ("invalid argument name: %s", name));
253 const char * check_arg_pathname(const char * name)
257 if ((arg = find_named_arg(name)) &&
258 (arg->arg_type == ARG_PATHNAME)) {
259 return(arg->arg_val.pval);
262 DEBUG(0, ("invalid argument name: %s", name));
267 static void dump_args(void)
271 DEBUG(10, ("dumping argument values:\n"));
272 for (i = 0; i < ARRAY_SIZE(args); ++i) {
273 switch (args[i].arg_type) {
276 DEBUG(10, ("\t%s=%llu\n", args[i].arg_name,
277 (unsigned long long)args[i].arg_val.nval));
280 DEBUG(10, ("\t%s=%s\n", args[i].arg_name,
281 args[i].arg_val.bval ? "yes" : "no"));
284 DEBUG(10, ("\t%s=%s\n", args[i].arg_name,
285 args[i].arg_val.pval ?
286 args[i].arg_val.pval :
295 static void cifsdd_help_message(poptContext pctx,
296 enum poptCallbackReason preason,
297 struct poptOption * poption,
301 static const char notes[] =
302 "FILE can be a local filename or a UNC path of the form //server/share/path.\n";
307 if (poption->shortName != '?') {
308 poptPrintUsage(pctx, stdout, 0);
309 fprintf(stdout, " [dd options]\n");
313 poptPrintHelp(pctx, stdout, 0);
314 fprintf(stdout, "\nCIFS dd options:\n");
316 for (i = 0; i < ARRAY_SIZE(args); ++i) {
317 if (args[i].arg_name == NULL) {
321 snprintf(prefix, sizeof(prefix), "%s=%-*s",
323 (int)(sizeof(prefix) - strlen(args[i].arg_name) - 2),
324 argtype_str(args[i].arg_type));
325 prefix[sizeof(prefix) - 1] = '\0';
326 fprintf(stdout, " %s%s\n", prefix, args[i].arg_help);
329 fprintf(stdout, "\n%s\n", notes);
333 /* ------------------------------------------------------------------------- */
334 /* Main block copying routine. */
335 /* ------------------------------------------------------------------------- */
337 static void print_transfer_stats(void)
339 if (DEBUGLEVEL > 0) {
340 printf("%llu+%llu records in (%llu bytes)\n"
341 "%llu+%llu records out (%llu bytes)\n",
342 (unsigned long long)dd_stats.in.fblocks,
343 (unsigned long long)dd_stats.in.pblocks,
344 (unsigned long long)dd_stats.in.bytes,
345 (unsigned long long)dd_stats.out.fblocks,
346 (unsigned long long)dd_stats.out.pblocks,
347 (unsigned long long)dd_stats.out.bytes);
349 printf("%llu+%llu records in\n%llu+%llu records out\n",
350 (unsigned long long)dd_stats.in.fblocks,
351 (unsigned long long)dd_stats.in.pblocks,
352 (unsigned long long)dd_stats.out.fblocks,
353 (unsigned long long)dd_stats.out.pblocks);
357 static struct dd_iohandle * open_file(struct resolve_context *resolve_ctx,
358 struct event_context *ev,
359 const char * which, const char **ports,
360 struct smbcli_options *smb_options,
361 const char *socket_options,
362 struct smbcli_session_options *smb_session_options,
363 struct smb_iconv_convenience *iconv_convenience)
366 const char * path = NULL;
367 struct dd_iohandle * handle = NULL;
369 if (check_arg_bool("direct")) {
370 options |= DD_DIRECT_IO;
373 if (check_arg_bool("sync")) {
374 options |= DD_SYNC_IO;
377 if (check_arg_bool("oplock")) {
378 options |= DD_OPLOCK;
381 if (strcmp(which, "if") == 0) {
382 path = check_arg_pathname("if");
383 handle = dd_open_path(resolve_ctx, ev, path, ports,
384 check_arg_numeric("ibs"), options,
386 smb_options, smb_session_options,
388 } else if (strcmp(which, "of") == 0) {
390 path = check_arg_pathname("of");
391 handle = dd_open_path(resolve_ctx, ev, path, ports,
392 check_arg_numeric("obs"), options,
394 smb_options, smb_session_options,
402 fprintf(stderr, "%s: failed to open %s\n", PROGNAME, path);
408 static int copy_files(struct event_context *ev, struct loadparm_context *lp_ctx)
410 uint8_t * iobuf; /* IO buffer. */
411 uint64_t iomax; /* Size of the IO buffer. */
412 uint64_t data_size; /* Amount of data in the IO buffer. */
418 struct dd_iohandle * ifile;
419 struct dd_iohandle * ofile;
421 struct smbcli_options options;
422 struct smbcli_session_options session_options;
424 ibs = check_arg_numeric("ibs");
425 obs = check_arg_numeric("obs");
426 count = check_arg_numeric("count");
428 lp_smbcli_options(lp_ctx, &options);
429 lp_smbcli_session_options(lp_ctx, &session_options);
431 /* Allocate IO buffer. We need more than the max IO size because we
432 * could accumulate a remainder if ibs and obs don't match.
434 iomax = 2 * MAX(ibs, obs);
435 if ((iobuf = malloc_array_p(uint8_t, iomax)) == NULL) {
437 "%s: failed to allocate IO buffer of %llu bytes\n",
438 PROGNAME, (unsigned long long)iomax);
439 return(EOM_EXIT_CODE);
442 options.max_xmit = MAX(ibs, obs);
444 DEBUG(4, ("IO buffer size is %llu, max xmit is %d\n",
445 (unsigned long long)iomax, options.max_xmit));
447 if (!(ifile = open_file(lp_resolve_context(lp_ctx), ev, "if",
448 lp_smb_ports(lp_ctx), &options,
449 lp_socket_options(lp_ctx),
450 &session_options, lp_iconv_convenience(lp_ctx)))) {
451 return(FILESYS_EXIT_CODE);
454 if (!(ofile = open_file(lp_resolve_context(lp_ctx), ev, "of",
455 lp_smb_ports(lp_ctx), &options,
456 lp_socket_options(lp_ctx),
458 lp_iconv_convenience(lp_ctx)))) {
459 return(FILESYS_EXIT_CODE);
462 /* Seek the files to their respective starting points. */
463 ifile->io_seek(ifile, check_arg_numeric("skip") * ibs);
464 ofile->io_seek(ofile, check_arg_numeric("seek") * obs);
466 DEBUG(4, ("max xmit was negotiated to be %d\n", options.max_xmit));
468 for (data_size = 0;;) {
470 /* Handle signals. We are somewhat compatible with GNU dd.
471 * SIGINT makes us stop, but still print transfer statistics.
472 * SIGUSR1 makes us print transfer statistics but we continue
480 print_transfer_stats();
484 if (ifile->io_flags & DD_END_OF_FILE) {
485 DEBUG(4, ("flushing %llu bytes at EOF\n",
486 (unsigned long long)data_size));
487 while (data_size > 0) {
488 if (!dd_flush_block(ofile, iobuf,
490 return(IOERROR_EXIT_CODE);
496 /* Try and read enough blocks of ibs bytes to be able write
497 * out one of obs bytes.
499 if (!dd_fill_block(ifile, iobuf, &data_size, obs, ibs)) {
500 return(IOERROR_EXIT_CODE);
503 if (data_size == 0) {
505 SMB_ASSERT(ifile->io_flags & DD_END_OF_FILE);
508 /* Stop reading when we hit the block count. */
509 if (dd_stats.in.bytes >= (ibs * count)) {
510 ifile->io_flags |= DD_END_OF_FILE;
513 /* If we wanted to be a legitimate dd, we would do character
514 * conversions and other shenanigans here.
517 /* Flush what we read in units of obs bytes. We want to have
518 * at least obs bytes in the IO buffer but might not if the
522 !dd_flush_block(ofile, iobuf, &data_size, obs)) {
523 return(IOERROR_EXIT_CODE);
528 print_transfer_stats();
532 /* ------------------------------------------------------------------------- */
534 /* ------------------------------------------------------------------------- */
536 struct poptOption cifsddHelpOptions[] = {
537 { NULL, '\0', POPT_ARG_CALLBACK, (void *)&cifsdd_help_message, '\0', NULL, NULL },
538 { "help", '?', 0, NULL, '?', "Show this help message", NULL },
539 { "usage", '\0', 0, NULL, 'u', "Display brief usage message", NULL },
543 int main(int argc, const char ** argv)
546 const char ** dd_args;
547 struct event_context *ev;
550 struct poptOption poptions[] = {
552 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, cifsddHelpOptions,
553 0, "Help options:", NULL },
555 POPT_COMMON_CONNECTION
556 POPT_COMMON_CREDENTIALS
562 set_arg_val("bs", (uint64_t)4096);
563 set_arg_val("ibs", (uint64_t)4096);
564 set_arg_val("obs", (uint64_t)4096);
566 set_arg_val("count", (uint64_t)-1);
567 set_arg_val("seek", (uint64_t)0);
568 set_arg_val("seek", (uint64_t)0);
570 set_arg_val("if", NULL);
571 set_arg_val("of", NULL);
573 set_arg_val("direct", false);
574 set_arg_val("sync", false);
575 set_arg_val("oplock", false);
577 pctx = poptGetContext(PROGNAME, argc, argv, poptions, 0);
578 while ((i = poptGetNextOpt(pctx)) != -1) {
582 for (dd_args = poptGetArgs(pctx); dd_args && *dd_args; ++dd_args) {
584 if (!set_arg_argv(*dd_args)) {
585 fprintf(stderr, "%s: invalid option: %s\n",
587 exit(SYNTAX_EXIT_CODE);
590 /* "bs" has the side-effect of setting "ibs" and "obs". */
591 if (strncmp(*dd_args, "bs=", 3) == 0) {
592 uint64_t bs = check_arg_numeric("bs");
593 set_arg_val("ibs", bs);
594 set_arg_val("obs", bs);
598 ev = s4_event_context_init(talloc_autofree_context());
600 gensec_init(cmdline_lp_ctx);
603 if (check_arg_numeric("ibs") == 0 || check_arg_numeric("ibs") == 0) {
604 fprintf(stderr, "%s: block sizes must be greater that zero\n",
606 exit(SYNTAX_EXIT_CODE);
609 if (check_arg_pathname("if") == NULL) {
610 fprintf(stderr, "%s: missing input filename\n", PROGNAME);
611 exit(SYNTAX_EXIT_CODE);
614 if (check_arg_pathname("of") == NULL) {
615 fprintf(stderr, "%s: missing output filename\n", PROGNAME);
616 exit(SYNTAX_EXIT_CODE);
619 CatchSignal(SIGINT, dd_handle_signal);
620 CatchSignal(SIGUSR1, dd_handle_signal);
621 return(copy_files(ev, cmdline_lp_ctx));
624 /* vim: set sw=8 sts=8 ts=8 tw=79 : */