123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427 |
- #include "libbb.h"
- static int ask_and_unlink(const char *dest, int flags)
- {
- int e = errno;
- #if !ENABLE_FEATURE_NON_POSIX_CP
- if (!(flags & (FILEUTILS_FORCE|FILEUTILS_INTERACTIVE))) {
-
- bb_perror_msg("can't create '%s'", dest);
- return -1;
- }
- #endif
-
-
-
-
-
-
-
- if (flags & FILEUTILS_INTERACTIVE) {
-
-
-
-
- fprintf(stderr, "%s: overwrite '%s'? ", applet_name, dest);
- if (!bb_ask_confirmation())
- return 0;
- }
- if (unlink(dest) < 0) {
- #if ENABLE_FEATURE_VERBOSE_CP_MESSAGE
- if (e == errno && e == ENOENT) {
-
- bb_error_msg("can't create '%s': Path does not exist", dest);
- return -1;
- }
- #endif
- errno = e;
- bb_perror_msg("can't create '%s'", dest);
- return -1;
- }
- #if ENABLE_FEATURE_CP_LONG_OPTIONS
- if (flags & FILEUTILS_RMDEST)
- if (flags & FILEUTILS_VERBOSE)
- printf("removed '%s'\n", dest);
- #endif
- return 1;
- }
- int FAST_FUNC copy_file(const char *source, const char *dest, int flags)
- {
-
-
- struct stat source_stat;
- struct stat dest_stat;
- smallint retval = 0;
- smallint dest_exists = 0;
- smallint ovr;
- #define FLAGS_DEREF (flags & (FILEUTILS_DEREFERENCE + FILEUTILS_DEREFERENCE_L0))
- if ((FLAGS_DEREF ? stat : lstat)(source, &source_stat) < 0) {
-
- if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK))
- goto make_links;
- bb_perror_msg("can't stat '%s'", source);
- return -1;
- }
- if (lstat(dest, &dest_stat) < 0) {
- if (errno != ENOENT) {
- bb_perror_msg("can't stat '%s'", dest);
- return -1;
- }
- } else {
- if (source_stat.st_dev == dest_stat.st_dev
- && source_stat.st_ino == dest_stat.st_ino
- ) {
- bb_error_msg("'%s' and '%s' are the same file", source, dest);
- return -1;
- }
- dest_exists = 1;
- }
- #if ENABLE_SELINUX
- if ((flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) && is_selinux_enabled() > 0) {
- security_context_t con;
- if (lgetfilecon(source, &con) >= 0) {
- if (setfscreatecon(con) < 0) {
- bb_perror_msg("can't set setfscreatecon %s", con);
- freecon(con);
- return -1;
- }
- } else if (errno == ENOTSUP || errno == ENODATA) {
- setfscreatecon_or_die(NULL);
- } else {
- bb_perror_msg("can't lgetfilecon %s", source);
- return -1;
- }
- }
- #endif
- if (S_ISDIR(source_stat.st_mode)) {
- DIR *dp;
- const char *tp;
- struct dirent *d;
- mode_t saved_umask = 0;
- if (!(flags & FILEUTILS_RECUR)) {
- bb_error_msg("omitting directory '%s'", source);
- return -1;
- }
-
- tp = is_in_ino_dev_hashtable(&source_stat);
- if (tp) {
-
- bb_error_msg("recursion detected, omitting directory '%s'",
- source);
- return -1;
- }
- if (dest_exists) {
- if (!S_ISDIR(dest_stat.st_mode)) {
- bb_error_msg("target '%s' is not a directory", dest);
- return -1;
- }
-
- } else {
-
- mode_t mode;
- saved_umask = umask(0);
- mode = source_stat.st_mode;
- if (!(flags & FILEUTILS_PRESERVE_STATUS))
- mode = source_stat.st_mode & ~saved_umask;
-
- mode |= S_IRWXU;
- if (mkdir(dest, mode) < 0) {
- umask(saved_umask);
- bb_perror_msg("can't create directory '%s'", dest);
- return -1;
- }
- umask(saved_umask);
-
- if (lstat(dest, &dest_stat) < 0) {
- bb_perror_msg("can't stat '%s'", dest);
- return -1;
- }
- }
-
- add_to_ino_dev_hashtable(&dest_stat, NULL);
-
- dp = opendir(source);
- if (dp == NULL) {
- retval = -1;
- goto preserve_mode_ugid_time;
- }
- while ((d = readdir(dp)) != NULL) {
- char *new_source, *new_dest;
- new_source = concat_subpath_file(source, d->d_name);
- if (new_source == NULL)
- continue;
- new_dest = concat_path_file(dest, d->d_name);
- if (copy_file(new_source, new_dest, flags & ~FILEUTILS_DEREFERENCE_L0) < 0)
- retval = -1;
- free(new_source);
- free(new_dest);
- }
- closedir(dp);
- if (!dest_exists
- && chmod(dest, source_stat.st_mode & ~saved_umask) < 0
- ) {
- bb_perror_msg("can't preserve %s of '%s'", "permissions", dest);
-
- }
- goto preserve_mode_ugid_time;
- }
- if (dest_exists) {
- if (flags & FILEUTILS_UPDATE) {
- if (source_stat.st_mtime <= dest_stat.st_mtime) {
- return 0;
- }
- }
- #if ENABLE_FEATURE_CP_LONG_OPTIONS
- if (flags & FILEUTILS_RMDEST) {
- ovr = ask_and_unlink(dest, flags);
- if (ovr <= 0)
- return ovr;
- dest_exists = 0;
- }
- #endif
- }
- if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) {
- int (*lf)(const char *oldpath, const char *newpath);
- make_links:
-
- lf = (flags & FILEUTILS_MAKE_SOFTLINK) ? symlink : link;
- if (lf(source, dest) < 0) {
- ovr = ask_and_unlink(dest, flags);
- if (ovr <= 0)
- return ovr;
- if (lf(source, dest) < 0) {
- bb_perror_msg("can't create link '%s'", dest);
- return -1;
- }
- }
-
- return 0;
- }
- if (
- !(flags & FILEUTILS_RECUR)
-
- || S_ISREG(source_stat.st_mode)
-
-
- ) {
- int src_fd;
- int dst_fd;
- mode_t new_mode;
- if (!FLAGS_DEREF && S_ISLNK(source_stat.st_mode)) {
-
- goto dont_cat;
- }
- if (ENABLE_FEATURE_PRESERVE_HARDLINKS && !FLAGS_DEREF) {
- const char *link_target;
- link_target = is_in_ino_dev_hashtable(&source_stat);
- if (link_target) {
- if (link(link_target, dest) < 0) {
- ovr = ask_and_unlink(dest, flags);
- if (ovr <= 0)
- return ovr;
- if (link(link_target, dest) < 0) {
- bb_perror_msg("can't create link '%s'", dest);
- return -1;
- }
- }
- return 0;
- }
- add_to_ino_dev_hashtable(&source_stat, dest);
- }
- src_fd = open_or_warn(source, O_RDONLY);
- if (src_fd < 0)
- return -1;
-
- new_mode = source_stat.st_mode;
- if (!S_ISREG(source_stat.st_mode))
- new_mode = 0666;
- if (ENABLE_FEATURE_NON_POSIX_CP || (flags & FILEUTILS_INTERACTIVE)) {
-
- dst_fd = open(dest, O_WRONLY|O_CREAT|O_EXCL, new_mode);
- } else {
-
- dst_fd = open(dest, O_WRONLY|O_CREAT|O_TRUNC, new_mode);
- }
- if (dst_fd == -1) {
- ovr = ask_and_unlink(dest, flags);
- if (ovr <= 0) {
- close(src_fd);
- return ovr;
- }
-
- dst_fd = open3_or_warn(dest, O_WRONLY|O_CREAT|O_EXCL, new_mode);
- if (dst_fd < 0) {
- close(src_fd);
- return -1;
- }
- }
- #if ENABLE_SELINUX
- if ((flags & (FILEUTILS_PRESERVE_SECURITY_CONTEXT|FILEUTILS_SET_SECURITY_CONTEXT))
- && is_selinux_enabled() > 0
- ) {
- security_context_t con;
- if (getfscreatecon(&con) == -1) {
- bb_perror_msg("getfscreatecon");
- return -1;
- }
- if (con) {
- if (setfilecon(dest, con) == -1) {
- bb_perror_msg("setfilecon:%s,%s", dest, con);
- freecon(con);
- return -1;
- }
- freecon(con);
- }
- }
- #endif
- if (bb_copyfd_eof(src_fd, dst_fd) == -1)
- retval = -1;
-
- if (close(dst_fd) < 0) {
- bb_perror_msg("error writing to '%s'", dest);
- retval = -1;
- }
-
- close(src_fd);
-
- if (!S_ISREG(source_stat.st_mode))
- return retval;
- goto preserve_mode_ugid_time;
- }
- dont_cat:
-
-
- if (dest_exists) {
- errno = EEXIST;
- ovr = ask_and_unlink(dest, flags);
- if (ovr <= 0)
- return ovr;
- }
- if (S_ISLNK(source_stat.st_mode)) {
- char *lpath = xmalloc_readlink_or_warn(source);
- if (lpath) {
- int r = symlink(lpath, dest);
- free(lpath);
- if (r < 0) {
-
- bb_perror_msg("can't create %slink '%s' to '%s'",
- "sym", dest, lpath
- );
- return -1;
- }
- if (flags & FILEUTILS_PRESERVE_STATUS)
- if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0)
- bb_perror_msg("can't preserve %s of '%s'", "ownership", dest);
- }
-
- goto verb_and_exit;
- }
- if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode)
- || S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode)
- ) {
- if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) {
- bb_perror_msg("can't create '%s'", dest);
- return -1;
- }
- } else {
- bb_error_msg("unrecognized file '%s' with mode %x", source, source_stat.st_mode);
- return -1;
- }
- preserve_mode_ugid_time:
- if (flags & FILEUTILS_PRESERVE_STATUS
-
-
- ) {
- struct timeval times[2];
- times[1].tv_sec = times[0].tv_sec = source_stat.st_mtime;
- times[1].tv_usec = times[0].tv_usec = 0;
-
- if (utimes(dest, times) < 0)
- bb_perror_msg("can't preserve %s of '%s'", "times", dest);
- if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) {
- source_stat.st_mode &= ~(S_ISUID | S_ISGID);
- bb_perror_msg("can't preserve %s of '%s'", "ownership", dest);
- }
- if (chmod(dest, source_stat.st_mode) < 0)
- bb_perror_msg("can't preserve %s of '%s'", "permissions", dest);
- }
- verb_and_exit:
- if (flags & FILEUTILS_VERBOSE) {
- printf("'%s' -> '%s'\n", source, dest);
- }
- return retval;
- }
|