diff options
author | Paul Eggert <eggert@cs.ucla.edu> | 2006-09-16 20:03:56 +0000 |
---|---|---|
committer | Paul Eggert <eggert@cs.ucla.edu> | 2006-09-16 20:03:56 +0000 |
commit | b67faf329cebf0805b2b73cc775ccfc7a05390de (patch) | |
tree | 61071dc36a049a4685e757f637bc36ac928ade0e /src | |
parent | adbad7626cbbf81745482b4ddb4a0bcea97a6db6 (diff) | |
download | coreutils-b67faf329cebf0805b2b73cc775ccfc7a05390de.tar.xz |
* NEWS: Document that mkdir -p and install -d now fork on occasion.
* bootstrap.conf (gnulib_modules): Add savewd.
* src/install.c: Include savewd.h.
(process_dir): New function.
(main, install_file_in_file_parents): Use it, along with the new
savewd module, to avoid some race conditions.
* src/mkdir.c: Include savewd.h.
(struct mkdir_options): New members make_ancestor_function, mode,
mode_bits.
(make_ancestor): Return 1 if the resulting directory is not readable.
(process_dir): New function.
(main): Use it, along with new savewd module, to avoid some
race conditions. Fill in new slots of struct mkdir_options, so
that callees get the values.
* tests/install/basic-1: Test for coreutils 5.97 bug that was
fixed in coreutils 6.0, and which should still be fixed with
this change.
* tests/mkdir/p-3: Likewise.
Diffstat (limited to 'src')
-rw-r--r-- | src/install.c | 67 | ||||
-rw-r--r-- | src/mkdir.c | 61 |
2 files changed, 90 insertions, 38 deletions
diff --git a/src/install.c b/src/install.c index 78fa28e0b..9c5150fbc 100644 --- a/src/install.c +++ b/src/install.c @@ -35,6 +35,7 @@ #include "mkdir-p.h" #include "modechange.h" #include "quote.h" +#include "savewd.h" #include "stat-time.h" #include "utimens.h" #include "xstrtol.h" @@ -193,11 +194,23 @@ target_directory_operand (char const *file) return is_a_dir; } +/* Process a command-line file name, for the -d option. */ +static int +process_dir (char *dir, struct savewd *wd, void *options) +{ + return (make_dir_parents (dir, wd, + make_ancestor, options, + dir_mode, announce_mkdir, + dir_mode_bits, owner_id, group_id, false) + ? EXIT_SUCCESS + : EXIT_FAILURE); +} + int main (int argc, char **argv) { int optc; - bool ok = true; + int exit_status = EXIT_SUCCESS; const char *specified_mode = NULL; bool make_backups = false; char *backup_suffix_string; @@ -361,13 +374,7 @@ main (int argc, char **argv) get_ids (); if (dir_arg) - { - int i; - for (i = 0; i < n_files; i++) - ok &= make_dir_parents (file[i], make_ancestor, &x, - dir_mode, announce_mkdir, - dir_mode_bits, owner_id, group_id, false); - } + exit_status = savewd_process_files (n_files, file, process_dir, &x); else { /* FIXME: it's a little gross that this initialization is @@ -376,23 +383,22 @@ main (int argc, char **argv) if (!target_directory) { - if (mkdir_and_install) - ok = install_file_in_file_parents (file[0], file[1], &x); - else - ok = install_file_in_file (file[0], file[1], &x); + if (! (mkdir_and_install + ? install_file_in_file_parents (file[0], file[1], &x) + : install_file_in_file (file[0], file[1], &x))) + exit_status = EXIT_FAILURE; } else { int i; dest_info_init (&x); for (i = 0; i < n_files; i++) - { - ok &= install_file_in_dir (file[i], target_directory, &x); - } + if (! install_file_in_dir (file[i], target_directory, &x)) + exit_status = EXIT_FAILURE; } } - exit (ok ? EXIT_SUCCESS : EXIT_FAILURE); + exit (exit_status); } /* Copy file FROM onto file TO, creating any missing parent directories of TO. @@ -402,13 +408,36 @@ static bool install_file_in_file_parents (char const *from, char *to, struct cp_options *x) { - if (mkancesdirs (to, make_ancestor, x) != 0) + bool save_working_directory = + ! (IS_ABSOLUTE_FILE_NAME (from) && IS_ABSOLUTE_FILE_NAME (to)); + int status = EXIT_SUCCESS; + + struct savewd wd; + savewd_init (&wd); + if (! save_working_directory) + savewd_finish (&wd); + + if (mkancesdirs (to, &wd, make_ancestor, x) == -1) { error (0, errno, _("cannot create directory %s"), to); - return false; + status = EXIT_FAILURE; + } + + if (save_working_directory) + { + int restore_result = savewd_restore (&wd, status); + int restore_errno = errno; + savewd_finish (&wd); + if (EXIT_SUCCESS < restore_result) + return false; + if (restore_result < 0 && status == EXIT_SUCCESS) + { + error (0, restore_errno, _("cannot create directory %s"), to); + return false; + } } - return install_file_in_file (from, to, x); + return (status == EXIT_SUCCESS && install_file_in_file (from, to, x)); } /* Copy file FROM onto file TO and give TO the appropriate diff --git a/src/mkdir.c b/src/mkdir.c index 852bc3e15..b28a02ac0 100644 --- a/src/mkdir.c +++ b/src/mkdir.c @@ -28,6 +28,7 @@ #include "mkdir-p.h" #include "modechange.h" #include "quote.h" +#include "savewd.h" /* The official name of this program (e.g., no `g' prefix). */ #define PROGRAM_NAME "mkdir" @@ -75,12 +76,22 @@ Mandatory arguments to long options are mandatory for short options too.\n\ exit (status); } -/* Options for announce_mkdir and make_ancestor. */ +/* Options passed to subsidiary functions. */ struct mkdir_options { + /* Function to make an ancestor, or NULL if ancestors should not be + made. */ + int (*make_ancestor_function) (char const *, void *); + /* Mode for ancestor directory. */ mode_t ancestor_mode; + /* Mode for directory itself. */ + mode_t mode; + + /* File mode bits affected by MODE. */ + mode_t mode_bits; + /* If not null, format to use when reporting newly made directories. */ char const *created_directory_format; }; @@ -94,27 +105,44 @@ announce_mkdir (char const *dir, void *options) error (0, 0, o->created_directory_format, quote (dir)); } -/* Make ancestor directory DIR, with options OPTIONS. */ +/* Make ancestor directory DIR, with options OPTIONS. Return 0 if + successful and the resulting directory is readable, 1 if successful + but the resulting directory is not readable, -1 (setting errno) + otherwise. */ static int make_ancestor (char const *dir, void *options) { struct mkdir_options const *o = options; int r = mkdir (dir, o->ancestor_mode); if (r == 0) - announce_mkdir (dir, options); + { + r = ! (o->ancestor_mode & S_IRUSR); + announce_mkdir (dir, options); + } return r; } +/* Process a command-line file name. */ +static int +process_dir (char *dir, struct savewd *wd, void *options) +{ + struct mkdir_options const *o = options; + return (make_dir_parents (dir, wd, o->make_ancestor_function, options, + o->mode, announce_mkdir, + o->mode_bits, (uid_t) -1, (gid_t) -1, true) + ? EXIT_SUCCESS + : EXIT_FAILURE); +} + int main (int argc, char **argv) { - mode_t mode = S_IRWXUGO; - mode_t mode_bits = 0; - int (*make_ancestor_function) (char const *, void *) = NULL; const char *specified_mode = NULL; - int exit_status = EXIT_SUCCESS; int optc; struct mkdir_options options; + options.make_ancestor_function = NULL; + options.mode = S_IRWXUGO; + options.mode_bits = 0; options.created_directory_format = NULL; initialize_main (&argc, &argv); @@ -130,7 +158,7 @@ main (int argc, char **argv) switch (optc) { case 'p': - make_ancestor_function = make_ancestor; + options.make_ancestor_function = make_ancestor; break; case 'm': specified_mode = optarg; @@ -151,7 +179,7 @@ main (int argc, char **argv) usage (EXIT_FAILURE); } - if (make_ancestor_function || specified_mode) + if (options.make_ancestor_function || specified_mode) { mode_t umask_value = umask (0); @@ -163,19 +191,14 @@ main (int argc, char **argv) if (!change) error (EXIT_FAILURE, 0, _("invalid mode %s"), quote (specified_mode)); - mode = mode_adjust (S_IRWXUGO, true, umask_value, change, - &mode_bits); + options.mode = mode_adjust (S_IRWXUGO, true, umask_value, change, + &options.mode_bits); free (change); } else - mode &= ~umask_value; + options.mode = S_IRWXUGO & ~umask_value; } - for (; optind < argc; ++optind) - if (! make_dir_parents (argv[optind], make_ancestor_function, &options, - mode, announce_mkdir, - mode_bits, (uid_t) -1, (gid_t) -1, true)) - exit_status = EXIT_FAILURE; - - exit (exit_status); + exit (savewd_process_files (argc - optind, argv + optind, + process_dir, &options)); } |