2 smbget: a wget-like utility with support for recursive downloading of
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/>. */
20 #include "system/filesys.h"
21 #include "popt_common.h"
22 #include "libsmbclient.h"
24 static int columns = 0;
26 static int debuglevel;
27 static char *outputfile;
30 static time_t total_start_time = 0;
31 static off_t total_bytes = 0;
33 #define SMB_MAXPATHLEN MAXPATHLEN
36 * Number of bytes to read when checking whether local and remote file
37 * are really the same file
39 #define RESUME_CHECK_SIZE 512
40 #define RESUME_DOWNLOAD_OFFSET 1024
41 #define RESUME_CHECK_OFFSET (RESUME_DOWNLOAD_OFFSET+RESUME_CHECK_SIZE)
42 /* Number of bytes to read at once */
43 #define SMB_DEFAULT_BLOCKSIZE 64000
45 static const char *username = NULL, *password = NULL, *workgroup = NULL;
46 static bool nonprompt = false, quiet = false, dots = false,
47 keep_permissions = false, verbose = false, send_stdout = false,
49 static unsigned int blocksize = SMB_DEFAULT_BLOCKSIZE;
51 static bool smb_download_file(const char *base, const char *name,
52 bool recursive, bool resume, bool toplevel,
55 static int get_num_cols(void)
59 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0) {
64 #warning No support for TIOCGWINSZ
65 char *cols = getenv("COLUMNS");
73 static void change_columns(int sig)
75 columns = get_num_cols();
78 static void human_readable(off_t s, char *buffer, int l)
80 if (s > 1024 * 1024 * 1024) {
81 snprintf(buffer, l, "%.2fGB", 1.0 * s / (1024 * 1024 * 1024));
82 } else if (s > 1024 * 1024) {
83 snprintf(buffer, l, "%.2fMB", 1.0 * s / (1024 * 1024));
84 } else if (s > 1024) {
85 snprintf(buffer, l, "%.2fkB", 1.0 * s / 1024);
87 snprintf(buffer, l, "%jdb", (intmax_t)s);
91 static void get_auth_data(const char *srv, const char *shr, char *wg, int wglen,
92 char *un, int unlen, char *pw, int pwlen)
94 static bool hasasked = false;
101 strncpy(wg, savedwg, wglen - 1);
102 strncpy(un, savedun, unlen - 1);
103 strncpy(pw, savedpw, pwlen - 1);
108 if (!nonprompt && !username) {
109 printf("Username for %s at %s [guest] ", shr, srv);
110 if (fgets(tmp, sizeof(tmp), stdin) == NULL) {
113 if ((strlen(tmp) > 0) && (tmp[strlen(tmp) - 1] == '\n')) {
114 tmp[strlen(tmp) - 1] = '\0';
116 strncpy(un, tmp, unlen - 1);
117 } else if (username) {
118 strncpy(un, username, unlen - 1);
121 if (!nonprompt && !password) {
123 if (asprintf(&prompt, "Password for %s at %s: ", shr, srv) ==
127 (void)samba_getpass(prompt, pw, pwlen, false, false);
129 } else if (password) {
130 strncpy(pw, password, pwlen - 1);
134 strncpy(wg, workgroup, wglen - 1);
137 /* save the values found for later */
138 savedwg = SMB_STRDUP(wg);
139 savedun = SMB_STRDUP(un);
140 savedpw = SMB_STRDUP(pw);
143 char *wgtmp, *usertmp;
144 wgtmp = SMB_STRNDUP(wg, wglen);
145 usertmp = SMB_STRNDUP(un, unlen);
146 printf("Using workgroup %s, %s%s\n", wgtmp,
147 *usertmp ? "user " : "guest user", usertmp);
153 /* Return 1 on error, 0 on success. */
155 static int smb_download_dir(const char *base, const char *name, int resume)
157 char path[SMB_MAXPATHLEN];
159 struct smbc_dirent *dirent;
160 const char *relname = name;
162 struct stat remotestat;
165 snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base,
166 (base[0] && name[0] && name[0] != '/' &&
167 base[strlen(base)-1] != '/') ? "/" : "",
170 /* List files in directory and call smb_download_file on them */
171 dirhandle = smbc_opendir(path);
173 if (errno == ENOTDIR) {
174 return smb_download_file(base, name, true, resume,
177 fprintf(stderr, "Can't open directory %s: %s\n", path,
182 while (*relname == '/') {
185 mkdir(relname, 0755);
187 tmpname = SMB_STRDUP(name);
189 while ((dirent = smbc_readdir(dirhandle))) {
191 if (!strcmp(dirent->name, ".") || !strcmp(dirent->name, "..")) {
194 if (asprintf(&newname, "%s/%s", tmpname, dirent->name) == -1) {
198 switch (dirent->smbc_type) {
200 ret = smb_download_dir(base, newname, resume);
204 ret = smb_download_dir("smb://", dirent->name, resume);
208 ret = smb_download_dir("smb://", dirent->name, resume);
212 ret = smb_download_file(base, newname, true, resume,
216 case SMBC_FILE_SHARE:
217 ret = smb_download_dir(base, newname, resume);
220 case SMBC_PRINTER_SHARE:
222 printf("Ignoring printer share %s\n",
227 case SMBC_COMMS_SHARE:
229 printf("Ignoring comms share %s\n",
236 printf("Ignoring ipc$ share %s\n",
242 fprintf(stderr, "Ignoring file '%s' of type '%d'\n",
243 newname, dirent->smbc_type);
250 if (keep_permissions) {
251 if (smbc_fstat(dirhandle, &remotestat) < 0) {
253 "Unable to get stats on %s on remote server\n",
255 smbc_closedir(dirhandle);
259 if (chmod(relname, remotestat.st_mode) < 0) {
261 "Unable to change mode of local dir %s to %o\n",
262 relname, (unsigned int)remotestat.st_mode);
263 smbc_closedir(dirhandle);
268 smbc_closedir(dirhandle);
272 static char *print_time(long t)
274 static char buffer[100];
275 int secs, mins, hours;
277 strncpy(buffer, "Unknown", sizeof(buffer));
282 mins = (int)t / 60 % 60;
283 hours = (int)t / (60 * 60);
284 snprintf(buffer, sizeof(buffer) - 1, "%02d:%02d:%02d", hours, mins,
289 static void print_progress(const char *name, time_t start, time_t now,
290 off_t start_pos, off_t pos, off_t total)
295 char hpos[20], htotal[20], havg[20];
296 char *status, *filename;
299 avg = 1.0 * (pos - start_pos) / (now - start);
301 eta = (total - pos) / avg;
303 prcnt = 100.0 * pos / total;
306 human_readable(pos, hpos, sizeof(hpos));
307 human_readable(total, htotal, sizeof(htotal));
308 human_readable(avg, havg, sizeof(havg));
310 len = asprintf(&status, "%s of %s (%.2f%%) at %s/s ETA: %s", hpos,
311 htotal, prcnt, havg, print_time(eta));
317 int required = strlen(name),
318 available = columns - len - strlen("[] ");
319 if (required > available) {
320 if (asprintf(&filename, "...%s",
321 name + required - available + 3) == -1) {
325 filename = SMB_STRNDUP(name, available);
328 filename = SMB_STRDUP(name);
331 fprintf(stderr, "\r[%s] %s", filename, status);
337 /* Return false on error, true on success. */
339 static bool smb_download_file(const char *base, const char *name,
340 bool recursive, bool resume, bool toplevel,
343 int remotehandle, localhandle;
344 time_t start_time = time_mono(NULL);
346 char path[SMB_MAXPATHLEN];
347 char checkbuf[2][RESUME_CHECK_SIZE];
348 char *readbuf = NULL;
349 off_t offset_download = 0, offset_check = 0, curpos = 0,
351 struct stat localstat, remotestat;
353 snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base,
354 (*base && *name && name[0] != '/' &&
355 base[strlen(base)-1] != '/') ? "/" : "",
358 remotehandle = smbc_open(path, O_RDONLY, 0755);
360 if (remotehandle < 0) {
365 "%s is a directory. Specify -R "
366 "to download recursively\n",
370 return smb_download_dir(base, name, resume);
374 "%s can't be found on the remote server\n",
379 fprintf(stderr, "Not enough memory\n");
384 "The share name used in %s does not exist\n",
389 fprintf(stderr, "You don't have enough permissions "
400 if (smbc_fstat(remotehandle, &remotestat) < 0) {
401 fprintf(stderr, "Can't stat %s: %s\n", path, strerror(errno));
407 } else if (!name[0]) {
408 newpath = strrchr(base, '/');
418 if (!toplevel && (newpath[0] == '/')) {
422 /* Open local file according to the mode */
424 /* if it is up-to-date, skip */
425 if (stat(newpath, &localstat) == 0 &&
426 localstat.st_mtime >= remotestat.st_mtime) {
428 printf("%s is up-to-date, skipping\n", newpath);
430 smbc_close(remotehandle);
433 /* else open it for writing and truncate if it exists */
435 newpath, O_CREAT | O_NONBLOCK | O_RDWR | O_TRUNC, 0775);
436 if (localhandle < 0) {
437 fprintf(stderr, "Can't open %s : %s\n", newpath,
439 smbc_close(remotehandle);
443 } else if (!send_stdout) {
444 localhandle = open(newpath, O_CREAT | O_NONBLOCK | O_RDWR |
445 (!resume ? O_EXCL : 0),
447 if (localhandle < 0) {
448 fprintf(stderr, "Can't open %s: %s\n", newpath,
450 smbc_close(remotehandle);
454 if (fstat(localhandle, &localstat) != 0) {
455 fprintf(stderr, "Can't fstat %s: %s\n", newpath,
457 smbc_close(remotehandle);
462 start_offset = localstat.st_size;
464 if (localstat.st_size &&
465 localstat.st_size == remotestat.st_size) {
467 fprintf(stderr, "%s is already downloaded "
471 fprintf(stderr, "%s\n", path);
473 smbc_close(remotehandle);
478 if (localstat.st_size > RESUME_CHECK_OFFSET &&
479 remotestat.st_size > RESUME_CHECK_OFFSET) {
481 localstat.st_size - RESUME_DOWNLOAD_OFFSET;
482 offset_check = localstat.st_size - RESUME_CHECK_OFFSET;
484 printf("Trying to start resume of %s at %jd\n"
485 "At the moment %jd of %jd bytes have "
487 newpath, (intmax_t)offset_check,
488 (intmax_t)localstat.st_size,
489 (intmax_t)remotestat.st_size);
495 /* First, check all bytes from offset_check to
497 off1 = lseek(localhandle, offset_check, SEEK_SET);
500 "Can't seek to %jd in local file %s\n",
501 (intmax_t)offset_check, newpath);
502 smbc_close(remotehandle);
507 off2 = smbc_lseek(remotehandle, offset_check, SEEK_SET);
510 "Can't seek to %jd in remote file %s\n",
511 (intmax_t)offset_check, newpath);
512 smbc_close(remotehandle);
518 fprintf(stderr, "Offset in local and remote "
519 "files are different "
520 "(local: %jd, remote: %jd)\n",
521 (intmax_t)off1, (intmax_t)off2);
522 smbc_close(remotehandle);
527 if (smbc_read(remotehandle, checkbuf[0],
528 RESUME_CHECK_SIZE) != RESUME_CHECK_SIZE) {
529 fprintf(stderr, "Can't read %d bytes from "
531 RESUME_CHECK_SIZE, path);
532 smbc_close(remotehandle);
537 if (read(localhandle, checkbuf[1], RESUME_CHECK_SIZE) !=
539 fprintf(stderr, "Can't read %d bytes from "
541 RESUME_CHECK_SIZE, name);
542 smbc_close(remotehandle);
547 if (memcmp(checkbuf[0], checkbuf[1],
548 RESUME_CHECK_SIZE) == 0) {
550 printf("Current local and remote file "
551 "appear to be the same. "
552 "Starting download from "
554 (intmax_t)offset_download);
557 fprintf(stderr, "Local and remote file appear "
558 "to be different, not "
559 "doing resume for %s\n",
561 smbc_close(remotehandle);
567 localhandle = STDOUT_FILENO;
573 readbuf = (char *)SMB_MALLOC(blocksize);
575 if (localhandle != STDOUT_FILENO) {
581 /* Now, download all bytes from offset_download to the end */
582 for (curpos = offset_download; curpos < remotestat.st_size;
583 curpos += blocksize) {
584 ssize_t bytesread = smbc_read(remotehandle, readbuf, blocksize);
587 "Can't read %u bytes at offset %jd, file %s\n",
588 blocksize, (intmax_t)curpos, path);
589 smbc_close(remotehandle);
590 if (localhandle != STDOUT_FILENO) {
597 total_bytes += bytesread;
599 if(write(localhandle, readbuf, bytesread) < 0) {
601 "Can't write %zd bytes to local file %s at "
602 "offset %jd\n", bytesread, path,
605 smbc_close(remotehandle);
606 if (localhandle != STDOUT_FILENO) {
615 print_progress(newpath, start_time, time_mono(NULL),
616 start_offset, curpos,
625 printf("%s downloaded\n", path);
628 fprintf(stderr, "\r%s", path);
630 for (i = strlen(path); i < columns; i++) {
637 if (keep_permissions && !send_stdout) {
638 if (fchmod(localhandle, remotestat.st_mode) < 0) {
639 fprintf(stderr, "Unable to change mode of local "
641 path, (unsigned int)remotestat.st_mode);
642 smbc_close(remotehandle);
648 smbc_close(remotehandle);
649 if (localhandle != STDOUT_FILENO) {
655 static void clean_exit(void)
658 human_readable(total_bytes, bs, sizeof(bs));
660 fprintf(stderr, "Downloaded %s in %lu seconds\n", bs,
661 (unsigned long)(time_mono(NULL) - total_start_time));
666 static void signal_quit(int v)
671 static int readrcfile(const char *name, const struct poptOption long_options[])
673 FILE *fd = fopen(name, "r");
675 char var[101], val[101];
680 fprintf(stderr, "Can't open RC file %s\n", name);
686 if (fscanf(fd, "%100s %100s\n", var, val) < 2) {
688 "Can't parse line %d of %s, ignoring.\n",
695 for (i = 0; long_options[i].shortName; i++) {
696 if (!long_options[i].longName) {
699 if (strcmp(long_options[i].longName, var)) {
702 if (!long_options[i].arg) {
706 switch (long_options[i].argInfo) {
708 intdata = (int *)long_options[i].arg;
709 if (!strcmp(val, "on")) {
711 } else if (!strcmp(val, "off")) {
714 fprintf(stderr, "Illegal value %s for "
715 "%s at line %d in %s\n",
716 val, var, lineno, name);
720 intdata = (int *)long_options[i].arg;
721 *intdata = atoi(val);
723 case POPT_ARG_STRING:
724 stringdata = (char **)long_options[i].arg;
725 *stringdata = SMB_STRDUP(val);
728 fprintf(stderr, "Invalid variable %s at "
738 "Invalid variable %s at line %d in %s\n", var,
747 int main(int argc, const char **argv)
750 const char *file = NULL;
752 bool smb_encrypt = false;
753 int resume = 0, recursive = 0;
754 TALLOC_CTX *frame = talloc_stackframe();
756 struct poptOption long_options[] = {
757 {"guest", 'a', POPT_ARG_NONE, NULL, 'a', "Work as user guest" },
758 {"encrypt", 'e', POPT_ARG_NONE, NULL, 'e', "Encrypt SMB transport" },
759 {"resume", 'r', POPT_ARG_NONE, &resume, 0, "Automatically resume aborted files" },
760 {"update", 'U', POPT_ARG_NONE, &update, 0, "Download only when remote file is newer than local file or local file is missing"},
761 {"recursive", 'R', POPT_ARG_NONE, &recursive, 0, "Recursively download files" },
762 {"username", 'u', POPT_ARG_STRING, &username, 'u', "Username to use" },
763 {"password", 'p', POPT_ARG_STRING, &password, 'p', "Password to use" },
764 {"workgroup", 'w', POPT_ARG_STRING, &workgroup, 'w', "Workgroup to use (optional)" },
765 {"nonprompt", 'n', POPT_ARG_NONE, &nonprompt, 'n', "Don't ask anything (non-interactive)" },
766 {"debuglevel", 'd', POPT_ARG_INT, &debuglevel, 'd', "Debuglevel to use" },
767 {"outputfile", 'o', POPT_ARG_STRING, &outputfile, 'o', "Write downloaded data to specified file" },
768 {"stdout", 'O', POPT_ARG_NONE, &send_stdout, 'O', "Write data to stdout" },
769 {"dots", 'D', POPT_ARG_NONE, &dots, 'D', "Show dots as progress indication" },
770 {"quiet", 'q', POPT_ARG_NONE, &quiet, 'q', "Be quiet" },
771 {"verbose", 'v', POPT_ARG_NONE, &verbose, 'v', "Be verbose" },
772 {"keep-permissions", 'P', POPT_ARG_NONE, &keep_permissions, 'P', "Keep permissions" },
773 {"blocksize", 'b', POPT_ARG_INT, &blocksize, 'b', "Change number of bytes in a block"},
774 {"rcfile", 'f', POPT_ARG_STRING, NULL, 'f', "Use specified rc file"},
782 /* only read rcfile if it exists */
783 if (asprintf(&rcfile, "%s/.smbgetrc", getenv("HOME")) == -1) {
786 if (access(rcfile, F_OK) == 0) {
787 readrcfile(rcfile, long_options);
792 signal(SIGWINCH, change_columns);
794 signal(SIGINT, signal_quit);
795 signal(SIGTERM, signal_quit);
797 pc = poptGetContext(argv[0], argc, argv, long_options, 0);
799 while ((c = poptGetNextOpt(pc)) >= 0) {
802 readrcfile(poptGetOptArg(pc), long_options);
814 if ((send_stdout || resume || outputfile) && update) {
815 fprintf(stderr, "The -o, -R or -O and -U options can not be "
819 if ((send_stdout || outputfile) && recursive) {
820 fprintf(stderr, "The -o or -O and -R options can not be "
825 if (outputfile && send_stdout) {
826 fprintf(stderr, "The -o and -O options can not be "
831 if (smbc_init(get_auth_data, debuglevel) < 0) {
832 fprintf(stderr, "Unable to initialize libsmbclient\n");
837 SMBCCTX *smb_ctx = smbc_set_context(NULL);
838 smbc_option_set(smb_ctx,
839 discard_const_p(char, "smb_encrypt_level"),
843 columns = get_num_cols();
845 total_start_time = time_mono(NULL);
847 while ((file = poptGetArg(pc))) {
849 ret = smb_download_file(file, "", recursive, resume,
852 ret = smb_download_dir(file, "", resume);