Change owner of parent directory 59/35459/3
authorSangyoon Jang <s89.jang@samsung.com>
Mon, 16 Feb 2015 07:20:08 +0000 (16:20 +0900)
committerSangyoon Jang <s89.jang@samsung.com>
Mon, 16 Feb 2015 10:51:19 +0000 (19:51 +0900)
revise making directory
when create directory recursively, change owner of parent directory

Change-Id: I5beda08252f050a6104699b28bcde26e0a6957f5
Signed-off-by: Sangyoon Jang <s89.jang@samsung.com>
src/pkgmgr-info.c

index 9bd9b01..84bf3dc 100644 (file)
@@ -34,6 +34,7 @@
 #include <dlfcn.h>
 #include <sys/smack.h>
 #include <linux/limits.h>
+#include <libgen.h>
 
 #include <libxml/parser.h>
 #include <libxml/xmlreader.h>
@@ -308,40 +309,42 @@ static int __delete_certinfo(const char *pkgid, uid_t uid);
 static int _check_create_Cert_db( sqlite3 *certdb);
 static int __exec_db_query(sqlite3 *db, char *query, sqlite_query_callback callback, void *data);
 
-static int _mkdir(const char *dir, mode_t mode)
+static int _mkdir_for_user(const char* dir, uid_t uid, gid_t gid)
 {
-       char tmp[PATH_MAX];
-       char *p = NULL;
-       size_t len;
        int ret;
-
-       snprintf(tmp, sizeof(tmp), "%s", dir);
-       len = strlen(tmp);
-       if(tmp[len - 1] == '/')
-               tmp[len - 1] = 0;
-       for(p = tmp + 1; *p; p++) {
-               if(*p == '/') {
-                       *p = 0;
-                       ret = mkdir(tmp, mode);
-                       if (ret && errno != EEXIST)
-                               return ret;
-                       *p = '/';
+       char *fullpath;
+       char *subpath;
+
+       _LOGD("uid:%d gid:%d", uid, gid);
+       fullpath = strdup(dir);
+       subpath = dirname(fullpath);
+       if (strlen(subpath) > 1) {
+               ret = _mkdir_for_user(fullpath, uid, gid);
+               if (ret == -1) {
+                       free(fullpath);
+                       return ret;
                }
        }
-       return mkdir(tmp, mode);
-}
 
-static void _mkdir_for_user(const char* dir, uid_t uid, gid_t gid) {
-       int ret = 0;
+       ret = mkdir(dir, S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH);
+       if (ret && errno != EEXIST) {
+               free(fullpath);
+               return ret;
+       } else if (ret && errno == EEXIST) {
+               free(fullpath);
+               return 0;
+       }
 
-       ret = _mkdir(dir, S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH);
-       if (ret == -1 && errno != EEXIST) {
-               _LOGE("FAIL : to create directory %s %d", dir, errno);
-       } else if (getuid() == ROOT_UID) {
+       if (getuid() == ROOT_UID) {
                ret = chown(dir, uid, gid);
                if (ret == -1)
-                       _LOGE("FAIL : chown %s %d.%d, because %s", dir, uid, gid, strerror(errno));
+                       _LOGE("FAIL : chown %s %d.%d, because %s", dir, uid,
+                                       gid, strerror(errno));
        }
+
+       free(fullpath);
+
+       return 0;
 }
 
 static const char *_get_db_path(uid_t uid) {