Fix rename folder API 12/151112/2 accepted/tizen/unified/20170925.071142 submit/tizen/20170922.060558
authorMinje Ahn <minje.ahn@samsung.com>
Wed, 20 Sep 2017 02:04:14 +0000 (11:04 +0900)
committerMinje Ahn <minje.ahn@samsung.com>
Wed, 20 Sep 2017 08:08:43 +0000 (17:08 +0900)
Use modified time in file system instead of current time

Change-Id: I8d52cce7f8c1cc27da99847254ec39febd926f83
Signed-off-by: Minje Ahn <minje.ahn@samsung.com>
src/common/media-svc.c

index c20024b..d6fbd66 100755 (executable)
@@ -897,8 +897,13 @@ int media_svc_refresh_item(MediaSvcHandle *handle, const char *storage_id, media
 
 int media_svc_rename_folder(MediaSvcHandle *handle, const char *storage_id, const char *src_path, const char *dst_path, uid_t uid)
 {
-       sqlite3 *db_handle = (sqlite3 *)handle;
        int ret = MS_MEDIA_ERR_NONE;
+       sqlite3 *db_handle = (sqlite3 *)handle;
+       char *name = NULL;
+       char *name_pinyin = NULL;
+       bool pinyin_support = FALSE;
+       char *sql = NULL;
+       GArray *query_array = NULL;
 
        media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
        media_svc_retvm_if(src_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
@@ -909,136 +914,117 @@ int media_svc_rename_folder(MediaSvcHandle *handle, const char *storage_id, cons
        ret = _media_svc_sql_begin_trans(uid);
        media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
 
-       /* Update all folder record's modified date, which are changed above */
-       char *update_folder_modified_time_sql = NULL;
-       time_t date;
-       time(&date);
+       /*1. Rename directory */
+       /*Update Pinyin If Support Pinyin*/
+       name = g_path_get_basename(dst_path);
+       media_svc_check_pinyin_support(&pinyin_support);
+       if (pinyin_support) {
+               media_svc_get_pinyin(name, &name_pinyin);
+               sql = sqlite3_mprintf("UPDATE %Q SET path='%q', name='%q', name_pinyin='%q' WHERE path = %Q",
+                                               MEDIA_SVC_DB_TABLE_FOLDER, dst_path, name, name_pinyin, src_path);
+       } else {
+               sql = sqlite3_mprintf("UPDATE %Q SET path='%q', name='%q' WHERE path = %Q",
+                                               MEDIA_SVC_DB_TABLE_FOLDER, dst_path, name, src_path);
+       }
 
-       update_folder_modified_time_sql = sqlite3_mprintf("UPDATE folder SET modified_time = %d WHERE path LIKE '%q';", date, dst_path);
+       ret = media_db_request_update_db_batch(sql, uid);
+       SQLITE3_SAFE_FREE(sql);
+       SAFE_FREE(name);
+       SAFE_FREE(name_pinyin);
+       media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Failed to batch update request");
 
-       ret = media_db_request_update_db_batch(update_folder_modified_time_sql, uid);
-       SQLITE3_SAFE_FREE(update_folder_modified_time_sql);
+       /* 2. Update sub-dir, sub-file */
+       /* Sub folder */
+       sql = sqlite3_mprintf("UPDATE folder SET path = REPLACE( path, '%q/', '%q/');", src_path, dst_path);
+       ret = media_db_request_update_db_batch(sql, uid);
+       SQLITE3_SAFE_FREE(sql);
+       media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Failed to batch update request");
 
-       if (ret != SQLITE_OK) {
-               media_svc_error("failed to update folder modified time");
+       /* Sub files */
+       sql = sqlite3_mprintf("UPDATE '%q' SET path = REPLACE( path, '%q/', '%q/');", storage_id, src_path, dst_path);
+       ret = media_db_request_update_db_batch(sql, uid);
+       SQLITE3_SAFE_FREE(sql);
+       media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Failed to batch update request");
 
-               return MS_MEDIA_ERR_DB_INTERNAL;
+       ret = _media_svc_sql_end_trans(uid);
+       if (ret != MS_MEDIA_ERR_NONE) {
+               media_svc_error("mb_svc_sqlite3_commit_trans failed.. Now start to rollback");
+               return ret;
        }
 
-       /* Update all items */
-       char *select_all_sql = NULL;
+       /*3. Get and update thumbnail_path if exists */
        sqlite3_stmt *sql_stmt = NULL;
-       char dst_child_path[MEDIA_SVC_PATHNAME_SIZE + 1];
 
-       snprintf(dst_child_path, sizeof(dst_child_path), "%s/%%", dst_path);
+       sql = sqlite3_mprintf("SELECT path, thumbnail_path from '%q' where path LIKE '%q/%%' AND thumbnail_path is not null", storage_id, dst_path);
+       media_svc_debug("[SQL query] : %s", sql);
 
-       select_all_sql = sqlite3_mprintf("SELECT media_uuid, path, thumbnail_path, media_type from '%q' where folder_uuid IN ( SELECT folder_uuid FROM folder where path='%q' or path like '%q');", storage_id, dst_path, dst_child_path);
+       ret = _media_svc_sql_prepare_to_step_simple(db_handle, sql, &sql_stmt);
+       media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "error when media_svc_rename_folder. err = [%d]", ret);
 
-       media_svc_debug("[SQL query] : %s", select_all_sql);
 
-       ret = _media_svc_sql_prepare_to_step_simple(db_handle, select_all_sql, &sql_stmt);
-       if (ret != MS_MEDIA_ERR_NONE) {
-               media_svc_error("error when media_svc_rename_folder. err = [%d]", ret);
-               return ret;
-       }
+       query_array = g_array_new(FALSE, FALSE, sizeof(char *));
+       media_svc_retvm_if(query_array == NULL, MS_MEDIA_ERR_INTERNAL, "g_array_new failed");
 
        while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
-               char media_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
                char media_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
                char media_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
                char media_new_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
-               /*int media_type; */
-               bool no_thumb = FALSE;
+               char *thumb_query = NULL;
 
                if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 0))) {
-                       SAFE_STRLCPY(media_uuid, (const char *)sqlite3_column_text(sql_stmt, 0), sizeof(media_uuid));
-               } else {
-                       media_svc_error("media UUID is NULL");
-                       return MS_MEDIA_ERR_DB_INTERNAL;
-               }
-
-               if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 1))) {
-                       SAFE_STRLCPY(media_path, (const char *)sqlite3_column_text(sql_stmt, 1), sizeof(media_path));
+                       SAFE_STRLCPY(media_path, (const char *)sqlite3_column_text(sql_stmt, 0), sizeof(media_path));
                } else {
                        media_svc_error("media path is NULL");
+                       SQLITE3_FINALIZE(sql_stmt);
                        return MS_MEDIA_ERR_DB_INTERNAL;
                }
 
