Apply tizen coding rule
[platform/core/multimedia/libmedia-service.git] / src / common / media-svc-media-folder.c
1 /*
2  * libmedia-service
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <glib/gstdio.h>
23 #include <media-util-err.h>
24 #include "media-svc-media-folder.h"
25 #include "media-svc-debug.h"
26 #include "media-svc-env.h"
27 #include "media-svc-util.h"
28 #include "media-svc-db-utils.h"
29
30 extern __thread GList *g_media_svc_move_item_query_list;
31 static __thread GList *g_media_svc_insert_folder_query_list;
32
33 static int __media_svc_is_root_path(const char *folder_path, bool *is_root, uid_t uid)
34 {
35         media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
36
37         *is_root = FALSE;
38
39         char *internal_path = _media_svc_get_path(uid);
40
41         if ((STRING_VALID(internal_path) && (strcmp(folder_path, internal_path) == 0)) ||
42                 strcmp(folder_path, MEDIA_ROOT_PATH_SDCARD) == 0 ||
43                 (STRING_VALID(MEDIA_ROOT_PATH_CLOUD) && strcmp(folder_path, MEDIA_ROOT_PATH_CLOUD) == 0)) {
44                 media_svc_debug("ROOT PATH [%s]", folder_path);
45                 *is_root = TRUE;
46         }
47
48         SAFE_FREE(internal_path);
49         return MS_MEDIA_ERR_NONE;
50 }
51
52 static int __media_svc_parent_is_ext_root_path(const char *folder_path, bool *is_root)
53 {
54         char *parent_folder_path = NULL;
55
56         media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
57
58         *is_root = FALSE;
59
60         parent_folder_path = g_path_get_dirname(folder_path);
61
62         if (!STRING_VALID(parent_folder_path)) {
63                 media_svc_error("error : g_path_get_dirname falied");
64                 SAFE_FREE(parent_folder_path);
65                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
66         }
67
68         if (STRING_VALID(MEDIA_ROOT_PATH_EXTERNAL) && (strcmp(parent_folder_path, MEDIA_ROOT_PATH_EXTERNAL) == 0)) {
69                 media_svc_debug("parent folder is ROOT PATH [%s]", parent_folder_path);
70                 *is_root = TRUE;
71         }
72
73         SAFE_FREE(parent_folder_path);
74
75         return MS_MEDIA_ERR_NONE;
76 }
77
78 int _media_svc_get_folder_id_by_foldername(sqlite3 *handle, const char *storage_id, const char *folder_name, char *folder_id, uid_t uid)
79 {
80         int ret = MS_MEDIA_ERR_NONE;
81         sqlite3_stmt *sql_stmt = NULL;
82         char *sql = NULL;
83         char parent_folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0, };
84         char *temp_parent_uuid = NULL;
85         char *parent_folder_path = NULL;
86
87         sql = sqlite3_mprintf("SELECT folder_uuid, parent_folder_uuid FROM '%s' WHERE storage_uuid = '%q' AND path = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, folder_name);
88
89         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
90
91         if (ret != MS_MEDIA_ERR_NONE) {
92                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
93                         media_svc_debug("there is no folder.");
94                 } else {
95                         media_svc_error("error when _media_svc_get_folder_id_by_foldername. err = [%d]", ret);
96                 }
97                 return ret;
98         }
99
100         memset(parent_folder_uuid, 0, sizeof(parent_folder_uuid));
101
102         _strncpy_safe(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE + 1);
103         if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 1)))       /*root path can be null*/
104                 _strncpy_safe(parent_folder_uuid, (const char *)sqlite3_column_text(sql_stmt, 1), MEDIA_SVC_UUID_SIZE+1);
105
106         SQLITE3_FINALIZE(sql_stmt);
107
108         /*root path can be null*/
109         if (!STRING_VALID(parent_folder_uuid)) {
110                 bool is_root = FALSE;
111
112                 ret = __media_svc_is_root_path(folder_name, &is_root, uid);
113                 if (is_root)
114                         return MS_MEDIA_ERR_NONE;
115
116                 ret = __media_svc_parent_is_ext_root_path(folder_name, &is_root);
117                 if (is_root)
118                         return MS_MEDIA_ERR_NONE;
119         }
120
121         /* Notice : Below code is the code only for inserting parent folder uuid when upgrade the media db version 3 to 4 */
122         /* Check parent folder uuid */
123         if (!STRING_VALID(parent_folder_uuid)) {
124                 /* update parent_uuid */
125                 media_svc_error("[No-Error] there is no parent folder uuid. PLEASE CHECK IT");
126
127                 parent_folder_path = g_path_get_dirname(folder_name);
128                 if (!STRING_VALID(parent_folder_path)) {
129                         media_svc_error("error : g_path_get_dirname falied.");
130                         return MS_MEDIA_ERR_OUT_OF_MEMORY;
131                 }
132
133                 if (STRING_VALID(storage_id))
134                         sql = sqlite3_mprintf("SELECT folder_uuid FROM '%s' WHERE storage_uuid = '%q' AND path = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, parent_folder_path);
135                 else
136                         sql = sqlite3_mprintf("SELECT folder_uuid FROM '%s' WHERE path = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, parent_folder_path);
137
138                 SAFE_FREE(parent_folder_path);
139
140                 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
141                 if (ret == MS_MEDIA_ERR_NONE) {
142                         temp_parent_uuid = (char *)sqlite3_column_text(sql_stmt, 0);
143                         if (temp_parent_uuid != NULL) {
144                                 _strncpy_safe(parent_folder_uuid, temp_parent_uuid, MEDIA_SVC_UUID_SIZE+1);
145                         }
146
147                         SQLITE3_FINALIZE(sql_stmt);
148                 }
149
150                 if (STRING_VALID(parent_folder_uuid)) {
151                         if (STRING_VALID(storage_id))
152                                 sql = sqlite3_mprintf("UPDATE %q SET parent_folder_uuid = '%q' WHERE storage_uuid = '%q' AND path = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, parent_folder_uuid, storage_id, folder_name);
153                         else
154                                 sql = sqlite3_mprintf("UPDATE %q SET parent_folder_uuid = '%q' WHERE path = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, parent_folder_uuid, folder_name);
155                         ret = _media_svc_sql_query(handle, sql, uid);
156                         sqlite3_free(sql);
157                 } else {
158                         media_svc_error("error when get parent folder uuid");
159                 }
160         }
161
162         return ret;
163 }
164
165 static int __media_svc_append_folder(sqlite3 *handle, const char *storage_id, media_svc_storage_type_e storage_type,
166                         const char *folder_id, const char *folder_path, const char *parent_folder_uuid, bool stack_query, uid_t uid)
167 {
168         int ret = MS_MEDIA_ERR_NONE;
169         char *folder_name = NULL;
170         int folder_modified_date = 0;
171
172         folder_name = g_path_get_basename(folder_path);
173         folder_modified_date = _media_svc_get_file_time(folder_path);
174
175         /*Update Pinyin If Support Pinyin*/
176         char *folder_name_pinyin = NULL;
177         if (_media_svc_check_pinyin_support())
178                 _media_svc_get_pinyin_str(folder_name, &folder_name_pinyin);
179
180         char *sql = sqlite3_mprintf("INSERT INTO %s (folder_uuid, path, name, storage_uuid, storage_type, modified_time, name_pinyin, parent_folder_uuid) \
181                                                         values (%Q, %Q, %Q, %Q, '%d', '%d', %Q, %Q); ",
182                                                         MEDIA_SVC_DB_TABLE_FOLDER, folder_id, folder_path, folder_name, storage_id, storage_type, folder_modified_date, folder_name_pinyin, parent_folder_uuid);
183
184         if (!stack_query)
185         {
186                 ret = _media_svc_sql_query(handle, sql, uid);
187                 sqlite3_free(sql);
188         }
189         else
190         {
191                 _media_svc_sql_query_add(&g_media_svc_insert_folder_query_list, &sql);
192         }
193
194         SAFE_FREE(folder_name);
195         SAFE_FREE(folder_name_pinyin);
196
197         return ret;
198 }
199
200 int _media_svc_update_folder_modified_time_by_folder_uuid(sqlite3 *handle, const char *folder_uuid, const char *folder_path, bool stack_query, uid_t uid)
201 {
202         int ret = MS_MEDIA_ERR_NONE;
203         int modified_time = 0;
204
205         modified_time = _media_svc_get_file_time(folder_path);
206
207         char *sql = sqlite3_mprintf("UPDATE %s SET modified_time=%d WHERE folder_uuid=%Q;", MEDIA_SVC_DB_TABLE_FOLDER, modified_time, folder_uuid);
208
209         if (!stack_query) {
210                 ret = _media_svc_sql_query(handle, sql, uid);
211                 sqlite3_free(sql);
212         } else {
213                 _media_svc_sql_query_add(&g_media_svc_move_item_query_list, &sql);
214         }
215
216         return ret;
217 }
218
219 static int __media_svc_get_and_append_parent_folder(sqlite3 *handle, const char *storage_id, const char *path, media_svc_storage_type_e storage_type, char *folder_id, uid_t uid)
220 {
221         int ret = MS_MEDIA_ERR_NONE;
222         unsigned int next_pos = 0;
223         char *next = NULL;
224         char *dir_path = NULL;
225         const char *token = "/";
226         char *folder_uuid = NULL;
227         char parent_folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
228         bool folder_search_end = FALSE;
229         char *internal_path = NULL;
230
231         memset(parent_folder_uuid, 0, sizeof(parent_folder_uuid));
232         internal_path = _media_svc_get_path(uid);
233
234         if (STRING_VALID(internal_path) && (strncmp(path, internal_path, strlen(internal_path)) == 0))
235                 next_pos = strlen(internal_path);
236         else if (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0)
237                 next_pos = strlen(MEDIA_ROOT_PATH_SDCARD);
238         else if (STRING_VALID(MEDIA_ROOT_PATH_CLOUD) && (strncmp(path, MEDIA_ROOT_PATH_CLOUD, strlen(MEDIA_ROOT_PATH_CLOUD)) == 0))
239                 next_pos = strlen(MEDIA_ROOT_PATH_CLOUD);
240         else if (strncmp(path, MEDIA_ROOT_PATH_EXTERNAL, strlen(MEDIA_ROOT_PATH_EXTERNAL)) == 0)
241                 next_pos = strlen(MEDIA_ROOT_PATH_EXTERNAL);
242         else {
243                 media_svc_error("Invalid Path");
244                 SAFE_FREE(internal_path);
245                 return MS_MEDIA_ERR_INTERNAL;
246         }
247
248         SAFE_FREE(internal_path);
249
250         while (!folder_search_end) {
251                 next = strstr(path + next_pos, token);
252                 if (next != NULL) {
253                         next_pos = (next - path);
254                         dir_path = strndup(path, next_pos);
255                         next_pos++;
256                 } else {
257                         dir_path = strndup(path, strlen(path));
258                         folder_search_end = TRUE;
259                         media_svc_error("[No-Error] End Path [%s]", dir_path);
260                 }
261
262                 if (STRING_VALID(MEDIA_ROOT_PATH_EXTERNAL) && (strcmp(dir_path, MEDIA_ROOT_PATH_EXTERNAL) == 0)) {
263                         /*To avoid insert MEDIA_ROOT_PATH_EXTERNAL path*/
264                         continue;
265                 }
266
267                 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, dir_path, parent_folder_uuid, uid);
268                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
269                         folder_uuid = _media_info_generate_uuid();
270                         if (folder_uuid == NULL) {
271                                 media_svc_error("Invalid UUID");
272                                 SAFE_FREE(dir_path);
273                                 return MS_MEDIA_ERR_INTERNAL;
274                         }
275
276                         ret = __media_svc_append_folder(handle, storage_id, storage_type, folder_uuid, dir_path, parent_folder_uuid, FALSE, uid);
277                         if (ret != MS_MEDIA_ERR_NONE) {
278                                 media_svc_error("__media_svc_append_folder is failed");
279                         }
280
281                         media_svc_error("[No-Error] New Appended folder path [%s], folder_uuid [%s], parent_folder_uuid [%s]", dir_path, folder_uuid, parent_folder_uuid);
282                         _strncpy_safe(parent_folder_uuid, folder_uuid, MEDIA_SVC_UUID_SIZE + 1);
283                 } else {
284                         media_svc_error("EXIST dir path : %s\n", dir_path);
285                 }
286
287                 SAFE_FREE(dir_path);
288         }
289
290         if (STRING_VALID(folder_uuid)) {
291                 _strncpy_safe(folder_id, folder_uuid, MEDIA_SVC_UUID_SIZE + 1);
292         } else {
293                 media_svc_error("Fail to get folder_uuid");
294                 return MS_MEDIA_ERR_INTERNAL;
295         }
296
297         return MS_MEDIA_ERR_NONE;
298 }
299
300 int _media_svc_get_and_append_folder(sqlite3 *handle, const char *storage_id, const char *path, media_svc_storage_type_e storage_type, char *folder_id, uid_t uid)
301 {
302         int ret = MS_MEDIA_ERR_NONE;
303
304         ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, path, folder_id, uid);
305
306         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
307                 ret = __media_svc_get_and_append_parent_folder(handle, storage_id, path, storage_type, folder_id, uid);
308         }
309
310         return ret;
311 }
312
313 int _media_svc_get_and_append_folder_id_by_path(sqlite3 *handle, const char *storage_id, const char *path, media_svc_storage_type_e storage_type, char *folder_id, uid_t uid)
314 {
315         char *dir_path = NULL;
316         int ret = MS_MEDIA_ERR_NONE;
317
318         dir_path = g_path_get_dirname(path);
319
320         ret = _media_svc_get_and_append_folder(handle, storage_id, dir_path, storage_type, folder_id, uid);
321
322         SAFE_FREE(dir_path);
323
324         return ret;
325 }
326
327 int _media_svc_update_folder_table(sqlite3 *handle, const char *storage_id, uid_t uid)
328 {
329         int ret = MS_MEDIA_ERR_NONE;
330         char *sql = NULL;
331
332         sql = sqlite3_mprintf("DELETE FROM '%s' WHERE folder_uuid IN (SELECT folder_uuid FROM '%s' WHERE folder_uuid NOT IN (SELECT folder_uuid FROM '%s'))",
333                 MEDIA_SVC_DB_TABLE_FOLDER, MEDIA_SVC_DB_TABLE_FOLDER, storage_id);
334
335         ret = _media_svc_sql_query(handle, sql, uid);
336         sqlite3_free(sql);
337
338         return ret;
339 }
340
341 static int __media_svc_count_all_folders(sqlite3 *handle, char *start_path, int *count)
342 {
343         int ret = MS_MEDIA_ERR_NONE;
344         sqlite3_stmt *sql_stmt = NULL;
345         char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE path LIKE '%q%%'", MEDIA_SVC_DB_TABLE_FOLDER, start_path);
346
347         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
348         if (ret != MS_MEDIA_ERR_NONE) {
349                 media_svc_error("error when _media_svc_sql_prepare_to_step. err = [%d]", ret);
350                 return ret;
351         }
352
353         *count = sqlite3_column_int(sql_stmt, 0);
354
355         SQLITE3_FINALIZE(sql_stmt);
356
357         return MS_MEDIA_ERR_NONE;
358 }
359
360 int _media_svc_get_all_folders(sqlite3 *handle, char *start_path, char ***folder_list, time_t **modified_time_list, int **item_num_list, int *count)
361 {
362         int ret = MS_MEDIA_ERR_NONE;
363         int idx = 0;
364         sqlite3_stmt *sql_stmt = NULL;
365         char *sql = NULL;
366         int cnt = 0;
367         char **folder_uuid = NULL;
368         int i = 0;
369
370         ret = __media_svc_count_all_folders(handle, start_path, &cnt);
371         if (ret != MS_MEDIA_ERR_NONE) {
372                 media_svc_error("error when __media_svc_count_all_folders. err = [%d]", ret);
373                 return ret;
374         }
375
376         if (cnt > 0) {
377                 sql = sqlite3_mprintf("SELECT path, modified_time, folder_uuid FROM '%s' WHERE path LIKE '%q%%'", MEDIA_SVC_DB_TABLE_FOLDER, start_path);
378         } else {
379                 *folder_list = NULL;
380                 *modified_time_list = NULL;
381                 *item_num_list = NULL;
382                 return MS_MEDIA_ERR_NONE;
383         }
384
385         *folder_list = malloc(sizeof(char *) * cnt);
386         *modified_time_list = malloc(sizeof(int) * cnt);
387         *item_num_list = malloc(sizeof(int) * cnt);
388         folder_uuid = malloc(sizeof(char *) * cnt);
389
390         if ((*folder_list == NULL) || (*modified_time_list == NULL) || (*item_num_list == NULL) || (folder_uuid == NULL)) {
391                 media_svc_error("Out of memory");
392                 goto ERROR;
393         }
394         memset(folder_uuid, 0x0, sizeof(char *) * cnt);
395
396         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
397         if (ret != MS_MEDIA_ERR_NONE) {
398                 media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
399                 goto ERROR;
400         }
401
402         media_svc_debug("QEURY OK");
403
404         while (1) {
405                 (*folder_list)[idx] = g_strdup((char *)sqlite3_column_text(sql_stmt, 0));
406                 (*modified_time_list)[idx] = (int)sqlite3_column_int(sql_stmt, 1);
407
408                 /* get the folder's id */
409                 folder_uuid[idx] = g_strdup((char *)sqlite3_column_text(sql_stmt, 2));
410
411                 idx++;
412
413                 if (sqlite3_step(sql_stmt) != SQLITE_ROW)
414                         break;
415         }
416         SQLITE3_FINALIZE(sql_stmt);
417
418         /*get the numbder of item in the folder by using folder's id */
419         for (i = 0; i < idx; i++) {
420                 if (STRING_VALID(folder_uuid[i])) {
421                         sql = sqlite3_mprintf("SELECT COUNT(*) FROM %s WHERE (folder_uuid='%q' AND validity = 1)", MEDIA_SVC_DB_TABLE_MEDIA, folder_uuid[i]);
422                         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
423                         if (ret != MS_MEDIA_ERR_NONE) {
424                                 media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
425                                 goto ERROR;
426                         }
427
428                         (*item_num_list)[i] = (int)sqlite3_column_int(sql_stmt, 0);
429
430                         SQLITE3_FINALIZE(sql_stmt);
431                 } else {
432                         media_svc_error("Invalid Folder Id");
433                 }
434         }
435
436         if (cnt == idx) {
437                 *count = cnt;
438                 media_svc_debug("Get Folder is OK");
439         } else {
440                 media_svc_error("Fail to get folder");
441                 ret = MS_MEDIA_ERR_INTERNAL;
442                 goto ERROR;
443         }
444
445         /* free all data */
446         for (i = 0; i < idx; i++) {
447                 SAFE_FREE(folder_uuid[i]);
448         }
449         SAFE_FREE(folder_uuid);
450
451         return ret;
452
453 ERROR:
454
455         /* free all data */
456         for (i = 0; i < idx; i++) {
457                 SAFE_FREE((*folder_list)[i]);
458                 SAFE_FREE(folder_uuid[i]);
459         }
460         SAFE_FREE(*folder_list);
461         SAFE_FREE(*modified_time_list);
462         SAFE_FREE(*item_num_list);
463         SAFE_FREE(folder_uuid);
464
465         *count = 0;
466
467         return ret;
468 }
469
470 int _media_svc_get_folder_info_by_foldername(sqlite3 *handle, const char *storage_id, const char *folder_name, char *folder_id, time_t *modified_time)
471 {
472         int ret = MS_MEDIA_ERR_NONE;
473         sqlite3_stmt *sql_stmt = NULL;
474
475         char *sql = sqlite3_mprintf("SELECT folder_uuid, modified_time FROM %s WHERE (storage_uuid = '%q' AND path = '%q');", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, folder_name);
476
477         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
478
479         if (ret != MS_MEDIA_ERR_NONE) {
480                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
481                         media_svc_debug("there is no folder.");
482                 } else {
483                         media_svc_error("error when _media_svc_get_folder_id_by_foldername. err = [%d]", ret);
484                 }
485                 return ret;
486         }
487
488         _strncpy_safe(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE + 1);
489         *modified_time = (int)sqlite3_column_int(sql_stmt, 1);
490
491         SQLITE3_FINALIZE(sql_stmt);
492
493         return ret;
494 }
495
496 int _media_svc_get_and_append_folder_id_by_folder_path(sqlite3 *handle, const char *storage_id, const char *path, media_svc_storage_type_e storage_type, char *folder_id, bool stack_query, uid_t uid)
497 {
498         char *path_name = NULL;
499         int ret = MS_MEDIA_ERR_NONE;
500         char *sql = NULL;
501
502         path_name = strdup(path);
503         if (path_name == NULL) {
504                 media_svc_error("out of memory");
505                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
506         }
507
508         ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, path_name, folder_id, uid);
509         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
510                 bool is_root = FALSE;
511                 bool is_parent_root = FALSE;
512
513                 ret = __media_svc_is_root_path(path_name, &is_root, uid);
514                 ret = __media_svc_parent_is_ext_root_path(path_name, &is_parent_root);
515
516                 char parent_folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0, };
517                 memset(parent_folder_uuid, 0x00, sizeof(parent_folder_uuid));
518
519                 if ((is_root == FALSE) && (is_parent_root == FALSE)) {
520                         /*get parent folder id*/
521                         char *parent_path_name = g_path_get_dirname(path_name);
522
523                         ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, parent_path_name, parent_folder_uuid, uid);
524                         if (ret != MS_MEDIA_ERR_NONE) {
525                                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
526                                 /*if No-Root directory scanning start before doing storage scanning, parent_folder_uuid can be NULL.
527                                 You can remove above logic if use below logic always. but I keep above code for the performance issue.
528                                 Most case (except No-Root directory scanning start before doing storage scanning) get parent_folder_uuid well*/
529                                 media_svc_error("[No-Error] There is no proper parent_folder_uuid. so try to make it");
530                                 ret = _media_svc_get_and_append_folder(handle, storage_id, path, storage_type, folder_id, uid);
531                                 } else {
532                                         media_svc_error("error when _media_svc_get_parent_folder_id_by_foldername. err = [%d]", ret);
533                                 }
534
535                                 SAFE_FREE(path_name);
536                                 SAFE_FREE(parent_path_name);
537
538                                 return ret;
539                         }
540                 }
541
542                 char *folder_uuid = _media_info_generate_uuid();
543                 if (folder_uuid == NULL) {
544                         media_svc_error("Invalid UUID");
545                         SAFE_FREE(path_name);
546                         return MS_MEDIA_ERR_INTERNAL;
547                 }
548
549                 ret = __media_svc_append_folder(handle, storage_id, storage_type, folder_uuid, path_name, parent_folder_uuid, stack_query, uid);
550
551                 _strncpy_safe(folder_id, folder_uuid, MEDIA_SVC_UUID_SIZE+1);
552
553         } else {
554                 sql = sqlite3_mprintf("UPDATE '%s' SET validity=1 WHERE storage_uuid = '%q' AND path = '%q'", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
555
556                 if (!stack_query)
557                 {
558                         ret = _media_svc_sql_query(handle, sql, uid);
559                         sqlite3_free(sql);
560                 }
561                 else
562                 {
563                         _media_svc_sql_query_add(&g_media_svc_insert_folder_query_list, &sql);
564                 }
565         }
566
567         SAFE_FREE(path_name);
568
569         return ret;
570 }
571
572 int _media_svc_delete_invalid_folder(sqlite3 *handle, const char *storage_id, uid_t uid)
573 {
574         int ret = MS_MEDIA_ERR_NONE;
575         char *sql = NULL;
576
577         sql = sqlite3_mprintf("DELETE FROM '%s' WHERE storage_uuid = '%q' AND validity = 0", MEDIA_SVC_DB_TABLE_FOLDER, storage_id);
578         ret = _media_svc_sql_query(handle, sql, uid);
579
580         sqlite3_free(sql);
581
582         return ret;
583 }
584
585 int _media_svc_set_folder_validity(sqlite3 *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
586 {
587         int ret = MS_MEDIA_ERR_NONE;
588         char *sql = NULL;
589         char start_path_id[MEDIA_SVC_UUID_SIZE+1] = {0,};
590
591         if (is_recursive) {
592                 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, start_path, start_path_id, uid);
593                 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_folder_id_by_foldername fail");
594                 media_svc_retvm_if(!STRING_VALID(start_path_id), MS_MEDIA_ERR_INVALID_PARAMETER, "start_path_id is NULL");
595
596                 sql = sqlite3_mprintf("UPDATE '%s' SET validity = %d WHERE storage_uuid = '%q' AND (parent_folder_uuid = '%q' OR folder_uuid ='%q')",
597                                                 MEDIA_SVC_DB_TABLE_FOLDER, validity, storage_id, start_path_id, start_path_id);
598         } else {
599                 sql = sqlite3_mprintf("UPDATE '%s' SET validity = %d WHERE storage_uuid = '%q' AND path = '%q'",
600                                                 MEDIA_SVC_DB_TABLE_FOLDER, validity, storage_id, start_path);
601         }
602
603         ret = _media_svc_sql_query(handle, sql, uid);
604
605         sqlite3_free(sql);
606
607         return ret;
608 }
609
610 int _media_svc_delete_folder_by_storage_id(sqlite3 *handle, const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
611 {
612         int ret = MS_MEDIA_ERR_NONE;
613         char *sql = NULL;
614
615         sql = sqlite3_mprintf("DELETE FROM '%s' WHERE storage_uuid = '%q' AND storage_type = %d", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, storage_type);
616         ret = _media_svc_sql_query(handle, sql, uid);
617
618         sqlite3_free(sql);
619
620         return ret;
621 }
622
623 GList ** _media_svc_get_folder_list_ptr(void)
624 {
625         return &g_media_svc_insert_folder_query_list;
626 }
627
628 int _media_svc_delete_invalid_folder_by_path(sqlite3 *handle, const char *storage_id, const char *folder_path, uid_t uid, int *delete_count)
629 {
630         int ret = MS_MEDIA_ERR_NONE;
631         char *sql = NULL;
632         int del_count = 0;
633         sqlite3_stmt *sql_stmt = NULL;
634
635         if (folder_path == NULL)
636                 return MS_MEDIA_ERR_INVALID_PARAMETER;
637
638         /*check the number of the deleted folder*/
639         sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE (storage_uuid = '%q' AND validity = 0 AND PATH LIKE '%q/%%')", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, folder_path);
640
641         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
642         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
643
644         del_count = sqlite3_column_int(sql_stmt, 0);
645
646         SQLITE3_FINALIZE(sql_stmt);
647         sql = NULL;
648
649         /*delete invalid folder*/
650         sql = sqlite3_mprintf("DELETE FROM '%s' WHERE (storage_uuid = '%q' AND validity = 0 AND PATH LIKE '%q%%')", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, folder_path);
651         ret = _media_svc_sql_query(handle, sql, uid);
652
653         sqlite3_free(sql);
654
655         *delete_count = del_count;
656
657         return ret;
658 }
659
660 int _media_svc_count_folder_with_path(sqlite3 *handle, const char *storage_id, const char *path, int *count)
661 {
662         int ret = MS_MEDIA_ERR_NONE;
663         sqlite3_stmt *sql_stmt = NULL;
664
665         char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE (storage_uuid='%q' AND path='%q')", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
666
667         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
668
669         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
670
671         *count = sqlite3_column_int(sql_stmt, 0);
672
673         SQLITE3_FINALIZE(sql_stmt);
674
675         return MS_MEDIA_ERR_NONE;
676 }
677
678 int _media_svc_count_subfolder_with_path(sqlite3 *handle, const char *storage_id, const char *path, int *count)
679 {
680         int ret = MS_MEDIA_ERR_NONE;
681         sqlite3_stmt *sql_stmt = NULL;
682
683         char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE (storage_uuid='%q' AND path LIKE'%q/%%')", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
684
685         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
686
687         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
688
689         *count = sqlite3_column_int(sql_stmt, 0);
690
691         SQLITE3_FINALIZE(sql_stmt);
692
693         return MS_MEDIA_ERR_NONE;
694 }
695
696 int _media_svc_get_folder_uuid(sqlite3 *handle, const char *storage_id, const char *path, char *folder_id)
697 {
698         int ret = MS_MEDIA_ERR_NONE;
699         sqlite3_stmt *sql_stmt = NULL;
700         char *sql = NULL;
701
702         sql = sqlite3_mprintf("SELECT folder_uuid FROM '%s' WHERE (storage_uuid='%q' AND path='%q')", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
703
704         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
705
706         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
707
708         _strncpy_safe(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE+1);
709
710         SQLITE3_FINALIZE(sql_stmt);
711
712         if (!STRING_VALID(folder_id))
713         {
714                 media_svc_error("Not found valid storage id [%s]", path);
715                 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
716         }
717
718         return ret;
719 }
720