Clean-up buffer allocation codes 33/109933/7
authorjongmyeongko <jongmyeong.ko@samsung.com>
Wed, 14 Dec 2016 12:30:33 +0000 (21:30 +0900)
committerjongmyeong ko <jongmyeong.ko@samsung.com>
Tue, 17 Jan 2017 09:01:33 +0000 (01:01 -0800)
Change-Id: Ic7a4a1c8510889eaa3fdca1a58a5517764a215a2
Signed-off-by: jongmyeongko <jongmyeong.ko@samsung.com>
plugin/app2sd/common/inc/app2sd_utils.h
plugin/app2sd/common/src/app2sd_utils.c
plugin/app2sd/lib/app2sd_client_interface.c
plugin/app2sd/server/app2sd_interface.c
plugin/app2sd/server/app2sd_internals.c
plugin/app2sd/server/app2sd_internals_utils.c

index e2c4d5e..4ed6e72 100644 (file)
@@ -68,6 +68,9 @@ char *_app2sd_get_encoded_name(const char *pkgid, uid_t uid);
 
 int _app2sd_delete_directory(const char *dirname);
 
+void _app2sd_set_application_path(const char *pkgid, uid_t uid, char path[],
+               int size);
+
 #ifdef __cplusplus
 }
 #endif
index 270f5fc..79db27e 100644 (file)
@@ -37,9 +37,10 @@ char *_app2sd_get_encoded_name(const char *pkgid, uid_t uid)
        char source_name[FILENAME_MAX] = { 0, };
        GChecksum *checksum;
 
-       snprintf(source_name, FILENAME_MAX - 1, "%s_%d", pkgid, uid);
+       snprintf(source_name, sizeof(source_name), "%s_%d", pkgid, uid);
        checksum = g_checksum_new(G_CHECKSUM_MD5);
-       g_checksum_update(checksum, (const guchar *)source_name, strlen(source_name));
+       g_checksum_update(checksum, (const guchar *)source_name,
+               strlen(source_name));
        temp_string = (char *)g_checksum_get_string(checksum);
        new_name = strdup(temp_string);
        g_checksum_free(checksum);
@@ -47,6 +48,20 @@ char *_app2sd_get_encoded_name(const char *pkgid, uid_t uid)
        return new_name;
 }
 
+void _app2sd_set_application_path(const char *pkgid, uid_t uid,
+               char path[], int size)
+{
+       if (_is_global(uid)) {
+               snprintf(path, size, "%s/%s",
+                       tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
+       } else {
+               tzplatform_set_user(uid);
+               snprintf(path, size, "%s/%s",
+                       tzplatform_getenv(TZ_USER_APP), pkgid);
+               tzplatform_reset_user();
+       }
+}
+
 int _app2sd_delete_directory(const char *dirname)
 {
        DIR *dp = NULL;
@@ -55,42 +70,45 @@ int _app2sd_delete_directory(const char *dirname)
        int ret = 0;
 
        dp = opendir(dirname);
-       if (dp != NULL) {
-               while ((ep = readdir(dp)) != NULL) {
-                       struct stat stFileInfo;
+       if (dp == NULL) {
+               _W("couldn't open the directory[%s], errno[%d]",
+                       dirname, errno);
+               return 0;
+       }
 
-                       snprintf(abs_filename, FILENAME_MAX, "%s/%s", dirname,
-                               ep->d_name);
+       while ((ep = readdir(dp)) != NULL) {
+               struct stat stFileInfo;
 
-                       if (lstat(abs_filename, &stFileInfo) < 0) {
-                               perror(abs_filename);
-                               (void)closedir(dp);
-                               return -1;
-                       }
+               snprintf(abs_filename, sizeof(abs_filename), "%s/%s",
+                       dirname, ep->d_name);
 
-                       if (S_ISDIR(stFileInfo.st_mode)) {
-                               if (strcmp(ep->d_name, ".")
-                                   && strcmp(ep->d_name, "..")) {
-                                       ret = _app2sd_delete_directory(abs_filename);
-                                       if (ret < 0) {
-                                               (void)closedir(dp);
-                                               return -1;
-                                       }
-                               }
-                       } else {
-                               ret = remove(abs_filename);
+               if (lstat(abs_filename, &stFileInfo) < 0) {
+                       perror(abs_filename);
+                       (void)closedir(dp);
+                       return -1;
+               }
+
+               if (S_ISDIR(stFileInfo.st_mode)) {
+                       if (strcmp(ep->d_name, ".")
+                           && strcmp(ep->d_name, "..")) {
+                               ret = _app2sd_delete_directory(abs_filename);
                                if (ret < 0) {
                                        (void)closedir(dp);
                                        return -1;
                                }
                        }
+               } else {
+                       ret = remove(abs_filename);
+                       if (ret < 0) {
+                               (void)closedir(dp);
+                               return -1;
+                       }
                }
-               (void)closedir(dp);
-               ret = remove(dirname);
-               if (ret < 0)
-                       return -1;
-       } else {
-               _W("couldn't open the directory[%s]", dirname);
        }
+       (void)closedir(dp);
+       ret = remove(dirname);
+       if (ret < 0)
+               return -1;
+
        return 0;
 }
index 2f8437d..52313d6 100644 (file)
@@ -131,20 +131,14 @@ static int __app2sd_delete_temp_directories(const char *pkgid,
                app2sd_cmd cmd, uid_t uid)
 {
        int ret = 0;
+       char app_path[FILENAME_MAX] = { 0, };
        char temp_path[FILENAME_MAX] = { 0, };
 
        if (cmd != APP2SD_PRE_UPGRADE)
                return APP2EXT_SUCCESS;
 
-       if (_is_global(uid)) {
-               snprintf(temp_path, FILENAME_MAX - 1, "%s/%s.new",
-                       tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
-       } else {
-               tzplatform_set_user(uid);
-               snprintf(temp_path, FILENAME_MAX - 1, "%s/%s.new",
-                       tzplatform_getenv(TZ_USER_APP), pkgid);
-               tzplatform_reset_user();
-       }
+       _app2sd_set_application_path(pkgid, uid, app_path, sizeof(app_path));
+       snprintf(temp_path, sizeof(temp_path), "%s.new", app_path);
 
        /* this will delete all files under temp_path */
        ret = _app2sd_delete_directory(temp_path);
@@ -162,19 +156,11 @@ static int __app2sd_create_default_directories(const char *pkgid,
        int ret = 0;
        mode_t mode = DIR_PERMS;
        char application_path[FILENAME_MAX] = { 0, };
-       char application_mmc_path[FILENAME_MAX] = { 0, };
+       char app_mmc_path[FILENAME_MAX] = { 0, };
        char temp_path[FILENAME_MAX] = { 0, };
 
-       if (_is_global(uid)) {
-               snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
-                       tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
-       } else {
-               tzplatform_set_user(uid);
-               snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
-                       tzplatform_getenv(TZ_USER_APP), pkgid);
-               tzplatform_reset_user();
-       }
-
+       _app2sd_set_application_path(pkgid, uid, application_path,
+               sizeof(application_path));
        ret = mkdir(application_path, mode);
        if (ret) {
                if (errno != EEXIST) {
@@ -184,10 +170,9 @@ static int __app2sd_create_default_directories(const char *pkgid,
                }
        }
 
-       snprintf(application_mmc_path, FILENAME_MAX - 1, "%s/.mmc",
+       snprintf(app_mmc_path, sizeof(app_mmc_path), "%s/.mmc",
                application_path);
-
-       ret = mkdir(application_mmc_path, mode);
+       ret = mkdir(app_mmc_path, mode);
        if (ret) {
                if (errno != EEXIST) {
                        _E("create directory failed," \
@@ -198,7 +183,7 @@ static int __app2sd_create_default_directories(const char *pkgid,
 
        if (cmd == APP2SD_PRE_UPGRADE) {
                /* application_path for {pkgid}.new */
-               snprintf(temp_path, FILENAME_MAX - 1, "%s.new",
+               snprintf(temp_path, sizeof(temp_path), "%s.new",
                        application_path);
                ret = mkdir(temp_path, mode);
                if (ret) {
@@ -208,10 +193,10 @@ static int __app2sd_create_default_directories(const char *pkgid,
                                return APP2EXT_ERROR_CREATE_DIRECTORY;
                        }
                }
-               /* application_mmc_path for {pkgid}.new */
-               snprintf(application_mmc_path, FILENAME_MAX - 1, "%s/.mmc",
+               /* app_mmc_path for {pkgid}.new */
+               snprintf(app_mmc_path, sizeof(app_mmc_path), "%s/.mmc",
                        temp_path);
-               ret = mkdir(application_mmc_path, mode);
+               ret = mkdir(app_mmc_path, mode);
                if (ret) {
                        if (errno != EEXIST) {
                                _E("create directory failed," \
index 70e1bcb..799cc70 100644 (file)
@@ -33,8 +33,8 @@ static int __app2sd_create_app2sd_directories(uid_t uid, char *mmc_path)
        char app2sd_path[FILENAME_MAX] = { 0, };
        mode_t mode = DIR_PERMS;
 
-       snprintf(app2sd_path, FILENAME_MAX - 1, "%s/%s",
-                       mmc_path, EXTIMG_DIR);
+       snprintf(app2sd_path, sizeof(app2sd_path), "%s/%s",
+               mmc_path, EXTIMG_DIR);
        ret = mkdir(app2sd_path, mode);
        if (ret) {
                if (errno != EEXIST) {
@@ -138,7 +138,7 @@ int app2sd_usr_pre_app_install(const char *pkgid, GList *dir_list, int size, uid
                _E("MMC not present OR Not ready (%d)", ret);
                return APP2EXT_ERROR_MMC_STATUS;
        }
-       snprintf(mmc_path, FILENAME_MAX - 1, "%s", sdpath);
+       snprintf(mmc_path, sizeof(mmc_path), "%s", sdpath);
        free(sdpath);
        sync();
 
@@ -164,22 +164,13 @@ int app2sd_usr_pre_app_install(const char *pkgid, GList *dir_list, int size, uid
        encoded_id = _app2sd_get_encoded_name(pkgid, uid);
        if (encoded_id == NULL)
                return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
-
-       if (_is_global(uid)) {
-               snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
-                       tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
-               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
-                       mmc_path, EXTIMG_DIR, encoded_id);
-       } else {
-               tzplatform_set_user(uid);
-               snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
-                       tzplatform_getenv(TZ_USER_APP), pkgid);
-               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
+       snprintf(loopback_device, sizeof(loopback_device), "%s/%s/%s",
                        mmc_path, EXTIMG_DIR, encoded_id);
-               tzplatform_reset_user();
-       }
        free(encoded_id);
 
+       _app2sd_set_application_path(pkgid, uid, application_path,
+               sizeof(application_path));
+
        ret = __app2sd_create_app2sd_directories(uid, mmc_path);
        if (ret) {
                _E("failed to create app2sd dirs");
@@ -288,7 +279,7 @@ int app2sd_usr_post_app_install(const char *pkgid,
        char *sdpath = NULL;
        char mmc_path[FILENAME_MAX] = { 0, };
        char application_path[FILENAME_MAX] = { 0, };
-       char application_mmc_path[FILENAME_MAX] = { 0, };
+       char app_mmc_path[FILENAME_MAX] = { 0, };
        char loopback_device[FILENAME_MAX] = { 0, };
        char *encoded_id = NULL;
        int ret = APP2EXT_SUCCESS;
@@ -307,29 +298,22 @@ int app2sd_usr_post_app_install(const char *pkgid,
                _E("MMC not present OR Not ready (%d)", ret);
                return APP2EXT_ERROR_MMC_STATUS;
        }
-       snprintf(mmc_path, FILENAME_MAX - 1, "%s", sdpath);
+       snprintf(mmc_path, sizeof(mmc_path), "%s", sdpath);
        free(sdpath);
        sync();
 
        encoded_id = _app2sd_get_encoded_name(pkgid, uid);
        if (encoded_id == NULL)
                return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
-
-       if (_is_global(uid)) {
-               snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
-                       tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
-               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
-                       mmc_path, EXTIMG_DIR, encoded_id);
-       } else {
-               tzplatform_set_user(uid);
-               snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
-                       tzplatform_getenv(TZ_USER_APP), pkgid);
-               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
-                       mmc_path, EXTIMG_DIR, encoded_id);
-               tzplatform_reset_user();
-       }
+       snprintf(loopback_device, sizeof(loopback_device), "%s/%s/%s",
+               mmc_path, EXTIMG_DIR, encoded_id);
        free(encoded_id);
 
+       _app2sd_set_application_path(pkgid, uid, application_path,
+               sizeof(application_path));
+       snprintf(app_mmc_path, sizeof(app_mmc_path), "%s/.mmc",
+               application_path);
+
        ret = __app2sd_finalize_device_setup(pkgid, loopback_device,
                application_path, uid);
        if (ret) {
@@ -351,9 +335,7 @@ int app2sd_usr_post_app_install(const char *pkgid,
                if (ret)
                        _E("unable to delete info");
 
-               snprintf(application_mmc_path, FILENAME_MAX - 1, "%s/.mmc",
-                       application_path);
-               ret = _app2sd_delete_directory(application_mmc_path);
+               ret = _app2sd_delete_directory(app_mmc_path);
                if (ret)
                        _E("unable to delete the directory (%s)", application_path);
        } else {
@@ -400,28 +382,17 @@ int app2sd_usr_on_demand_setup_init(const char *pkgid, uid_t uid)
                _E("MMC not present OR Not ready (%d)", ret);
                return APP2EXT_ERROR_MMC_STATUS;
        }
-       snprintf(mmc_path, FILENAME_MAX - 1, "%s", sdpath);
+       snprintf(mmc_path, sizeof(mmc_path), "%s", sdpath);
        free(sdpath);
 
        encoded_id = _app2sd_get_encoded_name(pkgid, uid);
        if (encoded_id == NULL)
                return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
-
-       /* check app entry is there in sd card or not. */
-       if (_is_global(uid)) {
-               snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
-                       tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
-               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
-                       mmc_path, EXTIMG_DIR, encoded_id);
-       } else {
-               tzplatform_set_user(uid);
-               snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
-                       tzplatform_getenv(TZ_USER_APP), pkgid);
-               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
+       snprintf(loopback_device, sizeof(loopback_device), "%s/%s/%s",
                        mmc_path, EXTIMG_DIR, encoded_id);
-               tzplatform_reset_user();
-       }
        free(encoded_id);
+       _app2sd_set_application_path(pkgid, uid, application_path,
+               sizeof(application_path));
 
        fp = fopen(loopback_device, "r+");
        if (fp == NULL) {
@@ -570,28 +541,17 @@ int app2sd_usr_on_demand_setup_exit(const char *pkgid, uid_t uid)
                _W("MMC not present OR Not ready (%d)", ret);
                mmc_present = 0;
        }
-       snprintf(mmc_path, FILENAME_MAX - 1, "%s", sdpath);
+       snprintf(mmc_path, sizeof(mmc_path), "%s", sdpath);
        free(sdpath);
 
        encoded_id = _app2sd_get_encoded_name(pkgid, uid);
        if (encoded_id == NULL)
                return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
-
-       /* check app entry is there in sd card or not. */
-       if (_is_global(uid)) {
-               snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
-                       tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
-               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
-                       mmc_path, EXTIMG_DIR, encoded_id);
-       } else {
-               tzplatform_set_user(uid);
-               snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
-                       tzplatform_getenv(TZ_USER_APP), pkgid);
-               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
-                       mmc_path, EXTIMG_DIR, encoded_id);
-               tzplatform_reset_user();
-       }
+       snprintf(loopback_device, sizeof(loopback_device), "%s/%s/%s",
+               mmc_path, EXTIMG_DIR, encoded_id);
        free(encoded_id);
+       _app2sd_set_application_path(pkgid, uid, application_path,
+               sizeof(application_path));
 
        if (mmc_present) {
                fp = fopen(loopback_device, "r+");
@@ -637,28 +597,18 @@ int app2sd_usr_pre_app_uninstall(const char *pkgid, uid_t uid)
                ret = APP2EXT_ERROR_MMC_STATUS;
                goto END;
        }
-       snprintf(mmc_path, FILENAME_MAX - 1, "%s", sdpath);
+       snprintf(mmc_path, sizeof(mmc_path), "%s", sdpath);
        free(sdpath);
        sync();
 
        encoded_id = _app2sd_get_encoded_name(pkgid, uid);
        if (encoded_id == NULL)
                return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
-
-       if (_is_global(uid)) {
-               snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
-                       tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
-               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
-                       mmc_path, EXTIMG_DIR, encoded_id);
-       } else {
-               tzplatform_set_user(uid);
-               snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
-                       tzplatform_getenv(TZ_USER_APP), pkgid);
-               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
-                       mmc_path, EXTIMG_DIR, encoded_id);
-               tzplatform_reset_user();
-       }
+       snprintf(loopback_device, sizeof(loopback_device), "%s/%s/%s",
+               mmc_path, EXTIMG_DIR, encoded_id);
        free(encoded_id);
+       _app2sd_set_application_path(pkgid, uid, application_path,
+               sizeof(application_path));
 
        /* check app entry is there in sd card or not. */
        fp = fopen(loopback_device, "r+");
@@ -736,7 +686,7 @@ int app2sd_usr_post_app_uninstall(const char *pkgid, uid_t uid)
 {
        char mmc_path[FILENAME_MAX] = { 0, };
        char application_path[FILENAME_MAX] = { 0, };
-       char application_mmc_path[FILENAME_MAX] = { 0, };
+       char app_mmc_path[FILENAME_MAX] = { 0, };
        char loopback_device[FILENAME_MAX] = { 0, };
        char *sdpath = NULL;
        char *encoded_id = NULL;
@@ -756,29 +706,22 @@ int app2sd_usr_post_app_uninstall(const char *pkgid, uid_t uid)
                ret = APP2EXT_ERROR_MMC_STATUS;
                goto END;
        }
-       snprintf(mmc_path, FILENAME_MAX - 1, "%s", sdpath);
+       snprintf(mmc_path, sizeof(mmc_path), "%s", sdpath);
        free(sdpath);
        sync();
 
        encoded_id = _app2sd_get_encoded_name(pkgid, uid);
        if (encoded_id == NULL)
                return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
-
-       if (_is_global(uid)) {
-               snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
-                       tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
-               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
-                       mmc_path, EXTIMG_DIR, encoded_id);
-       } else {
-               tzplatform_set_user(uid);
-               snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
-                       tzplatform_getenv(TZ_USER_APP), pkgid);
-               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
-                       mmc_path, EXTIMG_DIR, encoded_id);
-               tzplatform_reset_user();
-       }
+       snprintf(loopback_device, sizeof(loopback_device), "%s/%s/%s",
+               mmc_path, EXTIMG_DIR, encoded_id);
        free(encoded_id);
 
+       _app2sd_set_application_path(pkgid, uid, application_path,
+               sizeof(application_path));
+       snprintf(app_mmc_path, sizeof(app_mmc_path), "%s/.mmc",
+               application_path);
+
        ret = __app2sd_finalize_device_setup(pkgid, loopback_device,
                application_path, uid);
        if (ret) {
@@ -795,9 +738,7 @@ int app2sd_usr_post_app_uninstall(const char *pkgid, uid_t uid)
                goto END;
        }
 
-       snprintf(application_mmc_path, FILENAME_MAX - 1, "%s/.mmc",
-               application_path);
-       ret = _app2sd_delete_directory(application_mmc_path);
+       ret = _app2sd_delete_directory(app_mmc_path);
        if (ret) {
                _E("unable to delete the directory (%s)",
                application_path);
@@ -847,7 +788,7 @@ int app2sd_usr_pre_move_installed_app(const char *pkgid,
                _E("MMC not present OR Not ready(%d)", ret);
                return APP2EXT_ERROR_MMC_STATUS;
        }
-       snprintf(mmc_path, FILENAME_MAX - 1, "%s", sdpath);
+       snprintf(mmc_path, sizeof(mmc_path), "%s", sdpath);
        free(sdpath);
        sync();
 
@@ -921,7 +862,7 @@ int app2sd_usr_post_move_installed_app(const char *pkgid,
                _E("MMC not present OR Not ready(%d)", ret);
                return APP2EXT_ERROR_MMC_STATUS;
        }
-       snprintf(mmc_path, FILENAME_MAX - 1, "%s", sdpath);
+       snprintf(mmc_path, sizeof(mmc_path), "%s", sdpath);
        free(sdpath);
        sync();
 
@@ -929,18 +870,11 @@ int app2sd_usr_post_move_installed_app(const char *pkgid,
        if (encoded_id == NULL)
                return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
 
-       snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
+       snprintf(loopback_device, sizeof(loopback_device), "%s/%s/%s",
                        mmc_path, EXTIMG_DIR, encoded_id);
        free(encoded_id);
-       if (_is_global(uid)) {
-               snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
-                       tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
-       } else {
-               tzplatform_set_user(uid);
-               snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
-                       tzplatform_getenv(TZ_USER_APP), pkgid);
-               tzplatform_reset_user();
-       }
+       _app2sd_set_application_path(pkgid, uid, application_path,
+               sizeof(application_path));
 
        ret = __app2sd_finalize_device_setup(pkgid, loopback_device,
                application_path, uid);
@@ -985,29 +919,19 @@ int app2sd_usr_pre_app_upgrade(const char *pkgid, GList *dir_list,
                _E("MMC not present OR Not ready (%d)", ret);
                return APP2EXT_ERROR_MMC_STATUS;
        }
-       snprintf(app2sd_path, FILENAME_MAX - 1, "%s/%s",
-                       sdpath, EXTIMG_DIR);
+       snprintf(app2sd_path, sizeof(app2sd_path), "%s/%s",
+               sdpath, EXTIMG_DIR);
        free(sdpath);
        sync();
 
        encoded_id = _app2sd_get_encoded_name(pkgid, uid);
        if (encoded_id == NULL)
                return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
-
-       if (_is_global(uid)) {
-               snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
-                       tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
-               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
-                       app2sd_path, encoded_id);
-       } else {
-               tzplatform_set_user(uid);
-               snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
-                       tzplatform_getenv(TZ_USER_APP), pkgid);
-               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
-                       app2sd_path, encoded_id);
-               tzplatform_reset_user();
-       }
+       snprintf(loopback_device, sizeof(loopback_device), "%s/%s",
+               app2sd_path, encoded_id);
        free(encoded_id);
+       _app2sd_set_application_path(pkgid, uid, application_path,
+               sizeof(application_path));
 
        /* check app entry is there in sd card or not. */
        fp = fopen(loopback_device, "r+");
@@ -1193,28 +1117,18 @@ int app2sd_usr_post_app_upgrade(const char *pkgid,
                _E("MMC not present OR Not ready (%d)", ret);
                return APP2EXT_ERROR_MMC_STATUS;
        }
-       snprintf(mmc_path, FILENAME_MAX - 1, "%s", sdpath);
+       snprintf(mmc_path, sizeof(mmc_path), "%s", sdpath);
        free(sdpath);
        sync();
 
        encoded_id = _app2sd_get_encoded_name(pkgid, uid);
        if (encoded_id == NULL)
                return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
-
-       if (_is_global(uid)) {
-               snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
-                       tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
-               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
-                       mmc_path, EXTIMG_DIR, encoded_id);
-       } else {
-               tzplatform_set_user(uid);
-               snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
-                       tzplatform_getenv(TZ_USER_APP), pkgid);
-               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
-                       mmc_path, EXTIMG_DIR, encoded_id);
-               tzplatform_reset_user();
-       }
+       snprintf(loopback_device, sizeof(loopback_device), "%s/%s/%s",
+               mmc_path, EXTIMG_DIR, encoded_id);
        free(encoded_id);
+       _app2sd_set_application_path(pkgid, uid, application_path,
+               sizeof(application_path));
 
        ret = __app2sd_finalize_device_setup(pkgid, loopback_device,
                application_path, uid);
@@ -1257,28 +1171,18 @@ int app2sd_usr_force_clean(const char *pkgid, uid_t uid)
                _E("MMC not present OR Not ready (%d)", ret);
                return APP2EXT_ERROR_MMC_STATUS;
        }
-       snprintf(mmc_path, FILENAME_MAX - 1, "%s", sdpath);
+       snprintf(mmc_path, sizeof(mmc_path), "%s", sdpath);
        free(sdpath);
        sync();
 
        encoded_id = _app2sd_get_encoded_name(pkgid, uid);
        if (encoded_id == NULL)
                return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
-
-       if (_is_global(uid)) {
-               snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
-                       tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
-               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
-                       mmc_path, EXTIMG_DIR, encoded_id);
-       } else {
-               tzplatform_set_user(uid);
-               snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
-                       tzplatform_getenv(TZ_USER_APP), pkgid);
-               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
-                       mmc_path, EXTIMG_DIR, encoded_id);
-               tzplatform_reset_user();
-       }
+       snprintf(loopback_device, sizeof(loopback_device), "%s/%s/%s",
+               mmc_path, EXTIMG_DIR, encoded_id);
        free(encoded_id);
