fixed
This commit is contained in:
parent
6d01433421
commit
e5b250b721
4
config.h
4
config.h
|
@ -1,8 +1,8 @@
|
|||
#ifndef _CONFIG_H
|
||||
#define _CONFIG_H
|
||||
|
||||
/* (cat tee wc xargs rev split) text buffer */
|
||||
#define BUF_SIZE 2048
|
||||
/* (cat tee wc xargs rev split cp) text buffer */
|
||||
#define BUF_SIZE 1024 * 1024
|
||||
|
||||
#ifdef _SHRED_C
|
||||
/* Random source (shred) */
|
||||
|
|
|
@ -6,112 +6,92 @@
|
|||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <dirent.h>
|
||||
#include <limits.h>
|
||||
#include <sys/mman.h>
|
||||
#include "make_path.h"
|
||||
#include "get_stat.h"
|
||||
#include "config.h"
|
||||
|
||||
char *f_flag = "cp";
|
||||
char r_flag;
|
||||
char v_flag;
|
||||
char L_flag;
|
||||
|
||||
int write_buffer(int mode, int ifd, int ofd, const char *dst) {
|
||||
off_t len = lseek(ifd, 0, SEEK_END);
|
||||
if (len <= 0) {
|
||||
if (len < 0)
|
||||
return 1;
|
||||
char copy_reg(int mode, const char *src, const char *dst) {
|
||||
int ret = 1;
|
||||
|
||||
int fd = open(dst, O_CREAT | O_RDONLY, mode);
|
||||
if (fd < 0)
|
||||
return 1;
|
||||
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ftruncate(ofd, len) < 0)
|
||||
return 1;
|
||||
|
||||
void *buf_in = mmap(NULL, len, PROT_READ, MAP_SHARED, ifd, 0);
|
||||
if (buf_in == MAP_FAILED)
|
||||
return 1;
|
||||
|
||||
void *buf_out = mmap(NULL, len, PROT_WRITE, MAP_SHARED, ofd, 0);
|
||||
if (buf_out == MAP_FAILED)
|
||||
return 1;
|
||||
|
||||
memcpy(buf_out, buf_in, len);
|
||||
|
||||
if (munmap(buf_in, len) || munmap(buf_out, len))
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
int copy(int mode, const char *src, const char *dst) {
|
||||
int ifd = open(src, O_RDONLY);
|
||||
if (ifd < 0)
|
||||
return 1;
|
||||
|
||||
/* test ./testdir/ -> test ./testdir/test */
|
||||
char *str_dup = NULL;
|
||||
char *new_path = (char *)dst;
|
||||
int flag = 0;
|
||||
int ret = 0;
|
||||
int ofd = open(dst, O_WRONLY | O_TRUNC | O_CREAT, mode);
|
||||
if (ofd < 0)
|
||||
goto CLOSE_OFD;
|
||||
|
||||
int ofd = open(dst, O_CREAT | O_TRUNC | O_RDWR, mode);
|
||||
if (ofd < 0) {
|
||||
ret = 1;
|
||||
char buf[BUF_SIZE + 1];
|
||||
while (1) {
|
||||
ssize_t n = read(ifd, buf, sizeof(buf));
|
||||
if (n <= 0) {
|
||||
if (n < 0)
|
||||
goto CLOSE;
|
||||
|
||||
str_dup = strdup(src);
|
||||
if (str_dup == NULL)
|
||||
break;
|
||||
}
|
||||
|
||||
if (write(ofd, buf, n) != n)
|
||||
goto CLOSE;
|
||||
|
||||
flag = 1;
|
||||
new_path = NULL;
|
||||
|
||||
new_path = mu_make_path(f_flag, dst, basename(str_dup));
|
||||
if (new_path == NULL)
|
||||
goto CLOSE;
|
||||
|
||||
if (!strcmp(new_path, src) || !strcmp(new_path + 2, src))
|
||||
goto CLOSE;
|
||||
|
||||
ofd = open(new_path, O_CREAT | O_TRUNC | O_RDWR, mode);
|
||||
if (ofd < 0)
|
||||
goto CLOSE;
|
||||
|
||||
ret = 0;
|
||||
}
|
||||
|
||||
if (write_buffer(mode, ifd, ofd, new_path))
|
||||
if (f_flag)
|
||||
fprintf(stderr, "cp: (%s %s) %s\n", src, dst, strerror(errno));
|
||||
|
||||
|
||||
close(ofd);
|
||||
ret = 0;
|
||||
|
||||
CLOSE:
|
||||
close(ifd);
|
||||
if (flag) {
|
||||
if (str_dup != NULL)
|
||||
free(str_dup);
|
||||
close(ofd);
|
||||
|
||||
if (new_path != NULL)
|
||||
free(new_path);
|
||||
CLOSE_OFD:
|
||||
close(ifd);
|
||||
return ret;
|
||||
}
|
||||
|
||||
char copy_lnk(const char *src, const char *dst) {
|
||||
char path[PATH_MAX + 1];
|
||||
ssize_t ret = readlink(src, path, sizeof(path));
|
||||
if (ret < 0)
|
||||
return 1;
|
||||
|
||||
path[ret] = '\0';
|
||||
if (symlink(path, dst) < 0)
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
char copy(struct stat st, const char *src, const char *dst) {
|
||||
if (v_flag)
|
||||
printf("Copying '%s' to '%s'\n", src, dst);
|
||||
|
||||
if (S_ISLNK(st.st_mode))
|
||||
return copy_lnk(src, dst);
|
||||
|
||||
else if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode) || S_ISFIFO(st.st_mode)) {
|
||||
if (mknod(dst, st.st_mode, st.st_dev) < 0)
|
||||
return 1;
|
||||
}
|
||||
|
||||
return ret;
|
||||
else
|
||||
return copy_reg(st.st_mode, src, dst);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cptree(const char *src, const char *dst) {
|
||||
struct stat stat_path;
|
||||
if (mu_get_lstat(f_flag, src, &stat_path))
|
||||
if (mu_get_stats(f_flag, L_flag, src, &stat_path))
|
||||
return 1;
|
||||
|
||||
if (!S_ISDIR(stat_path.st_mode)) {
|
||||
if (copy(stat_path.st_mode, src, dst)) {
|
||||
if (copy(stat_path, src, dst)) {
|
||||
if (f_flag)
|
||||
fprintf(stderr, "cp: (%s %s): copy() failed (%s)\n", src, dst, strerror(errno));
|
||||
fprintf(stderr, "cp: %s: %s\n", dst, strerror(errno));
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
@ -119,12 +99,17 @@ int cptree(const char *src, const char *dst) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
else if (!r_flag)
|
||||
return 0;
|
||||
else if (!r_flag) {
|
||||
fprintf(stderr, "cp: omitting directory: %s\n", dst);
|
||||
return 1;
|
||||
}
|
||||
|
||||
else if (mkdir(dst, 0777) < 0)
|
||||
else if (mkdir(dst, 0777) < 0) {
|
||||
if (f_flag)
|
||||
fprintf(stderr, "cp: %s\n", strerror(errno));
|
||||
fprintf(stderr, "cp: %s: %s\n", dst, strerror(errno));
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
DIR *dir = opendir(src);
|
||||
if (dir == NULL) {
|
||||
|
@ -136,9 +121,26 @@ int cptree(const char *src, const char *dst) {
|
|||
|
||||
struct dirent *ep;
|
||||
while ((ep = readdir(dir)) != NULL) {
|
||||
if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, "..") || !strcmp(dst, ep->d_name))
|
||||
if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
|
||||
continue;
|
||||
|
||||
/* Check for recursion */
|
||||
char *dup = strdup(dst);
|
||||
if (dup != NULL) {
|
||||
char *bname = basename(dup);
|
||||
if (bname != NULL) {
|
||||
if (!strcmp(bname, ep->d_name)) {
|
||||
fprintf(stderr, "cp: recursion detected, omitting directory '%s'\n", dst);
|
||||
|
||||
free(dup);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
free(dup);
|
||||
}
|
||||
|
||||
/* Copy */
|
||||
char *src_path = mu_make_path(f_flag, src, ep->d_name);
|
||||
if (src_path == NULL)
|
||||
continue;
|
||||
|
@ -154,6 +156,8 @@ int cptree(const char *src, const char *dst) {
|
|||
|
||||
free(src_path);
|
||||
free(dst_path);
|
||||
if (ret)
|
||||
break;
|
||||
}
|
||||
|
||||
closedir(dir);
|
||||
|
@ -162,7 +166,7 @@ int cptree(const char *src, const char *dst) {
|
|||
|
||||
int main(int argc, char **argv) {
|
||||
int opt;
|
||||
while ((opt = getopt(argc, argv, "frR")) != -1) {
|
||||
while ((opt = getopt(argc, argv, "frvL")) != -1) {
|
||||
switch (opt) {
|
||||
case 'f':
|
||||
f_flag = NULL;
|
||||
|
@ -172,8 +176,16 @@ int main(int argc, char **argv) {
|
|||
r_flag = 1;
|
||||
break;
|
||||
|
||||
case 'v':
|
||||
v_flag = 1;
|
||||
break;
|
||||
|
||||
case 'L':
|
||||
L_flag = 1;
|
||||
break;
|
||||
|
||||
default:
|
||||
printf("cp [rf] [src1 src2...] [dst]\n\t-f Silent\n\t-r Recursive\n");
|
||||
printf("cp [rfv] [src1 src2...] [dst]\n\t-f Silent\n\t-r Recursive\n\t-v Verbose\n\t-L Follow all symlinks\n");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue