2012-01-30 22:41:33 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
2014-07-21 16:27:20 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <limits.h>
|
2012-01-30 22:41:33 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
2014-07-09 21:28:43 +00:00
|
|
|
#include <sys/types.h>
|
2014-07-21 16:27:20 +00:00
|
|
|
#include <unistd.h>
|
2014-07-09 21:28:43 +00:00
|
|
|
#include <utime.h>
|
2013-03-05 20:46:48 +00:00
|
|
|
|
2012-01-30 22:41:33 +00:00
|
|
|
#include "../fs.h"
|
|
|
|
#include "../util.h"
|
|
|
|
|
2015-03-19 16:45:30 +00:00
|
|
|
int cp_aflag = 0;
|
|
|
|
int cp_fflag = 0;
|
|
|
|
int cp_pflag = 0;
|
|
|
|
int cp_rflag = 0;
|
|
|
|
int cp_vflag = 0;
|
2014-10-02 22:46:04 +00:00
|
|
|
int cp_status = 0;
|
cp: Default to -P when -R is specified
POSIX only specifies the -H, -L, and -P options for use with -R, and
the default is left to the implementation. Without -R, symlinks must
be followed.
Most implementations use -P as the default with -R, which makes sense
to me as default behavior (the source and destination trees are the same).
Since we use the same code for -R and without it, and we allow -H, -L,
and -P without -R, set the default based on the presence of -R. Without
it, use -L as before, as required by POSIX, and with it, use -P to match
the behavior of other implementations.
2019-12-22 05:08:36 +00:00
|
|
|
int cp_follow;
|
2012-01-30 22:41:33 +00:00
|
|
|
|
|
|
|
int
|
2015-03-02 20:43:56 +00:00
|
|
|
cp(const char *s1, const char *s2, int depth)
|
2012-01-30 22:41:33 +00:00
|
|
|
{
|
2015-03-19 16:45:30 +00:00
|
|
|
DIR *dp;
|
2019-11-02 03:43:32 +00:00
|
|
|
int f1, f2, flags = 0;
|
2012-01-30 22:41:33 +00:00
|
|
|
struct dirent *d;
|
|
|
|
struct stat st;
|
2015-05-16 04:57:25 +00:00
|
|
|
struct timespec times[2];
|
2015-03-19 16:45:30 +00:00
|
|
|
ssize_t r;
|
2019-11-02 03:43:32 +00:00
|
|
|
char target[PATH_MAX], ns1[PATH_MAX], ns2[PATH_MAX];
|
2012-01-30 22:41:33 +00:00
|
|
|
|
2019-11-02 03:43:32 +00:00
|
|
|
if (cp_follow == 'P' || (cp_follow == 'H' && depth))
|
|
|
|
flags |= AT_SYMLINK_NOFOLLOW;
|
2014-07-21 16:27:20 +00:00
|
|
|
|
2019-11-02 03:43:32 +00:00
|
|
|
if (fstatat(AT_FDCWD, s1, &st, flags) < 0) {
|
|
|
|
weprintf("stat %s:", s1);
|
2014-11-19 15:08:57 +00:00
|
|
|
cp_status = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2013-03-05 20:46:48 +00:00
|
|
|
|
2015-03-19 16:45:30 +00:00
|
|
|
if (cp_vflag)
|
|
|
|
printf("%s -> %s\n", s1, s2);
|
|
|
|
|
2014-12-08 03:25:24 +00:00
|
|
|
if (S_ISLNK(st.st_mode)) {
|
2015-03-19 16:45:30 +00:00
|
|
|
if ((r = readlink(s1, target, sizeof(target) - 1)) >= 0) {
|
|
|
|
target[r] = '\0';
|
|
|
|
if (cp_fflag && unlink(s2) < 0 && errno != ENOENT) {
|
|
|
|
weprintf("unlink %s:", s2);
|
|
|
|
cp_status = 1;
|
|
|
|
return 0;
|
|
|
|
} else if (symlink(target, s2) < 0) {
|
|
|
|
weprintf("symlink %s -> %s:", s2, target);
|
2014-11-19 15:08:57 +00:00
|
|
|
cp_status = 1;
|
|
|
|
return 0;
|
2014-07-09 21:28:43 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-19 16:45:30 +00:00
|
|
|
} else if (S_ISDIR(st.st_mode)) {
|
|
|
|
if (!cp_rflag) {
|
|
|
|
weprintf("%s is a directory\n", s1);
|
|
|
|
cp_status = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!(dp = opendir(s1))) {
|
|
|
|
weprintf("opendir %s:", s1);
|
|
|
|
cp_status = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (mkdir(s2, st.st_mode) < 0 && errno != EEXIST) {
|
|
|
|
weprintf("mkdir %s:", s2);
|
|
|
|
cp_status = 1;
|
2017-05-07 11:50:26 +00:00
|
|
|
closedir(dp);
|
2015-03-19 16:45:30 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2014-11-19 15:08:57 +00:00
|
|
|
|
2015-03-19 16:45:30 +00:00
|
|
|
while ((d = readdir(dp))) {
|
|
|
|
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
|
|
|
|
continue;
|
2013-03-05 20:46:48 +00:00
|
|
|
|
2015-03-19 16:45:30 +00:00
|
|
|
estrlcpy(ns1, s1, sizeof(ns1));
|
2015-09-06 20:33:41 +00:00
|
|
|
if (s1[strlen(s1) - 1] != '/')
|
2015-03-19 16:45:30 +00:00
|
|
|
estrlcat(ns1, "/", sizeof(ns1));
|
|
|
|
estrlcat(ns1, d->d_name, sizeof(ns1));
|
2014-07-09 21:28:43 +00:00
|
|
|
|
2015-03-19 16:45:30 +00:00
|
|
|
estrlcpy(ns2, s2, sizeof(ns2));
|
2015-09-06 20:33:41 +00:00
|
|
|
if (s2[strlen(s2) - 1] != '/')
|
2015-03-19 16:45:30 +00:00
|
|
|
estrlcat(ns2, "/", sizeof(ns2));
|
|
|
|
estrlcat(ns2, d->d_name, sizeof(ns2));
|
2014-07-09 21:28:43 +00:00
|
|
|
|
2015-03-19 16:45:30 +00:00
|
|
|
fnck(ns1, ns2, cp, depth + 1);
|
2014-07-09 21:28:43 +00:00
|
|
|
}
|
2014-07-21 16:27:20 +00:00
|
|
|
|
2015-03-19 16:45:30 +00:00
|
|
|
closedir(dp);
|
|
|
|
} else if (cp_aflag && (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode) ||
|
|
|
|
S_ISSOCK(st.st_mode) || S_ISFIFO(st.st_mode))) {
|
|
|
|
if (cp_fflag && unlink(s2) < 0 && errno != ENOENT) {
|
|
|
|
weprintf("unlink %s:", s2);
|
|
|
|
cp_status = 1;
|
|
|
|
return 0;
|
|
|
|
} else if (mknod(s2, st.st_mode, st.st_rdev) < 0) {
|
|
|
|
weprintf("mknod %s:", s2);
|
|
|
|
cp_status = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
2017-07-03 21:58:49 +00:00
|
|
|
if ((f1 = open(s1, O_RDONLY)) < 0) {
|
|
|
|
weprintf("open %s:", s1);
|
2015-03-19 16:45:30 +00:00
|
|
|
cp_status = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2017-07-03 21:58:49 +00:00
|
|
|
if ((f2 = creat(s2, st.st_mode)) < 0 && cp_fflag) {
|
|
|
|
if (unlink(s2) < 0 && errno != ENOENT) {
|
|
|
|
weprintf("unlink %s:", s2);
|
2014-10-02 22:46:04 +00:00
|
|
|
cp_status = 1;
|
2017-07-03 21:58:49 +00:00
|
|
|
close(f1);
|
2014-07-21 16:27:20 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2017-07-03 21:58:49 +00:00
|
|
|
f2 = creat(s2, st.st_mode);
|
2014-07-21 16:27:20 +00:00
|
|
|
}
|
2017-07-03 21:58:49 +00:00
|
|
|
if (f2 < 0) {
|
|
|
|
weprintf("creat %s:", s2);
|
2015-03-19 16:45:30 +00:00
|
|
|
cp_status = 1;
|
2017-07-03 21:58:49 +00:00
|
|
|
close(f1);
|
2015-03-19 16:45:30 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2017-07-03 21:58:49 +00:00
|
|
|
if (concat(f1, s1, f2, s2) < 0) {
|
2014-10-02 22:46:04 +00:00
|
|
|
cp_status = 1;
|
2017-07-03 21:58:49 +00:00
|
|
|
close(f1);
|
|
|
|
close(f2);
|
2014-08-02 14:40:04 +00:00
|
|
|
return 0;
|
2014-05-05 14:02:16 +00:00
|
|
|
}
|
2017-07-03 21:58:49 +00:00
|
|
|
|
|
|
|
close(f1);
|
|
|
|
close(f2);
|
2014-05-05 13:58:14 +00:00
|
|
|
}
|
2013-03-05 20:46:48 +00:00
|
|
|
|
2014-11-13 20:24:47 +00:00
|
|
|
if (cp_aflag || cp_pflag) {
|
2016-12-27 13:49:57 +00:00
|
|
|
/* atime and mtime */
|
|
|
|
times[0] = st.st_atim;
|
|
|
|
times[1] = st.st_mtim;
|
|
|
|
if (utimensat(AT_FDCWD, s2, times, AT_SYMLINK_NOFOLLOW) < 0) {
|
|
|
|
weprintf("utimensat %s:", s2);
|
|
|
|
cp_status = 1;
|
|
|
|
}
|
|
|
|
|
2017-07-03 21:58:50 +00:00
|
|
|
/* owner and mode */
|
2015-03-19 16:45:30 +00:00
|
|
|
if (!S_ISLNK(st.st_mode)) {
|
|
|
|
if (chown(s2, st.st_uid, st.st_gid) < 0) {
|
|
|
|
weprintf("chown %s:", s2);
|
|
|
|
cp_status = 1;
|
2017-07-03 21:58:50 +00:00
|
|
|
st.st_mode &= ~(S_ISUID | S_ISGID);
|
|
|
|
}
|
|
|
|
if (chmod(s2, st.st_mode) < 0) {
|
|
|
|
weprintf("chmod %s:", s2);
|
|
|
|
cp_status = 1;
|
2015-03-19 16:45:30 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (lchown(s2, st.st_uid, st.st_gid) < 0) {
|
|
|
|
weprintf("lchown %s:", s2);
|
|
|
|
cp_status = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2014-07-21 16:27:20 +00:00
|
|
|
}
|
2014-07-09 21:28:43 +00:00
|
|
|
}
|
2015-03-19 16:45:30 +00:00
|
|
|
|
2012-01-30 22:41:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|