From e2a579f8ac2e4f79d90c67a8df621e645efa61e6 Mon Sep 17 00:00:00 2001 From: Marcel Rodrigues Date: Sun, 13 Dec 2015 18:29:49 -0200 Subject: [PATCH] Do not dereference symlinks when copying. --- rover.c | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/rover.c b/rover.c index 3c5838a..64e06b1 100644 --- a/rover.c +++ b/rover.c @@ -786,22 +786,30 @@ static int cpyfile(const char *srcpath) { char buf[BUFSIZ]; char dstpath[PATH_MAX]; - ret = src = open(srcpath, O_RDONLY); - if (ret < 0) return ret; - ret = fstat(src, &st); - if (ret < 0) return ret; strcpy(dstpath, CWD); strcat(dstpath, srcpath + strlen(rover.marks.dirpath)); - ret = dst = creat(dstpath, st.st_mode); + ret = lstat(srcpath, &st); if (ret < 0) return ret; - while ((size = read(src, buf, BUFSIZ)) > 0) { - write(dst, buf, size); - update_progress(size); - sync_signals(); + if (S_ISLNK(st.st_mode)) { + ret = readlink(srcpath, BUF1, BUFLEN); + if (ret < 0) return ret; + BUF1[ret] = '\0'; + ret = symlink(BUF1, dstpath); + } else { + ret = src = open(srcpath, O_RDONLY); + if (ret < 0) return ret; + ret = dst = creat(dstpath, st.st_mode); + if (ret < 0) return ret; + while ((size = read(src, buf, BUFSIZ)) > 0) { + write(dst, buf, size); + update_progress(size); + sync_signals(); + } + close(src); + close(dst); + ret = 0; } - close(src); - close(dst); - return 0; + return ret; } static int adddir(const char *path) { int ret; -- 2.11.4.GIT