+       _app2sd_set_application_path(pkgid, uid, application_path,
+               sizeof(application_path));
 
        ret = _app2sd_force_clean(pkgid, application_path, loopback_device, uid);
 
@@ -1304,8 +1208,8 @@ int app2sd_enable_full_pkg(void)
                _E("MMC not present OR Not ready (%d)", ret);
                return APP2EXT_ERROR_MMC_STATUS;
        }
-       snprintf(app2sd_path, FILENAME_MAX - 1, "%s/%s",
-                       sdpath, EXTIMG_DIR);
+       snprintf(app2sd_path, sizeof(app2sd_path), "%s/%s",
+               sdpath, EXTIMG_DIR);
        free(sdpath);
 
        dir = opendir(app2sd_path);
@@ -1326,7 +1230,7 @@ int app2sd_enable_full_pkg(void)
                if (strcmp(entry->d_name, ".") == 0 ||
                        strcmp(entry->d_name, "..") == 0)
                        continue;
-               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
+               snprintf(loopback_device, sizeof(loopback_device), "%s/%s",
                        app2sd_path, entry->d_name);
                ret = _app2sd_get_info_from_db(loopback_device,
                        &pkgid, &uid);
@@ -1585,9 +1489,9 @@ int app2sd_post_migrate_legacy(const char *pkgid, uid_t uid)
        }
 
        encoded_id = _app2sd_get_encoded_name(pkgid, uid);
