⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 util.c

📁 Rsync 3.0.5 source code
💻 C
📖 第 1 页 / 共 3 页
字号:
/* * Utility routines used in rsync. * * Copyright (C) 1996-2000 Andrew Tridgell * Copyright (C) 1996 Paul Mackerras * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org> * Copyright (C) 2003-2008 Wayne Davison * * 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 3 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, visit the http://fsf.org website. */#include "rsync.h"#include "ifuncs.h"extern int verbose;extern int dry_run;extern int module_id;extern int modify_window;extern int relative_paths;extern int human_readable;extern int preserve_xattrs;extern char *module_dir;extern unsigned int module_dirlen;extern mode_t orig_umask;extern char *partial_dir;extern struct filter_list_struct daemon_filter_list;int sanitize_paths = 0;char curr_dir[MAXPATHLEN];unsigned int curr_dir_len;int curr_dir_depth; /* This is only set for a sanitizing daemon. *//* Set a fd into nonblocking mode. */void set_nonblocking(int fd){	int val;	if ((val = fcntl(fd, F_GETFL)) == -1)		return;	if (!(val & NONBLOCK_FLAG)) {		val |= NONBLOCK_FLAG;		fcntl(fd, F_SETFL, val);	}}/* Set a fd into blocking mode. */void set_blocking(int fd){	int val;	if ((val = fcntl(fd, F_GETFL)) == -1)		return;	if (val & NONBLOCK_FLAG) {		val &= ~NONBLOCK_FLAG;		fcntl(fd, F_SETFL, val);	}}/** * Create a file descriptor pair - like pipe() but use socketpair if * possible (because of blocking issues on pipes). * * Always set non-blocking. */int fd_pair(int fd[2]){	int ret;#ifdef HAVE_SOCKETPAIR	ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);#else	ret = pipe(fd);#endif	if (ret == 0) {		set_nonblocking(fd[0]);		set_nonblocking(fd[1]);	}	return ret;}void print_child_argv(const char *prefix, char **cmd){	rprintf(FCLIENT, "%s ", prefix);	for (; *cmd; cmd++) {		/* Look for characters that ought to be quoted.  This		* is not a great quoting algorithm, but it's		* sufficient for a log message. */		if (strspn(*cmd, "abcdefghijklmnopqrstuvwxyz"			   "ABCDEFGHIJKLMNOPQRSTUVWXYZ"			   "0123456789"			   ",.-_=+@/") != strlen(*cmd)) {			rprintf(FCLIENT, "\"%s\" ", *cmd);		} else {			rprintf(FCLIENT, "%s ", *cmd);		}	}	rprintf(FCLIENT, "\n");}NORETURN void out_of_memory(const char *str){	rprintf(FERROR, "ERROR: out of memory in %s [%s]\n", str, who_am_i());	exit_cleanup(RERR_MALLOC);}NORETURN void overflow_exit(const char *str){	rprintf(FERROR, "ERROR: buffer overflow in %s [%s]\n", str, who_am_i());	exit_cleanup(RERR_MALLOC);}int set_modtime(const char *fname, time_t modtime, mode_t mode){#if !defined HAVE_LUTIMES || !defined HAVE_UTIMES	if (S_ISLNK(mode))		return 1;#endif	if (verbose > 2) {		rprintf(FINFO, "set modtime of %s to (%ld) %s",			fname, (long)modtime,			asctime(localtime(&modtime)));	}	if (dry_run)		return 0;	{#ifdef HAVE_UTIMES		struct timeval t[2];		t[0].tv_sec = time(NULL);		t[0].tv_usec = 0;		t[1].tv_sec = modtime;		t[1].tv_usec = 0;# ifdef HAVE_LUTIMES		if (S_ISLNK(mode)) {			if (lutimes(fname, t) < 0)				return errno == ENOSYS ? 1 : -1;			return 0;		}# endif		return utimes(fname, t);#elif defined HAVE_STRUCT_UTIMBUF		struct utimbuf tbuf;		tbuf.actime = time(NULL);		tbuf.modtime = modtime;		return utime(fname,&tbuf);#elif defined HAVE_UTIME		time_t t[2];		t[0] = time(NULL);		t[1] = modtime;		return utime(fname,t);#else#error No file-time-modification routine found!#endif	}}/* This creates a new directory with default permissions.  Since there * might be some directory-default permissions affecting this, we can't * force the permissions directly using the original umask and mkdir(). */int mkdir_defmode(char *fname){	int ret;	umask(orig_umask);	ret = do_mkdir(fname, ACCESSPERMS);	umask(0);	return ret;}/* Create any necessary directories in fname.  Any missing directories are * created with default permissions. */int create_directory_path(char *fname){	char *p;	int ret = 0;	while (*fname == '/')		fname++;	while (strncmp(fname, "./", 2) == 0)		fname += 2;	umask(orig_umask);	p = fname;	while ((p = strchr(p,'/')) != NULL) {		*p = '\0';		if (do_mkdir(fname, ACCESSPERMS) < 0 && errno != EEXIST)		    ret = -1;		*p++ = '/';	}	umask(0);	return ret;}/** * Write @p len bytes at @p ptr to descriptor @p desc, retrying if * interrupted. * * @retval len upon success * * @retval <0 write's (negative) error code * * Derived from GNU C's cccp.c. */int full_write(int desc, const char *ptr, size_t len){	int total_written;	total_written = 0;	while (len > 0) {		int written = write(desc, ptr, len);		if (written < 0)  {			if (errno == EINTR)				continue;			return written;		}		total_written += written;		ptr += written;		len -= written;	}	return total_written;}/** * Read @p len bytes at @p ptr from descriptor @p desc, retrying if * interrupted. * * @retval >0 the actual number of bytes read * * @retval 0 for EOF * * @retval <0 for an error. * * Derived from GNU C's cccp.c. */static int safe_read(int desc, char *ptr, size_t len){	int n_chars;	if (len == 0)		return len;	do {		n_chars = read(desc, ptr, len);	} while (n_chars < 0 && errno == EINTR);	return n_chars;}/* Copy a file.  If ofd < 0, copy_file unlinks and opens the "dest" file. * Otherwise, it just writes to and closes the provided file descriptor. * In either case, if --xattrs are being preserved, the dest file will * have its xattrs set from the source file. * * This is used in conjunction with the --temp-dir, --backup, and * --copy-dest options. */int copy_file(const char *source, const char *dest, int ofd,	      mode_t mode, int create_bak_dir){	int ifd;	char buf[1024 * 8];	int len;   /* Number of bytes read into `buf'. */	if ((ifd = do_open(source, O_RDONLY, 0)) < 0) {		int save_errno = errno;		rsyserr(FERROR_XFER, errno, "open %s", full_fname(source));		errno = save_errno;		return -1;	}	if (ofd < 0) {		if (robust_unlink(dest) && errno != ENOENT) {			int save_errno = errno;			rsyserr(FERROR_XFER, errno, "unlink %s", full_fname(dest));			errno = save_errno;			return -1;		}		if ((ofd = do_open(dest, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, mode)) < 0) {			int save_errno = errno ? errno : EINVAL; /* 0 paranoia */			if (create_bak_dir && errno == ENOENT && make_bak_dir(dest) == 0) {				if ((ofd = do_open(dest, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, mode)) < 0)					save_errno = errno ? errno : save_errno;				else					save_errno = 0;			}			if (save_errno) {				rsyserr(FERROR_XFER, save_errno, "open %s", full_fname(dest));				close(ifd);				errno = save_errno;				return -1;			}		}	}	while ((len = safe_read(ifd, buf, sizeof buf)) > 0) {		if (full_write(ofd, buf, len) < 0) {			int save_errno = errno;			rsyserr(FERROR_XFER, errno, "write %s", full_fname(dest));			close(ifd);			close(ofd);			errno = save_errno;			return -1;		}	}	if (len < 0) {		int save_errno = errno;		rsyserr(FERROR_XFER, errno, "read %s", full_fname(source));		close(ifd);		close(ofd);		errno = save_errno;		return -1;	}	if (close(ifd) < 0) {		rsyserr(FWARNING, errno, "close failed on %s",			full_fname(source));	}	if (close(ofd) < 0) {		int save_errno = errno;		rsyserr(FERROR_XFER, errno, "close failed on %s",			full_fname(dest));		errno = save_errno;		return -1;	}#ifdef SUPPORT_XATTRS	if (preserve_xattrs)		copy_xattrs(source, dest);#endif	return 0;}/* MAX_RENAMES should be 10**MAX_RENAMES_DIGITS */#define MAX_RENAMES_DIGITS 3#define MAX_RENAMES 1000/** * Robust unlink: some OS'es (HPUX) refuse to unlink busy files, so * rename to <path>/.rsyncNNN instead. * * Note that successive rsync runs will shuffle the filenames around a * bit as long as the file is still busy; this is because this function * does not know if the unlink call is due to a new file coming in, or * --delete trying to remove old .rsyncNNN files, hence it renames it * each time. **/int robust_unlink(const char *fname){#ifndef ETXTBSY	return do_unlink(fname);#else	static int counter = 1;	int rc, pos, start;	char path[MAXPATHLEN];	rc = do_unlink(fname);	if (rc == 0 || errno != ETXTBSY)		return rc;	if ((pos = strlcpy(path, fname, MAXPATHLEN)) >= MAXPATHLEN)		pos = MAXPATHLEN - 1;	while (pos > 0 && path[pos-1] != '/')		pos--;	pos += strlcpy(path+pos, ".rsync", MAXPATHLEN-pos);	if (pos > (MAXPATHLEN-MAX_RENAMES_DIGITS-1)) {		errno = ETXTBSY;		return -1;	}	/* start where the last one left off to reduce chance of clashes */	start = counter;	do {		snprintf(&path[pos], MAX_RENAMES_DIGITS+1, "%03d", counter);		if (++counter >= MAX_RENAMES)			counter = 1;	} while ((rc = access(path, 0)) == 0 && counter != start);	if (verbose > 0) {		rprintf(FWARNING, "renaming %s to %s because of text busy\n",			fname, path);	}	/* maybe we should return rename()'s exit status? Nah. */	if (do_rename(fname, path) != 0) {		errno = ETXTBSY;		return -1;	}	return 0;#endif}/* Returns 0 on successful rename, 1 if we successfully copied the file * across filesystems, -2 if copy_file() failed, and -1 on other errors. * If partialptr is not NULL and we need to do a copy, copy the file into * the active partial-dir instead of over the destination file. */int robust_rename(const char *from, const char *to, const char *partialptr,		  int mode){	int tries = 4;	while (tries--) {		if (do_rename(from, to) == 0)			return 0;		switch (errno) {#ifdef ETXTBSY		case ETXTBSY:			if (robust_unlink(to) != 0) {				errno = ETXTBSY;				return -1;			}			errno = ETXTBSY;			break;#endif		case EXDEV:			if (partialptr) {				if (!handle_partial_dir(partialptr,PDIR_CREATE))					return -2;				to = partialptr;			}			if (copy_file(from, to, -1, mode, 0) != 0)				return -2;			do_unlink(from);			return 1;		default:			return -1;		}	}	return -1;}static pid_t all_pids[10];static int num_pids;/** Fork and record the pid of the child. **/pid_t do_fork(void){	pid_t newpid = fork();	if (newpid != 0  &&  newpid != -1) {		all_pids[num_pids++] = newpid;	}	return newpid;}/** * Kill all children. * * @todo It would be kind of nice to make sure that they are actually * all our children before we kill them, because their pids may have * been recycled by some other process.  Perhaps when we wait for a * child, we should remove it from this array.  Alternatively we could * perhaps use process groups, but I think that would not work on * ancient Unix versions that don't support them. **/void kill_all(int sig){	int i;	for (i = 0; i < num_pids; i++) {		/* Let's just be a little careful where we		 * point that gun, hey?  See kill(2) for the		 * magic caused by negative values. */		pid_t p = all_pids[i];		if (p == getpid())			continue;		if (p <= 0)			continue;		kill(p, sig);	}}/** Turn a user name into a uid */int name_to_uid(const char *name, uid_t *uid_p){	struct passwd *pass;	if (!name || !*name)		return 0;	if (!(pass = getpwnam(name)))		return 0;	*uid_p = pass->pw_uid;	return 1;}/** Turn a group name into a gid */int name_to_gid(const char *name, gid_t *gid_p){	struct group *grp;	if (!name || !*name)		return 0;	if (!(grp = getgrnam(name)))		return 0;	*gid_p = grp->gr_gid;	return 1;}/** Lock a byte range in a open file */int lock_range(int fd, int offset, int len){	struct flock lock;	lock.l_type = F_WRLCK;	lock.l_whence = SEEK_SET;	lock.l_start = offset;	lock.l_len = len;	lock.l_pid = 0;	return fcntl(fd,F_SETLK,&lock) == 0;}#define ENSURE_MEMSPACE(buf, type, sz, req) \	if ((req) > sz && !(buf = realloc_array(buf, type, sz = MAX(sz * 2, req)))) \		out_of_memory("glob_expand")static inline void call_glob_match(const char *name, int len, int from_glob,				   char *arg, int abpos, int fbpos);static struct glob_data {	char *arg_buf, *filt_buf, **argv;	int absize, fbsize, maxargs, argc;} glob;static void glob_match(char *arg, int abpos, int fbpos){	int len;	char *slash;	while (*arg == '.' && arg[1] == '/') {		if (fbpos < 0) {			ENSURE_MEMSPACE(glob.filt_buf, char, glob.fbsize, glob.absize);			memcpy(glob.filt_buf, glob.arg_buf, abpos + 1);			fbpos = abpos;		}		ENSURE_MEMSPACE(glob.arg_buf, char, glob.absize, abpos + 3);		glob.arg_buf[abpos++] = *arg++;		glob.arg_buf[abpos++] = *arg++;		glob.arg_buf[abpos] = '\0';	}	if ((slash = strchr(arg, '/')) != NULL) {		*slash = '\0';		len = slash - arg;	} else

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -