[SCM] Samba Shared Repository - branch master updated

Andreas Schneider asn at samba.org
Mon Jan 25 12:34:03 UTC 2016


The branch, master has been updated
       via  b7da062 s3:utils/smbget make use of bool for flags
       via  76f1679 s3:utils/smbget use C99 format identifiers
       via  4d033ba s3:utils/smbget code format
       via  ec802d2 s3:utils/smbget fix recursive download
      from  c8a5ab9 WHATSNEW: CTDB_NATGW_SLAVE_ONLY is no longer used

https://git.samba.org/?p=samba.git;a=shortlog;h=master


- Log -----------------------------------------------------------------
commit b7da062cba6303cd84f947a312404cffde7fa3c1
Author: Christian Ambach <ambi at samba.org>
Date:   Sun Jan 24 11:57:01 2016 +0100

    s3:utils/smbget make use of bool for flags
    
    convert flags stored as int to bool
    
    Signed-off-by: Christian Ambach <ambi at samba.org>
    Reviewed-by: Andreas Schneider <asn at samba.org>
    
    Autobuild-User(master): Andreas Schneider <asn at cryptomilk.org>
    Autobuild-Date(master): Mon Jan 25 13:33:12 CET 2016 on sn-devel-144

commit 76f16796ccf378c2d0d6a1a46ed127bf72ad0e4b
Author: Christian Ambach <ambi at samba.org>
Date:   Sun Jan 24 11:45:59 2016 +0100

    s3:utils/smbget use C99 format identifiers
    
    for ssize_t and off_t variables
    
    Signed-off-by: Christian Ambach <ambi at samba.org>
    Reviewed-by: Andreas Schneider <asn at samba.org>

commit 4d033bac766e5c7bae09447e6b3a1f23b03af998
Author: Christian Ambach <ambi at samba.org>
Date:   Sun Jan 24 11:17:20 2016 +0100

    s3:utils/smbget code format
    
    adopt the code to latest README.Coding standards
    e.g. curly braces everywhere, blanks before braces,
    obey 80 character limit (except for the popt definitions)
    
    Signed-off-by: Christian Ambach <ambi at samba.org>
    Reviewed-by: Andreas Schneider <asn at samba.org>

commit ec802d27ce4dc6dd9b5e5ebd6992f90364d855a2
Author: Christian Ambach <ambi at samba.org>
Date:   Wed Dec 30 21:25:13 2015 +0100

    s3:utils/smbget fix recursive download
    
    get_auth_data is called multiple times (once for the directory listing and then
    for every file to be downloaded). Save the obtained values across multiple calls
    to make smbclient use the correct username for each download.
    
    Bug: https://bugzilla.samba.org/show_bug.cgi?id=6482
    Signed-off-by: Christian Ambach <ambi at samba.org>
    Reviewed-by: Andreas Schneider <asn at samba.org>

-----------------------------------------------------------------------

Summary of changes:
 source3/utils/smbget.c | 614 +++++++++++++++++++++++++++++++------------------
 1 file changed, 389 insertions(+), 225 deletions(-)


Changeset truncated at 500 lines:

