2012-03-11 10:57:13 -04:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2014-06-27 16:46:50 -04:00
|
|
|
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013, 2014.
|
2012-03-11 10:57:13 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
This program is free software: you can redistribute it and/or modify it
|
|
|
|
under the terms of the GNU General Public License as published by the Free
|
|
|
|
Software Foundation, either version 3 of the License, or (at your option)
|
|
|
|
any later version.
|
2012-03-11 10:57:13 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
more details.
|
2012-03-11 10:57:13 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
this program. If not, see <http://www.gnu.org/licenses/>.
|
2012-03-11 10:57:13 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
cp.cpp
|
|
|
|
Copy files and directories.
|
2012-03-11 10:57:13 -04:00
|
|
|
|
|
|
|
*******************************************************************************/
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
#include <sys/stat.h>
|
2013-01-08 09:11:44 -05:00
|
|
|
#include <sys/types.h>
|
2012-08-07 18:19:44 -04:00
|
|
|
|
2013-01-08 09:11:44 -05:00
|
|
|
#include <dirent.h>
|
2011-11-22 11:26:47 -05:00
|
|
|
#include <errno.h>
|
2011-11-26 05:00:45 -05:00
|
|
|
#include <error.h>
|
2013-01-08 09:11:44 -05:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
2011-11-22 11:26:47 -05:00
|
|
|
#include <string.h>
|
2014-06-27 16:46:50 -04:00
|
|
|
#include <timespec.h>
|
2013-01-08 09:11:44 -05:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
const char* BaseName(const char* path)
|
2011-11-22 11:57:10 -05:00
|
|
|
{
|
|
|
|
size_t len = strlen(path);
|
2014-06-27 16:46:50 -04:00
|
|
|
while ( len-- )
|
|
|
|
if ( path[len] == '/' )
|
|
|
|
return path + len + 1;
|
2011-11-22 11:57:10 -05:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2013-01-08 09:11:44 -05:00
|
|
|
char* AddElemToPath(const char* path, const char* elem)
|
2011-11-21 08:57:02 -05:00
|
|
|
{
|
2013-01-08 09:11:44 -05:00
|
|
|
size_t pathlen = strlen(path);
|
|
|
|
size_t elemlen = strlen(elem);
|
|
|
|
if ( pathlen && path[pathlen-1] == '/' )
|
|
|
|
{
|
|
|
|
char* ret = (char*) malloc(sizeof(char) * (pathlen + elemlen + 1));
|
|
|
|
stpcpy(stpcpy(ret, path), elem);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
char* ret = (char*) malloc(sizeof(char) * (pathlen + 1 + elemlen + 1));
|
|
|
|
stpcpy(stpcpy(stpcpy(ret, path), "/"), elem);
|
|
|
|
return ret;
|
|
|
|
}
|
2011-11-21 08:57:02 -05:00
|
|
|
|
2013-01-08 09:11:44 -05:00
|
|
|
const int FLAG_RECURSIVE = 1 << 0;
|
|
|
|
const int FLAG_VERBOSE = 1 << 1;
|
2014-06-27 16:46:50 -04:00
|
|
|
const int FLAG_TARGET_DIR = 1 << 2;
|
|
|
|
const int FLAG_NO_TARGET_DIR = 1 << 3;
|
|
|
|
const int FLAG_UPDATE = 1 << 4;
|
2014-09-23 13:18:17 -04:00
|
|
|
|
|
|
|
enum symbolic_dereference
|
|
|
|
{
|
|
|
|
SYMBOLIC_DEREFERENCE_NONE,
|
|
|
|
SYMBOLIC_DEREFERENCE_ARGUMENTS,
|
|
|
|
SYMBOLIC_DEREFERENCE_ALWAYS,
|
|
|
|
SYMBOLIC_DEREFERENCE_DEFAULT,
|
|
|
|
};
|
2011-11-22 11:57:10 -05:00
|
|
|
|
2013-01-08 09:11:44 -05:00
|
|
|
bool CopyFileContents(int srcfd, const char* srcpath,
|
|
|
|
int dstfd, const char* dstpath, int flags);
|
|
|
|
bool CopyDirectoryContents(int srcfd, const char* srcpath,
|
2014-09-23 13:18:17 -04:00
|
|
|
int dstfd, const char* dstpath, int flags,
|
|
|
|
enum symbolic_dereference symbolic_dereference);
|
2013-01-08 09:11:44 -05:00
|
|
|
bool CopyToDest(int srcdirfd, const char* srcrel, const char* srcpath,
|
|
|
|
int dstdirfd, const char* dstrel, const char* dstpath,
|
2014-09-23 13:18:17 -04:00
|
|
|
int flags, enum symbolic_dereference symbolic_dereference);
|
2013-01-08 09:11:44 -05:00
|
|
|
bool CopyIntoDirectory(int srcdirfd, const char* srcrel, const char* srcpath,
|
|
|
|
int dstdirfd, const char* dstrel, const char* dstpath,
|
2014-09-23 13:18:17 -04:00
|
|
|
int flags, enum symbolic_dereference symbolic_dereference);
|
2013-01-08 09:11:44 -05:00
|
|
|
bool CopyAmbigious(int srcdirfd, const char* srcrel, const char* srcpath,
|
|
|
|
int dstdirfd, const char* dstrel, const char* dstpath,
|
2014-09-23 13:18:17 -04:00
|
|
|
int flags, enum symbolic_dereference symbolic_dereference);
|
2011-11-21 08:57:02 -05:00
|
|
|
|
2013-01-08 09:11:44 -05:00
|
|
|
bool CopyFileContents(int srcfd, const char* srcpath,
|
|
|
|
int dstfd, const char* dstpath, int flags)
|
|
|
|
{
|
|
|
|
struct stat srcst, dstst;
|
|
|
|
if ( fstat(srcfd, &srcst) )
|
|
|
|
{
|
|
|
|
error(0, errno, "stat: %s", srcpath);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ( fstat(dstfd, &dstst) )
|
|
|
|
{
|
|
|
|
error(0, errno, "stat: %s", dstpath);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ( srcst.st_dev == dstst.st_dev && srcst.st_ino == dstst.st_ino )
|
|
|
|
{
|
|
|
|
error(0, 0, "`%s' and `%s' are the same file", srcpath, dstpath);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ( S_ISDIR(dstst.st_mode) )
|
|
|
|
{
|
|
|
|
error(0, 0, "cannot overwrite directory `%s' with non-directory",
|
|
|
|
dstpath);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ( lseek(srcfd, 0, SEEK_SET) )
|
|
|
|
{
|
|
|
|
error(0, errno, "can't seek: %s", srcpath);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ( lseek(dstfd, 0, SEEK_SET) )
|
|
|
|
{
|
|
|
|
error(0, errno, "can't seek: %s", dstpath);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ( flags & FLAG_VERBOSE )
|
|
|
|
printf("`%s' -> `%s'\n", srcpath, dstpath);
|
|
|
|
ftruncate(dstfd, srcst.st_size);
|
|
|
|
size_t BUFFER_SIZE = 16 * 1024;
|
|
|
|
uint8_t buffer[BUFFER_SIZE];
|
|
|
|
while ( true )
|
|
|
|
{
|
|
|
|
ssize_t numbytes = read(srcfd, buffer, BUFFER_SIZE);
|
|
|
|
if ( numbytes == 0 )
|
|
|
|
break;
|
|
|
|
if ( numbytes < 0 )
|
|
|
|
{
|
|
|
|
error(0, errno, "read: %s", srcpath);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
size_t sofar = 0;
|
|
|
|
while ( sofar < (size_t) numbytes )
|
|
|
|
{
|
|
|
|
ssize_t amount = write(dstfd, buffer + sofar, numbytes - sofar);
|
|
|
|
if ( amount <= 0 )
|
|
|
|
{
|
|
|
|
error(0, errno, "write: %s", dstpath);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
sofar += amount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2012-08-07 18:19:44 -04:00
|
|
|
|
2013-01-08 09:11:44 -05:00
|
|
|
bool CopyDirectoryContentsInner(DIR* srcdir, const char* srcpath,
|
2014-09-23 13:18:17 -04:00
|
|
|
DIR* dstdir, const char* dstpath, int flags,
|
|
|
|
enum symbolic_dereference symbolic_dereference)
|
2013-01-08 09:11:44 -05:00
|
|
|
{
|
|
|
|
if ( flags & FLAG_VERBOSE )
|
|
|
|
printf("`%s' -> `%s'\n", srcpath, dstpath);
|
|
|
|
bool ret = true;
|
|
|
|
struct dirent* entry;
|
|
|
|
while ( (entry = readdir(srcdir)) )
|
|
|
|
{
|
|
|
|
const char* name = entry->d_name;
|
|
|
|
if ( !strcmp(name, ".") || !strcmp(name, "..") )
|
|
|
|
continue;
|
|
|
|
char* srcpath_new = AddElemToPath(srcpath, name);
|
|
|
|
char* dstpath_new = AddElemToPath(dstpath, name);
|
|
|
|
bool ok = CopyToDest(dirfd(srcdir), name, srcpath_new,
|
2014-09-23 13:18:17 -04:00
|
|
|
dirfd(dstdir), name, dstpath_new, flags,
|
|
|
|
symbolic_dereference);
|
2013-01-08 09:11:44 -05:00
|
|
|
free(srcpath_new);
|
|
|
|
free(dstpath_new);
|
|
|
|
if ( !ok )
|
|
|
|
ret = false;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2011-11-22 11:57:10 -05:00
|
|
|
|
2013-01-08 09:11:44 -05:00
|
|
|
bool CopyDirectoryContentsOuter(int srcfd, const char* srcpath,
|
2014-09-23 13:18:17 -04:00
|
|
|
int dstfd, const char* dstpath, int flags,
|
|
|
|
enum symbolic_dereference symbolic_dereference)
|
2013-01-08 09:11:44 -05:00
|
|
|
{
|
|
|
|
struct stat srcst, dstst;
|
2014-06-27 16:46:50 -04:00
|
|
|
if ( fstat(srcfd, &srcst) < 0 )
|
2013-01-08 09:11:44 -05:00
|
|
|
{
|
|
|
|
error(0, errno, "stat: %s", srcpath);
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-27 16:46:50 -04:00
|
|
|
if ( fstat(dstfd, &dstst) < 0 )
|
2013-01-08 09:11:44 -05:00
|
|
|
{
|
|
|
|
error(0, errno, "stat: %s", dstpath);
|
2014-06-27 16:46:50 -04:00
|
|
|
return false;
|
2013-01-08 09:11:44 -05:00
|
|
|
}
|
|
|
|
if ( srcst.st_dev == dstst.st_dev && srcst.st_ino == dstst.st_ino )
|
2012-08-07 18:19:44 -04:00
|
|
|
{
|
2013-01-08 09:11:44 -05:00
|
|
|
error(0, 0, "error: `%s' and `%s' are the same file", srcpath, dstpath);
|
|
|
|
return false;
|
2011-11-22 11:57:10 -05:00
|
|
|
}
|
2013-01-08 09:11:44 -05:00
|
|
|
DIR* srcdir = fdopendir(srcfd);
|
2014-06-27 16:46:50 -04:00
|
|
|
if ( !srcdir )
|
|
|
|
return error(0, errno, "fdopendir()"), false;
|
2013-01-08 09:11:44 -05:00
|
|
|
DIR* dstdir = fdopendir(dstfd);
|
2014-06-27 16:46:50 -04:00
|
|
|
if ( !dstdir )
|
|
|
|
return error(0, errno, "fdopendir()"), closedir(srcdir), false;
|
2013-01-08 09:11:44 -05:00
|
|
|
bool ret = CopyDirectoryContentsInner(srcdir, srcpath, dstdir, dstpath,
|
2014-09-23 13:18:17 -04:00
|
|
|
flags, symbolic_dereference);
|
2013-01-08 09:11:44 -05:00
|
|
|
closedir(dstdir);
|
|
|
|
closedir(srcdir);
|
|
|
|
return ret;
|
|
|
|
}
|
2011-11-21 08:57:02 -05:00
|
|
|
|
2013-01-08 09:11:44 -05:00
|
|
|
bool CopyDirectoryContents(int srcfd, const char* srcpath,
|
2014-09-23 13:18:17 -04:00
|
|
|
int dstfd, const char* dstpath, int flags,
|
|
|
|
enum symbolic_dereference symbolic_dereference)
|
2013-01-08 09:11:44 -05:00
|
|
|
{
|
|
|
|
int srcfd_copy = dup(srcfd);
|
2014-06-27 16:46:50 -04:00
|
|
|
if ( srcfd_copy < 0 )
|
|
|
|
return error(0, errno, "dup"), false;
|
2013-01-08 09:11:44 -05:00
|
|
|
int dstfd_copy = dup(dstfd);
|
2014-06-27 16:46:50 -04:00
|
|
|
if ( dstfd_copy < 0 )
|
|
|
|
return error(0, errno, "dup"), close(srcfd_copy), false;
|
2013-01-08 09:11:44 -05:00
|
|
|
bool ret = CopyDirectoryContentsOuter(srcfd_copy, srcpath,
|
2014-09-23 13:18:17 -04:00
|
|
|
dstfd_copy, dstpath, flags,
|
|
|
|
symbolic_dereference);
|
2013-01-08 09:11:44 -05:00
|
|
|
close(dstfd_copy);
|
|
|
|
close(srcfd_copy);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CopyToDest(int srcdirfd, const char* srcrel, const char* srcpath,
|
|
|
|
int dstdirfd, const char* dstrel, const char* dstpath,
|
2014-09-23 13:18:17 -04:00
|
|
|
int flags, enum symbolic_dereference symbolic_dereference)
|
2013-01-08 09:11:44 -05:00
|
|
|
{
|
|
|
|
struct stat srcst;
|
|
|
|
bool ret = false;
|
2014-09-23 13:18:17 -04:00
|
|
|
int srcfd_flags = O_RDONLY;
|
|
|
|
if ( symbolic_dereference == SYMBOLIC_DEREFERENCE_NONE )
|
|
|
|
srcfd_flags |= O_SYMLINK_NOFOLLOW;
|
|
|
|
int srcfd = openat(srcdirfd, srcrel, O_RDONLY | srcfd_flags);
|
2013-01-08 09:11:44 -05:00
|
|
|
if ( srcfd < 0 )
|
2011-11-21 08:57:02 -05:00
|
|
|
{
|
2013-01-08 09:11:44 -05:00
|
|
|
error(0, errno, "%s", srcpath);
|
2014-06-27 16:46:50 -04:00
|
|
|
return false;
|
2013-01-08 09:11:44 -05:00
|
|
|
}
|
|
|
|
if ( fstat(srcfd, &srcst) )
|
|
|
|
{
|
|
|
|
error(0, errno, "stat: %s", srcpath);
|
2014-06-27 16:46:50 -04:00
|
|
|
return close(srcfd), false;
|
2013-01-08 09:11:44 -05:00
|
|
|
}
|
2014-09-23 13:18:17 -04:00
|
|
|
if ( symbolic_dereference == SYMBOLIC_DEREFERENCE_ARGUMENTS )
|
|
|
|
symbolic_dereference = SYMBOLIC_DEREFERENCE_NONE;
|
|
|
|
// TODO: I'm really not satisfied with the atomicity properties in this case.
|
|
|
|
if ( S_ISLNK(srcst.st_mode) )
|
|
|
|
{
|
|
|
|
// TODO: How to handle the update flag in this case?
|
|
|
|
struct stat dstst;
|
|
|
|
int dstst_ret = fstatat(dstdirfd, dstrel, &dstst, AT_SYMLINK_NOFOLLOW);
|
|
|
|
if ( dstst_ret < 0 && errno != ENOENT )
|
|
|
|
{
|
|
|
|
error(0, errno, "%s", dstpath);
|
|
|
|
return close(srcfd), false;
|
|
|
|
}
|
|
|
|
bool already_is_symlink = false;
|
|
|
|
if ( dstst_ret == 0 )
|
|
|
|
{
|
|
|
|
if ( S_ISLNK(dstst.st_mode) )
|
|
|
|
{
|
|
|
|
already_is_symlink = true;
|
|
|
|
}
|
|
|
|
else if ( S_ISDIR(dstst.st_mode) )
|
|
|
|
{
|
|
|
|
error(0, 0, "cannot overwrite directory `%s' with non-directory",
|
|
|
|
dstpath);
|
|
|
|
return close(srcfd), false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( unlinkat(dstdirfd, dstrel, 0) == 0 )
|
|
|
|
{
|
|
|
|
if ( flags & FLAG_VERBOSE )
|
|
|
|
printf("removed `%s'\n", dstpath);
|
|
|
|
}
|
|
|
|
else if ( errno != ENOENT )
|
|
|
|
{
|
|
|
|
error(0, errno, "unlink: %s", dstpath);
|
|
|
|
return close(srcfd), false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( !already_is_symlink )
|
|
|
|
{
|
|
|
|
if ( symlinkat("", dstdirfd, dstrel) < 0 )
|
|
|
|
{
|
|
|
|
error(0, errno, "creating symlink: %s", dstpath);
|
|
|
|
return close(srcfd), false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int dstfd = openat(dstdirfd, dstrel, O_WRONLY | O_SYMLINK_NOFOLLOW);
|
|
|
|
if ( dstfd < 0 )
|
|
|
|
{
|
|
|
|
error(0, errno, "creating and opening symlink: %s", dstpath);
|
|
|
|
return close(srcfd), false;
|
|
|
|
}
|
|
|
|
if ( fstat(dstfd, &dstst) < 0 )
|
|
|
|
{
|
|
|
|
error(0, errno, "stat of symlink: %s", dstpath);
|
|
|
|
return close(dstfd), close(srcfd), false;
|
|
|
|
}
|
|
|
|
if ( !S_ISLNK(dstst.st_mode) )
|
|
|
|
{
|
|
|
|
error(0, errno, "unexpectedly not a symlink: %s", dstpath);
|
|
|
|
return close(dstfd), close(srcfd), false;
|
|
|
|
}
|
|
|
|
ret = CopyFileContents(srcfd, srcpath, dstfd, dstpath, flags);
|
|
|
|
close(dstfd);
|
|
|
|
}
|
|
|
|
else if ( S_ISDIR(srcst.st_mode) )
|
2013-01-08 09:11:44 -05:00
|
|
|
{
|
|
|
|
if ( !(flags & FLAG_RECURSIVE) )
|
|
|
|
{
|
|
|
|
error(0, 0, "omitting directory `%s'", srcpath);
|
2014-06-27 16:46:50 -04:00
|
|
|
return close(srcfd), false;
|
2013-01-08 09:11:44 -05:00
|
|
|
}
|
|
|
|
int dstfd = openat(dstdirfd, dstrel, O_RDONLY | O_DIRECTORY);
|
|
|
|
if ( dstfd < 0 )
|
|
|
|
{
|
|
|
|
if ( errno != ENOENT )
|
|
|
|
{
|
|
|
|
error(0, errno, "%s", dstpath);
|
2014-06-27 16:46:50 -04:00
|
|
|
return close(srcfd), false;
|
2013-01-08 09:11:44 -05:00
|
|
|
}
|
|
|
|
if ( mkdirat(dstdirfd, dstrel, srcst.st_mode & 03777) )
|
|
|
|
{
|
|
|
|
error(0, errno, "cannot create directory `%s'", dstpath);
|
2014-06-27 16:46:50 -04:00
|
|
|
return close(srcfd), false;
|
2013-01-08 09:11:44 -05:00
|
|
|
}
|
|
|
|
if ( (dstfd = openat(dstdirfd, dstrel, O_RDONLY | O_DIRECTORY)) < 0 )
|
|
|
|
{
|
|
|
|
error(0, errno, "%s", dstpath);
|
2014-06-27 16:46:50 -04:00
|
|
|
return close(srcfd), false;
|
2013-01-08 09:11:44 -05:00
|
|
|
}
|
|
|
|
}
|
2014-09-23 13:18:17 -04:00
|
|
|
ret = CopyDirectoryContents(srcfd, srcpath, dstfd, dstpath, flags,
|
|
|
|
symbolic_dereference);
|
2013-01-08 09:11:44 -05:00
|
|
|
close(dstfd);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-06-27 16:46:50 -04:00
|
|
|
if ( flags & FLAG_UPDATE )
|
|
|
|
{
|
|
|
|
struct stat dstst;
|
|
|
|
if ( fstatat(dstdirfd, dstrel, &dstst, 0) == 0 && S_ISREG(dstst.st_mode) )
|
|
|
|
{
|
|
|
|
if ( timespec_le(srcst.st_mtim, dstst.st_mtim) )
|
|
|
|
return close(srcfd), true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-08 09:11:44 -05:00
|
|
|
int dstfd = openat(dstdirfd, dstrel, O_WRONLY | O_CREAT, srcst.st_mode & 03777);
|
|
|
|
if ( dstfd < 0 )
|
2012-03-24 10:34:30 -04:00
|
|
|
{
|
2013-01-08 09:11:44 -05:00
|
|
|
error(0, errno, "%s", dstpath);
|
2014-06-27 16:46:50 -04:00
|
|
|
return close(srcfd), false;
|
2012-03-24 10:34:30 -04:00
|
|
|
}
|
2013-01-08 09:11:44 -05:00
|
|
|
ret = CopyFileContents(srcfd, srcpath, dstfd, dstpath, flags);
|
|
|
|
close(dstfd);
|
2011-11-21 08:57:02 -05:00
|
|
|
}
|
2013-01-08 09:11:44 -05:00
|
|
|
close(srcfd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CopyIntoDirectory(int srcdirfd, const char* srcrel, const char* srcpath,
|
|
|
|
int dstdirfd, const char* dstrel, const char* dstpath,
|
2014-09-23 13:18:17 -04:00
|
|
|
int flags, enum symbolic_dereference symbolic_dereference)
|
2013-01-08 09:11:44 -05:00
|
|
|
{
|
|
|
|
const char* src_basename = BaseName(srcrel);
|
|
|
|
int dstfd = openat(dstdirfd, dstrel, O_RDONLY | O_DIRECTORY);
|
|
|
|
if ( dstfd < 0 )
|
|
|
|
{
|
|
|
|
error(0, errno, "%s", dstpath);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
char* dstpath_new = AddElemToPath(dstpath, src_basename);
|
|
|
|
bool ret = CopyToDest(srcdirfd, srcrel, srcpath,
|
2014-09-23 13:18:17 -04:00
|
|
|
dstfd, src_basename, dstpath_new,
|
|
|
|
flags, symbolic_dereference);
|
2013-01-08 09:11:44 -05:00
|
|
|
free(dstpath_new);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CopyAmbigious(int srcdirfd, const char* srcrel, const char* srcpath,
|
|
|
|
int dstdirfd, const char* dstrel, const char* dstpath,
|
2014-09-23 13:18:17 -04:00
|
|
|
int flags, enum symbolic_dereference symbolic_dereference)
|
2013-01-08 09:11:44 -05:00
|
|
|
{
|
|
|
|
struct stat dstst;
|
2014-06-27 16:46:50 -04:00
|
|
|
if ( fstatat(dstdirfd, dstrel, &dstst, 0) < 0 )
|
2013-01-08 09:11:44 -05:00
|
|
|
{
|
|
|
|
if ( errno != ENOENT )
|
|
|
|
{
|
|
|
|
error(0, errno, "%s", dstpath);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
dstst.st_mode = S_IFREG;
|
|
|
|
}
|
|
|
|
if ( S_ISDIR(dstst.st_mode) )
|
|
|
|
return CopyIntoDirectory(srcdirfd, srcrel, srcpath,
|
2014-09-23 13:18:17 -04:00
|
|
|
dstdirfd, dstrel, dstpath,
|
|
|
|
flags, symbolic_dereference);
|
2013-01-08 09:11:44 -05:00
|
|
|
else
|
|
|
|
return CopyToDest(srcdirfd, srcrel, srcpath,
|
2014-09-23 13:18:17 -04:00
|
|
|
dstdirfd, dstrel, dstpath,
|
|
|
|
flags, symbolic_dereference);
|
2013-01-08 09:11:44 -05:00
|
|
|
}
|
|
|
|
|
2015-07-29 19:08:49 -04:00
|
|
|
static void compact_arguments(int* argc, char*** argv)
|
2014-06-27 16:46:50 -04:00
|
|
|
{
|
|
|
|
for ( int i = 0; i < *argc; i++ )
|
|
|
|
{
|
|
|
|
while ( i < *argc && !(*argv)[i] )
|
|
|
|
{
|
|
|
|
for ( int n = i; n < *argc; n++ )
|
|
|
|
(*argv)[n] = (*argv)[n+1];
|
|
|
|
(*argc)--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-29 19:08:49 -04:00
|
|
|
static void help(FILE* fp, const char* argv0)
|
2013-01-08 09:11:44 -05:00
|
|
|
{
|
|
|
|
fprintf(fp, "Usage: %s [OPTION]... [-T] SOURCE DEST\n", argv0);
|
|
|
|
fprintf(fp, " or: %s [OPTION]... SOURCE... DIRECTORY\n", argv0);
|
|
|
|
fprintf(fp, " or: %s [OPTION]... -t DIRECTORY SOURCE...\n", argv0);
|
2013-02-19 15:41:53 -05:00
|
|
|
#ifdef CP_PRETEND_TO_BE_INSTALL
|
|
|
|
fprintf(fp, "Copy files and set attributes.\n");
|
|
|
|
#else
|
2013-01-08 09:11:44 -05:00
|
|
|
fprintf(fp, "Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.\n");
|
2013-02-19 15:41:53 -05:00
|
|
|
#endif
|
2013-01-08 09:11:44 -05:00
|
|
|
}
|
|
|
|
|
2015-07-29 19:08:49 -04:00
|
|
|
static void version(FILE* fp, const char* argv0)
|
2013-01-08 09:11:44 -05:00
|
|
|
{
|
|
|
|
fprintf(fp, "%s (Sortix) %s\n", argv0, VERSIONSTR);
|
|
|
|
fprintf(fp, "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n");
|
|
|
|
fprintf(fp, "This is free software: you are free to change and redistribute it.\n");
|
|
|
|
fprintf(fp, "There is NO WARRANTY, to the extent permitted by law.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
const char* argv0 = argv[0];
|
2014-06-27 16:46:50 -04:00
|
|
|
int flags = 0;
|
|
|
|
const char* target_directory = NULL;
|
2014-08-02 06:15:29 -04:00
|
|
|
const char* preserve_list = NULL;
|
2014-09-23 13:18:17 -04:00
|
|
|
enum symbolic_dereference symbolic_dereference = SYMBOLIC_DEREFERENCE_DEFAULT;
|
2013-01-08 09:11:44 -05:00
|
|
|
for ( int i = 1; i < argc; i++ )
|
|
|
|
{
|
|
|
|
const char* arg = argv[i];
|
2015-07-29 19:08:49 -04:00
|
|
|
if ( arg[0] != '-' || !arg[1] )
|
2013-01-08 09:11:44 -05:00
|
|
|
continue;
|
|
|
|
argv[i] = NULL;
|
|
|
|
if ( !strcmp(arg, "--") )
|
|
|
|
break;
|
|
|
|
if ( arg[1] != '-' )
|
|
|
|
{
|
|
|
|
while ( char c = *++arg ) switch ( c )
|
|
|
|
{
|
2014-09-08 11:42:38 -04:00
|
|
|
#ifdef CP_PRETEND_TO_BE_INSTALL
|
|
|
|
case 'b': /* ignored */ break;
|
|
|
|
case 'c': /* ignored */ break;
|
|
|
|
case 'C': /* ignored */ break;
|
|
|
|
case 'd': /* ignored */ break;
|
|
|
|
case 'g': if ( *(arg + 1) ) arg = "g"; else if ( i + 1 != argc ) argv[++i] = NULL; break;
|
|
|
|
case 'm': if ( *(arg + 1) ) arg = "m"; else if ( i + 1 != argc ) argv[++i] = NULL; break;
|
|
|
|
case 'o': if ( *(arg + 1) ) arg = "o"; else if ( i + 1 != argc ) argv[++i] = NULL; break;
|
|
|
|
case 's': /* ignored */ break;
|
|
|
|
#endif
|
2014-09-23 13:18:17 -04:00
|
|
|
case 'H': symbolic_dereference = SYMBOLIC_DEREFERENCE_ARGUMENTS; break;
|
|
|
|
case 'L': symbolic_dereference = SYMBOLIC_DEREFERENCE_ALWAYS; break;
|
2013-01-08 09:11:44 -05:00
|
|
|
case 'r':
|
|
|
|
case 'R': flags |= FLAG_RECURSIVE; break;
|
|
|
|
case 'v': flags |= FLAG_VERBOSE; break;
|
2014-06-27 16:46:50 -04:00
|
|
|
case 't':
|
|
|
|
flags |= FLAG_TARGET_DIR;
|
|
|
|
if ( *(arg + 1) )
|
|
|
|
{
|
|
|
|
target_directory = arg + 1;
|
|
|
|
}
|
|
|
|
else if ( i + 1 == argc )
|
|
|
|
{
|
|
|
|
error(0, 0, "option requires an argument -- '%c'", c);
|
|
|
|
fprintf(stderr, "Try '%s --help' for more information\n", argv0);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
target_directory = argv[i+1];
|
|
|
|
argv[++i] = NULL;
|
|
|
|
}
|
|
|
|
arg = "t";
|
|
|
|
break;
|
2013-01-08 09:11:44 -05:00
|
|
|
case 'T': flags |= FLAG_NO_TARGET_DIR; break;
|
|
|
|
case 'u': flags |= FLAG_UPDATE; break;
|
2014-08-02 06:15:29 -04:00
|
|
|
case 'p': preserve_list = "mode,ownership,timestamps"; break;
|
2014-09-23 13:18:17 -04:00
|
|
|
case 'P': symbolic_dereference = SYMBOLIC_DEREFERENCE_NONE; break;
|
2013-01-08 09:11:44 -05:00
|
|
|
default:
|
2013-02-19 15:41:53 -05:00
|
|
|
#ifdef CP_PRETEND_TO_BE_INSTALL
|
|
|
|
fprintf(stderr, "%s (fake): unknown option, ignoring -- '%c'\n", argv0, c);
|
|
|
|
continue;
|
|
|
|
#endif
|
2013-01-08 09:11:44 -05:00
|
|
|
fprintf(stderr, "%s: unknown option -- '%c'\n", argv0, c);
|
2014-06-27 16:46:50 -04:00
|
|
|
help(stderr, argv0);
|
2013-01-08 09:11:44 -05:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2014-06-27 16:46:50 -04:00
|
|
|
else if ( !strcmp(arg, "--help") )
|
|
|
|
help(stdout, argv0), exit(0);
|
|
|
|
else if ( !strcmp(arg, "--version") )
|
|
|
|
version(stdout, argv0), exit(0);
|
|
|
|
else if ( !strcmp(arg, "--dereference") )
|
2014-09-23 13:18:17 -04:00
|
|
|
symbolic_dereference = SYMBOLIC_DEREFERENCE_ALWAYS;
|
2013-01-08 09:11:44 -05:00
|
|
|
else if ( !strcmp(arg, "--recursive") )
|
|
|
|
flags |= FLAG_RECURSIVE;
|
|
|
|
else if ( !strcmp(arg, "--verbose") )
|
|
|
|
flags |= FLAG_VERBOSE;
|
2014-08-02 06:15:29 -04:00
|
|
|
else if ( !strcmp(arg, "--preserve") )
|
|
|
|
preserve_list = "mode,ownership,timestamps";
|
|
|
|
else if ( !strncmp(arg, "--preserve=", strlen("--preserve=")) )
|
|
|
|
preserve_list = arg + strlen("--preserve=");
|
2013-01-08 09:11:44 -05:00
|
|
|
else if ( !strcmp(arg, "--target-directory") )
|
2014-06-27 16:46:50 -04:00
|
|
|
{
|
|
|
|
if ( i + 1 == argc )
|
|
|
|
{
|
2014-12-26 08:17:56 -05:00
|
|
|
error(0, 0, "option '--target-directory' requires an argument");
|
2014-06-27 16:46:50 -04:00
|
|
|
fprintf(stderr, "Try '%s --help' for more information\n", argv0);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
target_directory = argv[++i];
|
|
|
|
argv[i] = NULL;
|
2013-01-08 09:11:44 -05:00
|
|
|
flags |= FLAG_TARGET_DIR;
|
2014-06-27 16:46:50 -04:00
|
|
|
}
|
|
|
|
else if ( !strncmp(arg, "--target-directory=", strlen("--target-directory=")) )
|
|
|
|
{
|
|
|
|
target_directory = arg + strlen("--target-directory=");
|
|
|
|
flags |= FLAG_TARGET_DIR;
|
|
|
|
}
|
2013-01-08 09:11:44 -05:00
|
|
|
else if ( !strcmp(arg, "--no-target-directory") )
|
|
|
|
flags |= FLAG_NO_TARGET_DIR;
|
|
|
|
else if ( !strcmp(arg, "--update") )
|
|
|
|
flags |= FLAG_UPDATE;
|
|
|
|
else if ( !strcmp(arg, "--no-dereference") )
|
2014-09-23 13:18:17 -04:00
|
|
|
symbolic_dereference = SYMBOLIC_DEREFERENCE_NONE;
|
2013-01-08 09:11:44 -05:00
|
|
|
else
|
|
|
|
{
|
2013-02-19 15:41:53 -05:00
|
|
|
#ifdef CP_PRETEND_TO_BE_INSTALL
|
|
|
|
fprintf(stderr, "%s (fake): unknown option, ignoring: %s\n", argv0, arg);
|
|
|
|
continue;
|
|
|
|
#endif
|
2013-01-08 09:11:44 -05:00
|
|
|
fprintf(stderr, "%s: unknown option: %s\n", argv0, arg);
|
2014-06-27 16:46:50 -04:00
|
|
|
help(stderr, argv0);
|
2013-01-08 09:11:44 -05:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-27 16:46:50 -04:00
|
|
|
if ( (flags & FLAG_TARGET_DIR) && (flags & FLAG_NO_TARGET_DIR) )
|
|
|
|
error(1, 0, "cannot combine --target-directory (-t) and --no-target-directory (-T)");
|
2013-01-08 09:11:44 -05:00
|
|
|
|
2014-09-23 13:18:17 -04:00
|
|
|
if ( symbolic_dereference == SYMBOLIC_DEREFERENCE_DEFAULT )
|
|
|
|
{
|
|
|
|
if ( flags & FLAG_RECURSIVE )
|
|
|
|
symbolic_dereference = SYMBOLIC_DEREFERENCE_NONE;
|
|
|
|
else
|
|
|
|
symbolic_dereference = SYMBOLIC_DEREFERENCE_ALWAYS;
|
|
|
|
}
|
|
|
|
|
2014-08-02 06:15:29 -04:00
|
|
|
// TODO: Actually preserve.
|
|
|
|
(void) preserve_list;
|
|
|
|
|
2014-06-27 16:46:50 -04:00
|
|
|
compact_arguments(&argc, &argv);
|
2013-01-08 09:11:44 -05:00
|
|
|
|
2014-06-27 16:46:50 -04:00
|
|
|
if ( argc < 2 )
|
|
|
|
error(1, 0, "missing file operand");
|
|
|
|
|
|
|
|
if ( flags & FLAG_NO_TARGET_DIR )
|
2013-01-08 09:11:44 -05:00
|
|
|
{
|
|
|
|
const char* src = argv[1];
|
|
|
|
if ( argc < 3 )
|
|
|
|
error(1, 0, "missing destination file operand after `%s'", src);
|
|
|
|
const char* dst = argv[2];
|
|
|
|
if ( 3 < argc )
|
|
|
|
error(1, 0, "extra operand `%s'", argv[3]);
|
2014-09-23 13:18:17 -04:00
|
|
|
return CopyToDest(AT_FDCWD, src, src, AT_FDCWD, dst, dst, flags,
|
|
|
|
symbolic_dereference) ? 0 : 1;
|
2013-01-08 09:11:44 -05:00
|
|
|
}
|
|
|
|
|
2014-06-27 16:46:50 -04:00
|
|
|
if ( !(flags & FLAG_TARGET_DIR) && argc <= 3 )
|
|
|
|
{
|
|
|
|
const char* src = argv[1];
|
|
|
|
if ( argc < 3 )
|
|
|
|
error(1, 0, "missing destination file operand after `%s'", src);
|
|
|
|
const char* dst = argv[2];
|
|
|
|
if ( 3 < argc )
|
|
|
|
error(1, 0, "extra operand `%s'", argv[3]);
|
2014-09-23 13:18:17 -04:00
|
|
|
return CopyAmbigious(AT_FDCWD, src, src, AT_FDCWD, dst, dst, flags,
|
|
|
|
symbolic_dereference) ? 0 : 1;
|
2014-06-27 16:46:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( !target_directory )
|
|
|
|
{
|
|
|
|
target_directory = argv[--argc];
|
|
|
|
argv[argc] = NULL;
|
|
|
|
}
|
2013-01-08 09:11:44 -05:00
|
|
|
|
|
|
|
if ( argc < 2 )
|
|
|
|
error(1, 0, "missing file operand");
|
|
|
|
|
|
|
|
bool success = true;
|
|
|
|
for ( int i = 1; i < argc; i++ )
|
|
|
|
{
|
|
|
|
const char* src = argv[i];
|
2014-06-27 16:46:50 -04:00
|
|
|
const char* dst = target_directory;
|
2014-09-23 13:18:17 -04:00
|
|
|
if ( !CopyIntoDirectory(AT_FDCWD, src, src, AT_FDCWD, dst, dst, flags,
|
|
|
|
symbolic_dereference) )
|
2013-01-08 09:11:44 -05:00
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return success ? 0 : 1;
|
2011-11-21 08:57:02 -05:00
|
|
|
}
|