/*
* Mini copy_file implementation for busybox
*
- *
* Copyright (C) 2001 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
*
* This program is free software; you can redistribute it and/or modify
#include <errno.h>
#include <dirent.h>
#include <stdlib.h>
+#include <string.h>
#include "libbb.h"
int dest_exists = 1;
int status = 0;
- if (((flags & FILEUTILS_PRESERVE_SYMLINKS) &&
+ if ((!(flags & FILEUTILS_DEREFERENCE) &&
lstat(source, &source_stat) < 0) ||
- (!(flags & FILEUTILS_PRESERVE_SYMLINKS) &&
+ ((flags & FILEUTILS_DEREFERENCE) &&
stat(source, &source_stat) < 0)) {
perror_msg("%s", source);
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;
}
} else {
- mode_t mode, saved_umask;
+ mode_t mode;
saved_umask = umask(0);
mode = source_stat.st_mode;
umask(saved_umask);
}
-
+
/* Recursively copy files in SOURCE. */
if ((dp = opendir(source)) == NULL) {
perror_msg("unable to open directory `%s'", source);
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;
+ FILE *sfp, *dfp=NULL;
if (dest_exists) {
if (flags & FILEUTILS_INTERACTIVE) {
goto end;
}
- copy_file_chunk(sfp, dfp, source_stat.st_size);
+ if (copy_file_chunk(sfp, dfp, -1) < 0)
+ status = -1;
if (fclose(dfp) < 0) {
perror_msg("unable to close `%s'", dest);
return -1;
}
} else if (S_ISFIFO(source_stat.st_mode)) {
- mode_t mode, saved_umask;
- 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 (mkfifo(dest, mode) < 0) {
- umask(saved_umask);
+ if (mkfifo(dest, source_stat.st_mode) < 0) {
perror_msg("cannot create fifo `%s'", dest);
return -1;
}
-
- umask(saved_umask);
} else if (S_ISLNK(source_stat.st_mode)) {
- int size;
- char buf[BUFSIZ + 1];
-
- if ((size = readlink(source, buf, BUFSIZ)) < 0) {
- perror_msg("cannot read `%s'", source);
- return -1;
- }
- buf[size] = '\0';
-
- if (symlink(buf, dest) < 0) {
+ 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)