2 smbget: a wget-like utility with support for recursive downloading and
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 "libsmbclient.h"
22 #if _FILE_OFFSET_BITS==64
23 #define OFF_T_FORMAT "%lld"
24 #define OFF_T_FORMAT_CAST long long
26 #define OFF_T_FORMAT "%ld"
27 #define OFF_T_FORMAT_CAST long
32 static int _resume
, _recursive
, debuglevel
;
33 static char *outputfile
;
36 time_t total_start_time
= 0;
37 off_t total_bytes
= 0;
39 #define SMB_MAXPATHLEN MAXPATHLEN
41 /* Number of bytes to read when checking whether local and remote file are really the same file */
42 #define RESUME_CHECK_SIZE 512
43 #define RESUME_DOWNLOAD_OFFSET 1024
44 #define RESUME_CHECK_OFFSET RESUME_DOWNLOAD_OFFSET+RESUME_CHECK_SIZE
45 /* Number of bytes to read at once */
46 #define SMB_DEFAULT_BLOCKSIZE 64000
48 const char *username
= NULL
, *password
= NULL
, *workgroup
= NULL
;
49 int nonprompt
= 0, quiet
= 0, dots
= 0, keep_permissions
= 0, verbose
= 0, send_stdout
= 0;
50 int blocksize
= SMB_DEFAULT_BLOCKSIZE
;
52 static int smb_download_file(const char *base
, const char *name
, int recursive
, int resume
, char *outfile
);
54 static int get_num_cols(void)
58 if(ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
) < 0) {
63 #warning No support for TIOCGWINSZ
64 char *cols
= getenv("COLUMNS");
70 static void change_columns(int sig
)
72 columns
= get_num_cols();
75 static void human_readable(off_t s
, char *buffer
, int l
)
77 if(s
> 1024 * 1024 * 1024) snprintf(buffer
, l
, "%.2fGb", 1.0 * s
/ (1024 * 1024 * 1024));
78 else if(s
> 1024 * 1024) snprintf(buffer
, l
, "%.2fMb", 1.0 * s
/ (1024 * 1024));
79 else if(s
> 1024) snprintf(buffer
, l
, "%.2fkb", 1.0 * s
/ 1024);
80 else snprintf(buffer
, l
, OFF_T_FORMAT
"b", (OFF_T_FORMAT_CAST
)s
);
83 static void get_auth_data(const char *srv
, const char *shr
, char *wg
, int wglen
, char *un
, int unlen
, char *pw
, int pwlen
)
85 static char hasasked
= 0;
86 char *wgtmp
, *usertmp
;
92 if(!nonprompt
&& !username
) {
93 printf("Username for %s at %s [guest] ", shr
, srv
);
94 fgets(tmp
, sizeof(tmp
), stdin
);
95 if(tmp
[strlen(tmp
)-1] == '\n')tmp
[strlen(tmp
)-1] = '\0';
96 strncpy(un
, tmp
, unlen
-1);
97 } else if(username
) strncpy(un
, username
, unlen
-1);
99 if(!nonprompt
&& !password
) {
101 asprintf(&prompt
, "Password for %s at %s: ", shr
, srv
);
102 pass
= getpass(prompt
);
104 strncpy(pw
, pass
, pwlen
-1);
105 } else if(password
) strncpy(pw
, password
, pwlen
-1);
107 if(workgroup
)strncpy(wg
, workgroup
, wglen
-1);
109 wgtmp
= SMB_STRNDUP(wg
, wglen
);
110 usertmp
= SMB_STRNDUP(un
, unlen
);
111 if(!quiet
)printf("Using workgroup %s, %s%s\n", wgtmp
, *usertmp
?"user ":"guest user", usertmp
);
112 free(wgtmp
); free(usertmp
);
115 static int smb_download_dir(const char *base
, const char *name
, int resume
)
117 char path
[SMB_MAXPATHLEN
];
119 struct smbc_dirent
*dirent
;
120 const char *relname
= name
;
122 struct stat remotestat
;
123 snprintf(path
, SMB_MAXPATHLEN
-1, "%s%s%s", base
, (base
[0] && name
[0] && name
[0] != '/' && base
[strlen(base
)-1] != '/')?"/":"", name
);
125 /* List files in directory and call smb_download_file on them */
126 dirhandle
= smbc_opendir(path
);
128 if(errno
== ENOTDIR
) return smb_download_file(base
, name
, 1, resume
, NULL
);
129 fprintf(stderr
, "Can't open directory %s: %s\n", path
, strerror(errno
));
133 while(*relname
== '/')relname
++;
134 mkdir(relname
, 0755);
136 tmpname
= SMB_STRDUP(name
);
138 while((dirent
= smbc_readdir(dirhandle
))) {
140 if(!strcmp(dirent
->name
, ".") || !strcmp(dirent
->name
, ".."))continue;
141 asprintf(&newname
, "%s/%s", tmpname
, dirent
->name
);
142 switch(dirent
->smbc_type
) {
144 smb_download_dir(base
, newname
, resume
);
148 smb_download_dir("smb://", dirent
->name
, resume
);
152 smb_download_dir("smb://", dirent
->name
, resume
);
156 smb_download_file(base
, newname
, 1, resume
, NULL
);
159 case SMBC_FILE_SHARE
:
160 smb_download_dir(base
, newname
, resume
);
163 case SMBC_PRINTER_SHARE
:
164 if(!quiet
)printf("Ignoring printer share %s\n", dirent
->name
);
167 case SMBC_COMMS_SHARE
:
168 if(!quiet
)printf("Ignoring comms share %s\n", dirent
->name
);
172 if(!quiet
)printf("Ignoring ipc$ share %s\n", dirent
->name
);
176 fprintf(stderr
, "Ignoring file '%s' of type '%d'\n", newname
, dirent
->smbc_type
);
183 if(keep_permissions
) {
184 if(smbc_fstat(dirhandle
, &remotestat
) < 0) {
185 fprintf(stderr
, "Unable to get stats on %s on remote server\n", path
);
186 smbc_closedir(dirhandle
);
190 if(chmod(relname
, remotestat
.st_mode
) < 0) {
191 fprintf(stderr
, "Unable to change mode of local dir %s to %o\n", relname
, remotestat
.st_mode
);
192 smbc_closedir(dirhandle
);
197 smbc_closedir(dirhandle
);
201 static char *print_time(long t
)
203 static char buffer
[100];
204 int secs
, mins
, hours
;
206 strncpy(buffer
, "Unknown", sizeof(buffer
));
211 mins
= (int)t
/ 60 % 60;
212 hours
= (int)t
/ (60 * 60);
213 snprintf(buffer
, sizeof(buffer
)-1, "%02d:%02d:%02d", hours
, mins
, secs
);
217 static void print_progress(const char *name
, time_t start
, time_t now
, off_t start_pos
, off_t pos
, off_t total
)
222 char hpos
[20], htotal
[20], havg
[20];
223 char *status
, *filename
;
225 if(now
- start
)avg
= 1.0 * (pos
- start_pos
) / (now
- start
);
226 eta
= (total
- pos
) / avg
;
227 if(total
)prcnt
= 100.0 * pos
/ total
;
229 human_readable(pos
, hpos
, sizeof(hpos
));
230 human_readable(total
, htotal
, sizeof(htotal
));
231 human_readable(avg
, havg
, sizeof(havg
));
233 len
= asprintf(&status
, "%s of %s (%.2f%%) at %s/s ETA: %s", hpos
, htotal
, prcnt
, havg
, print_time(eta
));
236 int required
= strlen(name
), available
= columns
- len
- strlen("[] ");
237 if(required
> available
) asprintf(&filename
, "...%s", name
+ required
- available
+ 3);
238 else filename
= SMB_STRNDUP(name
, available
);
239 } else filename
= SMB_STRDUP(name
);
241 fprintf(stderr
, "\r[%s] %s", filename
, status
);
243 free(filename
); free(status
);
246 static int smb_download_file(const char *base
, const char *name
, int recursive
, int resume
, char *outfile
) {
247 int remotehandle
, localhandle
;
248 time_t start_time
= time(NULL
);
250 char path
[SMB_MAXPATHLEN
];
251 char checkbuf
[2][RESUME_CHECK_SIZE
];
252 char *readbuf
= NULL
;
253 off_t offset_download
= 0, offset_check
= 0, curpos
= 0, start_offset
= 0;
254 struct stat localstat
, remotestat
;
256 snprintf(path
, SMB_MAXPATHLEN
-1, "%s%s%s", base
, (*base
&& *name
&& name
[0] != '/' && base
[strlen(base
)-1] != '/')?"/":"", name
);
258 remotehandle
= smbc_open(path
, O_RDONLY
, 0755);
260 if(remotehandle
< 0) {
264 fprintf(stderr
, "%s is a directory. Specify -R to download recursively\n", path
);
267 smb_download_dir(base
, name
, resume
);
271 fprintf(stderr
, "%s can't be found on the remote server\n", path
);
275 fprintf(stderr
, "Not enough memory\n");
280 fprintf(stderr
, "The share name used in %s does not exist\n", path
);
284 fprintf(stderr
, "You don't have enough permissions to access %s\n", path
);
293 if(smbc_fstat(remotehandle
, &remotestat
) < 0) {
294 fprintf(stderr
, "Can't stat %s: %s\n", path
, strerror(errno
));
298 if(outfile
) newpath
= outfile
;
300 newpath
= strrchr(base
, '/');
301 if(newpath
)newpath
++; else newpath
= base
;
302 } else newpath
= name
;
304 if(newpath
[0] == '/')newpath
++;
306 /* Open local file and, if necessary, resume */
308 localhandle
= open(newpath
, O_CREAT
| O_NONBLOCK
| O_RDWR
| (!resume
?O_EXCL
:0), 0755);
309 if(localhandle
< 0) {
310 fprintf(stderr
, "Can't open %s: %s\n", newpath
, strerror(errno
));
311 smbc_close(remotehandle
);
315 fstat(localhandle
, &localstat
);
317 start_offset
= localstat
.st_size
;
319 if(localstat
.st_size
&& localstat
.st_size
== remotestat
.st_size
) {
320 if(verbose
)fprintf(stderr
, "%s is already downloaded completely.\n", path
);
321 else if(!quiet
)fprintf(stderr
, "%s\n", path
);
322 smbc_close(remotehandle
);
327 if(localstat
.st_size
> RESUME_CHECK_OFFSET
&& remotestat
.st_size
> RESUME_CHECK_OFFSET
) {
328 offset_download
= localstat
.st_size
- RESUME_DOWNLOAD_OFFSET
;
329 offset_check
= localstat
.st_size
- RESUME_CHECK_OFFSET
;
330 if(verbose
)printf("Trying to start resume of %s at "OFF_T_FORMAT
"\n"
331 "At the moment "OFF_T_FORMAT
" of "OFF_T_FORMAT
" bytes have been retrieved\n",
332 newpath
, (OFF_T_FORMAT_CAST
)offset_check
,
333 (OFF_T_FORMAT_CAST
)localstat
.st_size
,
334 (OFF_T_FORMAT_CAST
)remotestat
.st_size
);
339 /* First, check all bytes from offset_check to offset_download */
340 off1
= lseek(localhandle
, offset_check
, SEEK_SET
);
342 fprintf(stderr
, "Can't seek to "OFF_T_FORMAT
" in local file %s\n",
343 (OFF_T_FORMAT_CAST
)offset_check
, newpath
);
344 smbc_close(remotehandle
); close(localhandle
);
348 off2
= smbc_lseek(remotehandle
, offset_check
, SEEK_SET
);
350 fprintf(stderr
, "Can't seek to "OFF_T_FORMAT
" in remote file %s\n",
351 (OFF_T_FORMAT_CAST
)offset_check
, newpath
);
352 smbc_close(remotehandle
); close(localhandle
);
357 fprintf(stderr
, "Offset in local and remote files is different (local: "OFF_T_FORMAT
", remote: "OFF_T_FORMAT
")\n",
358 (OFF_T_FORMAT_CAST
)off1
,
359 (OFF_T_FORMAT_CAST
)off2
);
363 if(smbc_read(remotehandle
, checkbuf
[0], RESUME_CHECK_SIZE
) != RESUME_CHECK_SIZE
) {
364 fprintf(stderr
, "Can't read %d bytes from remote file %s\n", RESUME_CHECK_SIZE
, path
);
365 smbc_close(remotehandle
); close(localhandle
);
369 if(read(localhandle
, checkbuf
[1], RESUME_CHECK_SIZE
) != RESUME_CHECK_SIZE
) {
370 fprintf(stderr
, "Can't read %d bytes from local file %s\n", RESUME_CHECK_SIZE
, name
);
371 smbc_close(remotehandle
); close(localhandle
);
375 if(memcmp(checkbuf
[0], checkbuf
[1], RESUME_CHECK_SIZE
) == 0) {
376 if(verbose
)printf("Current local and remote file appear to be the same. Starting download from offset "OFF_T_FORMAT
"\n", (OFF_T_FORMAT_CAST
)offset_download
);
378 fprintf(stderr
, "Local and remote file appear to be different, not doing resume for %s\n", path
);
379 smbc_close(remotehandle
); close(localhandle
);
384 localhandle
= STDOUT_FILENO
;
390 readbuf
= (char *)SMB_MALLOC(blocksize
);
392 /* Now, download all bytes from offset_download to the end */
393 for(curpos
= offset_download
; curpos
< remotestat
.st_size
; curpos
+=blocksize
) {
394 ssize_t bytesread
= smbc_read(remotehandle
, readbuf
, blocksize
);
396 fprintf(stderr
, "Can't read %u bytes at offset "OFF_T_FORMAT
", file %s\n", (unsigned int)blocksize
, (OFF_T_FORMAT_CAST
)curpos
, path
);
397 smbc_close(remotehandle
);
398 if (localhandle
!= STDOUT_FILENO
) close(localhandle
);
403 total_bytes
+= bytesread
;
405 if(write(localhandle
, readbuf
, bytesread
) < 0) {
406 fprintf(stderr
, "Can't write %u bytes to local file %s at offset "OFF_T_FORMAT
"\n", (unsigned int)bytesread
, path
, (OFF_T_FORMAT_CAST
)curpos
);
408 smbc_close(remotehandle
);
409 if (localhandle
!= STDOUT_FILENO
) close(localhandle
);
413 if(dots
)fputc('.', stderr
);
415 print_progress(newpath
, start_time
, time(NULL
), start_offset
, curpos
, remotestat
.st_size
);
423 printf("%s downloaded\n", path
);
426 fprintf(stderr
, "\r%s", path
);
428 for(i
= strlen(path
); i
< columns
; i
++) {
435 if(keep_permissions
&& !send_stdout
) {
436 if(fchmod(localhandle
, remotestat
.st_mode
) < 0) {
437 fprintf(stderr
, "Unable to change mode of local file %s to %o\n", path
, remotestat
.st_mode
);
438 smbc_close(remotehandle
);
444 smbc_close(remotehandle
);
445 if (localhandle
!= STDOUT_FILENO
) close(localhandle
);
449 static void clean_exit(void)
452 human_readable(total_bytes
, bs
, sizeof(bs
));
453 if(!quiet
)fprintf(stderr
, "Downloaded %s in %lu seconds\n", bs
, time(NULL
) - total_start_time
);
457 static void signal_quit(int v
)
462 static int readrcfile(const char *name
, const struct poptOption long_options
[])
464 FILE *fd
= fopen(name
, "r");
466 char var
[101], val
[101];
468 int *intdata
; char **stringdata
;
470 fprintf(stderr
, "Can't open RC file %s\n", name
);
476 if(fscanf(fd
, "%100s %100s\n", var
, val
) < 2) {
477 fprintf(stderr
, "Can't parse line %d of %s, ignoring.\n", lineno
, name
);
483 for(i
= 0; long_options
[i
].shortName
; i
++) {
484 if(!long_options
[i
].longName
)continue;
485 if(strcmp(long_options
[i
].longName
, var
)) continue;
486 if(!long_options
[i
].arg
)continue;
488 switch(long_options
[i
].argInfo
) {
490 intdata
= (int *)long_options
[i
].arg
;
491 if(!strcmp(val
, "on")) *intdata
= 1;
492 else if(!strcmp(val
, "off")) *intdata
= 0;
493 else fprintf(stderr
, "Illegal value %s for %s at line %d in %s\n", val
, var
, lineno
, name
);
496 intdata
= (int *)long_options
[i
].arg
;
497 *intdata
= atoi(val
);
499 case POPT_ARG_STRING
:
500 stringdata
= (char **)long_options
[i
].arg
;
501 *stringdata
= SMB_STRDUP(val
);
504 fprintf(stderr
, "Invalid variable %s at line %d in %s\n", var
, lineno
, name
);
511 fprintf(stderr
, "Invalid variable %s at line %d in %s\n", var
, lineno
, name
);
519 int main(int argc
, const char **argv
)
522 const char *file
= NULL
;
524 struct poptOption long_options
[] = {
525 {"guest", 'a', POPT_ARG_NONE
, NULL
, 'a', "Work as user guest" },
526 {"resume", 'r', POPT_ARG_NONE
, &_resume
, 0, "Automatically resume aborted files" },
527 {"recursive", 'R', POPT_ARG_NONE
, &_recursive
, 0, "Recursively download files" },
528 {"username", 'u', POPT_ARG_STRING
, &username
, 'u', "Username to use" },
529 {"password", 'p', POPT_ARG_STRING
, &password
, 'p', "Password to use" },
530 {"workgroup", 'w', POPT_ARG_STRING
, &workgroup
, 'w', "Workgroup to use (optional)" },
531 {"nonprompt", 'n', POPT_ARG_NONE
, &nonprompt
, 'n', "Don't ask anything (non-interactive)" },
532 {"debuglevel", 'd', POPT_ARG_INT
, &debuglevel
, 'd', "Debuglevel to use" },
533 {"outputfile", 'o', POPT_ARG_STRING
, &outputfile
, 'o', "Write downloaded data to specified file" },
534 {"stdout", 'O', POPT_ARG_NONE
, &send_stdout
, 'O', "Write data to stdout" },
535 {"dots", 'D', POPT_ARG_NONE
, &dots
, 'D', "Show dots as progress indication" },
536 {"quiet", 'q', POPT_ARG_NONE
, &quiet
, 'q', "Be quiet" },
537 {"verbose", 'v', POPT_ARG_NONE
, &verbose
, 'v', "Be verbose" },
538 {"keep-permissions", 'P', POPT_ARG_NONE
, &keep_permissions
, 'P', "Keep permissions" },
539 {"blocksize", 'b', POPT_ARG_INT
, &blocksize
, 'b', "Change number of bytes in a block"},
540 {"rcfile", 'f', POPT_ARG_STRING
, NULL
, 0, "Use specified rc file"},
548 /* only read rcfile if it exists */
549 asprintf(&rcfile
, "%s/.smbgetrc", getenv("HOME"));
550 if(access(rcfile
, F_OK
) == 0)
551 readrcfile(rcfile
, long_options
);
555 signal(SIGWINCH
, change_columns
);
557 signal(SIGINT
, signal_quit
);
558 signal(SIGTERM
, signal_quit
);
560 pc
= poptGetContext(argv
[0], argc
, argv
, long_options
, 0);
562 while((c
= poptGetNextOpt(pc
)) >= 0) {
565 readrcfile(poptGetOptArg(pc
), long_options
);
568 username
= ""; password
= "";
573 if((send_stdout
|| outputfile
) && _recursive
) {
574 fprintf(stderr
, "The -o or -O and -R options can not be used together.\n");
578 if(outputfile
&& send_stdout
) {
579 fprintf(stderr
, "The -o and -O options cannot be used together.\n");
583 if(smbc_init(get_auth_data
, debuglevel
) < 0) {
584 fprintf(stderr
, "Unable to initialize libsmbclient\n");
588 columns
= get_num_cols();
590 total_start_time
= time(NULL
);
592 while ( (file
= poptGetArg(pc
)) ) {
594 return smb_download_file(file
, "", _recursive
, _resume
, outputfile
);
596 return smb_download_dir(file
, "", _resume
);