Add rpmMkdirs() to librpmio
authorPanu Matilainen <pmatilai@redhat.com>
Fri, 10 Oct 2008 11:28:41 +0000 (14:28 +0300)
committerPanu Matilainen <pmatilai@redhat.com>
Fri, 10 Oct 2008 11:28:41 +0000 (14:28 +0300)
- allows creating bunch of directories including parents at once

rpmio/rpmfileutil.c
rpmio/rpmfileutil.h

index 60c4ba5..f4aed4a 100644 (file)
@@ -704,3 +704,26 @@ char * rpmGetCwd(void)
     return currDir;
 }
 
+int rpmMkdirs(const char *root, const char *pathstr)
+{
+    ARGV_t dirs = NULL;
+    int rc = 0;
+    argvSplit(&dirs, pathstr, ":");
+    
+    for (char **d = dirs; *d; d++) {
+       char *path = rpmGetPath(root ? root : "", "/", *d, NULL);
+       if ((rc = rpmioMkpath(path, 0755, -1, -1)) != 0) {
+           const char *msg = _("failed to create directory");
+           /* try to be more informative if the failing part was a macro */
+           if (**d == '%') {
+               rpmlog(RPMLOG_ERR, "%s %s: %s: %m\n", msg, *d, path);
+           } else {
+               rpmlog(RPMLOG_ERR, "%s %s: %m\n", msg, path);
+           }
+       }
+       free(path);
+       if (rc) break;
+    }
+    argvFree(dirs);
+    return rc;
+}
index 3e10667..471509e 100644 (file)
@@ -63,6 +63,16 @@ FD_t rpmMkTempFile(const char * prefix, char **fn);
 int rpmioMkpath(const char * path, mode_t mode, uid_t uid, gid_t gid);
 
 /** \ingroup rpmfileutil
+ * Create several directories (including parents if needed) in one go.
+ * Macros in pathstr will be expanded in the process.
+ * @param root         leading root directory (or NULL for none)
+ * @param pathstr      list of directories separated with :
+ * @return             0 if all directories were successfully created
+ *                     (or already existed), non-zero otherwise
+ */
+int rpmMkdirs(const char *root, const char *pathstr);
+
+/** \ingroup rpmfileutil
  * Canonicalize file path.
  * @param path         path to canonicalize (in-place)
  * @return             pointer to path