-               if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 2))) {
-                       SAFE_STRLCPY(media_thumb_path,  (const char *)sqlite3_column_text(sql_stmt, 2), sizeof(media_thumb_path));
+               if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 1))) {
+                       SAFE_STRLCPY(media_thumb_path,  (const char *)sqlite3_column_text(sql_stmt, 1), sizeof(media_thumb_path));
                } else {
-                       media_svc_debug("media thumb path doesn't exist in DB");
-                       no_thumb = TRUE;
-               }
-
-               /*media_type = sqlite3_column_int(sql_stmt, 3); */
-
-               /* Update path, thumbnail path of this item */
-               char *replaced_path = NULL;
-               replaced_path = _media_svc_replace_path(media_path, src_path, dst_path);
-               if (replaced_path == NULL) {
-                       media_svc_error("_media_svc_replace_path failed");
+                       media_svc_error("thumbnail path is NULL");
                        SQLITE3_FINALIZE(sql_stmt);
-                       return MS_MEDIA_ERR_INTERNAL;
+                       return MS_MEDIA_ERR_DB_INTERNAL;
                }
 
-               media_svc_debug("New media path : %s", replaced_path);
                ms_user_storage_type_e storage_type = -1;
 
-               if (!no_thumb) {
-                       ret = ms_user_get_storage_type(uid, replaced_path, &storage_type);
-                       if (ret != MS_MEDIA_ERR_NONE) {
-                               media_svc_sec_error("ms_user_get_storage_type failed : [%d], path[%s] storage_type[%d]", ret, replaced_path, storage_type);
-                               SAFE_FREE(replaced_path);
-                               return ret;
-                       }
-
-                       ret = _media_svc_get_thumbnail_path(storage_type, media_new_thumb_path, replaced_path, THUMB_EXT, uid);
-                       if (ret != MS_MEDIA_ERR_NONE) {
-                               media_svc_error("_media_svc_get_thumbnail_path failed : %d", ret);
-                               SAFE_FREE(replaced_path);
-                               SQLITE3_FINALIZE(sql_stmt);
-                               return ret;
-                       }
-
-                       /*media_svc_debug("New media thumbnail path : %s", media_new_thumb_path); */
+               ret = ms_user_get_storage_type(uid, media_path, &storage_type);
+               if (ret != MS_MEDIA_ERR_NONE) {
+                       media_svc_sec_error("ms_user_get_storage_type failed : [%d], path[%s] storage_type[%d]", ret, media_path, storage_type);
+                       SQLITE3_FINALIZE(sql_stmt);
+                       return ret;
                }
 
-               char *update_item_sql = NULL;
-
-               if (no_thumb)
-                       update_item_sql = sqlite3_mprintf("UPDATE '%q' SET path='%q' WHERE media_uuid='%q'", storage_id, replaced_path, media_uuid);
-               else
-                       update_item_sql = sqlite3_mprintf("UPDATE '%q' SET path='%q', thumbnail_path='%q' WHERE media_uuid='%q'", storage_id, replaced_path, media_new_thumb_path, media_uuid);
-
-               ret = media_db_request_update_db_batch(update_item_sql, uid);
-               SQLITE3_SAFE_FREE(update_item_sql);
-               SAFE_FREE(replaced_path);
-
-               if (ret != SQLITE_OK) {
-                       media_svc_error("failed to update item");
+               ret = _media_svc_get_thumbnail_path(storage_type, media_new_thumb_path, media_path, THUMB_EXT, uid);
+               if (ret != MS_MEDIA_ERR_NONE) {
+                       media_svc_error("_media_svc_get_thumbnail_path failed : %d", ret);
                        SQLITE3_FINALIZE(sql_stmt);
-
-                       return MS_MEDIA_ERR_DB_INTERNAL;
+                       return ret;
                }
 
-               /* Rename thumbnail file of file system */
-               if (!no_thumb) {
-                       ret = _media_svc_rename_file(media_thumb_path, media_new_thumb_path);
-                       if (ret != MS_MEDIA_ERR_NONE)
-                               media_svc_error("_media_svc_rename_file failed : %d", ret);
-               }
+               thumb_query = sqlite3_mprintf("UPDATE '%q' SET thumbnail_path='%q' WHERE path='%q'", storage_id, media_new_thumb_path, media_path);
+               g_array_append_val(query_array, thumb_query);
 
+               ret = _media_svc_rename_file(media_thumb_path, media_new_thumb_path);
+               if (ret != MS_MEDIA_ERR_NONE)
+                       media_svc_error("_media_svc_rename_file failed : %d", ret);
        }
 
        SQLITE3_FINALIZE(sql_stmt);
 
-       ret = _media_svc_sql_end_trans(uid);
-       if (ret != MS_MEDIA_ERR_NONE) {
-               media_svc_error("mb_svc_sqlite3_commit_trans failed.. Now start to rollback");
-               return ret;
+       while (query_array->len != 0) {
+               sql = g_array_index(query_array , char*, 0);
+               g_array_remove_index(query_array, 0);
+               ret = media_db_request_update_db(sql, uid);
+               SQLITE3_SAFE_FREE(sql);
+               if (ret != MS_MEDIA_ERR_NONE)
+                       media_svc_error("media_db_request_update_db failed : %d", ret);
        }
 
+       g_array_free(query_array, FALSE);
+       query_array = NULL;
+
        media_svc_debug("Folder update is successful. Sending noti for this");
        /* Get notification info */
        media_svc_noti_item *noti_item = NULL;