diff --git a/source3/utils/smbget.c b/source3/utils/smbget.c
index b3ce743..f596a8c 100644
--- a/source3/utils/smbget.c
+++ b/source3/utils/smbget.c
@@ -1,6 +1,6 @@
 /*
-   smbget: a wget-like utility with support for recursive downloading and 
-   	smb:// urls
+   smbget: a wget-like utility with support for recursive downloading of
+	smb:// urls
    Copyright (C) 2003-2004 Jelmer Vernooij <jelmer at samba.org>
 
    This program is free software; you can redistribute it and/or modify
@@ -21,17 +21,9 @@
 #include "popt_common.h"
 #include "libsmbclient.h"
 
-#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
-
 static int columns = 0;
 
-static int debuglevel, update;
+static int debuglevel;
 static char *outputfile;
 
 
@@ -40,32 +32,40 @@ static 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 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
+#define SMB_DEFAULT_BLOCKSIZE 	64000
 
 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 bool nonprompt = false, quiet = false, dots = false,
+	    keep_permissions = false, verbose = false, send_stdout = false,
+	    update = false;
+static unsigned int blocksize = SMB_DEFAULT_BLOCKSIZE;
 
-static int smb_download_file(const char *base, const char *name, int recursive,
-			     int resume, int toplevel, char *outfile);
+static bool smb_download_file(const char *base, const char *name,
+			      bool recursive, bool resume, bool toplevel,
+			      char *outfile);
 
 static int get_num_cols(void)
 {
 #ifdef TIOCGWINSZ
 	struct winsize ws;
-	if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0) {
+	if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0) {
 		return 0;
 	}
 	return ws.ws_col;
 #else
 #warning No support for TIOCGWINSZ
 	char *cols = getenv("COLUMNS");
-	if(!cols) return 0;
+	if (!cols) {
+		return 0;
+	}
 	return atoi(cols);
 #endif
 }
@@ -84,45 +84,70 @@ static void human_readable(off_t s, char *buffer, int l)
 	} else if (s > 1024) {
 		snprintf(buffer, l, "%.2fkB", 1.0 * s / 1024);
 	} else {
-		snprintf(buffer, l, OFF_T_FORMAT"b", (OFF_T_FORMAT_CAST)s);
+		snprintf(buffer, l, "%jdb", (intmax_t)s);
 	}
 }
 
-static 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;
+	static bool hasasked = false;
+	static char *savedwg;
+	static char *savedun;
+	static char *savedpw;
 	char tmp[128];
 
-	if(hasasked) return;
-	hasasked = 1;
+	if (hasasked) {
+		strncpy(wg, savedwg, wglen - 1);
+		strncpy(un, savedun, unlen - 1);
+		strncpy(pw, savedpw, pwlen - 1);
+		return;
+	}
+	hasasked = true;
 
-	if(!nonprompt && !username) {
+	if (!nonprompt && !username) {
 		printf("Username for %s at %s [guest] ", shr, srv);
 		if (fgets(tmp, sizeof(tmp), stdin) == NULL) {
 			return;
 		}
-		if ((strlen(tmp) > 0) && (tmp[strlen(tmp)-1] == '\n')) {
-			tmp[strlen(tmp)-1] = '\0';
+		if ((strlen(tmp) > 0) && (tmp[strlen(tmp) - 1] == '\n')) {
+			tmp[strlen(tmp) - 1] = '\0';
 		}
-		strncpy(un, tmp, unlen-1);
-	} else if(username) strncpy(un, username, unlen-1);
+		strncpy(un, tmp, unlen - 1);
+	} else if (username) {
+		strncpy(un, username, unlen - 1);
+	}
 
-	if(!nonprompt && !password) {
+	if (!nonprompt && !password) {
 		char *prompt;
-		if (asprintf(&prompt, "Password for %s at %s: ", shr, srv) == -1) {
+		if (asprintf(&prompt, "Password for %s at %s: ", shr, srv) ==
+		    -1) {
 			return;
 		}
-		(void) samba_getpass(prompt, pw, pwlen, false, false);
+		(void)samba_getpass(prompt, pw, pwlen, false, false);
 		free(prompt);
-	} else if(password) strncpy(pw, password, pwlen-1);
+	} else if (password) {
+		strncpy(pw, password, pwlen - 1);
+	}
 
-	if(workgroup)strncpy(wg, workgroup, wglen-1);
+	if (workgroup) {
+		strncpy(wg, workgroup, wglen - 1);
+	}
 
-	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);
+	/* save the values found for later */
+	savedwg = SMB_STRDUP(wg);
+	savedun = SMB_STRDUP(un);
+	savedpw = SMB_STRDUP(pw);
+
+	if (!quiet) {
+		char *wgtmp, *usertmp;
+		wgtmp = SMB_STRNDUP(wg, wglen);
+		usertmp = SMB_STRNDUP(un, unlen);
+		printf("Using workgroup %s, %s%s\n", wgtmp,
+		       *usertmp ? "user " : "guest user", usertmp);
+		free(wgtmp);
+		free(usertmp);
+	}
 }
 
 /* Return 1 on error, 0 on success. */
