summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Schwenke <martin@meltin.net>2014-06-11 16:58:20 +1000
committerMartin Schwenke <martins@samba.org>2014-06-12 05:40:10 +0200
commitdd672431c0b8a3a416f6c244b131074d868250cc (patch)
tree719b05607ea558d15f25889e4bcedd2302c0d23c
parent8fa8b3b397853a29f12875454b890fa5c7b8cda8 (diff)
downloadsamba-dd672431c0b8a3a416f6c244b131074d868250cc.tar.gz
samba-dd672431c0b8a3a416f6c244b131074d868250cc.tar.xz
samba-dd672431c0b8a3a416f6c244b131074d868250cc.zip
ctdb-common: No dynamic memory allocation in mkdir_p()
Allocate an array of size PATH_MAX on the stack instead. To stop unnecessary recursion, try to create the desired directory before creating ancestors and only create ancestors on ENOENT. Signed-off-by: Martin Schwenke <martin@meltin.net> Reviewed-by: Amitay Isaacs <amitay@gmail.com>
-rw-r--r--ctdb/common/system_common.c37
1 files changed, 27 insertions, 10 deletions
diff --git a/ctdb/common/system_common.c b/ctdb/common/system_common.c
index cc22f69e37c..7fc7425b0db 100644
--- a/ctdb/common/system_common.c
+++ b/ctdb/common/system_common.c
@@ -162,7 +162,8 @@ char *ctdb_sys_find_ifname(ctdb_sock_addr *addr)
int mkdir_p(const char *dir, int mode)
{
- char * t;
+ char t[PATH_MAX];
+ ssize_t len;
int ret;
if (strcmp(dir, "/") == 0) {
@@ -173,18 +174,34 @@ int mkdir_p(const char *dir, int mode)
return 0;
}
- t = talloc_strdup(NULL, dir);
- if (t == NULL) {
- return ENOMEM;
+ /* Try to create directory */
+ ret = mkdir(dir, mode);
+ /* Succeed if that worked or if it already existed */
+ if (ret == 0 || errno == EEXIST) {
+ return 0;
+ }
+ /* Fail on anything else except ENOENT */
+ if (errno != ENOENT) {
+ return ret;
+ }
+
+ /* Create ancestors */
+ len = strlen(dir);
+ if (len >= PATH_MAX) {
+ errno = ENAMETOOLONG;
+ return -1;
}
+ strncpy(t, dir, len+1);
+
ret = mkdir_p(dirname(t), mode);
- talloc_free(t);
+ if (ret != 0) {
+ return ret;
+ }
- if (ret == 0) {
- ret = mkdir(dir, mode);
- if ((ret == -1) && (errno == EEXIST)) {
- ret = 0;
- }
+ /* Create directory */
+ ret = mkdir(dir, mode);
+ if ((ret == -1) && (errno == EEXIST)) {
+ ret = 0;
}
return ret;