python/tests: turn GraphError into failure inside of test_verify()
[Samba.git] / source3 / utils / smbget.c
blob4653c6894e0049898c093743263b27176b674294
1 /*
2 smbget: a wget-like utility with support for recursive downloading of
3 smb:// urls
4 Copyright (C) 2003-2004 Jelmer Vernooij <jelmer@samba.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "includes.h"
20 #include "system/filesys.h"
21 #include "popt_common_cmdline.h"
22 #include "libsmbclient.h"
23 #include "cmdline_contexts.h"
25 static int columns = 0;
27 static time_t total_start_time = 0;
28 static off_t total_bytes = 0;
30 #define SMB_MAXPATHLEN MAXPATHLEN
33 * Number of bytes to read when checking whether local and remote file
34 * are really the same file
36 #define RESUME_CHECK_SIZE 512
37 #define RESUME_DOWNLOAD_OFFSET 1024
38 #define RESUME_CHECK_OFFSET (RESUME_DOWNLOAD_OFFSET+RESUME_CHECK_SIZE)
39 /* Number of bytes to read at once */
40 #define SMB_DEFAULT_BLOCKSIZE 64000
42 struct opt {
43 char *workgroup;
44 bool username_specified;
45 char *username;
46 bool password_specified;
47 char *password;
49 char *outputfile;
50 size_t blocksize;
52 bool nonprompt;
53 bool quiet;
54 bool dots;
55 bool verbose;
56 bool send_stdout;
57 bool update;
58 int debuglevel;
60 static struct opt opt = { .blocksize = SMB_DEFAULT_BLOCKSIZE };
62 static bool smb_download_file(const char *base, const char *name,
63 bool recursive, bool resume, bool toplevel,
64 char *outfile);
66 static int get_num_cols(void)
68 #ifdef TIOCGWINSZ
69 struct winsize ws;
70 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0) {
71 return 0;
73 return ws.ws_col;
74 #else
75 #warning No support for TIOCGWINSZ
76 char *cols = getenv("COLUMNS");
77 if (!cols) {
78 return 0;
80 return atoi(cols);
81 #endif
84 static void change_columns(int sig)
86 columns = get_num_cols();
89 static void human_readable(off_t s, char *buffer, int l)
91 if (s > 1024 * 1024 * 1024) {
92 snprintf(buffer, l, "%.2fGB", 1.0 * s / (1024 * 1024 * 1024));
93 } else if (s > 1024 * 1024) {
94 snprintf(buffer, l, "%.2fMB", 1.0 * s / (1024 * 1024));
95 } else if (s > 1024) {
96 snprintf(buffer, l, "%.2fkB", 1.0 * s / 1024);
97 } else {
98 snprintf(buffer, l, "%jdb", (intmax_t)s);
102 static void get_auth_data(const char *srv, const char *shr, char *wg, int wglen,
103 char *un, int unlen, char *pw, int pwlen)
105 static bool hasasked = false;
106 static char *savedwg;
107 static char *savedun;
108 static char *savedpw;
110 if (hasasked) {
111 strncpy(wg, savedwg, wglen - 1);
112 strncpy(un, savedun, unlen - 1);
113 strncpy(pw, savedpw, pwlen - 1);
114 return;
116 hasasked = true;
119 * If no user has been specified un is initialized with the current
120 * username of the user who started smbget.
122 if (opt.username_specified) {
123 strncpy(un, opt.username, unlen - 1);
126 if (!opt.nonprompt && !opt.password_specified && pw[0] == '\0') {
127 char *prompt;
128 int rc;
130 rc = asprintf(&prompt,
131 "Password for [%s] connecting to //%s/%s: ",
132 un, shr, srv);
133 if (rc == -1) {
134 return;
136 (void)samba_getpass(prompt, pw, pwlen, false, false);
137 free(prompt);
138 } else if (opt.password != NULL) {
139 strncpy(pw, opt.password, pwlen-1);
142 if (opt.workgroup != NULL) {
143 strncpy(wg, opt.workgroup, wglen-1);
146 /* save the values found for later */
147 savedwg = SMB_STRDUP(wg);
148 savedun = SMB_STRDUP(un);
149 savedpw = SMB_STRDUP(pw);
151 if (!opt.quiet) {
152 char *wgtmp, *usertmp;
153 wgtmp = SMB_STRNDUP(wg, wglen);
154 usertmp = SMB_STRNDUP(un, unlen);
155 printf("Using workgroup %s, %s%s\n",
156 wgtmp,
157 *usertmp ? "user " : "guest user",
158 usertmp);
159 free(wgtmp);
160 free(usertmp);
164 static bool smb_download_dir(const char *base, const char *name, int resume)
166 char path[SMB_MAXPATHLEN];
167 int dirhandle;
168 struct smbc_dirent *dirent;
169 const char *relname = name;
170 char *tmpname;
171 bool ok = false;
173 snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base,
174 (base[0] && name[0] && name[0] != '/' &&
175 base[strlen(base)-1] != '/') ? "/" : "",
176 name);
178 /* List files in directory and call smb_download_file on them */
179 dirhandle = smbc_opendir(path);
180 if (dirhandle < 1) {
181 if (errno == ENOTDIR) {
182 return smb_download_file(base, name, true, resume,
183 false, NULL);
185 fprintf(stderr, "Can't open directory %s: %s\n", path,
186 strerror(errno));
187 return false;
190 while (*relname == '/') {
191 relname++;
193 mkdir(relname, 0755);
195 tmpname = SMB_STRDUP(name);
197 while ((dirent = smbc_readdir(dirhandle))) {
198 char *newname;
199 if (!strcmp(dirent->name, ".") || !strcmp(dirent->name, "..")) {
200 continue;
202 if (asprintf(&newname, "%s/%s", tmpname, dirent->name) == -1) {
203 free(tmpname);
204 return false;
206 switch (dirent->smbc_type) {
207 case SMBC_DIR:
208 ok = smb_download_dir(base, newname, resume);
209 break;
211 case SMBC_WORKGROUP:
212 ok = smb_download_dir("smb://", dirent->name, resume);
213 break;
215 case SMBC_SERVER:
216 ok = smb_download_dir("smb://", dirent->name, resume);
217 break;
219 case SMBC_FILE:
220 ok = smb_download_file(base, newname, true, resume,
221 false, NULL);
222 break;
224 case SMBC_FILE_SHARE:
225 ok = smb_download_dir(base, newname, resume);
226 break;
228 case SMBC_PRINTER_SHARE:
229 if (!opt.quiet) {
230 printf("Ignoring printer share %s\n",
231 dirent->name);
233 break;
235 case SMBC_COMMS_SHARE:
236 if (!opt.quiet) {
237 printf("Ignoring comms share %s\n",
238 dirent->name);
240 break;
242 case SMBC_IPC_SHARE:
243 if (!opt.quiet) {
244 printf("Ignoring ipc$ share %s\n",
245 dirent->name);
247 break;
249 default:
250 fprintf(stderr, "Ignoring file '%s' of type '%d'\n",
251 newname, dirent->smbc_type);
252 break;
255 if (!ok) {
256 fprintf(stderr, "Failed to download %s: %s\n",
257 newname, strerror(errno));
258 free(tmpname);
259 return false;
261 free(newname);
263 free(tmpname);
265 smbc_closedir(dirhandle);
266 return ok;
269 static char *print_time(long t)
271 static char buffer[100];
272 int secs, mins, hours;
273 if (t < -1) {
274 strncpy(buffer, "Unknown", sizeof(buffer));
275 return buffer;
278 secs = (int)t % 60;
279 mins = (int)t / 60 % 60;
280 hours = (int)t / (60 * 60);
281 snprintf(buffer, sizeof(buffer) - 1, "%02d:%02d:%02d", hours, mins,
282 secs);
283 return buffer;
286 static void print_progress(const char *name, time_t start, time_t now,
287 off_t start_pos, off_t pos, off_t total)
289 double avg = 0.0;
290 long eta = -1;
291 double prcnt = 0.0;
292 char hpos[22], htotal[22], havg[22];
293 char *status, *filename;
294 int len;
295 if (now - start) {
296 avg = 1.0 * (pos - start_pos) / (now - start);
298 eta = (total - pos) / avg;
299 if (total) {
300 prcnt = 100.0 * pos / total;
303 human_readable(pos, hpos, sizeof(hpos));
304 human_readable(total, htotal, sizeof(htotal));
305 human_readable(avg, havg, sizeof(havg));
307 len = asprintf(&status, "%s of %s (%.2f%%) at %s/s ETA: %s", hpos,
308 htotal, prcnt, havg, print_time(eta));
309 if (len == -1) {
310 return;
313 if (columns) {
314 int required = strlen(name),
315 available = columns - len - strlen("[] ");
316 if (required > available) {
317 if (asprintf(&filename, "...%s",
318 name + required - available + 3) == -1) {
319 return;
321 } else {
322 filename = SMB_STRNDUP(name, available);
324 } else {
325 filename = SMB_STRDUP(name);
328 fprintf(stderr, "\r[%s] %s", filename, status);
330 free(filename);
331 free(status);
334 /* Return false on error, true on success. */
336 static bool smb_download_file(const char *base, const char *name,
337 bool recursive, bool resume, bool toplevel,
338 char *outfile)
340 int remotehandle, localhandle;
341 time_t start_time = time_mono(NULL);
342 const char *newpath;
343 char path[SMB_MAXPATHLEN];
344 char checkbuf[2][RESUME_CHECK_SIZE];
345 char *readbuf = NULL;
346 off_t offset_download = 0, offset_check = 0, curpos = 0,
347 start_offset = 0;
348 struct stat localstat, remotestat;
350 snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base,
351 (*base && *name && name[0] != '/' &&
352 base[strlen(base)-1] != '/') ? "/" : "",
353 name);
355 remotehandle = smbc_open(path, O_RDONLY, 0755);
357 if (remotehandle < 0) {
358 switch (errno) {
359 case EISDIR:
360 if (!recursive) {
361 fprintf(stderr,
362 "%s is a directory. Specify -R "
363 "to download recursively\n",
364 path);
365 return false;
367 return smb_download_dir(base, name, resume);
369 case ENOENT:
370 fprintf(stderr,
371 "%s can't be found on the remote server\n",
372 path);
373 return false;
375 case ENOMEM:
376 fprintf(stderr, "Not enough memory\n");
377 return false;
379 case ENODEV:
380 fprintf(stderr,
381 "The share name used in %s does not exist\n",
382 path);
383 return false;
385 case EACCES:
386 fprintf(stderr, "You don't have enough permissions "
387 "to access %s\n",
388 path);
389 return false;
391 default:
392 perror("smbc_open");
393 return false;
397 if (smbc_fstat(remotehandle, &remotestat) < 0) {
398 fprintf(stderr, "Can't stat %s: %s\n", path, strerror(errno));
399 return false;
402 if (outfile) {
403 newpath = outfile;
404 } else if (!name[0]) {
405 newpath = strrchr(base, '/');
406 if (newpath) {
407 newpath++;
408 } else {
409 newpath = base;
411 } else {
412 newpath = name;
415 if (!toplevel && (newpath[0] == '/')) {
416 newpath++;
419 /* Open local file according to the mode */
420 if (opt.update) {
421 /* if it is up-to-date, skip */
422 if (stat(newpath, &localstat) == 0 &&
423 localstat.st_mtime >= remotestat.st_mtime) {
424 if (opt.verbose) {
425 printf("%s is up-to-date, skipping\n", newpath);
427 smbc_close(remotehandle);
428 return true;
430 /* else open it for writing and truncate if it exists */
431 localhandle = open(
432 newpath, O_CREAT | O_NONBLOCK | O_RDWR | O_TRUNC, 0775);
433 if (localhandle < 0) {
434 fprintf(stderr, "Can't open %s : %s\n", newpath,
435 strerror(errno));
436 smbc_close(remotehandle);
437 return false;
439 /* no offset */
440 } else if (!opt.send_stdout) {
441 localhandle = open(newpath, O_CREAT | O_NONBLOCK | O_RDWR |
442 (!resume ? O_EXCL : 0),
443 0755);
444 if (localhandle < 0) {
445 fprintf(stderr, "Can't open %s: %s\n", newpath,
446 strerror(errno));
447 smbc_close(remotehandle);
448 return false;
451 if (fstat(localhandle, &localstat) != 0) {
452 fprintf(stderr, "Can't fstat %s: %s\n", newpath,
453 strerror(errno));
454 smbc_close(remotehandle);
455 close(localhandle);
456 return false;
459 start_offset = localstat.st_size;
461 if (localstat.st_size &&
462 localstat.st_size == remotestat.st_size) {
463 if (opt.verbose) {
464 fprintf(stderr, "%s is already downloaded "
465 "completely.\n",
466 path);
467 } else if (!opt.quiet) {
468 fprintf(stderr, "%s\n", path);
470 smbc_close(remotehandle);
471 close(localhandle);
472 return true;
475 if (localstat.st_size > RESUME_CHECK_OFFSET &&
476 remotestat.st_size > RESUME_CHECK_OFFSET) {
477 offset_download =
478 localstat.st_size - RESUME_DOWNLOAD_OFFSET;
479 offset_check = localstat.st_size - RESUME_CHECK_OFFSET;
480 if (opt.verbose) {
481 printf("Trying to start resume of %s at %jd\n"
482 "At the moment %jd of %jd bytes have "
483 "been retrieved\n",
484 newpath, (intmax_t)offset_check,
485 (intmax_t)localstat.st_size,
486 (intmax_t)remotestat.st_size);
490 if (offset_check) {
491 off_t off1, off2;
492 /* First, check all bytes from offset_check to
493 * offset_download */
494 off1 = lseek(localhandle, offset_check, SEEK_SET);
495 if (off1 < 0) {
496 fprintf(stderr,
497 "Can't seek to %jd in local file %s\n",
498 (intmax_t)offset_check, newpath);
499 smbc_close(remotehandle);
500 close(localhandle);
501 return false;
504 off2 = smbc_lseek(remotehandle, offset_check, SEEK_SET);
505 if (off2 < 0) {
506 fprintf(stderr,
507 "Can't seek to %jd in remote file %s\n",
508 (intmax_t)offset_check, newpath);
509 smbc_close(remotehandle);
510 close(localhandle);
511 return false;
514 if (off1 != off2) {
515 fprintf(stderr, "Offset in local and remote "
516 "files are different "
517 "(local: %jd, remote: %jd)\n",
518 (intmax_t)off1, (intmax_t)off2);
519 smbc_close(remotehandle);
520 close(localhandle);
521 return false;
524 if (smbc_read(remotehandle, checkbuf[0],
525 RESUME_CHECK_SIZE) != RESUME_CHECK_SIZE) {
526 fprintf(stderr, "Can't read %d bytes from "
527 "remote file %s\n",
528 RESUME_CHECK_SIZE, path);
529 smbc_close(remotehandle);
530 close(localhandle);
531 return false;
534 if (read(localhandle, checkbuf[1], RESUME_CHECK_SIZE) !=
535 RESUME_CHECK_SIZE) {
536 fprintf(stderr, "Can't read %d bytes from "
537 "local file %s\n",
538 RESUME_CHECK_SIZE, name);
539 smbc_close(remotehandle);
540 close(localhandle);
541 return false;
544 if (memcmp(checkbuf[0], checkbuf[1],
545 RESUME_CHECK_SIZE) == 0) {
546 if (opt.verbose) {
547 printf("Current local and remote file "
548 "appear to be the same. "
549 "Starting download from "
550 "offset %jd\n",
551 (intmax_t)offset_download);
553 } else {
554 fprintf(stderr, "Local and remote file appear "
555 "to be different, not "
556 "doing resume for %s\n",
557 path);
558 smbc_close(remotehandle);
559 close(localhandle);
560 return false;
563 } else {
564 localhandle = STDOUT_FILENO;
565 start_offset = 0;
566 offset_download = 0;
567 offset_check = 0;
570 readbuf = (char *)SMB_MALLOC(opt.blocksize);
571 if (!readbuf) {
572 fprintf(stderr, "Failed to allocate %zu bytes for read "
573 "buffer (%s)", opt.blocksize, strerror(errno));
574 if (localhandle != STDOUT_FILENO) {
575 close(localhandle);
577 return false;
580 /* Now, download all bytes from offset_download to the end */
581 for (curpos = offset_download; curpos < remotestat.st_size;
582 curpos += opt.blocksize) {
583 ssize_t bytesread;
584 ssize_t byteswritten;
586 bytesread = smbc_read(remotehandle, readbuf, opt.blocksize);
587 if(bytesread < 0) {
588 fprintf(stderr,
589 "Can't read %zu bytes at offset %jd, file %s\n",
590 opt.blocksize, (intmax_t)curpos, path);
591 smbc_close(remotehandle);
592 if (localhandle != STDOUT_FILENO) {
593 close(localhandle);
595 free(readbuf);
596 return false;
599 total_bytes += bytesread;
601 byteswritten = write(localhandle, readbuf, bytesread);
602 if (byteswritten != bytesread) {
603 fprintf(stderr,
604 "Can't write %zd bytes to local file %s at "
605 "offset %jd\n", bytesread, path,
606 (intmax_t)curpos);
607 free(readbuf);
608 smbc_close(remotehandle);
609 if (localhandle != STDOUT_FILENO) {
610 close(localhandle);
612 return false;
615 if (opt.dots) {
616 fputc('.', stderr);
617 } else if (!opt.quiet) {
618 print_progress(newpath, start_time, time_mono(NULL),
619 start_offset, curpos,
620 remotestat.st_size);
624 free(readbuf);
626 if (opt.dots) {
627 fputc('\n', stderr);
628 printf("%s downloaded\n", path);
629 } else if (!opt.quiet) {
630 int i;
631 fprintf(stderr, "\r%s", path);
632 if (columns) {
633 for (i = strlen(path); i < columns; i++) {
634 fputc(' ', stderr);
637 fputc('\n', stderr);
640 smbc_close(remotehandle);
641 if (localhandle != STDOUT_FILENO) {
642 close(localhandle);
644 return true;
647 static void clean_exit(void)
649 char bs[100];
650 human_readable(total_bytes, bs, sizeof(bs));
651 if (!opt.quiet) {
652 fprintf(stderr, "Downloaded %s in %lu seconds\n", bs,
653 (unsigned long)(time_mono(NULL) - total_start_time));
655 exit(0);
658 static void signal_quit(int v)
660 clean_exit();
663 static int readrcfile(const char *name, const struct poptOption long_options[])
665 FILE *fd = fopen(name, "r");
666 int lineno = 0, i;
667 char var[101], val[101];
668 bool found;
669 int *intdata;
670 char **stringdata;
671 if (!fd) {
672 fprintf(stderr, "Can't open RC file %s\n", name);
673 return 1;
676 while (!feof(fd)) {
677 lineno++;
678 if (fscanf(fd, "%100s %100s\n", var, val) < 2) {
679 fprintf(stderr,
680 "Can't parse line %d of %s, ignoring.\n",
681 lineno, name);
682 continue;
685 found = false;
687 for (i = 0; long_options[i].argInfo; i++) {
688 if (!long_options[i].longName) {
689 continue;
691 if (strcmp(long_options[i].longName, var)) {
692 continue;
694 if (!long_options[i].arg) {
695 continue;
698 switch (long_options[i].argInfo) {
699 case POPT_ARG_NONE:
700 intdata = (int *)long_options[i].arg;
701 if (!strcmp(val, "on")) {
702 *intdata = 1;
703 } else if (!strcmp(val, "off")) {
704 *intdata = 0;
705 } else {
706 fprintf(stderr, "Illegal value %s for "
707 "%s at line %d in %s\n",
708 val, var, lineno, name);
710 break;
711 case POPT_ARG_INT:
712 intdata = (int *)long_options[i].arg;
713 *intdata = atoi(val);
714 break;
715 case POPT_ARG_STRING:
716 stringdata = (char **)long_options[i].arg;
717 *stringdata = SMB_STRDUP(val);
718 if (long_options[i].shortName == 'U') {
719 char *p;
720 opt.username_specified = true;
721 p = strchr(*stringdata, '%');
722 if (p != NULL) {
723 *p = '\0';
724 opt.password = p + 1;
725 opt.password_specified = true;
728 break;
729 default:
730 fprintf(stderr, "Invalid variable %s at "
731 "line %d in %s\n",
732 var, lineno, name);
733 break;
736 found = true;
738 if (!found) {
739 fprintf(stderr,
740 "Invalid variable %s at line %d in %s\n", var,
741 lineno, name);
745 fclose(fd);
746 return 0;
749 int main(int argc, char **argv)
751 int c = 0;
752 const char *file = NULL;
753 char *rcfile = NULL;
754 bool smb_encrypt = false;
755 int resume = 0, recursive = 0;
756 TALLOC_CTX *frame = talloc_stackframe();
757 bool ret = true;
758 char *p;
759 const char **argv_const = discard_const_p(const char *, argv);
760 struct poptOption long_options[] = {
761 POPT_AUTOHELP
763 {"workgroup", 'w', POPT_ARG_STRING, &opt.workgroup, 'w', "Workgroup to use (optional)" },
764 {"user", 'U', POPT_ARG_STRING, &opt.username, 'U', "Username to use" },
765 {"guest", 'a', POPT_ARG_NONE, NULL, 'a', "Work as user guest" },
767 {"nonprompt", 'n', POPT_ARG_NONE, NULL, 'n', "Don't ask anything (non-interactive)" },
768 {"debuglevel", 'd', POPT_ARG_INT, &opt.debuglevel, 'd', "Debuglevel to use" },
770 {"encrypt", 'e', POPT_ARG_NONE, NULL, 'e', "Encrypt SMB transport" },
771 {"resume", 'r', POPT_ARG_NONE, NULL, 'r', "Automatically resume aborted files" },
772 {"update", 'u', POPT_ARG_NONE, NULL, 'u', "Download only when remote file is newer than local file or local file is missing"},
773 {"recursive", 'R', POPT_ARG_NONE, NULL, 'R', "Recursively download files" },
774 {"blocksize", 'b', POPT_ARG_INT, &opt.blocksize, 'b', "Change number of bytes in a block"},
776 {"outputfile", 'o', POPT_ARG_STRING, &opt.outputfile, 'o', "Write downloaded data to specified file" },
777 {"stdout", 'O', POPT_ARG_NONE, NULL, 'O', "Write data to stdout" },
778 {"dots", 'D', POPT_ARG_NONE, NULL, 'D', "Show dots as progress indication" },
779 {"quiet", 'q', POPT_ARG_NONE, NULL, 'q', "Be quiet" },
780 {"verbose", 'v', POPT_ARG_NONE, NULL, 'v', "Be verbose" },
781 {"rcfile", 'f', POPT_ARG_STRING, NULL, 'f', "Use specified rc file"},
783 POPT_TABLEEND
785 poptContext pc;
787 smb_init_locale();
789 /* only read rcfile if it exists */
790 if (asprintf(&rcfile, "%s/.smbgetrc", getenv("HOME")) == -1) {
791 return 1;
793 if (access(rcfile, F_OK) == 0) {
794 readrcfile(rcfile, long_options);
796 free(rcfile);
798 #ifdef SIGWINCH
799 signal(SIGWINCH, change_columns);
800 #endif
801 signal(SIGINT, signal_quit);
802 signal(SIGTERM, signal_quit);
804 pc = poptGetContext(argv[0], argc, argv_const, long_options, 0);
806 while ((c = poptGetNextOpt(pc)) > 0) {
807 switch (c) {
808 case 'f':
809 readrcfile(poptGetOptArg(pc), long_options);
810 break;
811 case 'a':
812 opt.username_specified = true;
813 opt.username = talloc_strdup(frame, "");
814 opt.password_specified = true;
815 opt.password = talloc_strdup(frame, "");
816 break;
817 case 'e':
818 smb_encrypt = true;
819 break;
820 case 'U':
821 opt.username_specified = true;
822 opt.username = talloc_strdup(frame, opt.username);
823 p = strchr(opt.username,'%');
824 if (p != NULL) {
825 *p = '\0';
826 opt.password = p + 1;
827 opt.password_specified = true;
829 break;
830 case 'n':
831 opt.nonprompt = true;
832 break;
833 case 'r':
834 resume = true;
835 break;
836 case 'u':
837 opt.update = true;
838 break;
839 case 'R':
840 recursive = true;
841 break;
842 case 'O':
843 opt.send_stdout = true;
844 break;
845 case 'D':
846 opt.dots = true;
847 break;
848 case 'q':
849 opt.quiet = true;
850 break;
851 case 'v':
852 opt.verbose = true;
853 break;
857 if (c < -1) {
858 fprintf(stderr, "%s: %s\n",
859 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
860 poptStrerror(c));
861 return 1;
864 if ((opt.send_stdout || resume || opt.outputfile) && opt.update) {
865 fprintf(stderr, "The -o, -R or -O and -U options can not be "
866 "used together.\n");
867 return 1;
869 if ((opt.send_stdout || opt.outputfile) && recursive) {
870 fprintf(stderr, "The -o or -O and -R options can not be "
871 "used together.\n");
872 return 1;
875 if (opt.outputfile && opt.send_stdout) {
876 fprintf(stderr, "The -o and -O options can not be "
877 "used together.\n");
878 return 1;
881 popt_burn_cmdline_password(argc, argv);
883 cmdline_messaging_context(get_dyn_CONFIGFILE());
885 if (smbc_init(get_auth_data, opt.debuglevel) < 0) {
886 fprintf(stderr, "Unable to initialize libsmbclient\n");
887 return 1;
890 if (smb_encrypt) {
891 SMBCCTX *smb_ctx = smbc_set_context(NULL);
892 smbc_option_set(smb_ctx,
893 discard_const_p(char, "smb_encrypt_level"),
894 "require");
897 columns = get_num_cols();
899 total_start_time = time_mono(NULL);
901 while ((file = poptGetArg(pc))) {
902 if (!recursive) {
903 ret = smb_download_file(file, "", recursive, resume,
904 true, opt.outputfile);
905 } else {
906 ret = smb_download_dir(file, "", resume);
910 TALLOC_FREE(frame);
911 if (ret) {
912 clean_exit();
914 return ret?0:1;