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 "../text.h"
|
|
|
|
#include "../util.h"
|
|
|
|
|
2014-11-13 20:24:47 +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;
|
2015-02-16 16:23:12 +00:00
|
|
|
int cp_HLPflag = 'L';
|
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
|
|
|
{
|
|
|
|
FILE *f1, *f2;
|
2015-03-18 14:20:35 +00:00
|
|
|
char ns1[PATH_MAX], ns2[PATH_MAX];
|
2012-01-30 22:41:33 +00:00
|
|
|
struct dirent *d;
|
|
|
|
struct stat st;
|
2014-07-09 21:28:43 +00:00
|
|
|
struct utimbuf ut;
|
|
|
|
char buf[PATH_MAX];
|
2012-01-30 22:41:33 +00:00
|
|
|
DIR *dp;
|
2014-06-01 13:01:09 +00:00
|
|
|
int r;
|
2012-01-30 22:41:33 +00:00
|
|
|
|
2014-11-13 18:54:28 +00:00
|
|
|
if (cp_vflag)
|
2014-07-21 16:27:20 +00:00
|
|
|
printf("'%s' -> '%s'\n", s1, s2);
|
|
|
|
|
2015-03-02 20:43:56 +00:00
|
|
|
r = (cp_HLPflag == 'P' || (cp_HLPflag == 'H' && depth > 0)) ?
|
|
|
|
lstat(s1, &st) : stat(s1, &st);
|
2014-11-19 15:08:57 +00:00
|
|
|
if (r < 0) {
|
2015-03-02 20:43:56 +00:00
|
|
|
weprintf("%s %s:", (cp_HLPflag == 'P' ||
|
|
|
|
(cp_HLPflag == 'H' && depth > 0)) ? "lstat" : "stat", s1);
|
2014-11-19 15:08:57 +00:00
|
|
|
cp_status = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2013-03-05 20:46:48 +00:00
|
|
|
|
2014-12-08 03:25:24 +00:00
|
|
|
if (S_ISLNK(st.st_mode)) {
|
2014-11-19 15:08:57 +00:00
|
|
|
if (readlink(s1, buf, sizeof(buf) - 1) >= 0) {
|
|
|
|
if (cp_fflag)
|
|
|
|
unlink(s2);
|
|
|
|
if (symlink(buf, s2) != 0) {
|
|
|
|
weprintf("%s: can't create '%s'\n", argv0, s2);
|
|
|
|
cp_status = 1;
|
|
|
|
return 0;
|
2014-07-09 21:28:43 +00:00
|
|
|
}
|
|
|
|
}
|
2014-11-19 15:08:57 +00:00
|
|
|
goto preserve;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
|
|
|
if (!cp_rflag)
|
|
|
|
eprintf("%s: is a directory\n", s1);
|
2013-03-05 20:46:48 +00:00
|
|
|
|
2014-11-19 15:08:57 +00:00
|
|
|
if (!(dp = opendir(s1)))
|
|
|
|
eprintf("opendir %s:", s1);
|
2014-07-09 21:28:43 +00:00
|
|
|
|
2014-11-19 19:59:37 +00:00
|
|
|
if (mkdir(s2, st.st_mode) < 0 && errno != EEXIST)
|
2014-11-19 15:08:57 +00:00
|
|
|
eprintf("mkdir %s:", s2);
|
2014-07-09 21:28:43 +00:00
|
|
|
|
2014-11-19 15:08:57 +00:00
|
|
|
while ((d = readdir(dp))) {
|
|
|
|
if (strcmp(d->d_name, ".") && strcmp(d->d_name, "..")) {
|
2015-03-18 14:20:35 +00:00
|
|
|
r = snprintf(ns1, sizeof(ns1), "%s/%s", s1, d->d_name);
|
|
|
|
if (r >= sizeof(ns1) || r < 0) {
|
2014-11-19 15:08:57 +00:00
|
|
|
eprintf("%s/%s: filename too long\n",
|
|
|
|
s1, d->d_name);
|
2014-05-05 14:07:55 +00:00
|
|
|
}
|
2015-03-18 14:20:35 +00:00
|
|
|
r = snprintf(ns2, sizeof(ns2), "%s/%s", s2, d->d_name);
|
|
|
|
if (r >= sizeof(ns2) || r < 0) {
|
2014-11-19 15:08:57 +00:00
|
|
|
eprintf("%s/%s: filename too long\n",
|
|
|
|
s2, d->d_name);
|
|
|
|
}
|
2015-03-02 20:43:56 +00:00
|
|
|
fnck(ns1, ns2, cp, depth + 1);
|
2013-03-05 20:46:48 +00:00
|
|
|
}
|
2014-07-09 21:28:43 +00:00
|
|
|
}
|
2014-11-19 15:08:57 +00:00
|
|
|
closedir(dp);
|
|
|
|
goto preserve;
|
2012-01-30 22:41:33 +00:00
|
|
|
}
|
2014-07-21 16:27:20 +00:00
|
|
|
|
2014-11-13 20:24:47 +00:00
|
|
|
if (cp_aflag) {
|
2014-11-13 18:54:28 +00:00
|
|
|
if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode) ||
|
2014-11-19 15:08:57 +00:00
|
|
|
S_ISSOCK(st.st_mode) || S_ISFIFO(st.st_mode)) {
|
2014-07-21 16:27:20 +00:00
|
|
|
unlink(s2);
|
2014-11-13 18:54:28 +00:00
|
|
|
if (mknod(s2, st.st_mode, st.st_rdev) < 0) {
|
2014-07-21 16:27:20 +00:00
|
|
|
weprintf("%s: can't create '%s':", argv0, s2);
|
2014-10-02 22:46:04 +00:00
|
|
|
cp_status = 1;
|
2014-07-21 16:27:20 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
goto preserve;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-13 18:54:28 +00:00
|
|
|
if (!(f1 = fopen(s1, "r"))) {
|
2014-08-02 14:40:04 +00:00
|
|
|
weprintf("fopen %s:", s1);
|
2014-10-02 22:46:04 +00:00
|
|
|
cp_status = 1;
|
2014-08-02 14:40:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2013-03-05 20:46:48 +00:00
|
|
|
|
2014-11-13 18:54:28 +00:00
|
|
|
if (!(f2 = fopen(s2, "w"))) {
|
2014-11-13 20:24:47 +00:00
|
|
|
if (cp_fflag) {
|
2014-05-05 13:58:14 +00:00
|
|
|
unlink(s2);
|
2014-11-13 18:54:28 +00:00
|
|
|
if (!(f2 = fopen(s2, "w"))) {
|
2014-08-02 14:40:04 +00:00
|
|
|
weprintf("fopen %s:", s2);
|
2014-10-02 22:46:04 +00:00
|
|
|
cp_status = 1;
|
2014-08-02 14:40:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2014-05-05 14:02:16 +00:00
|
|
|
} else {
|
2014-08-02 14:40:04 +00:00
|
|
|
weprintf("fopen %s:", s2);
|
2014-10-02 22:46:04 +00:00
|
|
|
cp_status = 1;
|
2014-08-02 14:40:04 +00:00
|
|
|
return 0;
|
2014-05-05 14:02:16 +00:00
|
|
|
}
|
2014-05-05 13:58:14 +00:00
|
|
|
}
|
2012-01-30 22:41:33 +00:00
|
|
|
concat(f1, s1, f2, s2);
|
2014-11-19 15:08:57 +00:00
|
|
|
/* preserve permissions by default */
|
2014-02-18 16:49:41 +00:00
|
|
|
fchmod(fileno(f2), st.st_mode);
|
2012-01-30 22:41:33 +00:00
|
|
|
fclose(f2);
|
|
|
|
fclose(f1);
|
2013-03-05 20:46:48 +00:00
|
|
|
|
2014-07-09 21:28:43 +00:00
|
|
|
preserve:
|
2014-11-13 20:24:47 +00:00
|
|
|
if (cp_aflag || cp_pflag) {
|
2014-11-13 18:54:28 +00:00
|
|
|
if (!(S_ISLNK(st.st_mode))) {
|
2014-07-21 16:27:20 +00:00
|
|
|
/* timestamp */
|
|
|
|
ut.actime = st.st_atime;
|
|
|
|
ut.modtime = st.st_mtime;
|
|
|
|
utime(s2, &ut);
|
|
|
|
}
|
2014-07-09 21:28:43 +00:00
|
|
|
/* preserve owner ? */
|
2014-11-13 18:54:28 +00:00
|
|
|
if (S_ISLNK(st.st_mode))
|
2014-07-21 16:27:20 +00:00
|
|
|
r = lchown(s2, st.st_uid, st.st_gid);
|
|
|
|
else
|
|
|
|
r = chown(s2, st.st_uid, st.st_gid);
|
2014-11-19 19:59:37 +00:00
|
|
|
if (r < 0) {
|
2014-07-09 21:28:43 +00:00
|
|
|
weprintf("cp: can't preserve ownership of '%s':", s2);
|
2014-10-02 22:46:04 +00:00
|
|
|
cp_status = 1;
|
2014-07-21 16:27:20 +00:00
|
|
|
}
|
2014-07-09 21:28:43 +00:00
|
|
|
}
|
2012-01-30 22:41:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|