emuld: modify mkdir to make parent directories as needed 65/56565/1
authorChulHo Song <ch81.song@samsung.com>
Fri, 11 Sep 2015 04:59:49 +0000 (13:59 +0900)
committerChulHo Song <ch81.song@samsung.com>
Mon, 11 Jan 2016 08:17:56 +0000 (17:17 +0900)
Conflicts:
src/common.cpp

Change-Id: Icb1d92c16c5e54bc74d8da6eaafc7b28ab9d334c
Signed-off-by: ChulHo Song <ch81.song@samsung.com>
(cherry picked from commit 7395b87b2b425ea6339af9b28cd4ecd0bac97f69)

src/common.cpp

index d6d3c5928ab8450d188aa3535db277604ddfde55..d7ba09c2d87decaca5d400c9e3558994cc22b8d4 100644 (file)
@@ -147,6 +147,40 @@ void hds_unmount_all(void)
     systemcall(tmp);
 }
 
+static int do_mkdir(const char *dir)
+{
+    char tmp[MAX_PATH];
+    char *p = NULL;
+    size_t len;
+
+    if (!dir || *dir != '/')
+    {
+        LOGWARN("Invalid param. dir should be absolute path. dir = %s\n", dir);
+        return -1;
+    }
+
+    // Create directories by the size of "MAX_PATH"
+    // regardless of directory depth
+    snprintf(tmp, sizeof(tmp), "%s", dir);
+    len = strnlen(tmp, MAX_PATH);
+
+    if (tmp[len - 1] == '/')
+        tmp[len - 1] = 0;
+
+    for (p = tmp + 1; *p; p++)
+    {
+        if (*p == '/')
+        {
+            *p = 0;
+            if (mkdir(tmp, 0644) == -1 && errno != EEXIST)
+                return -1;
+            *p = '/';
+        }
+    }
+
+    return mkdir(tmp, 0644);
+}
+
 bool valid_hds_path(char* path) {
     struct stat buf;
     int ret = -1;
@@ -154,7 +188,7 @@ bool valid_hds_path(char* path) {
     ret = access(path, F_OK);
     if (ret == -1) {
         if (errno == ENOENT) {
-            ret = mkdir(path, 0644);
+            ret = do_mkdir(path);
             if (ret == -1) {
                 LOGERR("failed to create path : %d", errno);
                 return false;