static const char* xttyname(int fd) { #ifndef __ANDROID__ return ttyname(fd); #else return xreadlink(xaprintf("/proc/self/fd/%d", fd)); #endif }
int readlink_main(const struct cmd_readlink_info* info) { char* linkname = xreadlink(info->link); size_t len = strlen(linkname); if (fwrite(linkname, 1, len, stdout) != len) die_errno("fwrite"); return 0; }
static int do_test (void) { char *dir = support_create_temp_directory ("tst-xreadlink-"); char *symlink_name = xasprintf ("%s/symlink", dir); add_temp_file (symlink_name); /* The limit 10000 is arbitrary and simply there to prevent an attempt to exhaust all available disk space. */ for (int size = 1; size < 10000; ++size) { char *contents = xmalloc (size + 1); for (int i = 0; i < size; ++i) contents[i] = 'a' + (rand () % 26); contents[size] = '\0'; if (symlink (contents, symlink_name) != 0) { if (errno == ENAMETOOLONG) { printf ("info: ENAMETOOLONG failure at %d bytes\n", size); free (contents); break; } FAIL_EXIT1 ("symlink (%d bytes): %m", size); } char *readlink_result = xreadlink (symlink_name); TEST_VERIFY (strcmp (readlink_result, contents) == 0); free (readlink_result); xunlink (symlink_name); free (contents); } /* Create an empty file to suppress the temporary file deletion warning. */ xclose (xopen (symlink_name, O_WRONLY | O_CREAT, 0)); free (symlink_name); free (dir); return 0; }
/* Determine the full pathname of the current executable, freshly allocated. Return NULL if unknown. Guaranteed to work on Linux and Woe32. Likely to work on the other Unixes (maybe except BeOS), under most conditions. */ static char * find_executable (const char *argv0) { #if defined WIN32_NATIVE /* Native Win32 only. On Cygwin, it is better to use the Cygwin provided /proc interface, than to use native Win32 API and cygwin_conv_to_posix_path, because it supports longer file names (see <http://cygwin.com/ml/cygwin/2011-01/msg00410.html>). */ char location[MAX_PATH]; int length = GetModuleFileName (NULL, location, sizeof (location)); if (length < 0) return NULL; if (!IS_PATH_WITH_DIR (location)) /* Shouldn't happen. */ return NULL; return xstrdup (location); #else /* Unix */ # ifdef __linux__ /* The executable is accessible as /proc/<pid>/exe. In newer Linux versions, also as /proc/self/exe. Linux >= 2.1 provides a symlink to the true pathname; older Linux versions give only device and ino, enclosed in brackets, which we cannot use here. */ { char *link; link = xreadlink ("/proc/self/exe"); if (link != NULL && link[0] != '[') return link; if (executable_fd < 0) executable_fd = open ("/proc/self/exe", O_EXEC, 0); { char buf[6+10+5]; sprintf (buf, "/proc/%d/exe", getpid ()); link = xreadlink (buf); if (link != NULL && link[0] != '[') return link; if (executable_fd < 0) executable_fd = open (buf, O_EXEC, 0); } } # endif # ifdef __CYGWIN__ /* The executable is accessible as /proc/<pid>/exe, at least in Cygwin >= 1.5. */ { char *link; link = xreadlink ("/proc/self/exe"); if (link != NULL) return link; if (executable_fd < 0) executable_fd = open ("/proc/self/exe", O_EXEC, 0); } # endif # if HAVE_MACH_O_DYLD_H && HAVE__NSGETEXECUTABLEPATH /* On MacOS X 10.2 or newer, the function int _NSGetExecutablePath (char *buf, uint32_t *bufsize); can be used to retrieve the executable's full path. */ char location[4096]; unsigned int length = sizeof (location); if (_NSGetExecutablePath (location, &length) == 0 && location[0] == '/') return canonicalize_file_name (location); # endif /* Guess the executable's full path. We assume the executable has been called via execlp() or execvp() with properly set up argv[0]. The login(1) convention to add a '-' prefix to argv[0] is not supported. */ { bool has_slash = false; { const char *p; for (p = argv0; *p; p++) if (*p == '/') { has_slash = true; break; } } if (!has_slash) { /* exec searches paths without slashes in the directory list given by $PATH. */ const char *path = getenv ("PATH"); if (path != NULL) { const char *p; const char *p_next; for (p = path; *p; p = p_next) { const char *q; size_t p_len; char *concat_name; for (q = p; *q; q++) if (*q == ':') break; p_len = q - p; p_next = (*q == '\0' ? q : q + 1); /* We have a path item at p, of length p_len. Now concatenate the path item and argv0. */ concat_name = (char *) xmalloc (p_len + strlen (argv0) + 2); # ifdef NO_XMALLOC if (concat_name == NULL) return NULL; # endif if (p_len == 0) /* An empty PATH element designates the current directory. */ strcpy (concat_name, argv0); else { memcpy (concat_name, p, p_len); concat_name[p_len] = '/'; strcpy (concat_name + p_len + 1, argv0); } if (maybe_executable (concat_name)) return canonicalize_file_name (concat_name); free (concat_name); } } /* Not found in the PATH, assume the current directory. */ } /* exec treats paths containing slashes as relative to the current directory. */ if (maybe_executable (argv0)) return canonicalize_file_name (argv0); } /* No way to find the executable. */ return NULL; #endif }
int copy_file(const char *source, const char *dest, int flags) { struct stat source_stat; struct stat dest_stat; int dest_exists = 1; int status = 0; if (((flags & FILEUTILS_PRESERVE_SYMLINKS) && lstat(source, &source_stat) < 0) || (!(flags & FILEUTILS_PRESERVE_SYMLINKS) && stat(source, &source_stat) < 0)) { perror_msg("%s", source); return -1; } if (stat(dest, &dest_stat) < 0) { if (errno != ENOENT) { perror_msg("unable to stat `%s'", dest); return -1; } dest_exists = 0; } if (dest_exists && source_stat.st_rdev == dest_stat.st_rdev && source_stat.st_ino == dest_stat.st_ino) { error_msg("`%s' and `%s' are the same file", source, dest); return -1; } if (S_ISDIR(source_stat.st_mode)) { DIR *dp; struct dirent *d; mode_t saved_umask = 0; if (!(flags & FILEUTILS_RECUR)) { error_msg("%s: omitting directory", source); return -1; } /* Create DEST. */ if (dest_exists) { if (!S_ISDIR(dest_stat.st_mode)) { error_msg("`%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); perror_msg("cannot create directory `%s'", dest); return -1; } umask(saved_umask); } /* Recursively copy files in SOURCE. */ if ((dp = opendir(source)) == NULL) { perror_msg("unable to open directory `%s'", source); status = -1; goto end; } while ((d = readdir(dp)) != NULL) { char *new_source, *new_dest; if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) continue; new_source = concat_path_file(source, d->d_name); new_dest = concat_path_file(dest, d->d_name); if (copy_file(new_source, new_dest, flags) < 0) status = -1; free(new_source); free(new_dest); } /* ??? What if an error occurs in readdir? */ if (closedir(dp) < 0) { perror_msg("unable to close directory `%s'", source); status = -1; } if (!dest_exists && chmod(dest, source_stat.st_mode & ~saved_umask) < 0) { perror_msg("unable to change permissions of `%s'", dest); status = -1; } } else if (S_ISREG(source_stat.st_mode)) { FILE *sfp, *dfp; if (dest_exists) { if ((dfp = fopen(dest, "w")) == NULL) { if (!(flags & FILEUTILS_FORCE)) { perror_msg("unable to open `%s'", dest); return -1; } if (unlink(dest) < 0) { perror_msg("unable to remove `%s'", dest); return -1; } dest_exists = 0; } } if (!dest_exists) { int fd; if ((fd = open(dest, O_WRONLY|O_CREAT, source_stat.st_mode)) < 0 || (dfp = fdopen(fd, "w")) == NULL) { if (fd >= 0) close(fd); perror_msg("unable to open `%s'", dest); return -1; } } if ((sfp = fopen(source, "r")) == NULL) { fclose(dfp); perror_msg("unable to open `%s'", source); status = -1; goto end; } if (copy_file_chunk(sfp, dfp, -1) < 0) status = -1; if (fclose(dfp) < 0) { perror_msg("unable to close `%s'", dest); status = -1; } if (fclose(sfp) < 0) { perror_msg("unable to close `%s'", source); status = -1; } } else if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) || S_ISSOCK(source_stat.st_mode)) { if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) { perror_msg("unable to create `%s'", dest); return -1; } } else if (S_ISFIFO(source_stat.st_mode)) { if (mkfifo(dest, source_stat.st_mode) < 0) { perror_msg("cannot create fifo `%s'", dest); return -1; } } else if (S_ISLNK(source_stat.st_mode)) { char *lpath = xreadlink(source); if (symlink(lpath, dest) < 0) { perror_msg("cannot create symlink `%s'", dest); return -1; } free(lpath); #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) if (flags & FILEUTILS_PRESERVE_STATUS) if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0) perror_msg("unable to preserve ownership of `%s'", dest); #endif return 0; } else { error_msg("internal error: unrecognized file type"); return -1; } end: if (flags & FILEUTILS_PRESERVE_STATUS) { struct utimbuf times; times.actime = source_stat.st_atime; times.modtime = source_stat.st_mtime; if (utime(dest, ×) < 0) perror_msg("unable to preserve times of `%s'", dest); if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) { source_stat.st_mode &= ~(S_ISUID | S_ISGID); perror_msg("unable to preserve ownership of `%s'", dest); } if (chmod(dest, source_stat.st_mode) < 0) perror_msg("unable to preserve permissions of `%s'", dest); } return status; }
/* Determine the full pathname of the current executable, freshly allocated. Return NULL if unknown. Guaranteed to work on Linux and Woe32. Likely to work on the other Unixes (maybe except BeOS), under most conditions. */ static char * find_executable (const char *argv0) { #if defined WIN32_NATIVE || defined __CYGWIN__ char location[MAX_PATH]; int length = GetModuleFileName (NULL, location, sizeof (location)); if (length < 0) return NULL; if (!IS_PATH_WITH_DIR (location)) /* Shouldn't happen. */ return NULL; { #if defined __CYGWIN__ /* cygwin-1.5.13 (2005-03-01) or newer would also allow a Linux-like implementation: readlink of "/proc/self/exe". But using the result of the Win32 system call is simpler and is consistent with the code in relocatable.c. */ /* On Cygwin, we need to convert paths coming from Win32 system calls to the Unix-like slashified notation. */ static char location_as_posix_path[2 * MAX_PATH]; /* There's no error return defined for cygwin_conv_to_posix_path. See cygwin-api/func-cygwin-conv-to-posix-path.html. Does it overflow the buffer of expected size MAX_PATH or does it truncate the path? I don't know. Let's catch both. */ cygwin_conv_to_posix_path (location, location_as_posix_path); location_as_posix_path[MAX_PATH - 1] = '\0'; if (strlen (location_as_posix_path) >= MAX_PATH - 1) /* A sign of buffer overflow or path truncation. */ return NULL; /* Call canonicalize_file_name, because Cygwin supports symbolic links. */ return canonicalize_file_name (location_as_posix_path); #else return xstrdup (location); #endif } #else /* Unix && !Cygwin */ #ifdef __linux__ /* The executable is accessible as /proc/<pid>/exe. In newer Linux versions, also as /proc/self/exe. Linux >= 2.1 provides a symlink to the true pathname; older Linux versions give only device and ino, enclosed in brackets, which we cannot use here. */ { char *link; link = xreadlink ("/proc/self/exe"); if (link != NULL && link[0] != '[') return link; if (executable_fd < 0) executable_fd = open ("/proc/self/exe", O_RDONLY, 0); { char buf[6+10+5]; sprintf (buf, "/proc/%d/exe", getpid ()); link = xreadlink (buf); if (link != NULL && link[0] != '[') return link; if (executable_fd < 0) executable_fd = open (buf, O_RDONLY, 0); } } #endif #if HAVE_MACH_O_DYLD_H && HAVE__NSGETEXECUTABLEPATH /* On MacOS X 10.2 or newer, the function int _NSGetExecutablePath (char *buf, unsigned long *bufsize); can be used to retrieve the executable's full path. */ char location[4096]; unsigned long length = sizeof (location); if (_NSGetExecutablePath (location, &length) == 0 && location[0] == '/') return canonicalize_file_name (location); #endif /* Guess the executable's full path. We assume the executable has been called via execlp() or execvp() with properly set up argv[0]. The login(1) convention to add a '-' prefix to argv[0] is not supported. */ { bool has_slash = false; { const char *p; for (p = argv0; *p; p++) if (*p == '/') { has_slash = true; break; } } if (!has_slash) { /* exec searches paths without slashes in the directory list given by $PATH. */ const char *path = getenv ("PATH"); if (path != NULL) { const char *p; const char *p_next; for (p = path; *p; p = p_next) { const char *q; size_t p_len; char *concat_name; for (q = p; *q; q++) if (*q == ':') break; p_len = q - p; p_next = (*q == '\0' ? q : q + 1); /* We have a path item at p, of length p_len. Now concatenate the path item and argv0. */ concat_name = (char *) xmalloc (p_len + strlen (argv0) + 2); #ifdef NO_XMALLOC if (concat_name == NULL) return NULL; #endif if (p_len == 0) /* An empty PATH element designates the current directory. */ strcpy (concat_name, argv0); else { memcpy (concat_name, p, p_len); concat_name[p_len] = '/'; strcpy (concat_name + p_len + 1, argv0); } if (maybe_executable (concat_name)) return canonicalize_file_name (concat_name); free (concat_name); } } /* Not found in the PATH, assume the current directory. */ } /* exec treats paths containing slashes as relative to the current directory. */ if (maybe_executable (argv0)) return canonicalize_file_name (argv0); } /* No way to find the executable. */ return NULL; #endif }
/* Write out a tar header for the specified file/directory/whatever */ static inline int writeTarHeader(struct TarBallInfo *tbInfo, const char *header_name, const char *real_name, struct stat *statbuf) { long chksum = 0; struct TarHeader header; const unsigned char *cp = (const unsigned char *) &header; ssize_t size = sizeof(struct TarHeader); memset(&header, 0, size); strncpy(header.name, header_name, sizeof(header.name)); putOctal(header.mode, sizeof(header.mode), statbuf->st_mode); putOctal(header.uid, sizeof(header.uid), statbuf->st_uid); putOctal(header.gid, sizeof(header.gid), statbuf->st_gid); putOctal(header.size, sizeof(header.size), 0); /* Regular file size is handled later */ putOctal(header.mtime, sizeof(header.mtime), statbuf->st_mtime); strncpy(header.magic, TAR_MAGIC TAR_VERSION, TAR_MAGIC_LEN + TAR_VERSION_LEN); /* Enter the user and group names (default to root if it fails) */ if (my_getpwuid(header.uname, statbuf->st_uid) == NULL) strcpy(header.uname, "root"); if (my_getgrgid(header.gname, statbuf->st_gid) == NULL) strcpy(header.gname, "root"); if (tbInfo->hlInfo) { /* This is a hard link */ header.typeflag = LNKTYPE; strncpy(header.linkname, tbInfo->hlInfo->name, sizeof(header.linkname)); } else if (S_ISLNK(statbuf->st_mode)) { char *lpath = xreadlink(real_name); if (!lpath) /* Already printed err msg inside xreadlink() */ return (FALSE); header.typeflag = SYMTYPE; strncpy(header.linkname, lpath, sizeof(header.linkname)); free(lpath); } else if (S_ISDIR(statbuf->st_mode)) { header.typeflag = DIRTYPE; strncat(header.name, "/", sizeof(header.name)); } else if (S_ISCHR(statbuf->st_mode)) { header.typeflag = CHRTYPE; putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev)); putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev)); } else if (S_ISBLK(statbuf->st_mode)) { header.typeflag = BLKTYPE; putOctal(header.devmajor, sizeof(header.devmajor), MAJOR(statbuf->st_rdev)); putOctal(header.devminor, sizeof(header.devminor), MINOR(statbuf->st_rdev)); } else if (S_ISFIFO(statbuf->st_mode)) { header.typeflag = FIFOTYPE; } else if (S_ISREG(statbuf->st_mode)) { header.typeflag = REGTYPE; putOctal(header.size, sizeof(header.size), statbuf->st_size); } else { bb_error_msg("%s: Unknown file type", real_name); return (FALSE); } /* Calculate and store the checksum (i.e., the sum of all of the bytes of * the header). The checksum field must be filled with blanks for the * calculation. The checksum field is formatted differently from the * other fields: it has [6] digits, a null, then a space -- rather than * digits, followed by a null like the other fields... */ memset(header.chksum, ' ', sizeof(header.chksum)); cp = (const unsigned char *) &header; while (size-- > 0) chksum += *cp++; putOctal(header.chksum, 7, chksum); /* Now write the header out to disk */ if ((size = bb_full_write(tbInfo->tarFd, (char *) &header, sizeof(struct TarHeader))) < 0) { bb_error_msg(bb_msg_io_error, real_name); return (FALSE); } /* Pad the header up to the tar block size */ for (; size < TAR_BLOCK_SIZE; size++) { write(tbInfo->tarFd, "\0", 1); } /* Now do the verbose thing (or not) */ if (tbInfo->verboseFlag) { FILE *vbFd = stdout; if (tbInfo->tarFd == STDOUT_FILENO) /* If the archive goes to stdout, verbose to stderr */ vbFd = stderr; fprintf(vbFd, "%s\n", header.name); } return (TRUE); }
/* Determine the full pathname of the current executable, freshly allocated. Return NULL if unknown. Guaranteed to work on Linux and Woe32. Likely to work on the other Unixes (maybe except BeOS), under most conditions. */ static char * find_executable (const char *argv0) { #ifdef WIN32 char buf[1024]; int length = GetModuleFileName (NULL, buf, sizeof (buf)); if (length < 0) return NULL; if (!IS_PATH_WITH_DIR (buf)) /* Shouldn't happen. */ return NULL; return xstrdup (buf); #else /* Unix */ #ifdef __linux__ /* The executable is accessible as /proc/<pid>/exe. In newer Linux versions, also as /proc/self/exe. Linux >= 2.1 provides a symlink to the true pathname; older Linux versions give only device and ino, enclosed in brackets, which we cannot use here. */ { char *link; link = xreadlink ("/proc/self/exe"); if (link != NULL && link[0] != '[') return link; if (executable_fd < 0) executable_fd = open ("/proc/self/exe", O_RDONLY, 0); { char buf[6+10+5]; sprintf (buf, "/proc/%d/exe", getpid ()); link = xreadlink (buf); if (link != NULL && link[0] != '[') return link; if (executable_fd < 0) executable_fd = open (buf, O_RDONLY, 0); } } #endif /* Guess the executable's full path. We assume the executable has been called via execlp() or execvp() with properly set up argv[0]. The login(1) convention to add a '-' prefix to argv[0] is not supported. */ { bool has_slash = false; { const char *p; for (p = argv0; *p; p++) if (*p == '/') { has_slash = true; break; } } if (!has_slash) { /* exec searches paths without slashes in the directory list given by $PATH. */ const char *path = getenv ("PATH"); if (path != NULL) { const char *p; const char *p_next; for (p = path; *p; p = p_next) { const char *q; size_t p_len; char *concat_name; for (q = p; *q; q++) if (*q == ':') break; p_len = q - p; p_next = (*q == '\0' ? q : q + 1); /* We have a path item at p, of length p_len. Now concatenate the path item and argv0. */ concat_name = (char *) xmalloc (p_len + strlen (argv0) + 2); #ifdef NO_XMALLOC if (concat_name == NULL) return NULL; #endif if (p_len == 0) /* An empty PATH element designates the current directory. */ strcpy (concat_name, argv0); else { memcpy (concat_name, p, p_len); concat_name[p_len] = '/'; strcpy (concat_name + p_len + 1, argv0); } if (maybe_executable (concat_name)) return canonicalize_file_name (concat_name); free (concat_name); } } /* Not found in the PATH, assume the current directory. */ } /* exec treats paths containing slashes as relative to the current directory. */ if (maybe_executable (argv0)) return canonicalize_file_name (argv0); } /* No way to find the executable. */ return NULL; #endif }
/*----------------------------------------------------------------------*/ static int list_single(struct dnode *dn) { int i, column = 0; #ifdef CONFIG_FEATURE_LS_USERNAME char scratch[16]; #endif #ifdef CONFIG_FEATURE_LS_TIMESTAMPS char *filetime; time_t ttime, age; #endif #if defined(CONFIG_FEATURE_LS_FILETYPES) || defined (CONFIG_FEATURE_LS_COLOR) struct stat info; char append; #endif if (dn->fullname == NULL) return (0); #ifdef CONFIG_FEATURE_LS_TIMESTAMPS ttime = dn->dstat.st_mtime; /* the default time */ if (all_fmt & TIME_ACCESS) ttime = dn->dstat.st_atime; if (all_fmt & TIME_CHANGE) ttime = dn->dstat.st_ctime; filetime = ctime(&ttime); #endif #ifdef CONFIG_FEATURE_LS_FILETYPES append = append_char(dn->dstat.st_mode); #endif for (i = 0; i <= 31; i++) { switch (all_fmt & (1 << i)) { case LIST_INO: column += printf("%7ld ", (long int) dn->dstat.st_ino); break; case LIST_BLOCKS: #if _FILE_OFFSET_BITS == 64 column += printf("%4lld ", dn->dstat.st_blocks >> 1); #else column += printf("%4ld ", dn->dstat.st_blocks >> 1); #endif break; case LIST_MODEBITS: column += printf("%-10s ", (char *) bb_mode_string(dn->dstat.st_mode)); break; case LIST_NLINKS: column += printf("%4ld ", (long) dn->dstat.st_nlink); break; case LIST_ID_NAME: #ifdef CONFIG_FEATURE_LS_USERNAME my_getpwuid(scratch, dn->dstat.st_uid); printf("%-8.8s ", scratch); my_getgrgid(scratch, dn->dstat.st_gid); printf("%-8.8s", scratch); column += 17; break; #endif case LIST_ID_NUMERIC: column += printf("%-8d %-8d", dn->dstat.st_uid, dn->dstat.st_gid); break; case LIST_SIZE: case LIST_DEV: if (S_ISBLK(dn->dstat.st_mode) || S_ISCHR(dn->dstat.st_mode)) { column += printf("%4d, %3d ", (int) MAJOR(dn->dstat.st_rdev), (int) MINOR(dn->dstat.st_rdev)); } else { #ifdef CONFIG_FEATURE_HUMAN_READABLE if (all_fmt & LS_DISP_HR) { column += printf("%9s ", make_human_readable_str(dn->dstat.st_size, 1, 0)); } else #endif { #if _FILE_OFFSET_BITS == 64 column += printf("%9lld ", (long long) dn->dstat.st_size); #else column += printf("%9ld ", dn->dstat.st_size); #endif } } break; #ifdef CONFIG_FEATURE_LS_TIMESTAMPS case LIST_FULLTIME: printf("%24.24s ", filetime); column += 25; break; case LIST_DATE_TIME: if ((all_fmt & LIST_FULLTIME) == 0) { age = time(NULL) - ttime; printf("%6.6s ", filetime + 4); if (age < 3600L * 24 * 365 / 2 && age > -15 * 60) { /* hh:mm if less than 6 months old */ printf("%5.5s ", filetime + 11); } else { printf(" %4.4s ", filetime + 20); } column += 13; } break; #endif #ifdef CONFIG_SELINUX case LIST_CONTEXT: { char context[64]; int len = sizeof(context); if(security_sid_to_context(dn->sid, context, &len)) { strcpy(context, "unknown"); len = 7; } printf("%-32s ", context); column += MAX(33, len); } break; #endif case LIST_FILENAME: #ifdef CONFIG_FEATURE_LS_COLOR errno = 0; if (show_color && !lstat(dn->fullname, &info)) { printf("\033[%d;%dm", bgcolor(info.st_mode), fgcolor(info.st_mode)); } #endif column += printf("%s", dn->name); #ifdef CONFIG_FEATURE_LS_COLOR if (show_color) { printf("\033[0m"); } #endif break; case LIST_SYMLINK: if (S_ISLNK(dn->dstat.st_mode)) { char *lpath = xreadlink(dn->fullname); if (lpath) { printf(" -> "); #if defined(CONFIG_FEATURE_LS_FILETYPES) || defined (CONFIG_FEATURE_LS_COLOR) if (!stat(dn->fullname, &info)) { append = append_char(info.st_mode); } #endif #ifdef CONFIG_FEATURE_LS_COLOR if (show_color) { errno = 0; printf("\033[%d;%dm", bgcolor(info.st_mode), fgcolor(info.st_mode)); } #endif column += printf("%s", lpath) + 4; #ifdef CONFIG_FEATURE_LS_COLOR if (show_color) { printf("\033[0m"); } #endif free(lpath); } } break; #ifdef CONFIG_FEATURE_LS_FILETYPES case LIST_FILETYPE: if (append != '\0') { printf("%1c", append); column++; } break; #endif } } return column; }
/* * Where in the filesystem is this busybox? * [return] * malloc'd string w/ full pathname of busybox's location * NULL on failure */ static inline char *busybox_fullpath(void) { return xreadlink("/proc/self/exe"); }
int busybox_main(int argc, char **argv) { /* * This style of argument parsing doesn't scale well * in the event that busybox starts wanting more --options. * If someone has a cleaner approach, by all means implement it. */ if (ENABLE_FEATURE_INSTALLER && argc > 1 && !strcmp(argv[1], "--install")) { int use_symbolic_links = 0; int rc = 0; char *busybox; /* to use symlinks, or not to use symlinks... */ if (argc > 2) { if ((strcmp(argv[2], "-s") == 0)) { use_symbolic_links = 1; } } /* link */ busybox = xreadlink("/proc/self/exe"); if (busybox) { install_links(busybox, use_symbolic_links); free(busybox); } else { rc = 1; } return rc; } /* Deal with --help. (Also print help when called with no arguments) */ if (argc==1 || !strcmp(argv[1],"--help") ) { if (argc>2) { run_applet_by_name(bb_applet_name=argv[2], 2, argv); } else { const struct BB_applet *a; int col, output_width; if (ENABLE_FEATURE_AUTOWIDTH) { /* Obtain the terminal width. */ get_terminal_width_height(0, &output_width, NULL); /* leading tab and room to wrap */ output_width -= 20; } else output_width = 60; printf("%s\n\n" "Usage: busybox [function] [arguments]...\n" " or: [function] [arguments]...\n\n" "\tBusyBox is a multi-call binary that combines many common Unix\n" "\tutilities into a single executable. Most people will create a\n" "\tlink to busybox for each function they wish to use and BusyBox\n" "\twill act like whatever it was invoked as!\n" "\nCurrently defined functions:\n", bb_msg_full_version); col=0; for(a = applets; a->name;) { col += printf("%s%s", (col ? ", " : "\t"), (a++)->name); if (col > output_width && a->name) { printf(",\n"); col = 0; } } printf("\n\n"); exit(0); } } else run_applet_by_name(bb_applet_name=argv[1], argc-1, argv+1); bb_error_msg_and_die("applet not found"); }
/* * Copies "from" to "to". */ int copy_file (const char *from, const char *to, int force_overwrite, int must_exist) { struct stat sb; struct utimbuf t; int fdin, fdout; #ifdef UTIME_EXPECTS_WRITABLE int change_it_back = 0; #endif TRACE(1,"copy(%s,%s)",PATCH_NULL(from),PATCH_NULL(to)); if (noexec) return 0; /* If the file to be copied is a link or a device, then just create the new link or device appropriately. */ if (islink (from)) { char *source = xreadlink (from); symlink (source, to); xfree (source); return 0; } if (isdevice (from)) { #if defined(HAVE_MKNOD) && defined(HAVE_STRUCT_STAT_ST_RDEV) if (stat (from, &sb) < 0) error (1, errno, "cannot stat %s", fn_root(from)); mknod (to, sb.st_mode, sb.st_rdev); #else error (1, 0, "cannot copy device files on this system (%s)", fn_root(from)); #endif } else { #ifdef MAC_HFS_STUFF /* Only use the mac specific copying routine in the client, since the server shouldn't have any need to handle resource forks (all the resource fork handling is done in the client - the server only handles flat files) */ if ( !server_active ) { if (stat (from, &sb) < 0) { if(must_exist) error (1, errno, "cannot stat %s", fn_root(from)); else return -1; } mac_copy_file(from, to, force_overwrite, must_exist); } else { #endif /* Not a link or a device... probably a regular file. */ if ((fdin = CVS_OPEN (from, O_RDONLY)) < 0) { if(must_exist) error (1, errno, "cannot open %s for copying", fn_root(from)); else return -1; } if (fstat (fdin, &sb) < 0) { if(must_exist) error (1, errno, "cannot fstat %s", fn_root(from)); else { close(fdin); return -1; } } if (force_overwrite && unlink_file (to) && !existence_error (errno)) error (1, errno, "unable to remove %s", to); if ((fdout = creat (to, (int) sb.st_mode & 07777)) < 0) error (1, errno, "cannot create %s for copying", fn_root(to)); if (sb.st_size > 0) { char buf[BUFSIZ]; int n; for (;;) { n = read (fdin, buf, sizeof(buf)); if (n == -1) { #ifdef EINTR if (errno == EINTR) continue; #endif error (1, errno, "cannot read file %s for copying", fn_root(from)); } else if (n == 0) break; if (write(fdout, buf, n) != n) { error (1, errno, "cannot write file %s for copying", fn_root(to)); } } #ifdef HAVE_FSYNC if (fsync (fdout)) error (1, errno, "cannot fsync file %s after copying", fn_root(to)); #endif } if (close (fdin) < 0) error (0, errno, "cannot close %s", fn_root(from)); if (close (fdout) < 0) error (1, errno, "cannot close %s", fn_root(to)); #ifdef MAC_HFS_STUFF } #endif } #ifdef UTIME_EXPECTS_WRITABLE if (!iswritable (to)) { xchmod (to, 1); change_it_back = 1; } #endif /* UTIME_EXPECTS_WRITABLE */ /* now, set the times for the copied file to match those of the original */ memset ((char *) &t, 0, sizeof (t)); t.actime = sb.st_atime; t.modtime = sb.st_mtime; (void) utime (to, &t); #ifdef UTIME_EXPECTS_WRITABLE if (change_it_back) xchmod (to, 0); #endif /* UTIME_EXPECTS_WRITABLE */ return 0; }
/* * Compare "file1" to "file2". Return non-zero if they don't compare exactly. * If FILE1 and FILE2 are special files, compare their salient characteristics * (i.e. major/minor device numbers, links, etc. */ int xcmp (const char *file1, const char *file2) { char *buf1, *buf2; struct stat sb1, sb2; int fd1, fd2; int ret; if (CVS_LSTAT (file1, &sb1) < 0) error (1, errno, "cannot lstat %s", fn_root(file1)); if (CVS_LSTAT (file2, &sb2) < 0) error (1, errno, "cannot lstat %s", fn_root(file2)); /* If FILE1 and FILE2 are not the same file type, they are unequal. */ if ((sb1.st_mode & S_IFMT) != (sb2.st_mode & S_IFMT)) return 1; /* If FILE1 and FILE2 are symlinks, they are equal if they point to the same thing. */ if (S_ISLNK (sb1.st_mode) && S_ISLNK (sb2.st_mode)) { int result; buf1 = xreadlink (file1); buf2 = xreadlink (file2); result = (strcmp (buf1, buf2) == 0); xfree (buf1); xfree (buf2); return result; } /* If FILE1 and FILE2 are devices, they are equal if their device numbers match. */ if (S_ISBLK (sb1.st_mode) || S_ISCHR (sb1.st_mode)) { #ifdef HAVE_STRUCT_STAT_ST_RDEV if (sb1.st_rdev == sb2.st_rdev) return 0; else return 1; #else error (1, 0, "cannot compare device files on this system (%s and %s)", file1, file2); #endif } if ((fd1 = CVS_OPEN (file1, O_RDONLY)) < 0) error (1, errno, "cannot open file %s for comparing", fn_root(file1)); if ((fd2 = CVS_OPEN (file2, O_RDONLY)) < 0) error (1, errno, "cannot open file %s for comparing", fn_root(file2)); /* A generic file compare routine might compare st_dev & st_ino here to see if the two files being compared are actually the same file. But that won't happen in CVS, so we won't bother. */ if (sb1.st_size != sb2.st_size) ret = 1; else if (sb1.st_size == 0) ret = 0; else { /* FIXME: compute the optimal buffer size by computing the least common multiple of the files st_blocks field */ size_t buf_size = 8 * 1024; size_t read1; size_t read2; buf1 = (char*)xmalloc (buf_size); buf2 = (char*)xmalloc (buf_size); do { read1 = block_read (fd1, buf1, buf_size); if (read1 == (size_t)-1) error (1, errno, "cannot read file %s for comparing", fn_root(file1)); read2 = block_read (fd2, buf2, buf_size); if (read2 == (size_t)-1) error (1, errno, "cannot read file %s for comparing", fn_root(file2)); /* assert (read1 == read2); */ ret = memcmp(buf1, buf2, read1); } while (ret == 0 && read1 == buf_size); xfree (buf1); xfree (buf2); } (void) close (fd1); (void) close (fd2); return (ret); }
/* * Copies "from" to "to", decompressing "from" on the fly */ int copy_and_unzip_file (const char *from, const char *to, int force_overwrite, int must_exist) { struct stat sb; struct utimbuf t; int fdin, fdout; z_stream zstr = {0}; int zstatus; char buf[BUFSIZ*10]; char zbuf[BUFSIZ*20]; int n; #ifdef UTIME_EXPECTS_WRITABLE int change_it_back = 0; #endif TRACE(1,"copy_and_unzip(%s,%s)",PATCH_NULL(from),PATCH_NULL(to)); if (noexec) return 0; /* If the file to be copied is a link or a device, then just create the new link or device appropriately. */ if (islink (from)) { char *source = xreadlink (from); symlink (source, to); xfree (source); return 0; } if (isdevice (from)) { #if defined(HAVE_MKNOD) && defined(HAVE_STRUCT_STAT_ST_RDEV) if (stat (from, &sb) < 0) error (1, errno, "cannot stat %s", fn_root(from)); mknod (to, sb.st_mode, sb.st_rdev); #else error (1, 0, "cannot copy device files on this system (%s)", fn_root(from)); #endif } else { /* Not a link or a device... probably a regular file. */ if ((fdin = CVS_OPEN (from, O_RDONLY)) < 0) { if(must_exist) error (1, errno, "cannot open %s for copying", fn_root(from)); else return -1; } if (fstat (fdin, &sb) < 0) { if(must_exist) error (1, errno, "cannot fstat %s", fn_root(from)); else { close(fdin); return -1; } } if (force_overwrite && unlink_file (to) && !existence_error (errno)) error (1, errno, "unable to remove %s", to); if ((fdout = creat (to, (int) sb.st_mode & 07777)) < 0) error (1, errno, "cannot create %s for copying", to); zstatus = inflateInit2 (&zstr, 47); if(zstatus != Z_OK) error(1, 0, "expansion error (INIT): (%d)%s", zstatus,zstr.msg); if (sb.st_size > 0) { for (;;) { n = read (fdin, buf, sizeof(buf)); if (n == -1) { #ifdef EINTR if (errno == EINTR) continue; #endif error (1, errno, "cannot read file %s for copying", fn_root(from)); } else if (n == 0) break; zstr.next_in = (Bytef*)buf; zstr.avail_in = n; while(zstr.avail_in) { zstr.next_out = (Bytef*)zbuf; zstr.avail_out = sizeof(zbuf); zstatus = inflate (&zstr, 0); if(zstatus != Z_OK && zstatus != Z_STREAM_END) error(1,0, "expansion error (inflate): (%d)%s", zstatus,zstr.msg); n = sizeof(zbuf)-zstr.avail_out; if (n && write(fdout, zbuf, n) != n) { error (1, errno, "cannot write file %s for copying", to); } } if(zstatus == Z_STREAM_END) break; } #ifdef HAVE_FSYNC if (fsync (fdout)) error (1, errno, "cannot fsync file %s after copying", fn_root(to)); #endif } zstr.next_out = (Bytef*)zbuf; zstr.avail_out = sizeof(zbuf); zstatus = inflate (&zstr, Z_FINISH); if(zstatus != Z_OK && zstatus != Z_STREAM_END) error(1,0, "expansion error (Z_FINISH): (%d)%s", zstatus,zstr.msg); n = sizeof(zbuf)-zstr.avail_out; if (n && write(fdout, zbuf, n) != n) { error (1, errno, "cannot write file %s for copying", fn_root(to)); } zstr.next_in = (Bytef*)buf; zstr.avail_in = 0; zstr.next_out = (Bytef*)zbuf; zstr.avail_out = sizeof(zbuf); zstatus = inflateEnd(&zstr); if(zstatus != Z_OK) error(1,0, "expansion error: %s", zstr.msg); if (close (fdin) < 0) error (0, errno, "cannot close %s", fn_root(from)); if (close (fdout) < 0) error (1, errno, "cannot close %s", fn_root(to)); } #ifdef UTIME_EXPECTS_WRITABLE if (!iswritable (to)) { xchmod (to, 1); change_it_back = 1; } #endif /* UTIME_EXPECTS_WRITABLE */ /* now, set the times for the copied file to match those of the original */ memset ((char *) &t, 0, sizeof (t)); t.actime = sb.st_atime; t.modtime = sb.st_mtime; (void) utime (to, &t); #ifdef UTIME_EXPECTS_WRITABLE if (change_it_back) xchmod (to, 0); #endif /* UTIME_EXPECTS_WRITABLE */ return 0; }
char * canonicalize_filename_mode (const char *name, canonicalize_mode_t can_mode) { char *rname, *dest, *extra_buf = NULL; char const *start; char const *end; char const *rname_limit; size_t extra_len = 0; struct cycle_check_state cycle_state; if (name == NULL) { __set_errno (EINVAL); return NULL; } if (name[0] == '\0') { __set_errno (ENOENT); return NULL; } if (name[0] != '/') { rname = xgetcwd (); if (!rname) return NULL; dest = strchr (rname, '\0'); if (dest - rname < PATH_MAX) { char *p = xrealloc (rname, PATH_MAX); dest = p + (dest - rname); rname = p; rname_limit = rname + PATH_MAX; } else { rname_limit = dest; } } else { rname = xmalloc (PATH_MAX); rname_limit = rname + PATH_MAX; rname[0] = '/'; dest = rname + 1; } cycle_check_init (&cycle_state); for (start = end = name; *start; start = end) { /* Skip sequence of multiple file name separators. */ while (*start == '/') ++start; /* Find end of component. */ for (end = start; *end && *end != '/'; ++end) /* Nothing. */; if (end - start == 0) break; else if (end - start == 1 && start[0] == '.') /* nothing */; else if (end - start == 2 && start[0] == '.' && start[1] == '.') { /* Back up to previous component, ignore if at root already. */ if (dest > rname + 1) while ((--dest)[-1] != '/'); } else { struct stat st; if (dest[-1] != '/') *dest++ = '/'; if (dest + (end - start) >= rname_limit) { ptrdiff_t dest_offset = dest - rname; size_t new_size = rname_limit - rname; if (end - start + 1 > PATH_MAX) new_size += end - start + 1; else new_size += PATH_MAX; rname = xrealloc (rname, new_size); rname_limit = rname + new_size; dest = rname + dest_offset; } dest = memcpy (dest, start, end - start); dest += end - start; *dest = '\0'; if (lstat (rname, &st) != 0) { if (can_mode == CAN_EXISTING) goto error; if (can_mode == CAN_ALL_BUT_LAST && *end) goto error; st.st_mode = 0; } if (S_ISLNK (st.st_mode)) { char *buf; size_t n, len; if (cycle_check (&cycle_state, &st)) { __set_errno (ELOOP); if (can_mode == CAN_MISSING) continue; else goto error; } buf = xreadlink (rname, st.st_size); if (!buf) { if (can_mode == CAN_MISSING) continue; else goto error; } n = strlen (buf); len = strlen (end); if (!extra_len) { extra_len = ((n + len + 1) > PATH_MAX) ? (n + len + 1) : PATH_MAX; extra_buf = xmalloc (extra_len); } else if ((n + len + 1) > extra_len) { extra_len = n + len + 1; extra_buf = xrealloc (extra_buf, extra_len); } /* Careful here, end may be a pointer into extra_buf... */ memmove (&extra_buf[n], end, len + 1); name = end = memcpy (extra_buf, buf, n); if (buf[0] == '/') dest = rname + 1; /* It's an absolute symlink */ else /* Back up to previous component, ignore if at root already: */ if (dest > rname + 1) while ((--dest)[-1] != '/'); free (buf); } else { if (!S_ISDIR (st.st_mode) && *end && (can_mode != CAN_MISSING)) { errno = ENOTDIR; goto error; } } } } if (dest > rname + 1 && dest[-1] == '/') --dest; *dest = '\0'; free (extra_buf); return rname; error: free (extra_buf); free (rname); return NULL; }
static struct filesystem_entry *add_host_filesystem_entry( char *name, char *path, unsigned long uid, unsigned long gid, unsigned long mode, dev_t rdev, struct filesystem_entry *parent) { int status; char *tmp; struct stat sb; time_t timestamp = time(NULL); struct filesystem_entry *entry; memset(&sb, 0, sizeof(struct stat)); status = lstat(path, &sb); if (status >= 0) { /* It is ok for some types of files to not exit on disk (such as * device nodes), but if they _do_ exist the specified mode had * better match the actual file or strange things will happen.... */ if ((mode & S_IFMT) != (sb.st_mode & S_IFMT)) { error_msg_and_die ("%s: file type does not match specified type!", path); } timestamp = sb.st_mtime; } else { /* If this is a regular file, it _must_ exist on disk */ if ((mode & S_IFMT) == S_IFREG) { error_msg_and_die("%s: does not exist!", path); } } /* Squash all permissions so files are owned by root, all * timestamps are _right now_, and file permissions * have group and other write removed */ if (squash_uids) { uid = gid = 0; } if (squash_perms) { if (!S_ISLNK(mode)) { mode &= ~(S_IWGRP | S_IWOTH); mode &= ~(S_ISUID | S_ISGID); } } if (fake_times) { timestamp = 0; } entry = xcalloc(1, sizeof(struct filesystem_entry)); entry->hostname = xstrdup(path); entry->fullname = xstrdup(name); tmp = xstrdup(name); entry->name = xstrdup(basename(tmp)); free(tmp); tmp = xstrdup(name); entry->path = xstrdup(dirname(tmp)); free(tmp); entry->sb.st_uid = uid; entry->sb.st_gid = gid; entry->sb.st_mode = mode; entry->sb.st_rdev = rdev; entry->sb.st_atime = entry->sb.st_ctime = entry->sb.st_mtime = timestamp; if (S_ISREG(mode)) { entry->sb.st_size = sb.st_size; } if (S_ISLNK(mode)) { entry->link = xreadlink(path); entry->sb.st_size = strlen(entry->link); } /* This happens only for root */ if (!parent) return (entry); /* Hook the file into the parent directory */ entry->parent = parent; if (!parent->files) { parent->files = entry; } else { struct filesystem_entry *prev; for (prev = parent->files; prev->next; prev = prev->next); prev->next = entry; entry->prev = prev; } return (entry); }
static unsigned int parse_directory(struct entry *root_entry, const char *name, struct entry **prev, off_t *fslen_ub) { struct dirent **dirlist; int totalsize = 0, dircount, dirindex; char *path, *endpath; size_t len = strlen(name); /* Set up the path. */ /* TODO: Reuse the parent's buffer to save memcpy'ing and duplication. */ path = xmalloc(len + 1 + MAX_INPUT_NAMELEN + 1); memcpy(path, name, len); endpath = path + len; *endpath = '/'; endpath++; /* read in the directory and sort */ dircount = scandir(name, &dirlist, 0, cramsort); if (dircount < 0) { error_msg_and_die("scandir failed: %s", name); } /* process directory */ for (dirindex = 0; dirindex < dircount; dirindex++) { struct dirent *dirent; struct entry *entry; struct stat st; int size; size_t namelen; dirent = dirlist[dirindex]; /* Ignore "." and ".." - we won't be adding them to the archive */ if (dirent->d_name[0] == '.') { if (dirent->d_name[1] == '\0') continue; if (dirent->d_name[1] == '.') { if (dirent->d_name[2] == '\0') continue; } } namelen = strlen(dirent->d_name); if (namelen > MAX_INPUT_NAMELEN) { error_msg_and_die( "Very long (%u bytes) filename `%s' found.\n" " Please increase MAX_INPUT_NAMELEN in mkcramfs.c and recompile. Exiting.\n", namelen, dirent->d_name); } memcpy(endpath, dirent->d_name, namelen + 1); if (lstat(path, &st) < 0) { perror(endpath); warn_skip = 1; continue; } entry = xcalloc(1, sizeof(struct entry)); entry->name = xstrdup(dirent->d_name); /* truncate multi-byte UTF-8 filenames on character boundary */ if (namelen > CRAMFS_MAXPATHLEN) { namelen = CRAMFS_MAXPATHLEN; warn_namelen = 1; /* the first lost byte must not be a trail byte */ while ((entry->name[namelen] & 0xc0) == 0x80) { namelen--; /* are we reasonably certain it was UTF-8 ? */ if (entry->name[namelen] < 0x80 || !namelen) { error_msg_and_die("cannot truncate filenames not encoded in UTF-8"); } } entry->name[namelen] = '\0'; } entry->mode = st.st_mode; entry->size = st.st_size; entry->uid = opt_squash ? 0 : st.st_uid; if (entry->uid >= 1 << CRAMFS_UID_WIDTH) warn_uid = 1; entry->gid = opt_squash ? 0 : st.st_gid; if (entry->gid >= 1 << CRAMFS_GID_WIDTH) { /* TODO: We ought to replace with a default gid instead of truncating; otherwise there are security problems. Maybe mode should be &= ~070. Same goes for uid once Linux supports >16-bit uids. */ warn_gid = 1; } size = sizeof(struct cramfs_inode) + ((namelen + 3) & ~3); *fslen_ub += size; if (S_ISDIR(st.st_mode)) { entry->size = parse_directory(root_entry, path, &entry->child, fslen_ub); } else if (S_ISREG(st.st_mode)) { if (entry->size) { if (access(path, R_OK) < 0) { warn_skip = 1; continue; } entry->path = xstrdup(path); if ((entry->size >= 1 << CRAMFS_SIZE_WIDTH)) { warn_size = 1; entry->size = (1 << CRAMFS_SIZE_WIDTH) - 1; } } } else if (S_ISLNK(st.st_mode)) { entry->uncompressed = xreadlink(path); if (!entry->uncompressed) { warn_skip = 1; continue; } } else if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode)) { /* maybe we should skip sockets */ entry->size = 0; } else if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) { entry->size = st.st_rdev; if (entry->size & -(1<<CRAMFS_SIZE_WIDTH)) warn_dev = 1; } else { error_msg_and_die("bogus file type: %s", entry->name); } if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) { int blocks = ((entry->size - 1) / blksize + 1); /* block pointers & data expansion allowance + data */ if (entry->size) *fslen_ub += (4+26)*blocks + entry->size + 3; } /* Link it into the list */ *prev = entry; prev = &entry->next; totalsize += size; } free(path); free(dirlist); /* allocated by scandir() with malloc() */ return totalsize; }
static int copy_internal (const char *src_path, const char *dst_path, int new_dst, dev_t device, struct dir_list *ancestors, const struct cp_options *x, int command_line_arg, int *copy_into_self, int *rename_succeeded) { struct stat src_sb; struct stat dst_sb; mode_t src_mode; mode_t src_type; char *earlier_file = NULL; char *dst_backup = NULL; int backup_succeeded = 0; int delayed_fail; int copied_as_regular = 0; int ran_chown = 0; int preserve_metadata; if (x->move_mode && rename_succeeded) *rename_succeeded = 0; *copy_into_self = 0; if ((*(x->xstat)) (src_path, &src_sb)) { error (0, errno, _("cannot stat %s"), quote (src_path)); return 1; } src_type = src_sb.st_mode; src_mode = src_sb.st_mode; if (S_ISDIR (src_type) && !x->recursive) { error (0, 0, _("omitting directory %s"), quote (src_path)); return 1; } /* Detect the case in which the same source file appears more than once on the command line and no backup option has been selected. If so, simply warn and don't copy it the second time. This check is enabled only if x->src_info is non-NULL. */ if (command_line_arg) { if ( ! S_ISDIR (src_sb.st_mode) && x->backup_type == none && seen_file (x->src_info, src_path, &src_sb)) { error (0, 0, _("warning: source file %s specified more than once"), quote (src_path)); return 0; } record_file (x->src_info, src_path, &src_sb); } if (!new_dst) { if ((*(x->xstat)) (dst_path, &dst_sb)) { if (errno != ENOENT) { error (0, errno, _("cannot stat %s"), quote (dst_path)); return 1; } else { new_dst = 1; } } else { int return_now; int ok = same_file_ok (src_path, &src_sb, dst_path, &dst_sb, x, &return_now); if (return_now) return 0; if (! ok) { error (0, 0, _("%s and %s are the same file"), quote_n (0, src_path), quote_n (1, dst_path)); return 1; } if (!S_ISDIR (dst_sb.st_mode)) { if (S_ISDIR (src_type)) { error (0, 0, _("cannot overwrite non-directory %s with directory %s"), quote_n (0, dst_path), quote_n (1, src_path)); return 1; } /* Don't let the user destroy their data, even if they try hard: This mv command must fail (likewise for cp): rm -rf a b c; mkdir a b c; touch a/f b/f; mv a/f b/f c Otherwise, the contents of b/f would be lost. In the case of `cp', b/f would be lost if the user simulated a move using cp and rm. Note that it works fine if you use --backup=numbered. */ if (command_line_arg && x->backup_type != numbered && seen_file (x->dest_info, dst_path, &dst_sb)) { error (0, 0, _("will not overwrite just-created %s with %s"), quote_n (0, dst_path), quote_n (1, src_path)); return 1; } } if (!S_ISDIR (src_type)) { if (S_ISDIR (dst_sb.st_mode)) { error (0, 0, _("cannot overwrite directory %s with non-directory"), quote (dst_path)); return 1; } if (x->update && MTIME_CMP (src_sb, dst_sb) <= 0) { /* We're using --update and the source file is older than the destination file, so there is no need to copy or move. */ /* Pretend the rename succeeded, so the caller (mv) doesn't end up removing the source file. */ if (rename_succeeded) *rename_succeeded = 1; return 0; } } /* When there is an existing destination file, we may end up returning early, and hence not copying/moving the file. This may be due to an interactive `negative' reply to the prompt about the existing file. It may also be due to the use of the --reply=no option. */ if (!S_ISDIR (src_type)) { /* cp and mv treat -i and -f differently. */ if (x->move_mode) { if ((x->interactive == I_ALWAYS_NO && UNWRITABLE (dst_path, dst_sb.st_mode)) || ((x->interactive == I_ASK_USER || (x->interactive == I_UNSPECIFIED && x->stdin_tty && UNWRITABLE (dst_path, dst_sb.st_mode))) && (overwrite_prompt (dst_path, &dst_sb), 1) && ! yesno ())) { /* Pretend the rename succeeded, so the caller (mv) doesn't end up removing the source file. */ if (rename_succeeded) *rename_succeeded = 1; return 0; } } else { if (x->interactive == I_ALWAYS_NO || (x->interactive == I_ASK_USER && (overwrite_prompt (dst_path, &dst_sb), 1) && ! yesno ())) { return 0; } } } if (x->move_mode) { /* In move_mode, DEST may not be an existing directory. */ if (S_ISDIR (dst_sb.st_mode)) { error (0, 0, _("cannot overwrite directory %s"), quote (dst_path)); return 1; } /* Don't allow user to move a directory onto a non-directory. */ if (S_ISDIR (src_sb.st_mode) && !S_ISDIR (dst_sb.st_mode)) { error (0, 0, _("cannot move directory onto non-directory: %s -> %s"), quote_n (0, src_path), quote_n (0, dst_path)); return 1; } } if (x->backup_type != none && !S_ISDIR (dst_sb.st_mode)) { char *tmp_backup = find_backup_file_name (dst_path, x->backup_type); if (tmp_backup == NULL) xalloc_die (); /* Detect (and fail) when creating the backup file would destroy the source file. Before, running the commands cd /tmp; rm -f a a~; : > a; echo A > a~; cp --b=simple a~ a would leave two zero-length files: a and a~. */ /* FIXME: but simply change e.g., the final a~ to `./a~' and the source will still be destroyed. */ if (STREQ (tmp_backup, src_path)) { const char *fmt; fmt = (x->move_mode ? _("backing up %s would destroy source; %s not moved") : _("backing up %s would destroy source; %s not copied")); error (0, 0, fmt, quote_n (0, dst_path), quote_n (1, src_path)); free (tmp_backup); return 1; } dst_backup = (char *) alloca (strlen (tmp_backup) + 1); strcpy (dst_backup, tmp_backup); free (tmp_backup); if (rename (dst_path, dst_backup)) { if (errno != ENOENT) { error (0, errno, _("cannot backup %s"), quote (dst_path)); return 1; } else { dst_backup = NULL; } } else { backup_succeeded = 1; } new_dst = 1; } else if (! S_ISDIR (dst_sb.st_mode) && (x->unlink_dest_before_opening || (x->xstat == lstat && ! S_ISREG (src_sb.st_mode)))) { if (unlink (dst_path) && errno != ENOENT) { error (0, errno, _("cannot remove %s"), quote (dst_path)); return 1; } new_dst = 1; } } } /* If the source is a directory, we don't always create the destination directory. So --verbose should not announce anything until we're sure we'll create a directory. */ if (x->verbose && !S_ISDIR (src_type)) { printf ("%s -> %s", quote_n (0, src_path), quote_n (1, dst_path)); if (backup_succeeded) printf (_(" (backup: %s)"), quote (dst_backup)); putchar ('\n'); } /* Associate the destination path with the source device and inode so that if we encounter a matching dev/ino pair in the source tree we can arrange to create a hard link between the corresponding names in the destination tree. Sometimes, when preserving links, we have to record dev/ino even though st_nlink == 1: - when using -H and processing a command line argument; that command line argument could be a symlink pointing to another command line argument. With `cp -H --preserve=link', we hard-link those two destination files. - likewise for -L except that it applies to all files, not just command line arguments. Also record directory dev/ino when using --recursive. We'll use that info to detect this problem: cp -R dir dir. FIXME-maybe: ideally, directory info would be recorded in a separate hash table, since such entries are useful only while a single command line hierarchy is being copied -- so that separate table could be cleared between command line args. Using the same hash table to preserve hard links means that it may not be cleared. */ if ((x->preserve_links && (1 < src_sb.st_nlink || (command_line_arg && x->dereference == DEREF_COMMAND_LINE_ARGUMENTS) || x->dereference == DEREF_ALWAYS)) || (x->recursive && S_ISDIR (src_type))) { earlier_file = remember_copied (dst_path, src_sb.st_ino, src_sb.st_dev); } /* Did we copy this inode somewhere else (in this command line argument) and therefore this is a second hard link to the inode? */ if (earlier_file) { /* Avoid damaging the destination filesystem by refusing to preserve hard-linked directories (which are found at least in Netapp snapshot directories). */ if (S_ISDIR (src_type)) { /* If src_path and earlier_file refer to the same directory entry, then warn about copying a directory into itself. */ if (same_name (src_path, earlier_file)) { error (0, 0, _("cannot copy a directory, %s, into itself, %s"), quote_n (0, top_level_src_path), quote_n (1, top_level_dst_path)); *copy_into_self = 1; } else { error (0, 0, _("will not create hard link %s to directory %s"), quote_n (0, dst_path), quote_n (1, earlier_file)); } goto un_backup; } { int link_failed; link_failed = link (earlier_file, dst_path); /* If the link failed because of an existing destination, remove that file and then call link again. */ if (link_failed && errno == EEXIST) { if (unlink (dst_path)) { error (0, errno, _("cannot remove %s"), quote (dst_path)); goto un_backup; } link_failed = link (earlier_file, dst_path); } if (link_failed) { error (0, errno, _("cannot create hard link %s to %s"), quote_n (0, dst_path), quote_n (1, earlier_file)); goto un_backup; } return 0; } } if (x->move_mode) { if (rename (src_path, dst_path) == 0) { if (x->verbose && S_ISDIR (src_type)) printf ("%s -> %s\n", quote_n (0, src_path), quote_n (1, dst_path)); if (rename_succeeded) *rename_succeeded = 1; if (command_line_arg) { /* Record destination dev/ino/filename, so that if we are asked to overwrite that file again, we can detect it and fail. */ /* It's fine to use the _source_ stat buffer (src_sb) to get the _destination_ dev/ino, since the rename above can't have changed those, and `mv' always uses lstat. We could limit it further by operating only on non-directories. */ record_file (x->dest_info, dst_path, &src_sb); } return 0; } /* FIXME: someday, consider what to do when moving a directory into itself but when source and destination are on different devices. */ /* This happens when attempting to rename a directory to a subdirectory of itself. */ if (errno == EINVAL /* When src_path is on an NFS file system, some types of clients, e.g., SunOS4.1.4 and IRIX-5.3, set errno to EIO instead. Testing for this here risks misinterpreting a real I/O error as an attempt to move a directory into itself, so FIXME: consider not doing this. */ || errno == EIO /* And with SunOS-4.1.4 client and OpenBSD-2.3 server, we get ENOTEMPTY. */ || errno == ENOTEMPTY) { /* FIXME: this is a little fragile in that it relies on rename(2) failing with a specific errno value. Expect problems on non-POSIX systems. */ error (0, 0, _("cannot move %s to a subdirectory of itself, %s"), quote_n (0, top_level_src_path), quote_n (1, top_level_dst_path)); /* Note that there is no need to call forget_created here, (compare with the other calls in this file) since the destination directory didn't exist before. */ *copy_into_self = 1; /* FIXME-cleanup: Don't return zero here; adjust mv.c accordingly. The only caller that uses this code (mv.c) ends up setting its exit status to nonzero when copy_into_self is nonzero. */ return 0; } /* WARNING: there probably exist systems for which an inter-device rename fails with a value of errno not handled here. If/as those are reported, add them to the condition below. If this happens to you, please do the following and send the output to the bug-reporting address (e.g., in the output of cp --help): touch k; perl -e 'rename "k","/tmp/k" or print "$!(",$!+0,")\n"' where your current directory is on one partion and /tmp is the other. Also, please try to find the E* errno macro name corresponding to the diagnostic and parenthesized integer, and include that in your e-mail. One way to do that is to run a command like this find /usr/include/. -type f \ | xargs grep 'define.*\<E[A-Z]*\>.*\<18\>' /dev/null where you'd replace `18' with the integer in parentheses that was output from the perl one-liner above. If necessary, of course, change `/tmp' to some other directory. */ if (errno != EXDEV) { /* There are many ways this can happen due to a race condition. When something happens between the initial xstat and the subsequent rename, we can get many different types of errors. For example, if the destination is initially a non-directory or non-existent, but it is created as a directory, the rename fails. If two `mv' commands try to rename the same file at about the same time, one will succeed and the other will fail. If the permissions on the directory containing the source or destination file are made too restrictive, the rename will fail. Etc. */ error (0, errno, _("cannot move %s to %s"), quote_n (0, src_path), quote_n (1, dst_path)); forget_created (src_sb.st_ino, src_sb.st_dev); return 1; } /* The rename attempt has failed. Remove any existing destination file so that a cross-device `mv' acts as if it were really using the rename syscall. */ if (unlink (dst_path) && errno != ENOENT) { error (0, errno, _("inter-device move failed: %s to %s; unable to remove target"), quote_n (0, src_path), quote_n (1, dst_path)); forget_created (src_sb.st_ino, src_sb.st_dev); return 1; } new_dst = 1; } delayed_fail = 0; /* In certain modes (cp's --symbolic-link), and for certain file types (symlinks and hard links) it doesn't make sense to preserve metadata, or it's possible to preserve only some of it. In such cases, set this variable to zero. */ preserve_metadata = 1; if (S_ISDIR (src_type)) { struct dir_list *dir; /* If this directory has been copied before during the recursion, there is a symbolic link to an ancestor directory of the symbolic link. It is impossible to continue to copy this, unless we've got an infinite disk. */ if (is_ancestor (&src_sb, ancestors)) { error (0, 0, _("cannot copy cyclic symbolic link %s"), quote (src_path)); goto un_backup; } /* Insert the current directory in the list of parents. */ dir = (struct dir_list *) alloca (sizeof (struct dir_list)); dir->parent = ancestors; dir->ino = src_sb.st_ino; dir->dev = src_sb.st_dev; if (new_dst || !S_ISDIR (dst_sb.st_mode)) { /* Create the new directory writable and searchable, so we can create new entries in it. */ if (mkdir (dst_path, (src_mode & x->umask_kill) | S_IRWXU)) { error (0, errno, _("cannot create directory %s"), quote (dst_path)); goto un_backup; } /* Insert the created directory's inode and device numbers into the search structure, so that we can avoid copying it again. */ if (remember_created (dst_path)) goto un_backup; if (x->verbose) printf ("%s -> %s\n", quote_n (0, src_path), quote_n (1, dst_path)); } /* Are we crossing a file system boundary? */ if (x->one_file_system && device != 0 && device != src_sb.st_dev) return 0; /* Copy the contents of the directory. */ if (copy_dir (src_path, dst_path, new_dst, &src_sb, dir, x, copy_into_self)) { /* Don't just return here -- otherwise, the failure to read a single file in a source directory would cause the containing destination directory not to have owner/perms set properly. */ delayed_fail = 1; } } #ifdef S_ISLNK else if (x->symbolic_link) { preserve_metadata = 0; if (*src_path != '/') { /* Check that DST_PATH denotes a file in the current directory. */ struct stat dot_sb; struct stat dst_parent_sb; char *dst_parent; int in_current_dir; dst_parent = dir_name (dst_path); in_current_dir = (STREQ (".", dst_parent) /* If either stat call fails, it's ok not to report the failure and say dst_path is in the current directory. Other things will fail later. */ || stat (".", &dot_sb) || stat (dst_parent, &dst_parent_sb) || SAME_INODE (dot_sb, dst_parent_sb)); free (dst_parent); if (! in_current_dir) { error (0, 0, _("%s: can make relative symbolic links only in current directory"), quote (dst_path)); goto un_backup; } } if (symlink (src_path, dst_path)) { error (0, errno, _("cannot create symbolic link %s to %s"), quote_n (0, dst_path), quote_n (1, src_path)); goto un_backup; } } #endif else if (x->hard_link) { preserve_metadata = 0; if (link (src_path, dst_path)) { error (0, errno, _("cannot create link %s"), quote (dst_path)); goto un_backup; } } else if (S_ISREG (src_type) || (x->copy_as_regular && !S_ISDIR (src_type) #ifdef S_ISLNK && !S_ISLNK (src_type) #endif )) { copied_as_regular = 1; /* POSIX says the permission bits of the source file must be used as the 3rd argument in the open call, but that's not consistent with historical practice. */ if (copy_reg (src_path, dst_path, x, get_dest_mode (x, src_mode), &new_dst, &src_sb)) goto un_backup; } else #ifdef S_ISFIFO if (S_ISFIFO (src_type)) { if (mkfifo (dst_path, get_dest_mode (x, src_mode))) { error (0, errno, _("cannot create fifo %s"), quote (dst_path)); goto un_backup; } } else #endif if (S_ISBLK (src_type) || S_ISCHR (src_type) #ifdef S_ISSOCK || S_ISSOCK (src_type) #endif ) { if (mknod (dst_path, get_dest_mode (x, src_mode), src_sb.st_rdev)) { error (0, errno, _("cannot create special file %s"), quote (dst_path)); goto un_backup; } } else #ifdef S_ISLNK if (S_ISLNK (src_type)) { char *src_link_val = xreadlink (src_path); if (src_link_val == NULL) { error (0, errno, _("cannot read symbolic link %s"), quote (src_path)); goto un_backup; } if (!symlink (src_link_val, dst_path)) free (src_link_val); else { int saved_errno = errno; int same_link = 0; if (x->update && !new_dst && S_ISLNK (dst_sb.st_mode)) { /* See if the destination is already the desired symlink. */ size_t src_link_len = strlen (src_link_val); char *dest_link_val = (char *) alloca (src_link_len + 1); int dest_link_len = readlink (dst_path, dest_link_val, src_link_len + 1); if ((size_t) dest_link_len == src_link_len && strncmp (dest_link_val, src_link_val, src_link_len) == 0) same_link = 1; } free (src_link_val); if (! same_link) { error (0, saved_errno, _("cannot create symbolic link %s"), quote (dst_path)); goto un_backup; } } /* There's no need to preserve timestamps or permissions. */ preserve_metadata = 0; if (x->preserve_ownership) { /* Preserve the owner and group of the just-`copied' symbolic link, if possible. */ # if HAVE_LCHOWN if (DO_CHOWN (lchown, dst_path, src_sb.st_uid, src_sb.st_gid)) { error (0, errno, _("failed to preserve ownership for %s"), dst_path); goto un_backup; } # else /* Can't preserve ownership of symlinks. FIXME: maybe give a warning or even error for symlinks in directories with the sticky bit set -- there, not preserving owner/group is a potential security problem. */ # endif } } else #endif { error (0, 0, _("%s has unknown file type"), quote (src_path)); goto un_backup; } if (command_line_arg) record_file (x->dest_info, dst_path, NULL); if ( ! preserve_metadata) return 0; /* POSIX says that `cp -p' must restore the following: - permission bits - setuid, setgid bits - owner and group If it fails to restore any of those, we may give a warning but the destination must not be removed. FIXME: implement the above. */ /* Adjust the times (and if possible, ownership) for the copy. chown turns off set[ug]id bits for non-root, so do the chmod last. */ if (x->preserve_timestamps) { struct utimbuf utb; /* There's currently no interface to set file timestamps with better than 1-second resolution, so discard any fractional part of the source timestamp. */ utb.actime = src_sb.st_atime; utb.modtime = src_sb.st_mtime; if (utime (dst_path, &utb)) { error (0, errno, _("preserving times for %s"), quote (dst_path)); if (x->require_preserve) return 1; } } /* Avoid calling chown if we know it's not necessary. */ if (x->preserve_ownership && (new_dst || !SAME_OWNER_AND_GROUP (src_sb, dst_sb))) { ran_chown = 1; if (DO_CHOWN (chown, dst_path, src_sb.st_uid, src_sb.st_gid)) { error (0, errno, _("failed to preserve ownership for %s"), quote (dst_path)); if (x->require_preserve) return 1; } } #if HAVE_STRUCT_STAT_ST_AUTHOR /* Preserve the st_author field. */ { file_t file = file_name_lookup (dst_path, 0, 0); if (file_chauthor (file, src_sb.st_author)) error (0, errno, _("failed to preserve authorship for %s"), quote (dst_path)); mach_port_deallocate (mach_task_self (), file); } #endif /* Permissions of newly-created regular files were set upon `open' in copy_reg. But don't return early if there were any special bits and we had to run chown, because the chown must have reset those bits. */ if ((new_dst && copied_as_regular) && !(ran_chown && (src_mode & ~S_IRWXUGO))) return delayed_fail; if ((x->preserve_mode || new_dst) && (x->copy_as_regular || S_ISREG (src_type) || S_ISDIR (src_type))) { if (chmod (dst_path, get_dest_mode (x, src_mode))) { error (0, errno, _("setting permissions for %s"), quote (dst_path)); if (x->set_mode || x->require_preserve) return 1; } } return delayed_fail; un_backup: /* We have failed to create the destination file. If we've just added a dev/ino entry via the remember_copied call above (i.e., unless we've just failed to create a hard link), remove the entry associating the source dev/ino with the destination file name, so we don't try to `preserve' a link to a file we didn't create. */ if (earlier_file == NULL) forget_created (src_sb.st_ino, src_sb.st_dev); if (dst_backup) { if (rename (dst_backup, dst_path)) error (0, errno, _("cannot un-backup %s"), quote (dst_path)); else { if (x->verbose) printf (_("%s -> %s (unbackup)\n"), quote_n (0, dst_backup), quote_n (1, dst_path)); } } return 1; }
int copy_file(const char *source, const char *dest, int flags) { struct stat source_stat; struct stat dest_stat; int dest_exists = 0; int status = 0; if ((!(flags & FILEUTILS_DEREFERENCE) && lstat(source, &source_stat) < 0) || ((flags & FILEUTILS_DEREFERENCE) && stat(source, &source_stat) < 0)) { bb_perror_msg("%s", source); return -1; } if (lstat(dest, &dest_stat) < 0) { if (errno != ENOENT) { bb_perror_msg("unable to 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 (S_ISDIR(source_stat.st_mode)) { DIR *dp; struct dirent *d; mode_t saved_umask = 0; if (!(flags & FILEUTILS_RECUR)) { bb_error_msg("%s: omitting directory", source); return -1; } /* Create DEST. */ if (dest_exists) { if (!S_ISDIR(dest_stat.st_mode)) { bb_error_msg("`%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("cannot create directory `%s'", dest); return -1; } umask(saved_umask); } /* Recursively copy files in SOURCE. */ if ((dp = opendir(source)) == NULL) { bb_perror_msg("unable to open directory `%s'", source); status = -1; goto end; } 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) < 0) status = -1; free(new_source); free(new_dest); } /* closedir have only EBADF error, but "dp" not changes */ closedir(dp); if (!dest_exists && chmod(dest, source_stat.st_mode & ~saved_umask) < 0) { bb_perror_msg("unable to change permissions of `%s'", dest); status = -1; } } else if (S_ISREG(source_stat.st_mode)) { int src_fd; int dst_fd = -1; #ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS char *link_name; if (!(flags & FILEUTILS_DEREFERENCE) && is_in_ino_dev_hashtable(&source_stat, &link_name)) { if (link(link_name, dest) < 0) { bb_perror_msg("unable to link `%s'", dest); return -1; } return 0; } #endif src_fd = open(source, O_RDONLY); if (src_fd == -1) { bb_perror_msg("unable to open `%s'", source); return(-1); } if (dest_exists) { if (flags & FILEUTILS_INTERACTIVE) { fprintf(stderr, "%s: overwrite `%s'? ", bb_applet_name, dest); if (!bb_ask_confirmation()) { close (src_fd); return 0; } } dst_fd = open(dest, O_WRONLY|O_TRUNC); if (dst_fd == -1) { if (!(flags & FILEUTILS_FORCE)) { bb_perror_msg("unable to open `%s'", dest); close(src_fd); return -1; } if (unlink(dest) < 0) { bb_perror_msg("unable to remove `%s'", dest); close(src_fd); return -1; } dest_exists = 0; } } if (!dest_exists) { dst_fd = open(dest, O_WRONLY|O_CREAT, source_stat.st_mode); if (dst_fd == -1) { bb_perror_msg("unable to open `%s'", dest); close(src_fd); return(-1); } } if (bb_copyfd_eof(src_fd, dst_fd) == -1) status = -1; if (close(dst_fd) < 0) { bb_perror_msg("unable to close `%s'", dest); status = -1; } if (close(src_fd) < 0) { bb_perror_msg("unable to close `%s'", source); status = -1; } } else 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) || S_ISLNK(source_stat.st_mode)) { if (dest_exists) { if((flags & FILEUTILS_FORCE) == 0) { fprintf(stderr, "`%s' exists\n", dest); return -1; } if(unlink(dest) < 0) { bb_perror_msg("unable to remove `%s'", dest); return -1; } } } else { bb_error_msg("internal error: unrecognized file type"); return -1; } if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) || S_ISSOCK(source_stat.st_mode)) { if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) { bb_perror_msg("unable to create `%s'", dest); return -1; } } else if (S_ISFIFO(source_stat.st_mode)) { if (mkfifo(dest, source_stat.st_mode) < 0) { bb_perror_msg("cannot create fifo `%s'", dest); return -1; } } else if (S_ISLNK(source_stat.st_mode)) { char *lpath; lpath = xreadlink(source); if (symlink(lpath, dest) < 0) { bb_perror_msg("cannot create symlink `%s'", dest); return -1; } free(lpath); #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) if (flags & FILEUTILS_PRESERVE_STATUS) if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0) bb_perror_msg("unable to preserve ownership of `%s'", dest); #endif #ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS add_to_ino_dev_hashtable(&source_stat, dest); #endif return 0; } #ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS if (! S_ISDIR(source_stat.st_mode)) { add_to_ino_dev_hashtable(&source_stat, dest); } #endif end: if (flags & FILEUTILS_PRESERVE_STATUS) { struct utimbuf times; times.actime = source_stat.st_atime; times.modtime = source_stat.st_mtime; if (utime(dest, ×) < 0) bb_perror_msg("unable to preserve times of `%s'", dest); if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) { source_stat.st_mode &= ~(S_ISUID | S_ISGID); bb_perror_msg("unable to preserve ownership of `%s'", dest); } if (chmod(dest, source_stat.st_mode) < 0) bb_perror_msg("unable to preserve permissions of `%s'", dest); } return status; }
int copy_file(const char *source, const char *dest, int flags) { struct stat source_stat; struct stat dest_stat; int status = 0; signed char dest_exists = 0; signed char ovr; #define FLAGS_DEREF (flags & FILEUTILS_DEREFERENCE) if ((FLAGS_DEREF ? stat : lstat)(source, &source_stat) < 0) { // This may be a dangling symlink. // Making [sym]links to dangling symlinks works, so... if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) goto make_links; bb_perror_msg("cannot stat '%s'", source); return -1; } if (lstat(dest, &dest_stat) < 0) { if (errno != ENOENT) { bb_perror_msg("cannot 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 (S_ISDIR(source_stat.st_mode)) { DIR *dp; struct dirent *d; mode_t saved_umask = 0; if (!(flags & FILEUTILS_RECUR)) { bb_error_msg("omitting directory '%s'", source); return -1; } /* Create DEST. */ 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("cannot create directory '%s'", dest); return -1; } umask(saved_umask); } /* Recursively copy files in SOURCE. */ dp = opendir(source); if (dp == NULL) { status = -1; goto preserve_status; } 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) < 0) status = -1; free(new_source); free(new_dest); } closedir(dp); if (!dest_exists && chmod(dest, source_stat.st_mode & ~saved_umask) < 0 ) { bb_perror_msg("cannot change permissions of '%s'", dest); status = -1; } } else if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) { int (*lf)(const char *oldpath, const char *newpath); make_links: // Hmm... maybe // if (DEREF && MAKE_SOFTLINK) source = realpath(source) ? // (but realpath returns NULL on dangling symlinks...) lf = (flags & FILEUTILS_MAKE_SOFTLINK) ? symlink : link; if (lf(source, dest) < 0) { ovr = retry_overwrite(dest, flags); if (ovr <= 0) return ovr; if (lf(source, dest) < 0) { bb_perror_msg("cannot create link '%s'", dest); return -1; } } return 0; } else if (S_ISREG(source_stat.st_mode) // Huh? DEREF uses stat, which never returns links IIRC... || (FLAGS_DEREF && S_ISLNK(source_stat.st_mode)) ) { int src_fd; int dst_fd; if (ENABLE_FEATURE_PRESERVE_HARDLINKS) { char *link_name; if (!FLAGS_DEREF && is_in_ino_dev_hashtable(&source_stat, &link_name) ) { if (link(link_name, dest) < 0) { ovr = retry_overwrite(dest, flags); if (ovr <= 0) return ovr; if (link(link_name, dest) < 0) { bb_perror_msg("cannot create link '%s'", dest); return -1; } } return 0; } // TODO: probably is_in_.. and add_to_... // can be combined: find_or_add_... add_to_ino_dev_hashtable(&source_stat, dest); } src_fd = open(source, O_RDONLY); if (src_fd == -1) { bb_perror_msg("cannot open '%s'", source); return -1; } // POSIX: if exists and -i, ask (w/o -i assume yes). // Then open w/o EXCL. // If open still fails and -f, try unlink, then try open again. // Result: a mess: // If dest is a softlink, we overwrite softlink's destination! // (or fail, if it points to dir/nonexistent location/etc). // This is strange, but POSIX-correct. // coreutils cp has --remove-destination to override this... dst_fd = open(dest, (flags & FILEUTILS_INTERACTIVE) ? O_WRONLY|O_CREAT|O_TRUNC|O_EXCL : O_WRONLY|O_CREAT|O_TRUNC, source_stat.st_mode); if (dst_fd == -1) { // We would not do POSIX insanity. -i asks, // then _unlinks_ the offender. Presto. // Or else we will end up having 3 open()s! ovr = retry_overwrite(dest, flags); if (ovr <= 0) { close(src_fd); return ovr; } dst_fd = open(dest, O_WRONLY|O_CREAT|O_TRUNC, source_stat.st_mode); if (dst_fd == -1) { bb_perror_msg("cannot open '%s'", dest); close(src_fd); return -1; } } if (bb_copyfd_eof(src_fd, dst_fd) == -1) status = -1; if (close(dst_fd) < 0) { bb_perror_msg("cannot close '%s'", dest); status = -1; } if (close(src_fd) < 0) { bb_perror_msg("cannot close '%s'", source); status = -1; } } else 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) || S_ISLNK(source_stat.st_mode) ) { // We are lazy here, a bit lax with races... if (dest_exists) { ovr = retry_overwrite(dest, flags); if (ovr <= 0) return ovr; } if (S_ISFIFO(source_stat.st_mode)) { if (mkfifo(dest, source_stat.st_mode) < 0) { bb_perror_msg("cannot create fifo '%s'", dest); return -1; } } else if (S_ISLNK(source_stat.st_mode)) { char *lpath; lpath = xreadlink(source); if (symlink(lpath, dest) < 0) { bb_perror_msg("cannot create symlink '%s'", dest); free(lpath); return -1; } free(lpath); if (flags & FILEUTILS_PRESERVE_STATUS) if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0) bb_perror_msg("cannot preserve %s of '%s'", "ownership", dest); return 0; } else { if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) { bb_perror_msg("cannot create '%s'", dest); return -1; } } } else { bb_error_msg("internal error: unrecognized file type"); return -1; } preserve_status: if (flags & FILEUTILS_PRESERVE_STATUS /* Cannot happen: */ /* && !(flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) */ ) { struct utimbuf times; times.actime = source_stat.st_atime; times.modtime = source_stat.st_mtime; if (utime(dest, ×) < 0) bb_perror_msg("cannot 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("cannot preserve %s of '%s'", "ownership", dest); } if (chmod(dest, source_stat.st_mode) < 0) bb_perror_msg("cannot preserve %s of '%s'", "permissions", dest); } return status; }