cp.c (4613B)
1 /* see LICENSE file for copyright and license details */ 2 #if FEATURE_MKNOD 3 #ifndef _DEFAULT_SOURCE 4 #define _DEFAULT_SOURCE 5 #endif 6 #endif 7 #include <dirent.h> 8 #include <errno.h> 9 #include <fcntl.h> 10 #include <limits.h> 11 #include <stdarg.h> 12 #include <stdio.h> 13 #include <stdlib.h> 14 #include <string.h> 15 #include <sys/stat.h> 16 #include <sys/types.h> 17 #include <unistd.h> 18 #include <utime.h> 19 20 #include "../fs.h" 21 #include "../util.h" 22 23 int cp_aflag = 0; 24 int cp_fflag = 0; 25 int cp_iflag = 0; 26 int cp_pflag = 0; 27 int cp_rflag = 0; 28 int cp_vflag = 0; 29 int cp_qflag = 0; 30 int cp_status = 0; 31 int cp_errno = 0; 32 int cp_follow; 33 34 /* a failure: counted, the first one kept in cp_errno, printed unless the 35 * caller is a library that asked for quiet */ 36 static int 37 cp_fail(const char *fmt, ...) 38 { 39 va_list ap; 40 int err = errno; 41 42 if (!cp_status) 43 cp_errno = err; 44 cp_status = 1; 45 if (!cp_qflag) { 46 va_start(ap, fmt); 47 errno = err; 48 xvprintf(fmt, ap); 49 va_end(ap); 50 } 51 errno = err; 52 return 0; 53 } 54 55 /* dir/name into buf, -1 when it does not fit */ 56 static int 57 cp_join(char *buf, size_t n, const char *dir, const char *name) 58 { 59 if (strlcpy(buf, dir, n) >= n || (dir[strlen(dir) - 1] != '/' && strlcat(buf, "/", n) >= n) 60 || strlcat(buf, name, n) >= n) { 61 errno = ENAMETOOLONG; 62 return -1; 63 } 64 return 0; 65 } 66 67 int 68 cp(const char *s1, const char *s2, int depth) 69 { 70 DIR *dp; 71 int f1, f2, flags = 0; 72 struct dirent *d; 73 struct stat st, st2; 74 struct timespec times[2]; 75 ssize_t r; 76 char target[PATH_MAX], ns1[PATH_MAX], ns2[PATH_MAX]; 77 78 if (cp_follow == 'P' || (cp_follow == 'H' && depth)) 79 flags |= AT_SYMLINK_NOFOLLOW; 80 81 if (fstatat(AT_FDCWD, s1, &st, flags) < 0) 82 return cp_fail("stat %s:", s1); 83 if (stat(s2, &st2) == 0 && st.st_dev == st2.st_dev && st.st_ino == st2.st_ino) { 84 errno = EINVAL; 85 return cp_fail("%s -> %s: same file\n", s1, s2); 86 } 87 88 if (cp_iflag && !cp_qflag && access(s2, F_OK) == 0) { 89 if (!confirm("overwrite '%s'? ", s2)) 90 return 0; 91 } 92 93 if (cp_vflag && !cp_qflag) 94 printf("%s -> %s\n", s1, s2); 95 96 if (S_ISLNK(st.st_mode)) { 97 if ((r = readlink(s1, target, sizeof(target) - 1)) >= 0) { 98 target[r] = '\0'; 99 if (cp_fflag && unlink(s2) < 0 && errno != ENOENT) 100 return cp_fail("unlink %s:", s2); 101 else if (symlink(target, s2) < 0) 102 return cp_fail("symlink %s -> %s:", s2, target); 103 } 104 } else if (S_ISDIR(st.st_mode)) { 105 if (!cp_rflag) { 106 errno = EISDIR; 107 return cp_fail("%s is a directory\n", s1); 108 } 109 if (!(dp = opendir(s1))) 110 return cp_fail("opendir %s:", s1); 111 if (mkdir(s2, st.st_mode) < 0 && errno != EEXIST) { 112 cp_fail("mkdir %s:", s2); 113 closedir(dp); 114 return 0; 115 } 116 117 while ((d = readdir(dp))) { 118 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, "..")) 119 continue; 120 if (cp_join(ns1, sizeof(ns1), s1, d->d_name) < 0 121 || cp_join(ns2, sizeof(ns2), s2, d->d_name) < 0) 122 cp_fail("%s/%s:", s1, d->d_name); 123 else 124 cp(ns1, ns2, depth + 1); 125 } 126 127 closedir(dp); 128 } else if ( 129 cp_aflag 130 && (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode) || S_ISSOCK(st.st_mode) 131 || S_ISFIFO(st.st_mode)) 132 ) { 133 if (cp_fflag && unlink(s2) < 0 && errno != ENOENT) 134 return cp_fail("unlink %s:", s2); 135 else if (mknod(s2, st.st_mode, st.st_rdev) < 0) 136 return cp_fail("mknod %s:", s2); 137 } else { 138 if ((f1 = open(s1, O_RDONLY)) < 0) 139 return cp_fail("open %s:", s1); 140 if ((f2 = creat(s2, st.st_mode)) < 0 && cp_fflag) { 141 if (unlink(s2) < 0 && errno != ENOENT) { 142 cp_fail("unlink %s:", s2); 143 close(f1); 144 return 0; 145 } 146 f2 = creat(s2, st.st_mode); 147 } 148 if (f2 < 0) { 149 cp_fail("creat %s:", s2); 150 close(f1); 151 return 0; 152 } 153 if ((r = copyall(f1, f2)) == -1) 154 cp_fail("read %s:", s1); 155 else if (r == -2) 156 cp_fail("write %s:", s2); 157 158 close(f1); 159 close(f2); 160 if (r < 0) 161 return 0; 162 } 163 164 if (cp_aflag || cp_pflag) { 165 /* atime and mtime */ 166 times[0] = st.st_atim; 167 times[1] = st.st_mtim; 168 if (utimensat(AT_FDCWD, s2, times, AT_SYMLINK_NOFOLLOW) < 0) 169 cp_fail("utimensat %s:", s2); 170 171 /* owner and mode */ 172 if (!S_ISLNK(st.st_mode)) { 173 if (chown(s2, st.st_uid, st.st_gid) < 0) { 174 cp_fail("chown %s:", s2); 175 st.st_mode &= ~(S_ISUID | S_ISGID); 176 } 177 if (chmod(s2, st.st_mode) < 0) 178 cp_fail("chmod %s:", s2); 179 } else if (lchown(s2, st.st_uid, st.st_gid) < 0) { 180 return cp_fail("lchown %s:", s2); 181 } 182 } 183 184 return 0; 185 }