-       if (encoded_id == NULL) {
+       if (encoded_id == NULL)
                return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
-       }
+
        snprintf(loopback_device, sizeof(loopback_device), "%s/%s",
                mmc_path, encoded_id);
        free(encoded_id);
index 66a75e9..45d77b7 100644 (file)
@@ -104,7 +104,7 @@ char *_app2sd_find_associated_device_node(const char *loopback_device)
        }
 
        /* process the string*/
-       snprintf(dev, FILENAME_MAX - 1, "%s", result);
+       snprintf(dev, sizeof(dev), "%s", result);
 
        if (strstr(dev, "dev") == NULL) {
                _E("unable to find the associated file");
@@ -144,10 +144,10 @@ char *_app2sd_create_loopdevice_node(void)
                }
                count = 0;
                char dev_path[BUF_SIZE] = { 0, };
-               snprintf(dev_path, BUF_SIZE, "/dev/loop%d", count);
+               snprintf(dev_path, sizeof(dev_path), "/dev/loop%d", count);
                while ((fp = fopen(dev_path, "r+")) != NULL) {
                        count = count + 1;
-                       snprintf(dev_path, BUF_SIZE, "/dev/loop%d", count);
+                       snprintf(dev_path, sizeof(dev_path), "/dev/loop%d", count);
                        _D("next dev path for checking is (%s)",
                                     dev_path);
                        fclose(fp);
@@ -396,7 +396,8 @@ int _app2sd_dmcrypt_setup_device(const char *pkgid,
                }
        }
 
