summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/install.c49
1 files changed, 33 insertions, 16 deletions
diff --git a/src/install.c b/src/install.c
index c9bfdaef0..e7de260c6 100644
--- a/src/install.c
+++ b/src/install.c
@@ -706,8 +706,7 @@ install_file_in_file (const char *from, const char *to,
Return true if successful. */
static bool
-install_file_in_file_parents (char const *from, char *to,
- struct cp_options *x)
+mkancesdirs_safe_wd (char const *from, char *to, struct cp_options *x)
{
bool save_working_directory =
! (IS_ABSOLUTE_FILE_NAME (from) && IS_ABSOLUTE_FILE_NAME (to));
@@ -737,8 +736,18 @@ install_file_in_file_parents (char const *from, char *to,
return false;
}
}
+ return status == EXIT_SUCCESS;
+}
+
+/* Copy file FROM onto file TO, creating any missing parent directories of TO.
+ Return true if successful. */
- return (status == EXIT_SUCCESS && install_file_in_file (from, to, x));
+static bool
+install_file_in_file_parents (char const *from, char *to,
+ const struct cp_options *x)
+{
+ return (mkancesdirs_safe_wd (from, to, (struct cp_options *)x)
+ && install_file_in_file (from, to, x));
}
/* Copy file FROM into directory TO_DIR, keeping its same name,
@@ -747,11 +756,16 @@ install_file_in_file_parents (char const *from, char *to,
static bool
install_file_in_dir (const char *from, const char *to_dir,
- const struct cp_options *x)
+ const struct cp_options *x, bool mkdir_and_install)
{
const char *from_base = last_component (from);
char *to = file_name_concat (to_dir, from_base, NULL);
- bool ret = install_file_in_file (from, to, x);
+ bool ret = true;
+
+ if (mkdir_and_install)
+ ret = mkancesdirs_safe_wd (from, to, (struct cp_options *)x);
+
+ ret = ret && install_file_in_file (from, to, x);
free (to);
return ret;
}
@@ -851,16 +865,6 @@ main (int argc, char **argv)
if (target_directory)
error (EXIT_FAILURE, 0,
_("multiple target directories specified"));
- else
- {
- struct stat st;
- if (stat (optarg, &st) != 0)
- error (EXIT_FAILURE, errno, _("failed to access %s"),
- quote (optarg));
- if (! S_ISDIR (st.st_mode))
- error (EXIT_FAILURE, 0, _("target %s is not a directory"),
- quote (optarg));
- }
target_directory = optarg;
break;
case 'T':
@@ -915,6 +919,18 @@ main (int argc, char **argv)
error (EXIT_FAILURE, 0,
_("target directory not allowed when installing a directory"));
+ if (target_directory)
+ {
+ struct stat st;
+ bool stat_success = stat (target_directory, &st) == 0 ? true : false;
+ if (! mkdir_and_install && ! stat_success)
+ error (EXIT_FAILURE, errno, _("failed to access %s"),
+ quote (target_directory));
+ if (stat_success && ! S_ISDIR (st.st_mode))
+ error (EXIT_FAILURE, 0, _("target %s is not a directory"),
+ quote (target_directory));
+ }
+
if (backup_suffix_string)
simple_backup_suffix = xstrdup (backup_suffix_string);
@@ -1020,7 +1036,8 @@ main (int argc, char **argv)
int i;
dest_info_init (&x);
for (i = 0; i < n_files; i++)
- if (! install_file_in_dir (file[i], target_directory, &x))
+ if (! install_file_in_dir (file[i], target_directory, &x,
+ mkdir_and_install))
exit_status = EXIT_FAILURE;
}
}