bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Patch for touch to make it have a -p option, similar to mkdir


From: Marc Abramowitz
Subject: Patch for touch to make it have a -p option, similar to mkdir
Date: Thu, 28 Dec 2006 14:08:03 -0800 (PST)

The other day I realized that often when I'm using touch, I'd like it to have 
the ability to create ancestor directories that don't exist, a la mkdir -p. I 
quickly hacked together a shell script to do what I want: 
http://marc.abramowitz.info/archives/2006/12/22/adding-a-p-option-to-touch/

Then I thought that this might be a generally useful extension to touch, so I 
thought I'd take a stab at adding it to touch. Here it is:

--- coreutils-6.7.orig/src/touch.c 2006-10-22 09:54:15.000000000 -0700
+++ coreutils-6.7/src/touch.c 2006-12-28 13:45:22.000000000 -0800
@@ -34,6 +34,8 @@
 #include "safe-read.h"
 #include "stat-time.h"
 #include "utimens.h"
+#include "savewd.h"
+#include "mkdir-p.h"
 
 /* The official name of this program (e.g., no `g' prefix).  */
 #define PROGRAM_NAME "touch"
@@ -112,6 +114,60 @@
     error (EXIT_FAILURE, 0, _("invalid date format %s"), quote (flex_date));
 }
 
+/* <hack
+   comment="Copied from mkdir.c; move to lib?"
+   date="2006-12-28"
+   author="Marc Abramowitz">
+ */
+
+/* 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 *, 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;
+};
+
+static void
+announce_mkdir (char const *dir, void *options)
+{
+  struct mkdir_options const *o = options;
+  if (o->created_directory_format)
+    error (0, 0, o->created_directory_format, quote (dir));
+}
+
+/* Make ancestor directory DIR, whose last component is COMPONENT,
+   with options OPTIONS.  Assume the working directory is COMPONENT's
+   parent.  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, char const *component, void *options)
+{
+  struct mkdir_options const *o = options;
+  int r = mkdir (component, o->ancestor_mode);
+  if (r == 0)
+    {
+      r = ! (o->ancestor_mode & S_IRUSR);
+      announce_mkdir (dir, options);
+    }
+  return r;
+}
+
+/* </hack> */
+
 /* Update the time of file FILE according to the options given.
    Return true if successful.  */
 
@@ -129,6 +185,19 @@
     fd = STDOUT_FILENO;
   else if (! no_create)
     {
+      {
+        char *dir = dir_name (file);
+        struct savewd wd;
+        savewd_init (&wd);
+        void *make_ancestor_function = make_ancestor;
+        struct mkdir_options options;
+        mode_t mode = options.mode = options.ancestor_mode = S_IRWXUGO;
+        mode_t mode_bits = options.mode_bits = 0;
+        make_dir_parents (dir, &wd, make_ancestor_function, &options,
+                          mode, announce_mkdir,
+                          mode_bits, (uid_t) -1, (gid_t) -1, true);
+      }
+          
       /* Try to open FILE, creating it if necessary.  */
       fd = fd_reopen (STDIN_FILENO, file,
           O_WRONLY | O_CREAT | O_NONBLOCK | O_NOCTTY,


This feels fairly hacky to me, as there's a fair amount of code duplication 
from mkdir.c - my hope is that the coreutils maintainers can take this and 
clean it up, perhaps by moving some of the common functionality into the lib 
directory.

I hope this is useful. Happy holidays all.

-Marc
 http://marc.abramowitz.info/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]