-       snprintf(dmcrypt_setup_cmd, BUF_SIZE, "/bin/echo '%s' | /sbin/cryptsetup -q -i %d " \
+       snprintf(dmcrypt_setup_cmd, sizeof(dmcrypt_setup_cmd),
+               "/bin/echo '%s' | /sbin/cryptsetup -q -i %d " \
                "-c aes-cbc-lmk -s %d --align-payload=8 luksFormat %s",
                passwd, DMCRYPT_ITER_TIME, DMCRYPT_KEY_LEN, loopback_device);
 
@@ -436,9 +437,9 @@ int _app2sd_dmcrypt_open_device(const char *pkgid, const char *loopback_device,
        }
 
        if (is_dup)
-               snprintf(dev_name, BUF_SIZE, "%s.new_%d", pkgid, uid);
+               snprintf(dev_name, sizeof(dev_name), "%s.new_%d", pkgid, uid);
        else
-               snprintf(dev_name, BUF_SIZE, "%s_%d", pkgid, uid);
+               snprintf(dev_name, sizeof(dev_name), "%s_%d", pkgid, uid);
 
        /* get password for dmcrypt encryption */
        ret = _app2sd_initialize_db();
@@ -473,7 +474,7 @@ int _app2sd_dmcrypt_open_device(const char *pkgid, const char *loopback_device,
                return APP2EXT_ERROR_OPEN_DMCRYPT_DEVICE;
        }
 
-       snprintf(buf, BUF_SIZE, "/dev/mapper/%s", dev_name);
+       snprintf(buf, sizeof(buf), "/dev/mapper/%s", dev_name);
        len = strlen(buf);
        *dev_node = (char *)calloc(len + 1, sizeof(char));
        if (*dev_node == NULL) {
@@ -507,8 +508,9 @@ int _app2sd_dmcrypt_close_device(const char *pkgid, uid_t uid)
        free(t_dev_node);
        t_dev_node = NULL;
 
-       snprintf(dev_node, BUF_SIZE, "/dev/mapper/%s_%d", pkgid, uid);
-       snprintf(dmcrypt_close_cmd, BUF_SIZE, "/sbin/cryptsetup -q luksClose %s", dev_node);
+       snprintf(dev_node, sizeof(dev_node), "/dev/mapper/%s_%d", pkgid, uid);
+       snprintf(dmcrypt_close_cmd, sizeof(dmcrypt_close_cmd),
+               "/sbin/cryptsetup -q luksClose %s", dev_node);
        ret = system(dmcrypt_close_cmd);
        if (ret) {
                strerror_r(errno, err_buf, sizeof(err_buf));
@@ -526,7 +528,7 @@ char *_app2sd_find_associated_dmcrypt_device_node(const char *pkgid, uid_t uid)
        char buf[BUF_SIZE] = { 0, };
        int len = 0;
 
-       snprintf(buf, BUF_SIZE, "/dev/mapper/%s_%d", pkgid, uid);
+       snprintf(buf, sizeof(buf), "/dev/mapper/%s_%d", pkgid, uid);
        len = strlen(buf);
        dev_node = (char *)calloc(len + 1, sizeof(char));
        if (dev_node == NULL) {
@@ -587,8 +589,8 @@ int _app2sd_create_loopback_device(const char *pkgid,
                _E("invalid argument");
                return APP2EXT_ERROR_INVALID_ARGUMENTS;
        }
-       snprintf(command, FILENAME_MAX - 1, "of=%s", loopback_device);
-       snprintf(buff, BUF_SIZE - 1, "count=%d", size);
+       snprintf(command, sizeof(command), "of=%s", loopback_device);
+       snprintf(buff, sizeof(buff), "count=%d", size);
 
        const char *argv1[] = { "/bin/dd", "if=/dev/zero",
                command, "bs=1M", buff, NULL };
@@ -660,25 +662,25 @@ static int _app2sd_create_dir_with_link(const char *application_path,
                const char *pkgid, const char *dir_name, uid_t uid)
 {
        int ret = APP2EXT_SUCCESS;
-       char application_dir_mmc_path[FILENAME_MAX] = { 0, };
-       char application_dir_path[FILENAME_MAX] = { 0, };
+       char app_dir_mmc_path[FILENAME_MAX] = { 0, };
+       char app_dir_path[FILENAME_MAX] = { 0, };
 
-       snprintf(application_dir_mmc_path, FILENAME_MAX - 1, "%s/.mmc/%s",
+       snprintf(app_dir_mmc_path, sizeof(app_dir_mmc_path), "%s/.mmc/%s",
                application_path, dir_name);
-       snprintf(application_dir_path, FILENAME_MAX, "%s/%s",
+       snprintf(app_dir_path, sizeof(app_dir_path), "%s/%s",
                application_path, dir_name);
 
-       ret = _app2sd_make_directory(application_dir_mmc_path, uid);
+       ret = _app2sd_make_directory(app_dir_mmc_path, uid);
        if (ret) {
                _E("create directory failed");
                return APP2EXT_ERROR_CREATE_DIRECTORY;
        }
 
-       if ((ret = symlink(application_dir_mmc_path,
-               application_dir_path)) < 0) {
+       if ((ret = symlink(app_dir_mmc_path,
+               app_dir_path)) < 0) {
                if (errno == EEXIST) {
                        _D("file with symlink name present (%s)",
-                               application_dir_path);
+                               app_dir_path);
                } else {
                        _E("symbolic link creation "
                                "failed, error no is (%d)", errno);
@@ -717,7 +719,7 @@ int _app2sd_mount_app_content(const char *application_path, const char *pkgid,
 {
        int ret = APP2EXT_SUCCESS;
        int fd = -1;
-       char application_mmc_path[FILENAME_MAX] = { 0, };
+       char app_mmc_path[FILENAME_MAX] = { 0, };
        char temp_path[FILENAME_MAX] = { 0, };
        char err_buf[1024] = {0,};
        struct timespec time = {
@@ -739,11 +741,11 @@ int _app2sd_mount_app_content(const char *application_path, const char *pkgid,
        }
        close(fd);
 
-       snprintf(application_mmc_path, FILENAME_MAX - 1, "%s/.mmc",
+       snprintf(app_mmc_path, sizeof(app_mmc_path), "%s/.mmc",
                application_path);
-       fd = open(application_mmc_path, O_RDONLY|O_DIRECTORY);
+       fd = open(app_mmc_path, O_RDONLY|O_DIRECTORY);
        if (fd < 0) {
-               _E("path(%s) error(%d)", application_mmc_path, errno);
+               _E("path(%s) error(%d)", app_mmc_path, errno);
                return APP2EXT_ERROR_OPEN_DIR;
        }
        close(fd);
@@ -753,17 +755,17 @@ int _app2sd_mount_app_content(const char *application_path, const char *pkgid,
 
        switch (mount_type) {
        case MOUNT_TYPE_RD:
-               if ((ret = mount(dev, application_mmc_path, FS_TYPE,
+               if ((ret = mount(dev, app_mmc_path, FS_TYPE,
                        MS_MGC_VAL | MS_RDONLY | MS_NOSUID, NULL)) < 0) {
                        _E("read only mount failed, " \
                                "errono is (%d), " \
                                "dev is (%s) path is (%s)",
-                               errno, dev, application_mmc_path);
+                               errno, dev, app_mmc_path);
                        ret = APP2EXT_ERROR_MOUNT;
                }
                break;
        case MOUNT_TYPE_RW:
-               if ((ret = mount(dev, application_mmc_path, FS_TYPE,
+               if ((ret = mount(dev, app_mmc_path, FS_TYPE,
                        MS_MGC_VAL | MS_NOSUID, NULL)) < 0) {
                        _E("read write mount failed, " \
                                "errono is (%d)", errno);
@@ -771,7 +773,7 @@ int _app2sd_mount_app_content(const char *application_path, const char *pkgid,
                }
                break;
        case MOUNT_TYPE_RW_NOEXEC:
-               if ((ret = mount(dev, application_mmc_path, FS_TYPE,
+               if ((ret = mount(dev, app_mmc_path, FS_TYPE,
                        MS_MGC_VAL | MS_NOEXEC | MS_NOSUID, NULL)) < 0) {
                        _E("rwnx mount failed " \
                                "errono is (%d)", errno);
@@ -779,7 +781,7 @@ int _app2sd_mount_app_content(const char *application_path, const char *pkgid,
                }
                break;
        case MOUNT_TYPE_RD_REMOUNT:
-               if ((ret = mount(dev, application_mmc_path, FS_TYPE,
+               if ((ret = mount(dev, app_mmc_path, FS_TYPE,
                        MS_MGC_VAL | MS_RDONLY | MS_REMOUNT | MS_NOSUID,
                        NULL)) < 0) {
                        _E("read remount failed "
@@ -788,7 +790,7 @@ int _app2sd_mount_app_content(const char *application_path, const char *pkgid,
                }
                break;
        case MOUNT_TYPE_RW_REMOUNT:
-               if ((ret = mount(dev, application_mmc_path, FS_TYPE,
+               if ((ret = mount(dev, app_mmc_path, FS_TYPE,
                        MS_MGC_VAL | MS_REMOUNT | MS_NOSUID, NULL)) < 0) {
                        _E("read write remount failed "
                                "errono(%d), errstr(%s)", errno,
@@ -810,8 +812,8 @@ int _app2sd_mount_app_content(const char *application_path, const char *pkgid,
        if (mount_type != MOUNT_TYPE_RD &&
                mount_type != MOUNT_TYPE_RD_REMOUNT) {
                /* change lost+found permission */
-               snprintf(temp_path, FILENAME_MAX - 1, "%s/lost+found",
-                               application_mmc_path);
+               snprintf(temp_path, sizeof(temp_path), "%s/lost+found",
+                               app_mmc_path);
                ret = _app2sd_make_directory(temp_path, uid);
                if (ret) {
                        _E("create directory(%s) failed", temp_path);
@@ -825,12 +827,12 @@ int _app2sd_mount_app_content(const char *application_path, const char *pkgid,
 int _app2sd_unmount_app_content(const char *application_path)
 {
        int ret = APP2EXT_SUCCESS;
-       char application_dir_mmc_path[FILENAME_MAX] = { 0, };
+       char app_mmc_path[FILENAME_MAX] = { 0, };
        char err_buf[1024] = {0,};
 
-       snprintf(application_dir_mmc_path, FILENAME_MAX - 1, "%s/.mmc",
+       snprintf(app_mmc_path, sizeof(app_mmc_path), "%s/.mmc",
                application_path);
-       if ((ret = umount(application_dir_mmc_path)) < 0) {
+       if ((ret = umount(app_mmc_path)) < 0) {
                strerror_r(errno, err_buf, sizeof(err_buf));
                _D("unable to umount the dir, ret(%d) error(%d, %s)",
                        ret, errno, err_buf);
@@ -867,8 +869,8 @@ static int _app2sd_move_app_to_external(const char *pkgid, GList *dir_list,
        int ret = APP2EXT_SUCCESS;
        mode_t mode = DIR_PERMS;
        char temp_dir_path[FILENAME_MAX] = { 0, };
-       char application_mmc_path[FILENAME_MAX] = { 0, };
-       char application_archive_path[FILENAME_MAX] = { 0, };
+       char app_mmc_path[FILENAME_MAX] = { 0, };
+       char app_archive_path[FILENAME_MAX] = { 0, };
        char application_path[FILENAME_MAX] = { 0, };
        char loopback_device[FILENAME_MAX] = { 0, };
        unsigned long long total_size = 0;
@@ -889,18 +891,11 @@ static int _app2sd_move_app_to_external(const char *pkgid, GList *dir_list,
        if (encoded_id == NULL)
                return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
 
-       snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
+       snprintf(loopback_device, sizeof(loopback_device), "%s/%s/%s",
                        mmc_path, EXTIMG_DIR, encoded_id);
        free(encoded_id);
-       if (_is_global(uid)) {
-               snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
-                       tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
-       } else {
-               tzplatform_set_user(uid);
-               snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
-                       tzplatform_getenv(TZ_USER_APP), pkgid);
-               tzplatform_reset_user();
-       }
+       _app2sd_set_application_path(pkgid, uid, application_path,
+               sizeof(application_path));
 
        /* check whether application is in external memory or not */
        fp = fopen(loopback_device, "r+");
@@ -911,12 +906,12 @@ static int _app2sd_move_app_to_external(const char *pkgid, GList *dir_list,
                _app2sd_force_clean(pkgid, application_path, loopback_device, uid);
        }
 
-       snprintf(application_mmc_path, FILENAME_MAX - 1, "%s/.mmc",
+       snprintf(app_mmc_path, sizeof(app_mmc_path), "%s/.mmc",
                application_path);
-       snprintf(application_archive_path, FILENAME_MAX - 1, "%s/.archive",
+       snprintf(app_archive_path, sizeof(app_archive_path), "%s/.archive",
                application_path);
 
-       ret = mkdir(application_mmc_path, mode);
+       ret = mkdir(app_mmc_path, mode);
        if (ret) {
                if (errno != EEXIST) {
                        _E("unable to create directory for archiving," \
@@ -925,7 +920,7 @@ static int _app2sd_move_app_to_external(const char *pkgid, GList *dir_list,
                }
        }
 
-       ret = mkdir(application_archive_path, mode);
+       ret = mkdir(app_archive_path, mode);
        if (ret) {
                if (errno != EEXIST) {
                        _E("unable to create directory for archiving," \
@@ -939,8 +934,8 @@ static int _app2sd_move_app_to_external(const char *pkgid, GList *dir_list,
                dir_detail = (app2ext_dir_details *)list->data;
                if (dir_detail && dir_detail->name
                        && dir_detail->type == APP2EXT_DIR_RO) {
-                       memset(temp_dir_path, '\0', FILENAME_MAX);
-                       snprintf(temp_dir_path, FILENAME_MAX,
+                       memset(temp_dir_path, '\0', sizeof(temp_dir_path));
+                       snprintf(temp_dir_path, sizeof(temp_dir_path),
                                 "%s/%s", application_path,
                                 dir_detail->name);
                        _D("cal size of app dirs, temp_dir_path(%s)",
@@ -1023,13 +1018,13 @@ static int _app2sd_move_app_to_external(const char *pkgid, GList *dir_list,
                if (dir_detail && dir_detail->name
                        && dir_detail->type == APP2EXT_DIR_RO) {
                        memset(temp_dir_path, '\0', FILENAME_MAX);
-                       snprintf(temp_dir_path, FILENAME_MAX,
+                       snprintf(temp_dir_path, sizeof(temp_dir_path),
                                 "%s/%s", application_path,
                                dir_detail->name);
                        _D("app2archive, temp_dir_path(%s)",
                                temp_dir_path);
                        ret = _app2sd_move_to_archive(temp_dir_path,
-                               application_archive_path);
+                               app_archive_path);
                        if (ret) {
                                if (ret == APP2EXT_ERROR_ACCESS_FILE) {
                                        _E("unable to access (%s)",
@@ -1037,7 +1032,7 @@ static int _app2sd_move_app_to_external(const char *pkgid, GList *dir_list,
                                } else {
                                        _E("unable to copy from (%s) to (%s)",
                                             temp_dir_path,
-                                            application_archive_path);
+                                            app_archive_path);
                                }
                                goto ERR;
                        }
@@ -1060,14 +1055,14 @@ static int _app2sd_move_app_to_external(const char *pkgid, GList *dir_list,
                dir_detail = (app2ext_dir_details *)list->data;
                if (dir_detail && dir_detail->name
                        && dir_detail->type == APP2EXT_DIR_RO) {
-                       memset(temp_dir_path, '\0', FILENAME_MAX);
-                       snprintf(temp_dir_path, FILENAME_MAX,
-                               "%s/%s", application_archive_path,
+                       memset(temp_dir_path, '\0', sizeof(temp_dir_path));
+                       snprintf(temp_dir_path, sizeof(temp_dir_path),
+                               "%s/%s", app_archive_path,
                                dir_detail->name);
                        _D("archive2mmc, temp_dir_path(%s)",
                                temp_dir_path);
                        ret = _app2sd_copy_dir(temp_dir_path,
-                               application_mmc_path);
+                               app_mmc_path);
                        if (ret) {
                                if (ret == APP2EXT_ERROR_ACCESS_FILE) {
                                        _E("unable to access (%s)",
@@ -1078,7 +1073,7 @@ static int _app2sd_move_app_to_external(const char *pkgid, GList *dir_list,
                                        _E("unable to copy from (%s) to (%s)," \
                                                " error is (%s)",
                                                temp_dir_path,
-                                               application_mmc_path, err_buf);
+                                               app_mmc_path, err_buf);
                                }
                                goto ERR;
                        }
@@ -1091,9 +1086,9 @@ static int _app2sd_move_app_to_external(const char *pkgid, GList *dir_list,
                list = g_list_next(list);
        }
 
-       ret = _app2sd_delete_directory(application_archive_path);
+       ret = _app2sd_delete_directory(app_archive_path);
        if (ret) {
-               _E("unable to delete (%s)", application_archive_path);
+               _E("unable to delete (%s)", app_archive_path);
                ret = APP2EXT_ERROR_DELETE_DIRECTORY;
                goto ERR;
        }
@@ -1117,8 +1112,8 @@ static int _app2sd_move_app_to_internal(const char *pkgid, GList *dir_list,
        int ret = APP2EXT_SUCCESS;
        mode_t mode = DIR_PERMS;
        char temp_dir_path[FILENAME_MAX] = { 0, };
-       char application_mmc_path[FILENAME_MAX] = { 0, };
-       char application_archive_path[FILENAME_MAX] = { 0, };
+       char app_mmc_path[FILENAME_MAX] = { 0, };
+       char app_archive_path[FILENAME_MAX] = { 0, };
        char application_path[FILENAME_MAX] = { 0, };
        char loopback_device[FILENAME_MAX] = { 0, };
        char *device_node = NULL;
@@ -1136,18 +1131,11 @@ static int _app2sd_move_app_to_internal(const char *pkgid, GList *dir_list,
        if (encoded_id == NULL)
                return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
 
-       snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
+       snprintf(loopback_device, sizeof(loopback_device), "%s/%s/%s",
                        mmc_path, EXTIMG_DIR, encoded_id);
        free(encoded_id);
-       if (_is_global(uid)) {
-               snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
-                       tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
-       } else {
-               tzplatform_set_user(uid);
-               snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
-                       tzplatform_getenv(TZ_USER_APP), pkgid);
-               tzplatform_reset_user();
-       }
+       _app2sd_set_application_path(pkgid, uid, application_path,
+               sizeof(application_path));
 
        /* check whether application is in external memory or not */
        fp = fopen(loopback_device, "r+");
@@ -1243,12 +1231,12 @@ static int _app2sd_move_app_to_internal(const char *pkgid, GList *dir_list,
                }
        }
 
-       snprintf(application_mmc_path, FILENAME_MAX - 1, "%s/.mmc",
+       snprintf(app_mmc_path, sizeof(app_mmc_path), "%s/.mmc",
                application_path);
-       snprintf(application_archive_path, FILENAME_MAX - 1, "%s/.archive",
+       snprintf(app_archive_path, sizeof(app_archive_path), "%s/.archive",
                application_path);
 
-       ret = mkdir(application_archive_path, mode);
+       ret = mkdir(app_archive_path, mode);
        if (ret) {
                if (errno != EEXIST) {
                        _E("unable to create directory for archiving," \
@@ -1264,13 +1252,13 @@ static int _app2sd_move_app_to_internal(const char *pkgid, GList *dir_list,
                if (dir_detail && dir_detail->name
                        && dir_detail->type == APP2EXT_DIR_RO) {
                        /* archiving code */
-                       memset(temp_dir_path, '\0', FILENAME_MAX);
-                       snprintf(temp_dir_path, FILENAME_MAX,
-                                "%s/%s", application_mmc_path,
+                       memset(temp_dir_path, '\0', sizeof(temp_dir_path));
+                       snprintf(temp_dir_path, sizeof(temp_dir_path),
+                                "%s/%s", app_mmc_path,
                                 dir_detail->name);
                        _D("mmc2archive, temp_dir_path(%s)", temp_dir_path);
                        ret = _app2sd_copy_dir(temp_dir_path,
-                                    application_archive_path);
+                                    app_archive_path);
                        if (ret) {
                                if (ret == APP2EXT_ERROR_ACCESS_FILE) {
                                        _E("unable to access (%s)",
@@ -1281,14 +1269,14 @@ static int _app2sd_move_app_to_internal(const char *pkgid, GList *dir_list,
                                        _E("unable to copy from (%s) to (%s),"
                                                " error is (%s)",
                                                temp_dir_path,
-                                               application_archive_path, err_buf);
+                                               app_archive_path, err_buf);
                                }
                                goto ERR;
                        }
 
                        /* delete the symbolic link files [bin, lib, res]*/
-                       memset(temp_dir_path, '\0', FILENAME_MAX);
-                       snprintf(temp_dir_path, FILENAME_MAX,
+                       memset(temp_dir_path, '\0', sizeof(temp_dir_path));
+                       snprintf(temp_dir_path, sizeof(temp_dir_path),
                                 "%s/%s", application_path,
                                 dir_detail->name);
                        _D("unlink, temp_dir_path(%s)", temp_dir_path);
@@ -1306,9 +1294,9 @@ static int _app2sd_move_app_to_internal(const char *pkgid, GList *dir_list,
                        }
 
                        /* Copy content to destination */
-                       memset(temp_dir_path, '\0', FILENAME_MAX);
-                       snprintf(temp_dir_path, FILENAME_MAX,
-                               "%s/%s", application_archive_path,
+                       memset(temp_dir_path, '\0', sizeof(temp_dir_path));
+                       snprintf(temp_dir_path, sizeof(temp_dir_path),
+                               "%s/%s", app_archive_path,
                                dir_detail->name);
                        _D("archive2app, temp_dir_path(%s)", temp_dir_path);
                        ret = _app2sd_copy_dir(temp_dir_path, application_path);
@@ -1352,13 +1340,13 @@ static int _app2sd_move_app_to_internal(const char *pkgid, GList *dir_list,
                _E("unable to delete the loopback device for (%s)",
                     pkgid);
 
-       ret = _app2sd_delete_directory(application_mmc_path);
+       ret = _app2sd_delete_directory(app_mmc_path);
        if (ret)
-               _E("unable to delete (%s)", application_mmc_path);
+               _E("unable to delete (%s)", app_mmc_path);
 
-       ret = _app2sd_delete_directory(application_archive_path);
+       ret = _app2sd_delete_directory(app_archive_path);
        if (ret)
-               _E("unable to delete (%s)", application_archive_path);
+               _E("unable to delete (%s)", app_archive_path);
 
        /* remove passwrd from DB */
        ret = _app2sd_initialize_db();
@@ -1422,8 +1410,8 @@ int _app2sd_copy_ro_content(const char *src, const char *dest, GList *dir_list)
                dir_detail = (app2ext_dir_details *)list->data;
                if (dir_detail && dir_detail->name
                        && dir_detail->type == APP2EXT_DIR_RO) {
-                       memset((void *)&path, '\0', FILENAME_MAX);
-                       snprintf(path, FILENAME_MAX - 1, "%s/%s", src,
+                       memset((void *)&path, '\0', sizeof(path));
+                       snprintf(path, sizeof(path), "%s/%s", src,
                                 dir_detail->name);
                        ret = _app2sd_copy_dir(path, dest);
                        if (ret) {
@@ -1587,8 +1575,8 @@ int _app2sd_update_loopback_device_size(const char *pkgid,
        char *device_node = NULL;
        char *old_device_node = NULL;
        int err_res = 0;
-       char application_mmc_path[FILENAME_MAX] = { 0, };
-       char temp_application_mmc_path[FILENAME_MAX] = { 0, };
+       char app_mmc_path[FILENAME_MAX] = { 0, };
+       char temp_app_mmc_path[FILENAME_MAX] = { 0, };
 
        ret = _app2sd_duplicate_device(pkgid, loopback_device,
                temp_pkgid, temp_application_path, temp_loopback_device,
@@ -1643,13 +1631,13 @@ int _app2sd_update_loopback_device_size(const char *pkgid,
                }
        }
 
-       snprintf(application_mmc_path, FILENAME_MAX - 1,
+       snprintf(app_mmc_path, sizeof(app_mmc_path),
                 "%s/.mmc", application_path);
-       snprintf(temp_application_mmc_path, FILENAME_MAX - 1,
+       snprintf(temp_app_mmc_path, sizeof(temp_app_mmc_path),
                "%s/.mmc", temp_application_path);
 
-       ret = _app2sd_copy_ro_content(application_mmc_path,
-               temp_application_mmc_path, dir_list);
+       ret = _app2sd_copy_ro_content(app_mmc_path,
+               temp_app_mmc_path, dir_list);
        if (ret) {
                _E("copy ro content failed");
                err_res = ret;
index 970db56..fa5ea9b 100644 (file)
@@ -154,7 +154,7 @@ void _app2sd_delete_symlink(const char *dirname)
                                continue;
 
                        /*get realpath find symlink to ".mmc" and unlink it*/
-                       snprintf(abs_filename, FILENAME_MAX, "%s/%s", dirname, ep->d_name);
+                       snprintf(abs_filename, sizeof(abs_filename), "%s/%s", dirname, ep->d_name);
                        char *path = realpath(abs_filename, mmc_path);
                        if (!path)
                                _E("realpath failed");
@@ -173,7 +173,7 @@ void _app2sd_delete_symlink(const char *dirname)
                (void)closedir(dp);
 
                /*delete ".mmc" folder*/
-               snprintf(abs_filename, FILENAME_MAX, "%s/.mmc", dirname);
+               snprintf(abs_filename, sizeof(abs_filename), "%s/.mmc", dirname);
                ret = remove(abs_filename);
                if (ret == -1)
                        return;
@@ -245,7 +245,7 @@ unsigned long long _app2sd_calculate_dir_size(char *dirname)
                while ((ep = readdir(dp)) != NULL) {
                        struct stat stFileInfo;
 
-                       snprintf(abs_filename, FILENAME_MAX, "%s/%s", dirname,
+                       snprintf(abs_filename, sizeof(abs_filename), "%s/%s", dirname,
                                 ep->d_name);
 
                        if (stat(abs_filename, &stFileInfo) < 0)