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"
28 const char * const PROGNAME = "cifsdd";
30 #define SYNTAX_EXIT_CODE 1 /* Invokation syntax or logic error. */
31 #define EOM_EXIT_CODE 9 /* Out of memory error. */
32 #define FILESYS_EXIT_CODE 10 /* File manipulation error. */
33 #define IOERROR_EXIT_CODE 11 /* Error during IO phase. */
35 struct dd_stats_record dd_stats;
38 static int dd_sigusr1;
40 static void dd_handle_signal(int sig)
55 /* ------------------------------------------------------------------------- */
56 /* Argument handling. */
57 /* ------------------------------------------------------------------------- */
59 static const char * argtype_str(enum argtype arg_type)
62 enum argtype arg_type;
63 const char * arg_name;
66 { ARG_NUMERIC, "COUNT" },
68 { ARG_PATHNAME, "FILE" },
69 { ARG_BOOL, "BOOLEAN" },
74 for (i = 0; i < ARRAY_SIZE(names); ++i) {
75 if (arg_type == names[i].arg_type) {
76 return(names[i].arg_name);
83 static struct argdef args[] =
85 { "bs", ARG_SIZE, "force ibs and obs to SIZE bytes" },
86 { "ibs", ARG_SIZE, "read SIZE bytes at a time" },
87 { "obs", ARG_SIZE, "write SIZE bytes at a time" },
89 { "count", ARG_NUMERIC, "copy COUNT input blocks" },
90 { "seek",ARG_NUMERIC, "skip COUNT blocks at start of output" },
91 { "skip",ARG_NUMERIC, "skip COUNT blocks at start of input" },
93 { "if", ARG_PATHNAME, "read input from FILE" },
94 { "of", ARG_PATHNAME, "write output to FILE" },
96 { "direct", ARG_BOOL, "use direct I/O if non-zero" },
97 { "sync", ARG_BOOL, "use synchronous writes if non-zero" },
98 { "oplock", ARG_BOOL, "take oplocks on the input and output files" },
100 /* FIXME: We should support using iflags and oflags for setting oplock and I/O
101 * options. This would make us compatible with GNU dd.
105 struct argdef * find_named_arg(const char * arg)
109 for (i = 0; i < ARRAY_SIZE(args); ++i) {
110 if (strwicmp(arg, args[i].arg_name) == 0) {
118 int set_arg_argv(const char * argv)
125 if ((name = strdup(argv)) == NULL) {
129 if ((val = strchr(name, '=')) == NULL) {
130 fprintf(stderr, "%s: malformed argument \"%s\"\n",
138 if ((arg = find_named_arg(name)) == NULL) {
139 fprintf(stderr, "%s: ignoring unknown argument \"%s\"\n",
144 /* Found a matching name; convert the variable argument. */
145 switch (arg->arg_type) {
147 if (!conv_str_u64(val, &arg->arg_val.nval)) {
152 if (!conv_str_size(val, &arg->arg_val.nval)) {
157 if (!conv_str_bool(val, &arg->arg_val.bval)) {
162 if (!(arg->arg_val.pval = strdup(val))) {
167 fprintf(stderr, "%s: argument \"%s\" is of "
168 "unknown type\n", PROGNAME, name);
180 void set_arg_val(const char * name, ...)
186 if ((arg = find_named_arg(name)) == NULL) {
190 /* Found a matching name; convert the variable argument. */
191 switch (arg->arg_type) {
194 arg->arg_val.nval = va_arg(ap, uint64_t);
197 arg->arg_val.bval = va_arg(ap, int);
200 arg->arg_val.pval = va_arg(ap, char *);
201 if (arg->arg_val.pval) {
202 arg->arg_val.pval = strdup(arg->arg_val.pval);
206 fprintf(stderr, "%s: argument \"%s\" is of "
207 "unknown type\n", PROGNAME, name);
215 fprintf(stderr, "%s: ignoring unknown argument \"%s\"\n",
221 BOOL check_arg_bool(const char * name)
225 if ((arg = find_named_arg(name)) &&
226 (arg->arg_type == ARG_BOOL)) {
227 return(arg->arg_val.bval);
230 DEBUG(0, ("invalid argument name: %s", name));
235 uint64_t check_arg_numeric(const char * name)
239 if ((arg = find_named_arg(name)) &&
240 (arg->arg_type == ARG_NUMERIC || arg->arg_type == ARG_SIZE)) {
241 return(arg->arg_val.nval);
244 DEBUG(0, ("invalid argument name: %s", name));
249 const char * check_arg_pathname(const char * name)
253 if ((arg = find_named_arg(name)) &&
254 (arg->arg_type == ARG_PATHNAME)) {
255 return(arg->arg_val.pval);
258 DEBUG(0, ("invalid argument name: %s", name));
263 static void dump_args(void)
267 DEBUG(10, ("dumping argument values:\n"));
268 for (i = 0; i < ARRAY_SIZE(args); ++i) {
269 switch (args[i].arg_type) {
272 DEBUG(10, ("\t%s=%llu\n", args[i].arg_name,
273 (unsigned long long)args[i].arg_val.nval));
276 DEBUG(10, ("\t%s=%s\n", args[i].arg_name,
277 args[i].arg_val.bval ? "yes" : "no"));
280 DEBUG(10, ("\t%s=%s\n", args[i].arg_name,
281 args[i].arg_val.pval ?
282 args[i].arg_val.pval :
291 static void cifsdd_help_message(poptContext pctx,
292 enum poptCallbackReason preason,
293 struct poptOption * poption,
297 static const char notes[] =
298 "FILE can be a local filename or a UNC path of the form //server/share/path.\n";
303 if (poption->shortName != '?') {
304 poptPrintUsage(pctx, stdout, 0);
305 fprintf(stdout, " [dd options]\n");
309 poptPrintHelp(pctx, stdout, 0);
310 fprintf(stdout, "\nCIFS dd options:\n");
312 for (i = 0; i < ARRAY_SIZE(args); ++i) {
313 if (args[i].arg_name == NULL) {
317 snprintf(prefix, sizeof(prefix), "%s=%-*s",
319 (int)(sizeof(prefix) - strlen(args[i].arg_name) - 2),
320 argtype_str(args[i].arg_type));
321 prefix[sizeof(prefix) - 1] = '\0';
322 fprintf(stdout, " %s%s\n", prefix, args[i].arg_help);
325 fprintf(stdout, "\n%s\n", notes);
329 /* ------------------------------------------------------------------------- */
330 /* Main block copying routine. */
331 /* ------------------------------------------------------------------------- */
333 static void print_transfer_stats(void)
335 if (DEBUGLEVEL > 0) {
336 printf("%llu+%llu records in (%llu bytes)\n"
337 "%llu+%llu records out (%llu bytes)\n",
338 (unsigned long long)dd_stats.in.fblocks,
339 (unsigned long long)dd_stats.in.pblocks,
340 (unsigned long long)dd_stats.in.bytes,
341 (unsigned long long)dd_stats.out.fblocks,
342 (unsigned long long)dd_stats.out.pblocks,
343 (unsigned long long)dd_stats.out.bytes);
345 printf("%llu+%llu records in\n%llu+%llu records out\n",
346 (unsigned long long)dd_stats.in.fblocks,
347 (unsigned long long)dd_stats.in.pblocks,
348 (unsigned long long)dd_stats.out.fblocks,
349 (unsigned long long)dd_stats.out.pblocks);
353 static struct dd_iohandle * open_file(const char * which)
356 const char * path = NULL;
357 struct dd_iohandle * handle = NULL;
359 if (check_arg_bool("direct")) {
360 options |= DD_DIRECT_IO;
363 if (check_arg_bool("sync")) {
364 options |= DD_SYNC_IO;
367 if (check_arg_bool("oplock")) {
368 options |= DD_OPLOCK;
371 if (strcmp(which, "if") == 0) {
372 path = check_arg_pathname("if");
373 handle = dd_open_path(path, check_arg_numeric("ibs"),
375 } else if (strcmp(which, "of") == 0) {
377 path = check_arg_pathname("of");
378 handle = dd_open_path(path, check_arg_numeric("obs"),
386 fprintf(stderr, "%s: failed to open %s\n", PROGNAME, path);
392 static void set_max_xmit(uint64_t iomax)
396 snprintf(buf, sizeof(buf), "%llu", (unsigned long long)iomax);
397 lp_set_cmdline("max xmit", buf);
400 static int copy_files(void)
402 uint8_t * iobuf; /* IO buffer. */
403 uint64_t iomax; /* Size of the IO buffer. */
404 uint64_t data_size; /* Amount of data in the IO buffer. */
410 struct dd_iohandle * ifile;
411 struct dd_iohandle * ofile;
413 ibs = check_arg_numeric("ibs");
414 obs = check_arg_numeric("obs");
415 count = check_arg_numeric("count");
417 /* Allocate IO buffer. We need more than the max IO size because we
418 * could accumulate a remainder if ibs and obs don't match.
420 iomax = 2 * MAX(ibs, obs);
421 if ((iobuf = malloc(iomax)) == NULL) {
423 "%s: failed to allocate IO buffer of %llu bytes\n",
424 PROGNAME, (unsigned long long)iomax);
425 return(EOM_EXIT_CODE);
428 set_max_xmit(MAX(ibs, obs));
430 DEBUG(4, ("IO buffer size is %llu, max xmit is %d\n",
431 (unsigned long long)iomax, lp_max_xmit()));
433 if (!(ifile = open_file("if"))) {
434 return(FILESYS_EXIT_CODE);
437 if (!(ofile = open_file("of"))) {
438 return(FILESYS_EXIT_CODE);
441 /* Seek the files to their respective starting points. */
442 ifile->io_seek(ifile, check_arg_numeric("skip") * ibs);
443 ofile->io_seek(ofile, check_arg_numeric("seek") * obs);
445 DEBUG(4, ("max xmit was negotiated to be %d\n", lp_max_xmit()));
447 for (data_size = 0;;) {
449 /* Handle signals. We are somewhat compatible with GNU dd.
450 * SIGINT makes us stop, but still print transfer statistics.
451 * SIGUSR1 makes us print transfer statistics but we continue
459 print_transfer_stats();
463 if (ifile->io_flags & DD_END_OF_FILE) {
464 DEBUG(4, ("flushing %llu bytes at EOF\n",
465 (unsigned long long)data_size));
466 while (data_size > 0) {
467 if (!dd_flush_block(ofile, iobuf,
469 return(IOERROR_EXIT_CODE);
475 /* Try and read enough blocks of ibs bytes to be able write
476 * out one of obs bytes.
478 if (!dd_fill_block(ifile, iobuf, &data_size, obs, ibs)) {
479 return(IOERROR_EXIT_CODE);
482 if (data_size == 0) {
484 SMB_ASSERT(ifile->io_flags & DD_END_OF_FILE);
487 /* Stop reading when we hit the block count. */
488 if (dd_stats.in.bytes >= (ibs * count)) {
489 ifile->io_flags |= DD_END_OF_FILE;
492 /* If we wanted to be a legitimate dd, we would do character
493 * conversions and other shenanigans here.
496 /* Flush what we read in units of obs bytes. We want to have
497 * at least obs bytes in the IO buffer but might not if the
501 !dd_flush_block(ofile, iobuf, &data_size, obs)) {
502 return(IOERROR_EXIT_CODE);
507 print_transfer_stats();
511 /* ------------------------------------------------------------------------- */
513 /* ------------------------------------------------------------------------- */
515 struct poptOption cifsddHelpOptions[] = {
516 { NULL, '\0', POPT_ARG_CALLBACK, (void *)&cifsdd_help_message, '\0', NULL, NULL },
517 { "help", '?', 0, NULL, '?', "Show this help message", NULL },
518 { "usage", '\0', 0, NULL, 'u', "Display brief usage message", NULL },
522 int main(int argc, const char ** argv)
525 const char ** dd_args;
528 struct poptOption poptions[] = {
530 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, cifsddHelpOptions,
531 0, "Help options:", NULL },
533 POPT_COMMON_CONNECTION
534 POPT_COMMON_CREDENTIALS
540 set_arg_val("bs", (uint64_t)4096);
541 set_arg_val("ibs", (uint64_t)4096);
542 set_arg_val("obs", (uint64_t)4096);
544 set_arg_val("count", (uint64_t)-1);
545 set_arg_val("seek", (uint64_t)0);
546 set_arg_val("seek", (uint64_t)0);
548 set_arg_val("if", NULL);
549 set_arg_val("of", NULL);
551 set_arg_val("direct", False);
552 set_arg_val("sync", False);
553 set_arg_val("oplock", False);
555 pctx = poptGetContext(PROGNAME, argc, argv, poptions, 0);
556 while ((i = poptGetNextOpt(pctx)) != -1) {
560 for (dd_args = poptGetArgs(pctx); dd_args && *dd_args; ++dd_args) {
562 if (!set_arg_argv(*dd_args)) {
563 fprintf(stderr, "%s: invalid option: %s\n",
565 exit(SYNTAX_EXIT_CODE);
568 /* "bs" has the side-effect of setting "ibs" and "obs". */
569 if (strncmp(*dd_args, "bs=", 3) == 0) {
570 uint64_t bs = check_arg_numeric("bs");
571 set_arg_val("ibs", bs);
572 set_arg_val("obs", bs);
579 if (check_arg_numeric("ibs") == 0 || check_arg_numeric("ibs") == 0) {
580 fprintf(stderr, "%s: block sizes must be greater that zero\n",
582 exit(SYNTAX_EXIT_CODE);
585 if (check_arg_pathname("if") == NULL) {
586 fprintf(stderr, "%s: missing input filename\n", PROGNAME);
587 exit(SYNTAX_EXIT_CODE);
590 if (check_arg_pathname("of") == NULL) {
591 fprintf(stderr, "%s: missing output filename\n", PROGNAME);
592 exit(SYNTAX_EXIT_CODE);
595 CatchSignal(SIGINT, dd_handle_signal);
596 CatchSignal(SIGUSR1, dd_handle_signal);
597 return(copy_files());
600 /* vim: set sw=8 sts=8 ts=8 tw=79 : */