summaryrefslogtreecommitdiff
path: root/src/basic
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2016-12-10 01:08:13 -0500
committerGitHub <noreply@github.com>2016-12-10 01:08:13 -0500
commit4a5567d5d6ab01974dd089eb8907fecd6aff4fcf (patch)
tree4f40cdd195f08022e2846d5c9757147ca436fede /src/basic
parent2e1f244efd2dfc1a60d032bef3d88b9ba6e0444b (diff)
parent58abb66f4b9b0b3a16fe29211454d9936d35c35d (diff)
Merge pull request #4795 from poettering/dissect
Generalize image dissection logic of nspawn, and make it useful for other tools.
Diffstat (limited to 'src/basic')
-rw-r--r--src/basic/fileio.c19
-rw-r--r--src/basic/fileio.h2
-rw-r--r--src/basic/fs-util.h7
-rw-r--r--src/basic/rm-rf.h2
4 files changed, 28 insertions, 2 deletions
diff --git a/src/basic/fileio.c b/src/basic/fileio.c
index 1615456659..c43b0583a4 100644
--- a/src/basic/fileio.c
+++ b/src/basic/fileio.c
@@ -1409,3 +1409,22 @@ int read_nul_string(FILE *f, char **ret) {
return 0;
}
+
+int mkdtemp_malloc(const char *template, char **ret) {
+ char *p;
+
+ assert(template);
+ assert(ret);
+
+ p = strdup(template);
+ if (!p)
+ return -ENOMEM;
+
+ if (!mkdtemp(p)) {
+ free(p);
+ return -errno;
+ }
+
+ *ret = p;
+ return 0;
+}
diff --git a/src/basic/fileio.h b/src/basic/fileio.h
index b58c83e64a..17b38a5d60 100644
--- a/src/basic/fileio.h
+++ b/src/basic/fileio.h
@@ -88,3 +88,5 @@ int open_tmpfile_linkable(const char *target, int flags, char **ret_path);
int link_tmpfile(int fd, const char *path, const char *target);
int read_nul_string(FILE *f, char **ret);
+
+int mkdtemp_malloc(const char *template, char **ret);
diff --git a/src/basic/fs-util.h b/src/basic/fs-util.h
index 0d925c6b84..5fe5c71ff0 100644
--- a/src/basic/fs-util.h
+++ b/src/basic/fs-util.h
@@ -84,3 +84,10 @@ enum {
};
int chase_symlinks(const char *path_with_prefix, const char *root, unsigned flags, char **ret);
+
+/* Useful for usage with _cleanup_(), removes a directory and frees the pointer */
+static inline void rmdir_and_free(char *p) {
+ (void) rmdir(p);
+ free(p);
+}
+DEFINE_TRIVIAL_CLEANUP_FUNC(char*, rmdir_and_free);
diff --git a/src/basic/rm-rf.h b/src/basic/rm-rf.h
index f693a5bb7c..e13f7003e3 100644
--- a/src/basic/rm-rf.h
+++ b/src/basic/rm-rf.h
@@ -33,8 +33,6 @@ int rm_rf(const char *path, RemoveFlags flags);
/* Useful for usage with _cleanup_(), destroys a directory and frees the pointer */
static inline void rm_rf_physical_and_free(char *p) {
- if (!p)
- return;
(void) rm_rf(p, REMOVE_ROOT|REMOVE_PHYSICAL);
free(p);
}