@@ -137,32 +162,40 @@ static int smb_download_dir(const char *base, const char *name, int resume)
 	struct stat remotestat;
 	int ret = 0;
 
-	snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base, (base[0] && name[0] && name[0] != '/' && base[strlen(base)-1] != '/')?"/":"", name);
+	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 (dirhandle < 1) {
 		if (errno == ENOTDIR) {
-			return smb_download_file(base, name, 1, resume,
-						 0, NULL);
+			return smb_download_file(base, name, true, resume,
+						 false, NULL);
 		}
-		fprintf(stderr, "Can't open directory %s: %s\n", path, strerror(errno));
+		fprintf(stderr, "Can't open directory %s: %s\n", path,
+			strerror(errno));
 		return 1;
 	}
 
-	while(*relname == '/')relname++;
+	while (*relname == '/') {
+		relname++;
+	}
 	mkdir(relname, 0755);
 
 	tmpname = SMB_STRDUP(name);
 
-	while((dirent = smbc_readdir(dirhandle))) {
+	while ((dirent = smbc_readdir(dirhandle))) {
 		char *newname;
-		if(!strcmp(dirent->name, ".") || !strcmp(dirent->name, ".."))continue;
+		if (!strcmp(dirent->name, ".") || !strcmp(dirent->name, "..")) {
+			continue;
+		}
 		if (asprintf(&newname, "%s/%s", tmpname, dirent->name) == -1) {
 			free(tmpname);
 			return 1;
 		}
-		switch(dirent->smbc_type) {
+		switch (dirent->smbc_type) {
 		case SMBC_DIR:
 			ret = smb_download_dir(base, newname, resume);
 			break;
@@ -176,8 +209,8 @@ static int smb_download_dir(const char *base, const char *name, int resume)
 			break;
 
 		case SMBC_FILE:
-			ret = smb_download_file(base, newname, 1, resume, 0,
-						NULL);
+			ret = smb_download_file(base, newname, true, resume,
+						false, NULL);
 			break;
 
 		case SMBC_FILE_SHARE:
@@ -185,35 +218,48 @@ static int smb_download_dir(const char *base, const char *name, int resume)
 			break;
 
 		case SMBC_PRINTER_SHARE:
-			if(!quiet)printf("Ignoring printer share %s\n", dirent->name);
+			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);
+			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);
+			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);
+			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);
+	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 1;
 		}
 
-		if(chmod(relname, remotestat.st_mode) < 0) {
-			fprintf(stderr, "Unable to change mode of local dir %s to %o\n", relname,
-				(unsigned int)remotestat.st_mode);
+		if (chmod(relname, remotestat.st_mode) < 0) {
+			fprintf(stderr,
+				"Unable to change mode of local dir %s to %o\n",
+				relname, (unsigned int)remotestat.st_mode);
 			smbc_closedir(dirhandle);
 			return 1;
 		}
@@ -227,7 +273,7 @@ static char *print_time(long t)
 {
 	static char buffer[100];
 	int secs, mins, hours;
-	if(t < -1) {
+	if (t < -1) {
 		strncpy(buffer, "Unknown", sizeof(buffer));
 		return buffer;
 	}
@@ -235,51 +281,64 @@ static char *print_time(long t)
 	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);
+	snprintf(buffer, sizeof(buffer) - 1, "%02d:%02d:%02d", hours, mins,
+		 secs);
 	return buffer;
 }
 
-static 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; 
+	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);
+	if (now - start) {
+		avg = 1.0 * (pos - start_pos) / (now - start);
+	}
 	eta = (total - pos) / avg;
