version 1.47, 2007/09/05 08:58:34 |
version 1.48, 2009/03/27 07:31:27 |
|
|
files_match = 1; |
files_match = 1; |
(void)unlink(tempfile); |
(void)unlink(tempfile); |
} |
} |
(void) close(temp_fd); |
|
} |
} |
|
(void)close(to_fd); |
|
to_fd = temp_fd; |
} |
} |
|
|
/* |
/* |
* Move the new file into place if doing a safe copy |
|
* and the files are different (or just not compared). |
|
*/ |
|
if (safecopy && !files_match) { |
|
/* Try to turn off the immutable bits. */ |
|
if (to_sb.st_flags & (NOCHANGEBITS)) |
|
(void)chflags(to_name, to_sb.st_flags & ~(NOCHANGEBITS)); |
|
if (dobackup) { |
|
char backup[MAXPATHLEN]; |
|
(void)snprintf(backup, MAXPATHLEN, "%s%s", to_name, |
|
suffix); |
|
/* It is ok for the target file not to exist. */ |
|
if (rename(to_name, backup) < 0 && errno != ENOENT) { |
|
serrno = errno; |
|
unlink(tempfile); |
|
errx(EX_OSERR, "rename: %s to %s: %s", to_name, |
|
backup, strerror(serrno)); |
|
} |
|
} |
|
if (rename(tempfile, to_name) < 0 ) { |
|
serrno = errno; |
|
unlink(tempfile); |
|
errx(EX_OSERR, "rename: %s to %s: %s", tempfile, |
|
to_name, strerror(serrno)); |
|
} |
|
|
|
/* Re-open to_fd so we aren't hosed by the rename(2). */ |
|
(void) close(to_fd); |
|
if ((to_fd = open(to_name, O_RDONLY, 0)) < 0) |
|
err(EX_OSERR, "%s", to_name); |
|
} |
|
|
|
/* |
|
* Preserve the timestamp of the source file if necessary. |
* Preserve the timestamp of the source file if necessary. |
*/ |
*/ |
if (dopreserve && !files_match) { |
if (dopreserve && !files_match) { |
utb.actime = from_sb.st_atime; |
utb.actime = from_sb.st_atime; |
utb.modtime = from_sb.st_mtime; |
utb.modtime = from_sb.st_mtime; |
(void)utime(to_name, &utb); |
(void)utime(safecopy ? tempfile : to_name, &utb); |
} |
} |
|
|
/* |
/* |
* Set owner, group, mode for target; do the chown first, |
* Set owner, group, mode for target; do the chown first, |
* chown may lose the setuid bits. |
* chown may lose the setuid bits. |
*/ |
*/ |
if ((gid != (gid_t)-1 || uid != (uid_t)-1) && fchown(to_fd, uid, gid)) { |
if ((gid != (gid_t)-1 || uid != (uid_t)-1) && |
|
fchown(to_fd, uid, gid)) { |
serrno = errno; |
serrno = errno; |
(void)unlink(to_name); |
(void)unlink(safecopy ? tempfile : to_name); |
errx(EX_OSERR, "%s: chown/chgrp: %s", to_name, strerror(serrno)); |
errx(EX_OSERR, "%s: chown/chgrp: %s", |
|
safecopy ? tempfile : to_name, strerror(serrno)); |
} |
} |
if (fchmod(to_fd, mode)) { |
if (fchmod(to_fd, mode)) { |
serrno = errno; |
serrno = errno; |
(void)unlink(to_name); |
(void)unlink(safecopy ? tempfile : to_name); |
errx(EX_OSERR, "%s: chmod: %s", to_name, strerror(serrno)); |
errx(EX_OSERR, "%s: chmod: %s", safecopy ? tempfile : to_name, |
|
strerror(serrno)); |
} |
} |
|
|
/* |
/* |
|
|
if (fchflags(to_fd, |
if (fchflags(to_fd, |
flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) { |
flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) { |
if (errno != EOPNOTSUPP || (from_sb.st_flags & ~UF_NODUMP) != 0) |
if (errno != EOPNOTSUPP || (from_sb.st_flags & ~UF_NODUMP) != 0) |
warnx("%s: chflags: %s", to_name, strerror(errno)); |
warnx("%s: chflags: %s", |
|
safecopy ? tempfile :to_name, strerror(errno)); |
} |
} |
|
|
(void)close(to_fd); |
(void)close(to_fd); |
if (!devnull) |
if (!devnull) |
(void)close(from_fd); |
(void)close(from_fd); |
|
|
|
/* |
|
* Move the new file into place if doing a safe copy |
|
* and the files are different (or just not compared). |
|
*/ |
|
if (safecopy && !files_match) { |
|
/* Try to turn off the immutable bits. */ |
|
if (to_sb.st_flags & (NOCHANGEBITS)) |
|
(void)chflags(to_name, to_sb.st_flags & ~(NOCHANGEBITS)); |
|
if (dobackup) { |
|
char backup[MAXPATHLEN]; |
|
(void)snprintf(backup, MAXPATHLEN, "%s%s", to_name, |
|
suffix); |
|
/* It is ok for the target file not to exist. */ |
|
if (rename(to_name, backup) < 0 && errno != ENOENT) { |
|
serrno = errno; |
|
unlink(tempfile); |
|
errx(EX_OSERR, "rename: %s to %s: %s", to_name, |
|
backup, strerror(serrno)); |
|
} |
|
} |
|
if (rename(tempfile, to_name) < 0 ) { |
|
serrno = errno; |
|
unlink(tempfile); |
|
errx(EX_OSERR, "rename: %s to %s: %s", tempfile, |
|
to_name, strerror(serrno)); |
|
} |
|
} |
} |
} |
|
|
/* |
/* |