cp: improvements
- improve copying block, char devices, fifo and sockets with -a. - improve exit status code.
This commit is contained in:
parent
35959cd1c4
commit
323c45edb7
2
cp.c
2
cp.c
|
@ -49,5 +49,5 @@ main(int argc, char *argv[])
|
||||||
if(argc > 2 && !(stat(argv[argc-1], &st) == 0 && S_ISDIR(st.st_mode)))
|
if(argc > 2 && !(stat(argv[argc-1], &st) == 0 && S_ISDIR(st.st_mode)))
|
||||||
eprintf("%s: not a directory\n", argv[argc-1]);
|
eprintf("%s: not a directory\n", argv[argc-1]);
|
||||||
enmasse(argc, argv, cp);
|
enmasse(argc, argv, cp);
|
||||||
return EXIT_SUCCESS;
|
return cp_status;
|
||||||
}
|
}
|
||||||
|
|
1
fs.h
1
fs.h
|
@ -7,6 +7,7 @@ extern bool cp_fflag;
|
||||||
extern bool cp_pflag;
|
extern bool cp_pflag;
|
||||||
extern bool cp_rflag;
|
extern bool cp_rflag;
|
||||||
extern bool cp_vflag;
|
extern bool cp_vflag;
|
||||||
|
extern int cp_status;
|
||||||
|
|
||||||
extern bool rm_fflag;
|
extern bool rm_fflag;
|
||||||
extern bool rm_rflag;
|
extern bool rm_rflag;
|
||||||
|
|
72
util/cp.c
72
util/cp.c
|
@ -1,14 +1,15 @@
|
||||||
/* See LICENSE file for copyright and license details. */
|
/* See LICENSE file for copyright and license details. */
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <limits.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <limits.h>
|
#include <unistd.h>
|
||||||
#include <utime.h>
|
#include <utime.h>
|
||||||
|
|
||||||
#include "../fs.h"
|
#include "../fs.h"
|
||||||
|
@ -21,6 +22,7 @@ bool cp_fflag = false;
|
||||||
bool cp_pflag = false;
|
bool cp_pflag = false;
|
||||||
bool cp_rflag = false;
|
bool cp_rflag = false;
|
||||||
bool cp_vflag = false;
|
bool cp_vflag = false;
|
||||||
|
int cp_status = EXIT_SUCCESS;
|
||||||
|
|
||||||
int
|
int
|
||||||
cp(const char *s1, const char *s2)
|
cp(const char *s1, const char *s2)
|
||||||
|
@ -35,27 +37,26 @@ cp(const char *s1, const char *s2)
|
||||||
DIR *dp;
|
DIR *dp;
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
|
if(cp_vflag)
|
||||||
|
printf("'%s' -> '%s'\n", s1, s2);
|
||||||
|
|
||||||
if(cp_dflag == true)
|
if(cp_dflag == true)
|
||||||
r = lstat(s1, &st);
|
r = lstat(s1, &st);
|
||||||
else
|
else
|
||||||
r = stat(s1, &st);
|
r = stat(s1, &st);
|
||||||
|
|
||||||
if(cp_vflag)
|
|
||||||
printf("'%s' -> '%s'\n", s1, s2);
|
|
||||||
|
|
||||||
if(r == 0) {
|
if(r == 0) {
|
||||||
if(cp_dflag == true && S_ISLNK(st.st_mode)) {
|
if(cp_dflag == true && S_ISLNK(st.st_mode)) {
|
||||||
if(cp_fflag == true)
|
if(readlink(s1, buf, sizeof(buf) - 1) >= 0) {
|
||||||
remove(s2);
|
if(cp_fflag == true);
|
||||||
if(readlink(s1, buf, sizeof(buf) - 1) >= 0)
|
unlink(s2);
|
||||||
symlink(buf, s2);
|
if(symlink(buf, s2) != 0) {
|
||||||
|
weprintf("%s: can't create '%s'\n", argv0, s2);
|
||||||
/* preserve owner ? */
|
cp_status = EXIT_FAILURE;
|
||||||
if(cp_aflag == true || cp_pflag == true) {
|
return 0;
|
||||||
if(lchown(s2, st.st_uid, st.st_gid) == -1)
|
}
|
||||||
weprintf("cp: can't preserve ownership of '%s':", s2);
|
|
||||||
}
|
}
|
||||||
return 0;
|
goto preserve;
|
||||||
}
|
}
|
||||||
if(S_ISDIR(st.st_mode)) {
|
if(S_ISDIR(st.st_mode)) {
|
||||||
if (!cp_rflag)
|
if (!cp_rflag)
|
||||||
|
@ -70,8 +71,7 @@ cp(const char *s1, const char *s2)
|
||||||
apathmax(&ns1, &size1);
|
apathmax(&ns1, &size1);
|
||||||
apathmax(&ns2, &size2);
|
apathmax(&ns2, &size2);
|
||||||
while((d = readdir(dp))) {
|
while((d = readdir(dp))) {
|
||||||
if(strcmp(d->d_name, ".")
|
if(strcmp(d->d_name, ".") && strcmp(d->d_name, "..")) {
|
||||||
&& strcmp(d->d_name, "..")) {
|
|
||||||
r = snprintf(ns1, size1, "%s/%s", s1, d->d_name);
|
r = snprintf(ns1, size1, "%s/%s", s1, d->d_name);
|
||||||
if(r >= size1 || r < 0) {
|
if(r >= size1 || r < 0) {
|
||||||
eprintf("%s/%s: filename too long\n",
|
eprintf("%s/%s: filename too long\n",
|
||||||
|
@ -85,26 +85,38 @@ cp(const char *s1, const char *s2)
|
||||||
fnck(ns1, ns2, cp);
|
fnck(ns1, ns2, cp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
closedir(dp);
|
closedir(dp);
|
||||||
free(ns1);
|
free(ns1);
|
||||||
free(ns2);
|
free(ns2);
|
||||||
goto preserve;
|
goto preserve;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(cp_aflag == true) {
|
||||||
|
if(S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode) ||
|
||||||
|
S_ISSOCK(st.st_mode) || S_ISFIFO(st.st_mode)) {
|
||||||
|
unlink(s2);
|
||||||
|
if(mknod(s2, st.st_mode, st.st_rdev) < 0) {
|
||||||
|
weprintf("%s: can't create '%s':", argv0, s2);
|
||||||
|
cp_status = EXIT_FAILURE;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
goto preserve;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(!(f1 = fopen(s1, "r")))
|
if(!(f1 = fopen(s1, "r")))
|
||||||
eprintf("fopen %s:", s1);
|
eprintf("fopen %s:", s1);
|
||||||
|
|
||||||
if(!(f2 = fopen(s2, "w"))) {
|
if(!(f2 = fopen(s2, "w"))) {
|
||||||
if (cp_fflag == true) {
|
if (cp_fflag == true) {
|
||||||
unlink(s2);
|
unlink(s2);
|
||||||
if (!(f2 = fopen(s2, "w")))
|
if(!(f2 = fopen(s2, "w")))
|
||||||
eprintf("fopen %s:", s2);
|
eprintf("fopen %s:", s2);
|
||||||
} else {
|
} else {
|
||||||
eprintf("fopen %s:", s2);
|
eprintf("fopen %s:", s2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
concat(f1, s1, f2, s2);
|
concat(f1, s1, f2, s2);
|
||||||
fchmod(fileno(f2), st.st_mode);
|
fchmod(fileno(f2), st.st_mode);
|
||||||
fclose(f2);
|
fclose(f2);
|
||||||
|
@ -112,15 +124,21 @@ cp(const char *s1, const char *s2)
|
||||||
|
|
||||||
preserve:
|
preserve:
|
||||||
if(cp_aflag == true || cp_pflag == true) {
|
if(cp_aflag == true || cp_pflag == true) {
|
||||||
/* timestamp */
|
if(!(S_ISLNK(st.st_mode))) {
|
||||||
ut.actime = st.st_atime;
|
/* timestamp */
|
||||||
ut.modtime = st.st_mtime;
|
ut.actime = st.st_atime;
|
||||||
utime(s2, &ut);
|
ut.modtime = st.st_mtime;
|
||||||
|
utime(s2, &ut);
|
||||||
|
}
|
||||||
/* preserve owner ? */
|
/* preserve owner ? */
|
||||||
if(chown(s2, st.st_uid, st.st_gid) == -1)
|
if(S_ISLNK(st.st_mode))
|
||||||
|
r = lchown(s2, st.st_uid, st.st_gid);
|
||||||
|
else
|
||||||
|
r = chown(s2, st.st_uid, st.st_gid);
|
||||||
|
if(r == -1) {
|
||||||
weprintf("cp: can't preserve ownership of '%s':", s2);
|
weprintf("cp: can't preserve ownership of '%s':", s2);
|
||||||
|
cp_status = EXIT_FAILURE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user