-	if(total)prcnt = 100.0 * pos / total;
+	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));
+	len = asprintf(&status, "%s of %s (%.2f%%) at %s/s ETA: %s", hpos,
+		       htotal, prcnt, havg, print_time(eta));
 	if (len == -1) {
 		return;
 	}
 
-	if(columns) {
-		int required = strlen(name), available = columns - len - strlen("[] ");
-		if(required > available) {
-			if (asprintf(&filename, "...%s", name + required - available + 3) == -1) {
+	if (columns) {
+		int required = strlen(name),
+		    available = columns - len - strlen("[] ");
+		if (required > available) {
+			if (asprintf(&filename, "...%s",
+				     name + required - available + 3) == -1) {
 				return;
 			}
 		} else {
 			filename = SMB_STRNDUP(name, available);
 		}
-	} else filename = SMB_STRDUP(name);
+	} else {
+		filename = SMB_STRDUP(name);
+	}
 
 	fprintf(stderr, "\r[%s] %s", filename, status);
 
-	free(filename); free(status);
+	free(filename);
+	free(status);
 }
 
-/* Return 1 on error, 0 on success. */
+/* Return false on error, true on success. */
 
-static int smb_download_file(const char *base, const char *name, int recursive,
-			     int resume, int toplevel, char *outfile)
+static bool smb_download_file(const char *base, const char *name,
+			      bool recursive, bool resume, bool toplevel,
+			      char *outfile)
 {
 	int remotehandle, localhandle;
 	time_t start_time = time_mono(NULL);
@@ -287,158 +346,221 @@ static int smb_download_file(const char *base, const char *name, int recursive,
 	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;
+	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);
+	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 1;
+	if (remotehandle < 0) {
+		switch (errno) {
+		case EISDIR:
+			if (!recursive) {
+				fprintf(stderr,
+					"%s is a directory. Specify -R "
+					"to download recursively\n",
+					path);
+				return false;
 			}
 			return smb_download_dir(base, name, resume);
 
 		case ENOENT:
-			fprintf(stderr, "%s can't be found on the remote server\n", path);
-			return 1;
+			fprintf(stderr,
+				"%s can't be found on the remote server\n",
+				path);
+			return false;
 
 		case ENOMEM:
 			fprintf(stderr, "Not enough memory\n");
-			return 1;
+			return false;
 
 		case ENODEV:
-			fprintf(stderr, "The share name used in %s does not exist\n", path);
-			return 1;
+			fprintf(stderr,
+				"The share name used in %s does not exist\n",
+				path);
+			return false;
 
 		case EACCES:
-			fprintf(stderr, "You don't have enough permissions to access %s\n", path);
-			return 1;
+			fprintf(stderr, "You don't have enough permissions "
+				"to access %s\n",
+				path);
+			return false;
 
 		default:
 			perror("smbc_open");
-			return 1;
+			return false;
 		}
-	} 
+	}
 
-	if(smbc_fstat(remotehandle, &remotestat) < 0) {
+	if (smbc_fstat(remotehandle, &remotestat) < 0) {
 		fprintf(stderr, "Can't stat %s: %s\n", path, strerror(errno));
-		return 1;
+		return false;
 	}
 
-	if(outfile) newpath = outfile;
-	else if(!name[0]) {
+	if (outfile) {
+		newpath = outfile;
+	} else if (!name[0]) {
 		newpath = strrchr(base, '/');
-		if(newpath)newpath++; else newpath = base;
-	} else newpath = name;
+		if (newpath) {
+			newpath++;
+		} else {
+			newpath = base;
+		}
+	} else {
+		newpath = name;
+	}
 
 	if (!toplevel && (newpath[0] == '/')) {
 		newpath++;
 	}
 
 	/* Open local file according to the mode */
-	if(update) {
+	if (update) {
 		/* if it is up-to-date, skip */
-		if(stat(newpath, &localstat) == 0 &&
-				localstat.st_mtime >= remotestat.st_mtime) {
-			if(verbose)
+		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;
+			return true;
 		}


-- 
Samba Shared Repository



More information about the samba-cvs mailing list