From 950a6d980a738f0494a4f58ea588a0f5194da8c6 Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Wed, 7 Jan 2004 00:43:52 +0000 Subject: Add smbget utility, a simple wget-like utility that uses libsmbclient. Supports recursive downloads and resume, progress indication and shows estimated time remaining. (This used to be commit 82bd1b45a4205706b57bae42c7b03974f8b44753) --- source3/utils/smbget.c | 574 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 574 insertions(+) create mode 100644 source3/utils/smbget.c (limited to 'source3/utils/smbget.c') diff --git a/source3/utils/smbget.c b/source3/utils/smbget.c new file mode 100644 index 0000000000..92a3831752 --- /dev/null +++ b/source3/utils/smbget.c @@ -0,0 +1,574 @@ +/* + smbget: a wget-like utility with support for recursive downloading and + smb:// urls + Copyright (C) 2003-2004 Jelmer Vernooij + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ + +#include "includes.h" +#include "libsmbclient.h" + +#if _FILE_OFFSET_BITS==64 +#define OFF_T_FORMAT "%lld" +#else +#define OFF_T_FORMAT "%ld" +#endif + +int columns = 0; + +time_t total_start_time = 0; +off_t total_bytes = 0; + +#define SMB_MAXPATHLEN MAXPATHLEN + +/* Number of bytes to read when checking whether local and remote file are really the same file */ +#define RESUME_CHECK_SIZE 512 +#define RESUME_DOWNLOAD_OFFSET 1024 +#define RESUME_CHECK_OFFSET RESUME_DOWNLOAD_OFFSET+RESUME_CHECK_SIZE +/* Number of bytes to read at once */ +#define SMB_DEFAULT_BLOCKSIZE 64000 + +const char *username = NULL, *password = NULL, *workgroup = NULL; +int nonprompt = 0, quiet = 0, dots = 0, keep_permissions = 0, verbose = 0; +int blocksize = SMB_DEFAULT_BLOCKSIZE; + +int smb_download_file(const char *base, const char *name, int recursive, int resume, char *outfile); + +int get_num_cols(void) +{ +#ifdef TIOCGWINSZ + struct winsize ws; + if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0) { + perror("ioctl"); + return 0; + } + return ws.ws_col; +#else +#warning No support for TIOCGWINSZ + char *cols = getenv("COLUMNS"); + if(!cols) return 0; + return atoi(cols); +#endif +} + +void change_columns(int sig) +{ + columns = get_num_cols(); +} + +void human_readable(off_t s, char *buffer, int l) +{ + if(s > 1024 * 1024 * 1024) snprintf(buffer, l, "%.2fGb", 1.0 * s / (1024 * 1024 * 1024)); + else if(s > 1024 * 1024) snprintf(buffer, l, "%.2fMb", 1.0 * s / (1024 * 1024)); + else if(s > 1024) snprintf(buffer, l, "%.2fkb", 1.0 * s / 1024); + else snprintf(buffer, l, OFF_T_FORMAT"b", s); +} + +void get_auth_data(const char *srv, const char *shr, char *wg, int wglen, char *un, int unlen, char *pw, int pwlen) +{ + static char hasasked = 0; + char *wgtmp, *usertmp; + char tmp[128]; + + if(hasasked) return; + hasasked = 1; + + if(!nonprompt && !username) { + printf("Username for %s at %s [guest] ", shr, srv); + fgets(tmp, sizeof(tmp), stdin); + if(tmp[strlen(tmp)-1] == '\n')tmp[strlen(tmp)-1] = '\0'; + strncpy(un, tmp, unlen-1); + } else if(username) strncpy(un, username, unlen-1); + + if(!nonprompt && !password) { + char *prompt, *pass; + asprintf(&prompt, "Password for %s at %s: ", shr, srv); + pass = getpass(prompt); + free(prompt); + strncpy(pw, pass, pwlen-1); + } else if(password) strncpy(pw, password, pwlen-1); + + if(workgroup)strncpy(wg, workgroup, wglen-1); + + wgtmp = strndup(wg, wglen); + usertmp = strndup(un, unlen); + if(!quiet)printf("Using workgroup %s, %s%s\n", wgtmp, *usertmp?"user ":"guest user", usertmp); + free(wgtmp); free(usertmp); +} + +int smb_download_dir(const char *base, const char *name, int resume) +{ + char path[SMB_MAXPATHLEN]; + int dirhandle; + struct smbc_dirent *dirent; + const char *relname = name; + char *tmpname; + struct stat remotestat; + snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base, (base[0] && name[0] && name[0] != '/' && base[strlen(base)-1] != '/')?"/":"", name); + + /* List files in directory and call smb_download_file on them */ + dirhandle = smbc_opendir(path); + if(dirhandle < 1) { + if(errno == ENOTDIR) return smb_download_file(base, name, 1, resume, NULL); + fprintf(stderr, "Can't open directory %s: %s\n", path, strerror(errno)); + return 0; + } + + while(*relname == '/')relname++; + mkdir(relname, 0755); + + tmpname = strdup(name); + + while((dirent = smbc_readdir(dirhandle))) { + char *newname; + if(!strcmp(dirent->name, ".") || !strcmp(dirent->name, ".."))continue; + asprintf(&newname, "%s/%s", tmpname, dirent->name); + switch(dirent->smbc_type) { + case SMBC_DIR: + smb_download_dir(base, newname, resume); + break; + + case SMBC_WORKGROUP: + smb_download_dir("smb://", dirent->name, resume); + break; + + case SMBC_SERVER: + smb_download_dir("smb://", dirent->name, resume); + break; + + case SMBC_FILE: + smb_download_file(base, newname, 1, resume, NULL); + break; + + case SMBC_FILE_SHARE: + smb_download_dir(base, newname, resume); + break; + + case SMBC_PRINTER_SHARE: + if(!quiet)printf("Ignoring printer share %s\n", dirent->name); + break; + + case SMBC_COMMS_SHARE: + if(!quiet)printf("Ignoring comms share %s\n", dirent->name); + break; + + case SMBC_IPC_SHARE: + if(!quiet)printf("Ignoring ipc$ share %s\n", dirent->name); + break; + + default: + fprintf(stderr, "Ignoring file '%s' of type '%d'\n", newname, dirent->smbc_type); + break; + } + free(newname); + } + free(tmpname); + + if(keep_permissions) { + if(smbc_fstat(dirhandle, &remotestat) < 0) { + fprintf(stderr, "Unable to get stats on %s on remote server\n", path); + smbc_closedir(dirhandle); + return 0; + } + + if(chmod(relname, remotestat.st_mode) < 0) { + fprintf(stderr, "Unable to change mode of local dir %s to %o\n", relname, remotestat.st_mode); + smbc_closedir(dirhandle); + return 0; + } + } + + smbc_closedir(dirhandle); + return 1; +} + +char *print_time(long t) +{ + static char buffer[100]; + int secs, mins, hours; + if(t < -1) { + strncpy(buffer, "Unknown", sizeof(buffer)); + return buffer; + } + + secs = (int)t % 60; + mins = (int)t / 60 % 60; + hours = (int)t / (60 * 60); + snprintf(buffer, sizeof(buffer)-1, "%02d:%02d:%02d", hours, mins, secs); + return buffer; +} + +void print_progress(const char *name, time_t start, time_t now, off_t start_pos, off_t pos, off_t total) +{ + double avg = 0.0; + long eta = -1; + double prcnt = 0.0; + char hpos[20], htotal[20], havg[20]; + char *status, *filename; + int len; + if(now - start)avg = 1.0 * (pos - start_pos) / (now - start); + eta = (total - pos - start_pos) / avg; + if(total)prcnt = 100.0 * pos / total; + + human_readable(pos, hpos, sizeof(hpos)); + human_readable(total, htotal, sizeof(htotal)); + human_readable(avg, havg, sizeof(havg)); + + len = asprintf(&status, "%s of %s (%.2f%%) at %s/s ETA: %s", hpos, htotal, prcnt, havg, print_time(eta)); + + if(columns) { + int required = strlen(name), available = columns - len - strlen("[] "); + if(required > available) asprintf(&filename, "...%s", name + required - available + 3); + else filename = strndup(name, available); + } else filename = strdup(name); + + fprintf(stderr, "\r[%s] %s", filename, status); + + free(filename); free(status); +} + +int smb_download_file(const char *base, const char *name, int recursive, int resume, char *outfile) { + int remotehandle, localhandle; + time_t start_time = time(NULL); + const char *newpath; + char path[SMB_MAXPATHLEN]; + char checkbuf[2][RESUME_CHECK_SIZE]; + char *readbuf = NULL; + off_t offset_download = 0, offset_check = 0, curpos = 0, start_offset = 0; + struct stat localstat, remotestat; + + snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base, (*base && *name && name[0] != '/' && base[strlen(base)-1] != '/')?"/":"", name); + + remotehandle = smbc_open(path, O_RDONLY, 0755); + + if(remotehandle < 0) { + switch(errno) { + case EISDIR: + if(!recursive) { + fprintf(stderr, "%s is a directory. Specify -R to download recursively\n", path); + return 0; + } + smb_download_dir(base, name, resume); + return 0; + + case ENOENT: + fprintf(stderr, "%s can't be found on the remote server\n", path); + return 0; + + case ENOMEM: + fprintf(stderr, "Not enough memory\n"); + exit(1); + return 0; + + case ENODEV: + fprintf(stderr, "The share name used in %s does not exist\n", path); + return 0; + + case EACCES: + fprintf(stderr, "You don't have enough permissions to access %s\n", path); + return 0; + + default: + perror("smbc_open"); + return 0; + } + } + + if(smbc_fstat(remotehandle, &remotestat) < 0) { + fprintf(stderr, "Can't stat %s: %s\n", path, strerror(errno)); + return 0; + } + + if(outfile) newpath = outfile; + else if(!name[0]) { + newpath = strrchr(base, '/'); + if(newpath)newpath++; else newpath = base; + } else newpath = name; + + if(newpath[0] == '/')newpath++; + + /* Open local file and, if necessary, resume */ + localhandle = open(newpath, O_CREAT | O_NONBLOCK | O_RDWR | (!resume?O_EXCL:0), 0755); + if(localhandle < 0) { + fprintf(stderr, "Can't open %s: %s\n", newpath, strerror(errno)); + smbc_close(remotehandle); + return 0; + } + + fstat(localhandle, &localstat); + + start_offset = localstat.st_size; + + if(localstat.st_size && localstat.st_size == remotestat.st_size) { + if(verbose)fprintf(stderr, "%s is already downloaded completely.\n", path); + else if(!quiet)fprintf(stderr, "%s\n", path); + smbc_close(remotehandle); + close(localhandle); + return 1; + } + + if(localstat.st_size > RESUME_CHECK_OFFSET && remotestat.st_size > RESUME_CHECK_OFFSET) { + offset_download = localstat.st_size - RESUME_DOWNLOAD_OFFSET; + offset_check = localstat.st_size - RESUME_CHECK_OFFSET; + if(verbose)printf("Trying to start resume of %s at "OFF_T_FORMAT"\n" + "At the moment "OFF_T_FORMAT" of "OFF_T_FORMAT" bytes have been retrieved\n", newpath, offset_check, + localstat.st_size, remotestat.st_size); + } + + if(offset_check) { + off_t off1, off2; + /* First, check all bytes from offset_check to offset_download */ + off1 = lseek(localhandle, offset_check, SEEK_SET); + if(off1 < 0) { + fprintf(stderr, "Can't seek to "OFF_T_FORMAT" in local file %s\n", offset_check, newpath); + smbc_close(remotehandle); close(localhandle); + return 0; + } + + off2 = smbc_lseek(remotehandle, offset_check, SEEK_SET); + if(off2 < 0) { + fprintf(stderr, "Can't seek to "OFF_T_FORMAT" in remote file %s\n", offset_check, newpath); + smbc_close(remotehandle); close(localhandle); + return 0; + } + + if(off1 != off2) { + fprintf(stderr, "Offset in local and remote files is different (local: "OFF_T_FORMAT", remote: "OFF_T_FORMAT")\n", off1, off2); + return 0; + } + + if(smbc_read(remotehandle, checkbuf[0], RESUME_CHECK_SIZE) != RESUME_CHECK_SIZE) { + fprintf(stderr, "Can't read %d bytes from remote file %s\n", RESUME_CHECK_SIZE, path); + smbc_close(remotehandle); close(localhandle); + return 0; + } + + if(read(localhandle, checkbuf[1], RESUME_CHECK_SIZE) != RESUME_CHECK_SIZE) { + fprintf(stderr, "Can't read %d bytes from local file %s\n", RESUME_CHECK_SIZE, name); + smbc_close(remotehandle); close(localhandle); + return 0; + } + + if(memcmp(checkbuf[0], checkbuf[1], RESUME_CHECK_SIZE) == 0) { + if(verbose)printf("Current local and remote file appear to be the same. Starting download from offset "OFF_T_FORMAT"\n", offset_download); + } else { + fprintf(stderr, "Local and remote file appear to be different, not doing resume for %s\n", path); + smbc_close(remotehandle); close(localhandle); + return 0; + } + } + + readbuf = malloc(blocksize); + + /* Now, download all bytes from offset_download to the end */ + for(curpos = offset_download; curpos < remotestat.st_size; curpos+=blocksize) { + ssize_t bytesread = smbc_read(remotehandle, readbuf, blocksize); + if(bytesread < 0) { + fprintf(stderr, "Can't read %d bytes at offset "OFF_T_FORMAT", file %s\n", blocksize, curpos, path); + smbc_close(remotehandle); close(localhandle); + free(readbuf); + return 0; + } + + total_bytes += bytesread; + + if(write(localhandle, readbuf, bytesread) < 0) { + fprintf(stderr, "Can't write %d bytes to local file %s at offset "OFF_T_FORMAT"\n", bytesread, path, curpos); + free(readbuf); + smbc_close(remotehandle); close(localhandle); + return 0; + } + + if(dots)fputc('.', stderr); + else if(!quiet) { + print_progress(newpath, start_time, time(NULL), start_offset, curpos, remotestat.st_size); + } + } + + free(readbuf); + + if(dots){ + fputc('\n', stderr); + printf("%s downloaded\n", path); + } else if(!quiet) { + int i; + fprintf(stderr, "\r%s", path); + if(columns) { + for(i = strlen(path); i < columns; i++) { + fputc(' ', stderr); + } + } + fputc('\n', stderr); + } + + if(keep_permissions) { + if(fchmod(localhandle, remotestat.st_mode) < 0) { + fprintf(stderr, "Unable to change mode of local file %s to %o\n", path, remotestat.st_mode); + smbc_close(remotehandle); + close(localhandle); + return 0; + } + } + smbc_close(remotehandle); + close(localhandle); + return 1; +} + +void clean_exit(void) +{ + char bs[100]; + human_readable(total_bytes, bs, sizeof(bs)); + if(!quiet)fprintf(stderr, "Downloaded %s in %lu seconds\n", bs, time(NULL) - total_start_time); + exit(0); +} + +void signal_quit(int v) +{ + clean_exit(); +} + +int readrcfile(const char *name, const struct poptOption long_options[]) +{ + FILE *fd = fopen(name, "r"); + int lineno = 0, i; + char var[101], val[101]; + char found; + int *intdata; char **stringdata; + if(!fd) { + fprintf(stderr, "Can't open RC file %s\n", name); + return 1; + } + + while(!feof(fd)) { + lineno++; + if(fscanf(fd, "%100s %100s\n", var, val) < 2) { + fprintf(stderr, "Can't parse line %d of %s, ignoring.\n", lineno, name); + continue; + } + + found = 0; + + for(i = 0; long_options[i].shortName; i++) { + if(!long_options[i].longName)continue; + if(strcmp(long_options[i].longName, var)) continue; + if(!long_options[i].arg)continue; + + switch(long_options[i].argInfo) { + case POPT_ARG_NONE: + intdata = (int *)long_options[i].arg; + if(!strcmp(val, "on")) *intdata = 1; + else if(!strcmp(val, "off")) *intdata = 0; + else fprintf(stderr, "Illegal value %s for %s at line %d in %s\n", val, var, lineno, name); + break; + case POPT_ARG_INT: + intdata = (int *)long_options[i].arg; + *intdata = atoi(val); + break; + case POPT_ARG_STRING: + stringdata = (char **)long_options[i].arg; + *stringdata = strdup(val); + break; + default: + fprintf(stderr, "Invalid variable %s at line %d in %s\n", var, lineno, name); + break; + } + + found = 1; + } + if(!found) { + fprintf(stderr, "Invalid variable %s at line %d in %s\n", var, lineno, name); + } + } + + fclose(fd); + return 0; +} + +int main(int argc, const char **argv) +{ + int resume = 0, recursive = 0; + int c = 0; + int debuglevel = 0; + const char *file = NULL; + char *rcfile = NULL; + char *outputfile = NULL; + struct poptOption long_options[] = { + {"guest", 'a', POPT_ARG_NONE, NULL, 'a', "Work as user guest" }, + {"resume", 'r', POPT_ARG_NONE, &resume, 0, "Automatically resume aborted files" }, + {"recursive", 'R', POPT_ARG_NONE, &recursive, 0, "Recursively download files" }, + {"username", 'u', POPT_ARG_STRING, &username, 'u', "Username to use" }, + {"password", 'p', POPT_ARG_STRING, &password, 'p', "Password to use" }, + {"workgroup", 'w', POPT_ARG_STRING, &workgroup, 'w', "Workgroup to use (optional)" }, + {"nonprompt", 'n', POPT_ARG_NONE, &nonprompt, 'n', "Don't ask anything (non-interactive)" }, + {"debuglevel", 'd', POPT_ARG_INT, &debuglevel, 'd', "Debuglevel to use" }, + {"outputfile", 'o', POPT_ARG_STRING, &outputfile, 'o', "Write downloaded data to specified file" }, + {"dots", 'D', POPT_ARG_NONE, &dots, 'D', "Show dots as progress indication" }, + {"quiet", 'q', POPT_ARG_NONE, &quiet, 'q', "Be quiet" }, + {"verbose", 'v', POPT_ARG_NONE, &verbose, 'v', "Be verbose" }, + {"keep-permissions", 'P', POPT_ARG_NONE, &keep_permissions, 'P', "Keep permissions" }, + {"blocksize", 'b', POPT_ARG_INT, &blocksize, 'b', "Change number of bytes in a block"}, + {"rcfile", 'f', POPT_ARG_STRING, NULL, 0, "Use specified rc file"}, + POPT_AUTOHELP + POPT_TABLEEND + }; + poptContext pc; + + /* only read rcfile if it exists */ + asprintf(&rcfile, "%s/.smbgetrc", getenv("HOME")); + if(access(rcfile, F_OK) == 0) readrcfile(rcfile, long_options); + free(rcfile); + +#ifdef SIGWINCH + signal(SIGWINCH, change_columns); +#endif + signal(SIGINT, signal_quit); + signal(SIGTERM, signal_quit); + + pc = poptGetContext(argv[0], argc, argv, long_options, 0); + + while((c = poptGetNextOpt(pc)) >= 0) { + switch(c) { + case 'f': + readrcfile(poptGetOptArg(pc), long_options); + break; + case 'a': + username = ""; password = ""; + break; + } + } + + if(outputfile && recursive) { + fprintf(stderr, "The -o and -R options can not be used together.\n"); + return 1; + } + + if(smbc_init(get_auth_data, debuglevel) < 0) { + fprintf(stderr, "Unable to initialize libsmbclient\n"); + return 1; + } + + columns = get_num_cols(); + + total_start_time = time(NULL); + + while((file = poptGetArg(pc))) { + if(!recursive) return smb_download_file(file, "", recursive, resume, outputfile); + else return smb_download_dir(file, "", resume); + } + + clean_exit(); + + return 0; +} -- cgit From 91cc4a1900bf1e95244f178b6e76edf534f6186a Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Fri, 13 Feb 2004 22:09:53 +0000 Subject: Fix ETA Calculation when resuming (This used to be commit a5f09f0991e5a5bd9538211b0d430020052670c8) --- source3/utils/smbget.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/utils/smbget.c') diff --git a/source3/utils/smbget.c b/source3/utils/smbget.c index 92a3831752..ab6e368c70 100644 --- a/source3/utils/smbget.c +++ b/source3/utils/smbget.c @@ -219,7 +219,7 @@ void print_progress(const char *name, time_t start, time_t now, off_t start_pos, char *status, *filename; int len; if(now - start)avg = 1.0 * (pos - start_pos) / (now - start); - eta = (total - pos - start_pos) / avg; + eta = (total - pos) / avg; if(total)prcnt = 100.0 * pos / total; human_readable(pos, hpos, sizeof(hpos)); -- cgit From 6643781811199ec67bf191e1182cded2a3282bf1 Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Fri, 5 Mar 2004 17:17:31 +0000 Subject: Add -O (for writing downloaded files to standard out) based on patch by Bas van Sisseren (This used to be commit cc164e5afb0f6100bde547674b3de88b7e45f31f) --- source3/utils/smbget.c | 145 +++++++++++++++++++++++++++---------------------- 1 file changed, 80 insertions(+), 65 deletions(-) (limited to 'source3/utils/smbget.c') diff --git a/source3/utils/smbget.c b/source3/utils/smbget.c index ab6e368c70..64630bba8f 100644 --- a/source3/utils/smbget.c +++ b/source3/utils/smbget.c @@ -41,7 +41,7 @@ off_t total_bytes = 0; #define SMB_DEFAULT_BLOCKSIZE 64000 const char *username = NULL, *password = NULL, *workgroup = NULL; -int nonprompt = 0, quiet = 0, dots = 0, keep_permissions = 0, verbose = 0; +int nonprompt = 0, quiet = 0, dots = 0, keep_permissions = 0, verbose = 0, send_stdout = 0; int blocksize = SMB_DEFAULT_BLOCKSIZE; int smb_download_file(const char *base, const char *name, int recursive, int resume, char *outfile); @@ -51,7 +51,6 @@ int get_num_cols(void) #ifdef TIOCGWINSZ struct winsize ws; if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0) { - perror("ioctl"); return 0; } return ws.ws_col; @@ -300,74 +299,81 @@ int smb_download_file(const char *base, const char *name, int recursive, int res if(newpath[0] == '/')newpath++; /* Open local file and, if necessary, resume */ - localhandle = open(newpath, O_CREAT | O_NONBLOCK | O_RDWR | (!resume?O_EXCL:0), 0755); - if(localhandle < 0) { - fprintf(stderr, "Can't open %s: %s\n", newpath, strerror(errno)); - smbc_close(remotehandle); - return 0; - } + if(!send_stdout) { + localhandle = open(newpath, O_CREAT | O_NONBLOCK | O_RDWR | (!resume?O_EXCL:0), 0755); + if(localhandle < 0) { + fprintf(stderr, "Can't open %s: %s\n", newpath, strerror(errno)); + smbc_close(remotehandle); + return 0; + } - fstat(localhandle, &localstat); + fstat(localhandle, &localstat); - start_offset = localstat.st_size; + start_offset = localstat.st_size; - if(localstat.st_size && localstat.st_size == remotestat.st_size) { - if(verbose)fprintf(stderr, "%s is already downloaded completely.\n", path); - else if(!quiet)fprintf(stderr, "%s\n", path); - smbc_close(remotehandle); - close(localhandle); - return 1; - } - - if(localstat.st_size > RESUME_CHECK_OFFSET && remotestat.st_size > RESUME_CHECK_OFFSET) { - offset_download = localstat.st_size - RESUME_DOWNLOAD_OFFSET; - offset_check = localstat.st_size - RESUME_CHECK_OFFSET; - if(verbose)printf("Trying to start resume of %s at "OFF_T_FORMAT"\n" - "At the moment "OFF_T_FORMAT" of "OFF_T_FORMAT" bytes have been retrieved\n", newpath, offset_check, - localstat.st_size, remotestat.st_size); - } - - if(offset_check) { - off_t off1, off2; - /* First, check all bytes from offset_check to offset_download */ - off1 = lseek(localhandle, offset_check, SEEK_SET); - if(off1 < 0) { - fprintf(stderr, "Can't seek to "OFF_T_FORMAT" in local file %s\n", offset_check, newpath); - smbc_close(remotehandle); close(localhandle); - return 0; + if(localstat.st_size && localstat.st_size == remotestat.st_size) { + if(verbose)fprintf(stderr, "%s is already downloaded completely.\n", path); + else if(!quiet)fprintf(stderr, "%s\n", path); + smbc_close(remotehandle); + close(localhandle); + return 1; } - off2 = smbc_lseek(remotehandle, offset_check, SEEK_SET); - if(off2 < 0) { - fprintf(stderr, "Can't seek to "OFF_T_FORMAT" in remote file %s\n", offset_check, newpath); - smbc_close(remotehandle); close(localhandle); - return 0; + if(localstat.st_size > RESUME_CHECK_OFFSET && remotestat.st_size > RESUME_CHECK_OFFSET) { + offset_download = localstat.st_size - RESUME_DOWNLOAD_OFFSET; + offset_check = localstat.st_size - RESUME_CHECK_OFFSET; + if(verbose)printf("Trying to start resume of %s at "OFF_T_FORMAT"\n" + "At the moment "OFF_T_FORMAT" of "OFF_T_FORMAT" bytes have been retrieved\n", newpath, offset_check, + localstat.st_size, remotestat.st_size); } - if(off1 != off2) { - fprintf(stderr, "Offset in local and remote files is different (local: "OFF_T_FORMAT", remote: "OFF_T_FORMAT")\n", off1, off2); - return 0; - } + if(offset_check) { + off_t off1, off2; + /* First, check all bytes from offset_check to offset_download */ + off1 = lseek(localhandle, offset_check, SEEK_SET); + if(off1 < 0) { + fprintf(stderr, "Can't seek to "OFF_T_FORMAT" in local file %s\n", offset_check, newpath); + smbc_close(remotehandle); close(localhandle); + return 0; + } - if(smbc_read(remotehandle, checkbuf[0], RESUME_CHECK_SIZE) != RESUME_CHECK_SIZE) { - fprintf(stderr, "Can't read %d bytes from remote file %s\n", RESUME_CHECK_SIZE, path); - smbc_close(remotehandle); close(localhandle); - return 0; - } + off2 = smbc_lseek(remotehandle, offset_check, SEEK_SET); + if(off2 < 0) { + fprintf(stderr, "Can't seek to "OFF_T_FORMAT" in remote file %s\n", offset_check, newpath); + smbc_close(remotehandle); close(localhandle); + return 0; + } - if(read(localhandle, checkbuf[1], RESUME_CHECK_SIZE) != RESUME_CHECK_SIZE) { - fprintf(stderr, "Can't read %d bytes from local file %s\n", RESUME_CHECK_SIZE, name); - smbc_close(remotehandle); close(localhandle); - return 0; - } + if(off1 != off2) { + fprintf(stderr, "Offset in local and remote files is different (local: "OFF_T_FORMAT", remote: "OFF_T_FORMAT")\n", off1, off2); + return 0; + } - if(memcmp(checkbuf[0], checkbuf[1], RESUME_CHECK_SIZE) == 0) { - if(verbose)printf("Current local and remote file appear to be the same. Starting download from offset "OFF_T_FORMAT"\n", offset_download); - } else { - fprintf(stderr, "Local and remote file appear to be different, not doing resume for %s\n", path); - smbc_close(remotehandle); close(localhandle); - return 0; + if(smbc_read(remotehandle, checkbuf[0], RESUME_CHECK_SIZE) != RESUME_CHECK_SIZE) { + fprintf(stderr, "Can't read %d bytes from remote file %s\n", RESUME_CHECK_SIZE, path); + smbc_close(remotehandle); close(localhandle); + return 0; + } + + if(read(localhandle, checkbuf[1], RESUME_CHECK_SIZE) != RESUME_CHECK_SIZE) { + fprintf(stderr, "Can't read %d bytes from local file %s\n", RESUME_CHECK_SIZE, name); + smbc_close(remotehandle); close(localhandle); + return 0; + } + + if(memcmp(checkbuf[0], checkbuf[1], RESUME_CHECK_SIZE) == 0) { + if(verbose)printf("Current local and remote file appear to be the same. Starting download from offset "OFF_T_FORMAT"\n", offset_download); + } else { + fprintf(stderr, "Local and remote file appear to be different, not doing resume for %s\n", path); + smbc_close(remotehandle); close(localhandle); + return 0; + } } + } else { + localhandle = STDOUT_FILENO; + start_offset = 0; + offset_download = 0; + offset_check = 0; } readbuf = malloc(blocksize); @@ -377,7 +383,8 @@ int smb_download_file(const char *base, const char *name, int recursive, int res ssize_t bytesread = smbc_read(remotehandle, readbuf, blocksize); if(bytesread < 0) { fprintf(stderr, "Can't read %d bytes at offset "OFF_T_FORMAT", file %s\n", blocksize, curpos, path); - smbc_close(remotehandle); close(localhandle); + smbc_close(remotehandle); + if (localhandle != STDOUT_FILENO) close(localhandle); free(readbuf); return 0; } @@ -387,7 +394,8 @@ int smb_download_file(const char *base, const char *name, int recursive, int res if(write(localhandle, readbuf, bytesread) < 0) { fprintf(stderr, "Can't write %d bytes to local file %s at offset "OFF_T_FORMAT"\n", bytesread, path, curpos); free(readbuf); - smbc_close(remotehandle); close(localhandle); + smbc_close(remotehandle); + if (localhandle != STDOUT_FILENO) close(localhandle); return 0; } @@ -413,7 +421,7 @@ int smb_download_file(const char *base, const char *name, int recursive, int res fputc('\n', stderr); } - if(keep_permissions) { + if(keep_permissions && !send_stdout) { if(fchmod(localhandle, remotestat.st_mode) < 0) { fprintf(stderr, "Unable to change mode of local file %s to %o\n", path, remotestat.st_mode); smbc_close(remotehandle); @@ -421,8 +429,9 @@ int smb_download_file(const char *base, const char *name, int recursive, int res return 0; } } + smbc_close(remotehandle); - close(localhandle); + if (localhandle != STDOUT_FILENO) close(localhandle); return 1; } @@ -514,6 +523,7 @@ int main(int argc, const char **argv) {"nonprompt", 'n', POPT_ARG_NONE, &nonprompt, 'n', "Don't ask anything (non-interactive)" }, {"debuglevel", 'd', POPT_ARG_INT, &debuglevel, 'd', "Debuglevel to use" }, {"outputfile", 'o', POPT_ARG_STRING, &outputfile, 'o', "Write downloaded data to specified file" }, + {"stdout", 'O', POPT_ARG_NONE, &send_stdout, 'O', "Write data to stdout" }, {"dots", 'D', POPT_ARG_NONE, &dots, 'D', "Show dots as progress indication" }, {"quiet", 'q', POPT_ARG_NONE, &quiet, 'q', "Be quiet" }, {"verbose", 'v', POPT_ARG_NONE, &verbose, 'v', "Be verbose" }, @@ -549,8 +559,13 @@ int main(int argc, const char **argv) } } - if(outputfile && recursive) { - fprintf(stderr, "The -o and -R options can not be used together.\n"); + if((send_stdout || outputfile) && recursive) { + fprintf(stderr, "The -o or -O and -R options can not be used together.\n"); + return 1; + } + + if(outputfile && send_stdout) { + fprintf(stderr, "The -o and -O options cannot be used together.\n"); return 1; } -- cgit From 46f0e330f1e455e3338a7e7a3b7a287df752fe1a Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 16 Dec 2004 21:06:33 +0000 Subject: r4234: More malloc fixes to use the macros. Jeremy. (This used to be commit 61479f56be60a3c2ae0f7b931335cb1da77540c2) --- source3/utils/smbget.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'source3/utils/smbget.c') diff --git a/source3/utils/smbget.c b/source3/utils/smbget.c index 64630bba8f..2aca3001a3 100644 --- a/source3/utils/smbget.c +++ b/source3/utils/smbget.c @@ -128,7 +128,7 @@ int smb_download_dir(const char *base, const char *name, int resume) while(*relname == '/')relname++; mkdir(relname, 0755); - tmpname = strdup(name); + tmpname = SMB_STRDUP(name); while((dirent = smbc_readdir(dirhandle))) { char *newname; @@ -231,7 +231,7 @@ void print_progress(const char *name, time_t start, time_t now, off_t start_pos, int required = strlen(name), available = columns - len - strlen("[] "); if(required > available) asprintf(&filename, "...%s", name + required - available + 3); else filename = strndup(name, available); - } else filename = strdup(name); + } else filename = SMB_STRDUP(name); fprintf(stderr, "\r[%s] %s", filename, status); @@ -376,7 +376,7 @@ int smb_download_file(const char *base, const char *name, int recursive, int res offset_check = 0; } - readbuf = malloc(blocksize); + readbuf = SMB_MALLOC(blocksize); /* Now, download all bytes from offset_download to the end */ for(curpos = offset_download; curpos < remotestat.st_size; curpos+=blocksize) { @@ -487,7 +487,7 @@ int readrcfile(const char *name, const struct poptOption long_options[]) break; case POPT_ARG_STRING: stringdata = (char **)long_options[i].arg; - *stringdata = strdup(val); + *stringdata = SMB_STRDUP(val); break; default: fprintf(stderr, "Invalid variable %s at line %d in %s\n", var, lineno, name); -- cgit From 2a5087295292f65c63f9a81c2cc73a7d7a95b337 Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Mon, 25 Jul 2005 15:04:25 +0000 Subject: r8755: more malloc fixes (This used to be commit b46546c18a1bf541cb0f172184ca7ecee499b098) --- source3/utils/smbget.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'source3/utils/smbget.c') diff --git a/source3/utils/smbget.c b/source3/utils/smbget.c index 2aca3001a3..e8069802ca 100644 --- a/source3/utils/smbget.c +++ b/source3/utils/smbget.c @@ -101,8 +101,8 @@ void get_auth_data(const char *srv, const char *shr, char *wg, int wglen, char * if(workgroup)strncpy(wg, workgroup, wglen-1); - wgtmp = strndup(wg, wglen); - usertmp = strndup(un, unlen); + wgtmp = SMB_STRNDUP(wg, wglen); + usertmp = SMB_STRNDUP(un, unlen); if(!quiet)printf("Using workgroup %s, %s%s\n", wgtmp, *usertmp?"user ":"guest user", usertmp); free(wgtmp); free(usertmp); } @@ -230,7 +230,7 @@ void print_progress(const char *name, time_t start, time_t now, off_t start_pos, if(columns) { int required = strlen(name), available = columns - len - strlen("[] "); if(required > available) asprintf(&filename, "...%s", name + required - available + 3); - else filename = strndup(name, available); + else filename = SMB_STRNDUP(name, available); } else filename = SMB_STRDUP(name); fprintf(stderr, "\r[%s] %s", filename, status); -- cgit From aef9a5fdc11cffd3c489ed41794fbb3616006205 Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Mon, 21 Nov 2005 15:52:10 +0000 Subject: r11833: fix build issues in smbget with the Sun compiler. Reported by Richard Bollinger (This used to be commit d381c125b0ffc269fd8671a855c9b899966a2320) --- source3/utils/smbget.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) (limited to 'source3/utils/smbget.c') diff --git a/source3/utils/smbget.c b/source3/utils/smbget.c index e8069802ca..eab5e5ac02 100644 --- a/source3/utils/smbget.c +++ b/source3/utils/smbget.c @@ -28,6 +28,10 @@ int columns = 0; +static int _resume, _recursive, debuglevel; +static char *outputfile; + + time_t total_start_time = 0; off_t total_bytes = 0; @@ -507,16 +511,13 @@ int readrcfile(const char *name, const struct poptOption long_options[]) int main(int argc, const char **argv) { - int resume = 0, recursive = 0; int c = 0; - int debuglevel = 0; const char *file = NULL; char *rcfile = NULL; - char *outputfile = NULL; struct poptOption long_options[] = { {"guest", 'a', POPT_ARG_NONE, NULL, 'a', "Work as user guest" }, - {"resume", 'r', POPT_ARG_NONE, &resume, 0, "Automatically resume aborted files" }, - {"recursive", 'R', POPT_ARG_NONE, &recursive, 0, "Recursively download files" }, + {"resume", 'r', POPT_ARG_NONE, &_resume, 0, "Automatically resume aborted files" }, + {"recursive", 'R', POPT_ARG_NONE, &_recursive, 0, "Recursively download files" }, {"username", 'u', POPT_ARG_STRING, &username, 'u', "Username to use" }, {"password", 'p', POPT_ARG_STRING, &password, 'p', "Password to use" }, {"workgroup", 'w', POPT_ARG_STRING, &workgroup, 'w', "Workgroup to use (optional)" }, @@ -537,7 +538,8 @@ int main(int argc, const char **argv) /* only read rcfile if it exists */ asprintf(&rcfile, "%s/.smbgetrc", getenv("HOME")); - if(access(rcfile, F_OK) == 0) readrcfile(rcfile, long_options); + if(access(rcfile, F_OK) == 0) + readrcfile(rcfile, long_options); free(rcfile); #ifdef SIGWINCH @@ -559,7 +561,7 @@ int main(int argc, const char **argv) } } - if((send_stdout || outputfile) && recursive) { + if((send_stdout || outputfile) && _recursive) { fprintf(stderr, "The -o or -O and -R options can not be used together.\n"); return 1; } @@ -578,9 +580,11 @@ int main(int argc, const char **argv) total_start_time = time(NULL); - while((file = poptGetArg(pc))) { - if(!recursive) return smb_download_file(file, "", recursive, resume, outputfile); - else return smb_download_dir(file, "", resume); + while ( (file = poptGetArg(pc)) ) { + if (!_recursive) + return smb_download_file(file, "", _recursive, _resume, outputfile); + else + return smb_download_dir(file, "", _resume); } clean_exit(); -- cgit From 08a73ae7db43f3a157d0091762caaa1f2f6322f3 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Sat, 3 Dec 2005 20:18:43 +0000 Subject: r12054: We only have one more warning at -O6. That will take some more restructuring to fix.... Coming soon. Jeremy. (This used to be commit 739c1c4d438ccf31bb0167e1fc509d072c149d45) --- source3/utils/smbget.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) (limited to 'source3/utils/smbget.c') diff --git a/source3/utils/smbget.c b/source3/utils/smbget.c index eab5e5ac02..9d44017bd3 100644 --- a/source3/utils/smbget.c +++ b/source3/utils/smbget.c @@ -22,8 +22,10 @@ #if _FILE_OFFSET_BITS==64 #define OFF_T_FORMAT "%lld" +#define OFF_T_FORMAT_CAST long long #else #define OFF_T_FORMAT "%ld" +#define OFF_T_FORMAT_CAST long #endif int columns = 0; @@ -76,7 +78,7 @@ void human_readable(off_t s, char *buffer, int l) if(s > 1024 * 1024 * 1024) snprintf(buffer, l, "%.2fGb", 1.0 * s / (1024 * 1024 * 1024)); else if(s > 1024 * 1024) snprintf(buffer, l, "%.2fMb", 1.0 * s / (1024 * 1024)); else if(s > 1024) snprintf(buffer, l, "%.2fkb", 1.0 * s / 1024); - else snprintf(buffer, l, OFF_T_FORMAT"b", s); + else snprintf(buffer, l, OFF_T_FORMAT"b", (OFF_T_FORMAT_CAST)s); } void get_auth_data(const char *srv, const char *shr, char *wg, int wglen, char *un, int unlen, char *pw, int pwlen) @@ -327,8 +329,10 @@ int smb_download_file(const char *base, const char *name, int recursive, int res offset_download = localstat.st_size - RESUME_DOWNLOAD_OFFSET; offset_check = localstat.st_size - RESUME_CHECK_OFFSET; if(verbose)printf("Trying to start resume of %s at "OFF_T_FORMAT"\n" - "At the moment "OFF_T_FORMAT" of "OFF_T_FORMAT" bytes have been retrieved\n", newpath, offset_check, - localstat.st_size, remotestat.st_size); + "At the moment "OFF_T_FORMAT" of "OFF_T_FORMAT" bytes have been retrieved\n", + newpath, (OFF_T_FORMAT_CAST)offset_check, + (OFF_T_FORMAT_CAST)localstat.st_size, + (OFF_T_FORMAT_CAST)remotestat.st_size); } if(offset_check) { @@ -336,20 +340,24 @@ int smb_download_file(const char *base, const char *name, int recursive, int res /* First, check all bytes from offset_check to offset_download */ off1 = lseek(localhandle, offset_check, SEEK_SET); if(off1 < 0) { - fprintf(stderr, "Can't seek to "OFF_T_FORMAT" in local file %s\n", offset_check, newpath); + fprintf(stderr, "Can't seek to "OFF_T_FORMAT" in local file %s\n", + (OFF_T_FORMAT_CAST)offset_check, newpath); smbc_close(remotehandle); close(localhandle); return 0; } off2 = smbc_lseek(remotehandle, offset_check, SEEK_SET); if(off2 < 0) { - fprintf(stderr, "Can't seek to "OFF_T_FORMAT" in remote file %s\n", offset_check, newpath); + fprintf(stderr, "Can't seek to "OFF_T_FORMAT" in remote file %s\n", + (OFF_T_FORMAT_CAST)offset_check, newpath); smbc_close(remotehandle); close(localhandle); return 0; } if(off1 != off2) { - fprintf(stderr, "Offset in local and remote files is different (local: "OFF_T_FORMAT", remote: "OFF_T_FORMAT")\n", off1, off2); + fprintf(stderr, "Offset in local and remote files is different (local: "OFF_T_FORMAT", remote: "OFF_T_FORMAT")\n", + (OFF_T_FORMAT_CAST)off1, + (OFF_T_FORMAT_CAST)off2); return 0; } @@ -366,7 +374,7 @@ int smb_download_file(const char *base, const char *name, int recursive, int res } if(memcmp(checkbuf[0], checkbuf[1], RESUME_CHECK_SIZE) == 0) { - if(verbose)printf("Current local and remote file appear to be the same. Starting download from offset "OFF_T_FORMAT"\n", offset_download); + 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); } else { fprintf(stderr, "Local and remote file appear to be different, not doing resume for %s\n", path); smbc_close(remotehandle); close(localhandle); @@ -386,7 +394,7 @@ int smb_download_file(const char *base, const char *name, int recursive, int res for(curpos = offset_download; curpos < remotestat.st_size; curpos+=blocksize) { ssize_t bytesread = smbc_read(remotehandle, readbuf, blocksize); if(bytesread < 0) { - fprintf(stderr, "Can't read %d bytes at offset "OFF_T_FORMAT", file %s\n", blocksize, curpos, path); + fprintf(stderr, "Can't read %u bytes at offset "OFF_T_FORMAT", file %s\n", (unsigned int)blocksize, (OFF_T_FORMAT_CAST)curpos, path); smbc_close(remotehandle); if (localhandle != STDOUT_FILENO) close(localhandle); free(readbuf); @@ -396,7 +404,7 @@ int smb_download_file(const char *base, const char *name, int recursive, int res total_bytes += bytesread; if(write(localhandle, readbuf, bytesread) < 0) { - fprintf(stderr, "Can't write %d bytes to local file %s at offset "OFF_T_FORMAT"\n", bytesread, path, curpos); + 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); free(readbuf); smbc_close(remotehandle); if (localhandle != STDOUT_FILENO) close(localhandle); -- cgit From c8f28c92a7a96e278031b85f04b4671206bf3502 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Wed, 28 Dec 2005 22:48:54 +0000 Subject: r12555: Fix more load_case_table swegfaults. Arggg. What I'd give for a global constructor... Jeremy. (This used to be commit c970d7d0a5ba225465dfb0980989b8817b17c643) --- source3/utils/smbget.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'source3/utils/smbget.c') diff --git a/source3/utils/smbget.c b/source3/utils/smbget.c index 9d44017bd3..4a2670e0c1 100644 --- a/source3/utils/smbget.c +++ b/source3/utils/smbget.c @@ -544,6 +544,8 @@ int main(int argc, const char **argv) }; poptContext pc; + load_case_tables(); + /* only read rcfile if it exists */ asprintf(&rcfile, "%s/.smbgetrc", getenv("HOME")); if(access(rcfile, F_OK) == 0) -- cgit From 8f9a26cb4082d6f74aeef02dff29fc9ee55e7257 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Sat, 26 Aug 2006 22:59:58 +0000 Subject: r17847: Dummy commit (This used to be commit cdcea36095a50a92ec1311979b8b2c572b971eaf) --- source3/utils/smbget.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/utils/smbget.c') diff --git a/source3/utils/smbget.c b/source3/utils/smbget.c index 4a2670e0c1..83677359fe 100644 --- a/source3/utils/smbget.c +++ b/source3/utils/smbget.c @@ -388,7 +388,7 @@ int smb_download_file(const char *base, const char *name, int recursive, int res offset_check = 0; } - readbuf = SMB_MALLOC(blocksize); + readbuf = (char *)SMB_MALLOC(blocksize); /* Now, download all bytes from offset_download to the end */ for(curpos = offset_download; curpos < remotestat.st_size; curpos+=blocksize) { -- cgit From 55ed1d59455566d90a03e7123fbf7a05a4bd4539 Mon Sep 17 00:00:00 2001 From: Herb Lewis Date: Tue, 19 Dec 2006 20:16:52 +0000 Subject: r20261: merge 20260 from samba_3_0_24 clean up a bunch of no previous prototype warnings (This used to be commit c60687db112405262adf26dbf267804b04074e67) --- source3/utils/smbget.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'source3/utils/smbget.c') diff --git a/source3/utils/smbget.c b/source3/utils/smbget.c index 83677359fe..4142b230f2 100644 --- a/source3/utils/smbget.c +++ b/source3/utils/smbget.c @@ -50,9 +50,9 @@ const char *username = NULL, *password = NULL, *workgroup = NULL; int nonprompt = 0, quiet = 0, dots = 0, keep_permissions = 0, verbose = 0, send_stdout = 0; int blocksize = SMB_DEFAULT_BLOCKSIZE; -int smb_download_file(const char *base, const char *name, int recursive, int resume, char *outfile); +static int smb_download_file(const char *base, const char *name, int recursive, int resume, char *outfile); -int get_num_cols(void) +static int get_num_cols(void) { #ifdef TIOCGWINSZ struct winsize ws; @@ -68,12 +68,12 @@ int get_num_cols(void) #endif } -void change_columns(int sig) +static void change_columns(int sig) { columns = get_num_cols(); } -void human_readable(off_t s, char *buffer, int l) +static void human_readable(off_t s, char *buffer, int l) { if(s > 1024 * 1024 * 1024) snprintf(buffer, l, "%.2fGb", 1.0 * s / (1024 * 1024 * 1024)); else if(s > 1024 * 1024) snprintf(buffer, l, "%.2fMb", 1.0 * s / (1024 * 1024)); @@ -81,7 +81,7 @@ void human_readable(off_t s, char *buffer, int l) else snprintf(buffer, l, OFF_T_FORMAT"b", (OFF_T_FORMAT_CAST)s); } -void get_auth_data(const char *srv, const char *shr, char *wg, int wglen, char *un, int unlen, char *pw, int pwlen) +static void get_auth_data(const char *srv, const char *shr, char *wg, int wglen, char *un, int unlen, char *pw, int pwlen) { static char hasasked = 0; char *wgtmp, *usertmp; @@ -113,7 +113,7 @@ void get_auth_data(const char *srv, const char *shr, char *wg, int wglen, char * free(wgtmp); free(usertmp); } -int smb_download_dir(const char *base, const char *name, int resume) +static int smb_download_dir(const char *base, const char *name, int resume) { char path[SMB_MAXPATHLEN]; int dirhandle; @@ -199,7 +199,7 @@ int smb_download_dir(const char *base, const char *name, int resume) return 1; } -char *print_time(long t) +static char *print_time(long t) { static char buffer[100]; int secs, mins, hours; @@ -215,7 +215,7 @@ char *print_time(long t) return buffer; } -void print_progress(const char *name, time_t start, time_t now, off_t start_pos, off_t pos, off_t total) +static void print_progress(const char *name, time_t start, time_t now, off_t start_pos, off_t pos, off_t total) { double avg = 0.0; long eta = -1; @@ -244,7 +244,7 @@ void print_progress(const char *name, time_t start, time_t now, off_t start_pos, free(filename); free(status); } -int smb_download_file(const char *base, const char *name, int recursive, int resume, char *outfile) { +static int smb_download_file(const char *base, const char *name, int recursive, int resume, char *outfile) { int remotehandle, localhandle; time_t start_time = time(NULL); const char *newpath; @@ -447,7 +447,7 @@ int smb_download_file(const char *base, const char *name, int recursive, int res return 1; } -void clean_exit(void) +static void clean_exit(void) { char bs[100]; human_readable(total_bytes, bs, sizeof(bs)); @@ -455,12 +455,12 @@ void clean_exit(void) exit(0); } -void signal_quit(int v) +static void signal_quit(int v) { clean_exit(); } -int readrcfile(const char *name, const struct poptOption long_options[]) +static int readrcfile(const char *name, const struct poptOption long_options[]) { FILE *fd = fopen(name, "r"); int lineno = 0, i; -- cgit From d824b98f80ba186030cbb70b3a1e5daf80469ecd Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Mon, 9 Jul 2007 19:25:36 +0000 Subject: r23779: Change from v2 or later to v3 or later. Jeremy. (This used to be commit 407e6e695b8366369b7c76af1ff76869b45347b3) --- source3/utils/smbget.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/utils/smbget.c') diff --git a/source3/utils/smbget.c b/source3/utils/smbget.c index 4142b230f2..aa5739ae77 100644 --- a/source3/utils/smbget.c +++ b/source3/utils/smbget.c @@ -5,7 +5,7 @@ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or + the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, -- cgit From 5e54558c6dea67b56bbfaba5698f3a434d3dffb6 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Tue, 10 Jul 2007 00:52:41 +0000 Subject: r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text (This used to be commit b0132e94fc5fef936aa766fb99a306b3628e9f07) --- source3/utils/smbget.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'source3/utils/smbget.c') diff --git a/source3/utils/smbget.c b/source3/utils/smbget.c index aa5739ae77..5848fd4057 100644 --- a/source3/utils/smbget.c +++ b/source3/utils/smbget.c @@ -14,8 +14,7 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ + along with this program. If not, see . */ #include "includes.h" #include "libsmbclient.h" -- cgit From faefb22c61568c678476b4dad36bdc5ce3afb499 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Tue, 4 Sep 2007 05:39:06 +0000 Subject: r24943: Some stackframes (This used to be commit cddb9f11d5fafcd3797cb242775c37f0c04d4f15) --- source3/utils/smbget.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'source3/utils/smbget.c') diff --git a/source3/utils/smbget.c b/source3/utils/smbget.c index 5848fd4057..ac662e6ace 100644 --- a/source3/utils/smbget.c +++ b/source3/utils/smbget.c @@ -521,6 +521,7 @@ int main(int argc, const char **argv) int c = 0; const char *file = NULL; char *rcfile = NULL; + TALLOC_CTX *frame = talloc_stackframe(); struct poptOption long_options[] = { {"guest", 'a', POPT_ARG_NONE, NULL, 'a', "Work as user guest" }, {"resume", 'r', POPT_ARG_NONE, &_resume, 0, "Automatically resume aborted files" }, @@ -597,6 +598,6 @@ int main(int argc, const char **argv) } clean_exit(); - + TALLOC_FREE(frame); return 0; } -- cgit From af2a75ba61b754f30430df9b271e99d05c2cd1b2 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Sat, 5 Jan 2008 00:51:50 -0800 Subject: Add -e to smbget. Jeremy. (This used to be commit 0475bdcf44d21bbdefb57f15d403c91c44d8d90a) --- source3/utils/smbget.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'source3/utils/smbget.c') diff --git a/source3/utils/smbget.c b/source3/utils/smbget.c index ac662e6ace..63b7f48626 100644 --- a/source3/utils/smbget.c +++ b/source3/utils/smbget.c @@ -521,9 +521,11 @@ int main(int argc, const char **argv) int c = 0; const char *file = NULL; char *rcfile = NULL; + bool smb_encrypt = false; TALLOC_CTX *frame = talloc_stackframe(); struct poptOption long_options[] = { {"guest", 'a', POPT_ARG_NONE, NULL, 'a', "Work as user guest" }, + {"encrypt", 'e', POPT_ARG_NONE, NULL, 'e', "Encrypt SMB transport (UNIX extended servers only)" }, {"resume", 'r', POPT_ARG_NONE, &_resume, 0, "Automatically resume aborted files" }, {"recursive", 'R', POPT_ARG_NONE, &_recursive, 0, "Recursively download files" }, {"username", 'u', POPT_ARG_STRING, &username, 'u', "Username to use" }, @@ -568,6 +570,9 @@ int main(int argc, const char **argv) case 'a': username = ""; password = ""; break; + case 'e': + smb_encrypt = true; + break; } } @@ -586,6 +591,13 @@ int main(int argc, const char **argv) return 1; } + if (smb_encrypt) { + SMBCCTX *smb_ctx = smbc_set_context(NULL); + smbc_option_set(smb_ctx, + CONST_DISCARD(char *, "smb_encrypt_level"), + "require"); + } + columns = get_num_cols(); total_start_time = time(NULL); -- cgit From a7af17ef80983e3d6394dd97bb5e05d70b376a3b Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 5 Mar 2008 15:20:29 +0100 Subject: Add --update option to smbget. (This used to be commit 3cfa35fca64b059b307668ca0113503679e0b1fa) --- source3/utils/smbget.c | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) (limited to 'source3/utils/smbget.c') diff --git a/source3/utils/smbget.c b/source3/utils/smbget.c index 63b7f48626..5b2149b843 100644 --- a/source3/utils/smbget.c +++ b/source3/utils/smbget.c @@ -29,7 +29,7 @@ int columns = 0; -static int _resume, _recursive, debuglevel; +static int _resume, _recursive, debuglevel, update; static char *outputfile; @@ -303,8 +303,27 @@ static int smb_download_file(const char *base, const char *name, int recursive, if(newpath[0] == '/')newpath++; - /* Open local file and, if necessary, resume */ - if(!send_stdout) { + /* Open local file according to the mode */ + if(update) { + /* if it is up-to-date, skip */ + if(stat(newpath, &localstat) == 0 && + localstat.st_mtime >= remotestat.st_mtime) { + if(verbose) + printf("%s is up-to-date, skipping\n", newpath); + smbc_close(remotehandle); + return 0; + } + /* else open it for writting and truncate if it exists */ + localhandle = open(newpath, O_CREAT | O_NONBLOCK | O_RDWR | + O_TRUNC, 0775); + if(localhandle < 0) { + fprintf(stderr, "Can't open %s : %s\n", newpath, + strerror(errno)); + smbc_close(remotehandle); + return 1; + } + /* no offset */ + } else if(!send_stdout) { localhandle = open(newpath, O_CREAT | O_NONBLOCK | O_RDWR | (!resume?O_EXCL:0), 0755); if(localhandle < 0) { fprintf(stderr, "Can't open %s: %s\n", newpath, strerror(errno)); @@ -527,6 +546,7 @@ int main(int argc, const char **argv) {"guest", 'a', POPT_ARG_NONE, NULL, 'a', "Work as user guest" }, {"encrypt", 'e', POPT_ARG_NONE, NULL, 'e', "Encrypt SMB transport (UNIX extended servers only)" }, {"resume", 'r', POPT_ARG_NONE, &_resume, 0, "Automatically resume aborted files" }, + {"update", 'U', POPT_ARG_NONE, &update, 0, "Download only when remote file is newer than local file or local file is missing"}, {"recursive", 'R', POPT_ARG_NONE, &_recursive, 0, "Recursively download files" }, {"username", 'u', POPT_ARG_STRING, &username, 'u', "Username to use" }, {"password", 'p', POPT_ARG_STRING, &password, 'p', "Password to use" }, @@ -576,6 +596,10 @@ int main(int argc, const char **argv) } } + if((send_stdout || _resume || outputfile) && update) { + fprintf(stderr, "The -o, -R or -O and -U options can not be used together.\n"); + return 1; + } if((send_stdout || outputfile) && _recursive) { fprintf(stderr, "The -o or -O and -R options can not be used together.\n"); return 1; -- cgit From d41d580c600e3228ff8fee5c16c47580f661a240 Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Wed, 5 Mar 2008 17:30:18 +0100 Subject: Fix formatting. (This used to be commit e2345ce90c3f1548f63d720c5943c8d61fdc899a) --- source3/utils/smbget.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'source3/utils/smbget.c') diff --git a/source3/utils/smbget.c b/source3/utils/smbget.c index 5b2149b843..e5185d09f0 100644 --- a/source3/utils/smbget.c +++ b/source3/utils/smbget.c @@ -313,9 +313,8 @@ static int smb_download_file(const char *base, const char *name, int recursive, smbc_close(remotehandle); return 0; } - /* else open it for writting and truncate if it exists */ - localhandle = open(newpath, O_CREAT | O_NONBLOCK | O_RDWR | - O_TRUNC, 0775); + /* else open it for writing and truncate if it exists */ + localhandle = open(newpath, O_CREAT | O_NONBLOCK | O_RDWR | O_TRUNC, 0775); if(localhandle < 0) { fprintf(stderr, "Can't open %s : %s\n", newpath, strerror(errno)); -- cgit From 5b8823e49ac6601ffe4b06495b3e7a95e2337090 Mon Sep 17 00:00:00 2001 From: Charles Date: Sun, 9 Mar 2008 13:50:56 +0100 Subject: smbget: Make global variables static. (This used to be commit c430b7831d5dc6f81cfd18ee2bf24bc3f276fe5d) --- source3/utils/smbget.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'source3/utils/smbget.c') diff --git a/source3/utils/smbget.c b/source3/utils/smbget.c index e5185d09f0..1b916c8563 100644 --- a/source3/utils/smbget.c +++ b/source3/utils/smbget.c @@ -27,14 +27,14 @@ #define OFF_T_FORMAT_CAST long #endif -int columns = 0; +static int columns = 0; static int _resume, _recursive, debuglevel, update; static char *outputfile; -time_t total_start_time = 0; -off_t total_bytes = 0; +static time_t total_start_time = 0; +static off_t total_bytes = 0; #define SMB_MAXPATHLEN MAXPATHLEN @@ -45,9 +45,9 @@ off_t total_bytes = 0; /* Number of bytes to read at once */ #define SMB_DEFAULT_BLOCKSIZE 64000 -const char *username = NULL, *password = NULL, *workgroup = NULL; -int nonprompt = 0, quiet = 0, dots = 0, keep_permissions = 0, verbose = 0, send_stdout = 0; -int blocksize = SMB_DEFAULT_BLOCKSIZE; +static const char *username = NULL, *password = NULL, *workgroup = NULL; +static int nonprompt = 0, quiet = 0, dots = 0, keep_permissions = 0, verbose = 0, send_stdout = 0; +static int blocksize = SMB_DEFAULT_BLOCKSIZE; static int smb_download_file(const char *base, const char *name, int recursive, int resume, char *outfile); -- cgit From ad7c3455ff28d3bb2ebcb9e5b1af00cc30cb6d25 Mon Sep 17 00:00:00 2001 From: Charles Date: Sun, 9 Mar 2008 19:52:29 +0100 Subject: Fix return code. (This used to be commit 98b643366044f471ad69c9e5aac06022f30742d9) --- source3/utils/smbget.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/utils/smbget.c') diff --git a/source3/utils/smbget.c b/source3/utils/smbget.c index 1b916c8563..e8fd6be6e6 100644 --- a/source3/utils/smbget.c +++ b/source3/utils/smbget.c @@ -319,7 +319,7 @@ static int smb_download_file(const char *base, const char *name, int recursive, fprintf(stderr, "Can't open %s : %s\n", newpath, strerror(errno)); smbc_close(remotehandle); - return 1; + return 0; } /* no offset */ } else if(!send_stdout) { -- cgit From 3634d987840bd3ea3e679e02500fc76bb974818f Mon Sep 17 00:00:00 2001 From: Charles Date: Tue, 11 Mar 2008 19:49:08 +0100 Subject: The attached patch removes some global variables, and make them local to main(). (This used to be commit 50cab87d3ee6d701dd8847f4e0a058f5e2d80e0f) --- source3/utils/smbget.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'source3/utils/smbget.c') diff --git a/source3/utils/smbget.c b/source3/utils/smbget.c index e8fd6be6e6..1b284c1725 100644 --- a/source3/utils/smbget.c +++ b/source3/utils/smbget.c @@ -29,7 +29,7 @@ static int columns = 0; -static int _resume, _recursive, debuglevel, update; +static int debuglevel, update; static char *outputfile; @@ -540,13 +540,14 @@ int main(int argc, const char **argv) const char *file = NULL; char *rcfile = NULL; bool smb_encrypt = false; + int resume = 0, recursive = 0; TALLOC_CTX *frame = talloc_stackframe(); struct poptOption long_options[] = { {"guest", 'a', POPT_ARG_NONE, NULL, 'a', "Work as user guest" }, {"encrypt", 'e', POPT_ARG_NONE, NULL, 'e', "Encrypt SMB transport (UNIX extended servers only)" }, - {"resume", 'r', POPT_ARG_NONE, &_resume, 0, "Automatically resume aborted files" }, + {"resume", 'r', POPT_ARG_NONE, &resume, 0, "Automatically resume aborted files" }, {"update", 'U', POPT_ARG_NONE, &update, 0, "Download only when remote file is newer than local file or local file is missing"}, - {"recursive", 'R', POPT_ARG_NONE, &_recursive, 0, "Recursively download files" }, + {"recursive", 'R', POPT_ARG_NONE, &recursive, 0, "Recursively download files" }, {"username", 'u', POPT_ARG_STRING, &username, 'u', "Username to use" }, {"password", 'p', POPT_ARG_STRING, &password, 'p', "Password to use" }, {"workgroup", 'w', POPT_ARG_STRING, &workgroup, 'w', "Workgroup to use (optional)" }, @@ -595,11 +596,11 @@ int main(int argc, const char **argv) } } - if((send_stdout || _resume || outputfile) && update) { + if((send_stdout || resume || outputfile) && update) { fprintf(stderr, "The -o, -R or -O and -U options can not be used together.\n"); return 1; } - if((send_stdout || outputfile) && _recursive) { + if((send_stdout || outputfile) && recursive) { fprintf(stderr, "The -o or -O and -R options can not be used together.\n"); return 1; } @@ -626,10 +627,10 @@ int main(int argc, const char **argv) total_start_time = time(NULL); while ( (file = poptGetArg(pc)) ) { - if (!_recursive) - return smb_download_file(file, "", _recursive, _resume, outputfile); + if (!recursive) + return smb_download_file(file, "", recursive, resume, outputfile); else - return smb_download_dir(file, "", _resume); + return smb_download_dir(file, "", resume); } clean_exit(); -- cgit From e06aa46b9fab1e107fea8f6453fb13deffa91e96 Mon Sep 17 00:00:00 2001 From: Marc VanHeyningen Date: Fri, 14 Mar 2008 14:26:28 -0800 Subject: Coverity fixes (This used to be commit 3fc85d22590550f0539215d020e4411bf5b14363) --- source3/utils/smbget.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'source3/utils/smbget.c') diff --git a/source3/utils/smbget.c b/source3/utils/smbget.c index 1b284c1725..3d4a71b71d 100644 --- a/source3/utils/smbget.c +++ b/source3/utils/smbget.c @@ -330,7 +330,12 @@ static int smb_download_file(const char *base, const char *name, int recursive, return 0; } - fstat(localhandle, &localstat); + if (fstat(localhandle, &localstat) != 0) { + fprintf(stderr, "Can't fstat %s: %s\n", newpath, strerror(errno)); + smbc_close(remotehandle); + close(localhandle); + return 0; + } start_offset = localstat.st_size; -- cgit