last_patch56 from vodz to simplify copy_file logic

This commit is contained in:
Eric Andersen 2002-09-17 08:42:21 +00:00
parent bf8bf105fb
commit a9a220b92a

View File

@ -36,7 +36,7 @@ int copy_file(const char *source, const char *dest, int flags)
{ {
struct stat source_stat; struct stat source_stat;
struct stat dest_stat; struct stat dest_stat;
int dest_exists = 1; int dest_exists = 0;
int status = 0; int status = 0;
if ((!(flags & FILEUTILS_DEREFERENCE) && if ((!(flags & FILEUTILS_DEREFERENCE) &&
@ -52,14 +52,14 @@ int copy_file(const char *source, const char *dest, int flags)
perror_msg("unable to stat `%s'", dest); perror_msg("unable to stat `%s'", dest);
return -1; return -1;
} }
dest_exists = 0; } else {
} if (source_stat.st_dev == dest_stat.st_dev &&
if (dest_exists && source_stat.st_dev == dest_stat.st_dev &&
source_stat.st_ino == dest_stat.st_ino) { source_stat.st_ino == dest_stat.st_ino) {
error_msg("`%s' and `%s' are the same file", source, dest); error_msg("`%s' and `%s' are the same file", source, dest);
return -1; return -1;
} }
dest_exists = 1;
}
if (S_ISDIR(source_stat.st_mode)) { if (S_ISDIR(source_stat.st_mode)) {
DIR *dp; DIR *dp;
@ -116,13 +116,8 @@ int copy_file(const char *source, const char *dest, int flags)
free(new_source); free(new_source);
free(new_dest); free(new_dest);
} }
/* closedir have only EBADF error, but "dp" not changes */
/* ??? What if an error occurs in readdir? */ closedir(dp);
if (closedir(dp) < 0) {
perror_msg("unable to close directory `%s'", source);
status = -1;
}
if (!dest_exists && if (!dest_exists &&
chmod(dest, source_stat.st_mode & ~saved_umask) < 0) { chmod(dest, source_stat.st_mode & ~saved_umask) < 0) {
@ -145,8 +140,7 @@ int copy_file(const char *source, const char *dest, int flags)
} }
#endif #endif
if ((sfp = fopen(source, "r")) == NULL) { if ((sfp = wfopen(source, "r")) == NULL) {
perror_msg("unable to open `%s'", source);
return -1; return -1;
} }
@ -201,55 +195,28 @@ int copy_file(const char *source, const char *dest, int flags)
perror_msg("unable to close `%s'", source); perror_msg("unable to close `%s'", source);
status = -1; status = -1;
} }
} else if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) ||
S_ISSOCK(source_stat.st_mode)) {
if (dest_exists) {
if (flags & FILEUTILS_INTERACTIVE) {
fprintf(stderr, "%s: overwrite `%s'? ", applet_name, dest);
if (!ask_confirmation())
return 0;
} }
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 (!(flags & FILEUTILS_FORCE)) { if (dest_exists &&
((flags & FILEUTILS_FORCE) == 0 || unlink(dest) < 0)) {
perror_msg("unable to remove `%s'", dest); perror_msg("unable to remove `%s'", dest);
return -1; return -1;
}
if (unlink(dest) < 0) {
perror_msg("unable to remove `%s'", dest);
return -1;
} }
} else {
dest_exists = 0; 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) { if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) {
perror_msg("unable to create `%s'", dest); perror_msg("unable to create `%s'", dest);
return -1; return -1;
} }
} else if (S_ISFIFO(source_stat.st_mode)) { } else if (S_ISFIFO(source_stat.st_mode)) {
if (dest_exists) {
if (flags & FILEUTILS_INTERACTIVE) {
fprintf(stderr, "%s: overwrite `%s'? ", applet_name, dest);
if (!ask_confirmation())
return 0;
}
if (!(flags & FILEUTILS_FORCE)) {
perror_msg("unable to remove `%s'", dest);
return -1;
}
if (unlink(dest) < 0) {
perror_msg("unable to remove `%s'", dest);
return -1;
}
dest_exists = 0;
}
if (mkfifo(dest, source_stat.st_mode) < 0) { if (mkfifo(dest, source_stat.st_mode) < 0) {
perror_msg("cannot create fifo `%s'", dest); perror_msg("cannot create fifo `%s'", dest);
return -1; return -1;
@ -257,26 +224,6 @@ int copy_file(const char *source, const char *dest, int flags)
} else if (S_ISLNK(source_stat.st_mode)) { } else if (S_ISLNK(source_stat.st_mode)) {
char *lpath; char *lpath;
if (dest_exists) {
if (flags & FILEUTILS_INTERACTIVE) {
fprintf(stderr, "%s: overwrite `%s'? ", applet_name, dest);
if (!ask_confirmation())
return 0;
}
if (!(flags & FILEUTILS_FORCE)) {
perror_msg("unable to remove `%s'", dest);
return -1;
}
if (unlink(dest) < 0) {
perror_msg("unable to remove `%s'", dest);
return -1;
}
dest_exists = 0;
}
lpath = xreadlink(source); lpath = xreadlink(source);
if (symlink(lpath, dest) < 0) { if (symlink(lpath, dest) < 0) {
perror_msg("cannot create symlink `%s'", dest); perror_msg("cannot create symlink `%s'", dest);
@ -295,9 +242,6 @@ int copy_file(const char *source, const char *dest, int flags)
#endif #endif
return 0; return 0;
} else {
error_msg("internal error: unrecognized file type");
return -1;
} }
#ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS #ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS