-static int rmfileAction(const char *fileName, struct stat* statbuf)
-{
- if (unlink( fileName) < 0 ) {
- perror( fileName);
- return ( FALSE);
- }
- return ( TRUE);
+ if (stat(dest, &dest_stat) < 0) {
+ if (errno != ENOENT) {
+ perror_msg("unable to stat `%s'", dest);
+ return -1;
+ }
+ dest_exists = 0;
+ }
+
+ if (dest_exists) {
+ if (S_ISDIR(dest_stat.st_mode) &&
+ (!source_exists || !S_ISDIR(source_stat.st_mode))) {
+ error_msg("cannot overwrite directory with non-directory");
+ return -1;
+ }
+
+ if (!S_ISDIR(dest_stat.st_mode) && source_exists &&
+ S_ISDIR(source_stat.st_mode)) {
+ error_msg("cannot overwrite non-directory with directory");
+ return -1;
+ }
+
+ if (unlink(dest) < 0) {
+ perror_msg("cannot remove `%s'", dest);
+ return -1;
+ }
+ }
+
+ if (copy_file(source, dest,
+ FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS) < 0)
+ return -1;
+
+ if (remove_file(source, FILEUTILS_RECUR | FILEUTILS_FORCE) < 0)
+ return -1;
+
+ return 0;