From: Peter Schiffer Date: Thu, 16 May 2013 15:10:32 +0000 (+0200) Subject: Make cg_mkdir_p() function compatible with read-only fs X-Git-Tag: v0.41~66 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=74b3eb1db015fce921e0d450a66e13af2edec99f;p=thirdparty%2Flibcgroup.git Make cg_mkdir_p() function compatible with read-only fs mkdir(2) function returns EROFS error even when the path already exists on the read only file system, so it is impossible to determine whether the path already exists on this kind of fs only be return code from the mkdir(2). To make cg_mkdir_p() compatible with the ro fs, the function checks whether the path exists with stat(2) before trying to create it. Signed-off-by: Peter Schiffer Acked-By: Jan Safranek --- diff --git a/src/api.c b/src/api.c index ee18797b..c4204750 100644 --- a/src/api.c +++ b/src/api.c @@ -1292,7 +1292,8 @@ int cg_mkdir_p(const char *path) char *real_path = NULL; int i = 0; char pos; - int ret = 0; + int ret = 0, stat_ret; + struct stat st; real_path = strdup(path); if (!real_path) { @@ -1320,6 +1321,14 @@ int cg_mkdir_p(const char *path) ret = ECGROUPNOTOWNER; goto done; default: + /* Check if path exists */ + real_path[i] = '\0'; + stat_ret = stat(real_path, &st); + real_path[i] = pos; + if (stat_ret == 0) { + ret = 0; /* Path exists */ + break; + } ret = ECGROUPNOTALLOWED; goto done; }