Fix svace issue
[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 #define FOLDER_SCAN_DONE 4
31
32 extern __thread GList *g_media_svc_move_item_query_list;
33 static __thread GList *g_media_svc_insert_folder_query_list;
34
35 static int __media_svc_is_root_path(const char *folder_path, bool *is_root, uid_t uid)
36 {
37         media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
38
39         *is_root = FALSE;
40
41         char *internal_path = _media_svc_get_path(uid);
42
43         if ((STRING_VALID(internal_path) && (strcmp(folder_path, internal_path) == 0)) ||
44                 (STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && strcmp(folder_path, MEDIA_ROOT_PATH_SDCARD) == 0) ||
45                 (STRING_VALID(MEDIA_ROOT_PATH_CLOUD) && strcmp(folder_path, MEDIA_ROOT_PATH_CLOUD) == 0)) {
46                 media_svc_debug("ROOT PATH [%s]", folder_path);
47                 *is_root = TRUE;
48         }
49
50         SAFE_FREE(internal_path);
51         return MS_MEDIA_ERR_NONE;
52 }
53
54 static int __media_svc_parent_is_ext_root_path(const char *folder_path, bool *is_root)
55 {
56         char *parent_folder_path = NULL;
57
58         media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
59
60         *is_root = FALSE;
61
62         parent_folder_path = g_path_get_dirname(folder_path);
63
64         if (!STRING_VALID(parent_folder_path)) {
65                 media_svc_error("error : g_path_get_dirname falied");
66                 SAFE_FREE(parent_folder_path);
67                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
68         }
69
70         if (STRING_VALID(MEDIA_ROOT_PATH_EXTERNAL) && (strcmp(parent_folder_path, MEDIA_ROOT_PATH_EXTERNAL) == 0)) {
71                 media_svc_debug("parent folder is ROOT PATH [%s]", parent_folder_path);
72                 *is_root = TRUE;
73         }
74
75         SAFE_FREE(parent_folder_path);
76
77         return MS_MEDIA_ERR_NONE;
78 }
79
80 int _media_svc_get_folder_id_by_foldername(sqlite3 *handle, const char *storage_id, const char *folder_name, char *folder_id, uid_t uid)
81 {
82         int ret = MS_MEDIA_ERR_NONE;
83         sqlite3_stmt *sql_stmt = NULL;
84         char *sql = NULL;
85         char parent_folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0, };
86         char *temp_parent_uuid = NULL;
87         char *parent_folder_path = NULL;
88
89         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);
90
91         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
92
93         if (ret != MS_MEDIA_ERR_NONE) {
94                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
95                         media_svc_debug("there is no folder.");
96                 } else {
97                         media_svc_error("error when _media_svc_get_folder_id_by_foldername. err = [%d]", ret);
98                 }
99                 return ret;
100         }
101
102         memset(parent_folder_uuid, 0, sizeof(parent_folder_uuid));
103
104         _strncpy_safe(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE + 1);
105         if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 1)))       /*root path can be null*/
106                 _strncpy_safe(parent_folder_uuid, (const char *)sqlite3_column_text(sql_stmt, 1), MEDIA_SVC_UUID_SIZE+1);
107
108         SQLITE3_FINALIZE(sql_stmt);
109
110         /*root path can be null*/
111         if (!STRING_VALID(parent_folder_uuid)) {
112                 bool is_root = FALSE;
113
114                 ret = __media_svc_is_root_path(folder_name, &is_root, uid);
115                 if (is_root)
116                         return MS_MEDIA_ERR_NONE;
117
118                 ret = __media_svc_parent_is_ext_root_path(folder_name, &is_root);
119                 if (is_root)
120                         return MS_MEDIA_ERR_NONE;
121         }
122
123         /* Notice : Below code is the code only for inserting parent folder uuid when upgrade the media db version 3 to 4 */
124         /* Check parent folder uuid */
125         if (!STRING_VALID(parent_folder_uuid)) {
126                 /* update parent_uuid */
127                 media_svc_error("[No-Error] there is no parent folder uuid. PLEASE CHECK IT");
128
129                 parent_folder_path = g_path_get_dirname(folder_name);
130                 if (!STRING_VALID(parent_folder_path)) {
131                         media_svc_error("error : g_path_get_dirname falied.");
132                         return MS_MEDIA_ERR_OUT_OF_MEMORY;
133                 }
134
135                 if (STRING_VALID(storage_id))
136                         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);
137                 else
138                         sql = sqlite3_mprintf("SELECT folder_uuid FROM '%s' WHERE path = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, parent_folder_path);
139
140                 SAFE_FREE(parent_folder_path);
141
142                 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
143                 if (ret == MS_MEDIA_ERR_NONE) {
144                         temp_parent_uuid = (char *)sqlite3_column_text(sql_stmt, 0);
145                         if (temp_parent_uuid != NULL) {
146                                 _strncpy_safe(parent_folder_uuid, temp_parent_uuid, MEDIA_SVC_UUID_SIZE+1);
147                         }
148
149                         SQLITE3_FINALIZE(sql_stmt);
150                 }
151
152                 if (STRING_VALID(parent_folder_uuid)) {
153                         if (STRING_VALID(storage_id))
154                                 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);
155                         else
156                                 sql = sqlite3_mprintf("UPDATE %q SET parent_folder_uuid = '%q' WHERE path = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, parent_folder_uuid, folder_name);
157                         ret = _media_svc_sql_query(sql, uid);
158                         sqlite3_free(sql);
159                 } else {
160                         media_svc_error("error when get parent folder uuid");
161                 }
162         }
163
164         return ret;
165 }
166
167 static int __media_svc_append_folder(const char *storage_id, media_svc_storage_type_e storage_type,
168                         const char *folder_id, const char *folder_path, const char *parent_folder_uuid, bool stack_query, uid_t uid)
169 {
170         int ret = MS_MEDIA_ERR_NONE;
171         char *folder_name = NULL;
172         int folder_modified_date = 0;
173
174         folder_name = g_path_get_basename(folder_path);
175         folder_modified_date = _media_svc_get_file_time(folder_path);
176
177         /*Update Pinyin If Support Pinyin*/
178         char *folder_name_pinyin = NULL;
179         if (_media_svc_check_pinyin_support())
180                 _media_svc_get_pinyin_str(folder_name, &folder_name_pinyin);
181
182         char *sql = sqlite3_mprintf("INSERT INTO %s (folder_uuid, path, name, storage_uuid, storage_type, modified_time, name_pinyin, parent_folder_uuid) \
183                                                         values (%Q, %Q, %Q, %Q, '%d', '%d', %Q, %Q); ",
184                                                         MEDIA_SVC_DB_TABLE_FOLDER, folder_id, folder_path, folder_name, storage_id, storage_type, folder_modified_date, folder_name_pinyin, parent_folder_uuid);
185
186         if (!stack_query) {
187                 ret = _media_svc_sql_query(sql, uid);
188                 sqlite3_free(sql);
189         } else {
190                 _media_svc_sql_query_add(&g_media_svc_insert_folder_query_list, &sql);
191         }
192
193         SAFE_FREE(folder_name);
194         SAFE_FREE(folder_name_pinyin);
195
196         return ret;
197 }
198
199 int _media_svc_update_folder_modified_time_by_folder_uuid(const char *folder_uuid, const char *folder_path, bool stack_query, uid_t uid)
200 {
201         int ret = MS_MEDIA_ERR_NONE;
202         int modified_time = 0;
203
204         modified_time = _media_svc_get_file_time(folder_path);
205
206         char *sql = sqlite3_mprintf("UPDATE %s SET modified_time=%d WHERE folder_uuid=%Q;", MEDIA_SVC_DB_TABLE_FOLDER, modified_time, folder_uuid);
207
208         if (!stack_query) {
209                 ret = _media_svc_sql_query(sql, uid);
210                 sqlite3_free(sql);
211         } else {
212                 _media_svc_sql_query_add(&g_media_svc_move_item_query_list, &sql);
213         }
214
215         return ret;
216 }
217
218 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)
219 {
220         int ret = MS_MEDIA_ERR_NONE;
221         unsigned int next_pos = 0;
222         char *next = NULL;
223         char *dir_path = NULL;
224         const char *token = "/";
225         char *folder_uuid = NULL;
226         char parent_folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
227         bool folder_search_end = FALSE;
228         char *internal_path = NULL;
229
230         memset(parent_folder_uuid, 0, sizeof(parent_folder_uuid));
231         internal_path = _media_svc_get_path(uid);
232
233         if (STRING_VALID(internal_path) && (strncmp(path, internal_path, strlen(internal_path)) == 0))
234                 next_pos = strlen(internal_path);
235         else if (STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0)
236                 next_pos = strlen(MEDIA_ROOT_PATH_SDCARD);
237         else if (STRING_VALID(MEDIA_ROOT_PATH_CLOUD) && (strncmp(path, MEDIA_ROOT_PATH_CLOUD, strlen(MEDIA_ROOT_PATH_CLOUD)) == 0))
238                 next_pos = strlen(MEDIA_ROOT_PATH_CLOUD);
239         else if (STRING_VALID(MEDIA_ROOT_PATH_EXTERNAL) && strncmp(path, MEDIA_ROOT_PATH_EXTERNAL, strlen(MEDIA_ROOT_PATH_EXTERNAL)) == 0)
240                 next_pos = strlen(MEDIA_ROOT_PATH_EXTERNAL);
241         else {
242                 media_svc_error("Invalid Path");
243                 SAFE_FREE(internal_path);
244                 return MS_MEDIA_ERR_INTERNAL;
245         }
246
247         SAFE_FREE(internal_path);
248
249         while (!folder_search_end) {
250                 next = strstr(path + next_pos, token);
251                 if (next != NULL) {
252                         next_pos = (next - path);
253                         dir_path = strndup(path, next_pos);
254                         next_pos++;
255                 } else {
256                         dir_path = strndup(path, strlen(path));
257                         folder_search_end = TRUE;
258                         media_svc_error("[No-Error] End Path [%s]", dir_path);
259                 }
260
261                 if (STRING_VALID(MEDIA_ROOT_PATH_EXTERNAL) && (strcmp(dir_path, MEDIA_ROOT_PATH_EXTERNAL) == 0)) {
262                         /*To avoid insert MEDIA_ROOT_PATH_EXTERNAL path*/
263                         continue;
264                 }
265
266                 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, dir_path, parent_folder_uuid, uid);
267                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
268                         folder_uuid = _media_info_generate_uuid();
269                         if (folder_uuid == NULL) {
270                                 media_svc_error("Invalid UUID");
271                                 SAFE_FREE(dir_path);
272                                 return MS_MEDIA_ERR_INTERNAL;
273                         }
274
275                         ret = __media_svc_append_folder(storage_id, storage_type, folder_uuid, dir_path, parent_folder_uuid, FALSE, uid);
276                         if (ret != MS_MEDIA_ERR_NONE) {
277                                 media_svc_error("__media_svc_append_folder is failed");
278                         }
279
280                         media_svc_error("[No-Error] New Appended folder path [%s], folder_uuid [%s], parent_folder_uuid [%s]", dir_path, folder_uuid, parent_folder_uuid);
281                         _strncpy_safe(parent_folder_uuid, folder_uuid, MEDIA_SVC_UUID_SIZE + 1);
282                 } else {
283                         media_svc_error("EXIST dir path : %s\n", dir_path);
284                 }
285
286                 SAFE_FREE(dir_path);
287         }
288
289         if (STRING_VALID(folder_uuid)) {
290                 _strncpy_safe(folder_id, folder_uuid, MEDIA_SVC_UUID_SIZE + 1);
291         } else {
292                 media_svc_error("Fail to get folder_uuid");
293                 return MS_MEDIA_ERR_INTERNAL;
294         }
295
296         return MS_MEDIA_ERR_NONE;
297 }
298
299 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)
300 {
301         int ret = MS_MEDIA_ERR_NONE;
302
303         ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, path, folder_id, uid);
304
305         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
306                 ret = __media_svc_get_and_append_parent_folder(handle, storage_id, path, storage_type, folder_id, uid);
307         }
308
309         return ret;
310 }
311
312 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)
313 {
314         char *dir_path = NULL;
315         int ret = MS_MEDIA_ERR_NONE;
316
317         dir_path = g_path_get_dirname(path);
318
319         ret = _media_svc_get_and_append_folder(handle, storage_id, dir_path, storage_type, folder_id, uid);
320
321         SAFE_FREE(dir_path);
322
323         return ret;
324 }
325
326 int _media_svc_update_folder_table(const char *storage_id, uid_t uid)
327 {
328         int ret = MS_MEDIA_ERR_NONE;
329         char *sql = NULL;
330
331         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'));",
332                 MEDIA_SVC_DB_TABLE_FOLDER, MEDIA_SVC_DB_TABLE_FOLDER, storage_id);
333
334         ret = _media_svc_sql_query(sql, uid);
335         sqlite3_free(sql);
336
337         return ret;
338 }
339
340 static int __media_svc_count_all_folders(sqlite3 *handle, char *start_path, int *count)
341 {
342         int ret = MS_MEDIA_ERR_NONE;
343         sqlite3_stmt *sql_stmt = NULL;
344         char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE path LIKE '%q%%'", MEDIA_SVC_DB_TABLE_FOLDER, start_path);
345
346         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
347         if (ret != MS_MEDIA_ERR_NONE) {
348                 media_svc_error("error when _media_svc_sql_prepare_to_step. err = [%d]", ret);
349                 return ret;
350         }
351
352         *count = sqlite3_column_int(sql_stmt, 0);
353
354         SQLITE3_FINALIZE(sql_stmt);
355
356         return MS_MEDIA_ERR_NONE;
357 }
358
359 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)
360 {
361         int ret = MS_MEDIA_ERR_NONE;
362         int idx = 0;
363         sqlite3_stmt *sql_stmt = NULL;
364         char *sql = NULL;
365         int cnt = 0;
366         char **folder_uuid = NULL;
367         int i = 0;
368
369         ret = __media_svc_count_all_folders(handle, start_path, &cnt);
370         if (ret != MS_MEDIA_ERR_NONE) {
371                 media_svc_error("error when __media_svc_count_all_folders. err = [%d]", ret);
372                 return ret;
373         }
374
375         if (cnt > 0) {
376                 sql = sqlite3_mprintf("SELECT path, modified_time, folder_uuid FROM '%s' WHERE path LIKE '%q%%'", MEDIA_SVC_DB_TABLE_FOLDER, start_path);
377         } else {
378                 *folder_list = NULL;
379                 *modified_time_list = NULL;
380                 *item_num_list = NULL;
381                 return MS_MEDIA_ERR_NONE;
382         }
383
384         *folder_list = malloc(sizeof(char *) * cnt);
385         *modified_time_list = malloc(sizeof(int) * cnt);
386         *item_num_list = malloc(sizeof(int) * cnt);
387         folder_uuid = malloc(sizeof(char *) * cnt);
388
389         if ((*folder_list == NULL) || (*modified_time_list == NULL) || (*item_num_list == NULL) || (folder_uuid == NULL)) {
390                 media_svc_error("Out of memory");
391                 goto ERROR;
392         }
393         memset(folder_uuid, 0x0, sizeof(char *) * cnt);
394         memset(*folder_list, 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(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                         ret = _media_svc_sql_query(sql, uid);
558                         sqlite3_free(sql);
559                 } else {
560                         _media_svc_sql_query_add(&g_media_svc_insert_folder_query_list, &sql);
561                 }
562         }
563
564         SAFE_FREE(path_name);
565
566         return ret;
567 }
568
569 int _media_svc_delete_invalid_folder(const char *storage_id, uid_t uid)
570 {
571         int ret = MS_MEDIA_ERR_NONE;
572         char *sql = NULL;
573
574         sql = sqlite3_mprintf("DELETE FROM '%s' WHERE storage_uuid = '%q' AND validity = 0;", MEDIA_SVC_DB_TABLE_FOLDER, storage_id);
575         ret = _media_svc_sql_query(sql, uid);
576
577         sqlite3_free(sql);
578
579         return ret;
580 }
581
582 int _media_svc_set_folder_validity(sqlite3 *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
583 {
584         int ret = MS_MEDIA_ERR_NONE;
585         char *sql = NULL;
586         char start_path_id[MEDIA_SVC_UUID_SIZE+1] = {0,};
587
588         if (is_recursive) {
589                 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, start_path, start_path_id, uid);
590                 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_folder_id_by_foldername fail");
591                 media_svc_retvm_if(!STRING_VALID(start_path_id), MS_MEDIA_ERR_INVALID_PARAMETER, "start_path_id is NULL");
592
593                 sql = sqlite3_mprintf("UPDATE '%s' SET validity = %d WHERE storage_uuid = '%q' AND (path LIKE '%q/%%' OR folder_uuid ='%q')",
594                                                 MEDIA_SVC_DB_TABLE_FOLDER, validity, storage_id, start_path, start_path_id);
595         } else {
596                 sql = sqlite3_mprintf("UPDATE '%s' SET validity = %d WHERE storage_uuid = '%q' AND path = '%q';",
597                                                 MEDIA_SVC_DB_TABLE_FOLDER, validity, storage_id, start_path);
598         }
599
600         ret = _media_svc_sql_query(sql, uid);
601
602         sqlite3_free(sql);
603
604         return ret;
605 }
606
607 int _media_svc_delete_folder_by_storage_id(const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
608 {
609         int ret = MS_MEDIA_ERR_NONE;
610         char *sql = NULL;
611
612         sql = sqlite3_mprintf("DELETE FROM '%s' WHERE storage_uuid = '%q' AND storage_type = %d;", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, storage_type);
613         ret = _media_svc_sql_query(sql, uid);
614
615         sqlite3_free(sql);
616
617         return ret;
618 }
619
620 GList **_media_svc_get_folder_list_ptr(void)
621 {
622         return &g_media_svc_insert_folder_query_list;
623 }
624
625 int _media_svc_get_folder_scan_status(sqlite3 *handle, const char *storage_id, const char *path, int *scan_status)
626 {
627         int ret = MS_MEDIA_ERR_NONE;
628         sqlite3_stmt *sql_stmt = NULL;
629         char *sql = NULL;
630
631         if (!STRING_VALID(path)) {
632                 media_svc_error("Not found valid path");
633                 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
634         }
635
636         sql = sqlite3_mprintf("SELECT scan_status FROM '%s' WHERE path = '%q' AND storage_uuid = '%q'", MEDIA_SVC_DB_TABLE_FOLDER, path, storage_id);
637
638         ret = _media_svc_sql_prepare_to_step_simple(handle, sql, &sql_stmt);
639
640         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
641
642         while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
643                 *scan_status = sqlite3_column_int(sql_stmt, 0);
644         }
645
646         SQLITE3_FINALIZE(sql_stmt);
647
648         return ret;
649 }
650
651 int _media_svc_set_folder_scan_status(const char *storage_id, const char *path, int scan_status, uid_t uid)
652 {
653         int ret = MS_MEDIA_ERR_NONE;
654         char *sql = NULL;
655
656         if (path) {
657                 sql = sqlite3_mprintf("UPDATE '%s' SET scan_status=%d WHERE path = '%q' AND storage_uuid = '%q'", MEDIA_SVC_DB_TABLE_FOLDER, scan_status, path, storage_id);
658         } else {
659                 sql = sqlite3_mprintf("UPDATE '%s' SET scan_status=%d WHERE storage_uuid = '%q'", MEDIA_SVC_DB_TABLE_FOLDER, scan_status, storage_id);
660         }
661
662         ret = _media_svc_sql_query(sql, uid);
663         sqlite3_free(sql);
664
665         return ret;
666 }
667
668 int _media_svc_get_folder_modified_time_by_path(sqlite3 *handle, const char *path, const char *storage_id, time_t *modified_time)
669 {
670         int ret = MS_MEDIA_ERR_NONE;
671         sqlite3_stmt *sql_stmt = NULL;
672
673         char *sql = sqlite3_mprintf("SELECT modified_time FROM '%s' WHERE path = '%q' AND storage_uuid = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, path, storage_id);
674
675         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
676
677         if (ret != MS_MEDIA_ERR_NONE) {
678                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
679                         media_svc_debug("there is no folder.");
680                 } else {
681                         media_svc_error("error when _media_svc_get_folder_modified_time_by_path. err = [%d]", ret);
682                 }
683                 return ret;
684         }
685
686         *modified_time = (int)sqlite3_column_int(sql_stmt, 0);
687
688         SQLITE3_FINALIZE(sql_stmt);
689
690         return ret;
691 }
692
693 int _media_svc_get_null_scan_folder_list(sqlite3 *handle, const char *storage_id, const char *path, char ***folder_list, int *count)
694 {
695         int ret = MS_MEDIA_ERR_NONE;
696         int idx = 0;
697         int cnt = 0;
698         char *sql = NULL;
699         char folder_id[MEDIA_SVC_UUID_SIZE+1] = {0,};
700         sqlite3_stmt *sql_stmt = NULL;
701
702         if (path == NULL) {
703                 sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE storage_uuid = '%q' AND scan_status!=%d", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, FOLDER_SCAN_DONE);
704                 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
705
706                 if (ret != MS_MEDIA_ERR_NONE) {
707                         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
708                                 media_svc_debug("there is no folder.");
709                         } else {
710                                 media_svc_error("error when get folder_id by path. err = [%d]", ret);
711                         }
712
713                         *folder_list = NULL;
714                         *count = 0;
715
716                         return ret;
717                 }
718
719                 cnt = sqlite3_column_int(sql_stmt, 0);
720                 SQLITE3_FINALIZE(sql_stmt);
721                 if (cnt > 0) {
722                         sql = sqlite3_mprintf("SELECT path FROM '%s' WHERE storage_uuid = '%q' AND scan_status!=%d", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, FOLDER_SCAN_DONE);
723                 } else {
724                         *folder_list = NULL;
725                         *count = 0;
726
727                         return MS_MEDIA_ERR_INTERNAL;
728                 }
729
730                 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
731
732                 if (ret != MS_MEDIA_ERR_NONE) {
733                         media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
734
735                         *folder_list = NULL;
736                         *count = 0;
737
738                         return ret;
739                 }
740         } else {
741                 sql = sqlite3_mprintf("SELECT folder_uuid FROM '%s' WHERE storage_uuid = '%q' AND path = '%q'", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
742                 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
743
744                 if (ret != MS_MEDIA_ERR_NONE) {
745                         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
746                                 media_svc_debug("there is no folder.");
747                         } else {
748                                 media_svc_error("error when get folder_id by path [%s]. err = [%d]", path, ret);
749                         }
750                         return ret;
751                 }
752
753                 _strncpy_safe(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE+1);
754
755                 SQLITE3_FINALIZE(sql_stmt);
756
757                 sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE path LIKE '%q%%' AND parent_folder_uuid = '%q'", MEDIA_SVC_DB_TABLE_FOLDER, path, folder_id);
758
759                 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
760
761                 if (ret != MS_MEDIA_ERR_NONE) {
762                         media_svc_error("error when _media_svc_sql_prepare_to_step. err = [%d]", ret);
763                         return ret;
764                 }
765
766                 cnt = sqlite3_column_int(sql_stmt, 0);
767                 SQLITE3_FINALIZE(sql_stmt);
768                 if (cnt > 0) {
769                         sql = sqlite3_mprintf("SELECT path FROM '%s' WHERE path LIKE '%q%%' AND parent_folder_uuid = '%q'", MEDIA_SVC_DB_TABLE_FOLDER, path, folder_id);
770                 } else {
771                         *folder_list = NULL;
772                         *count = 0;
773
774                         return MS_MEDIA_ERR_NONE;
775                 }
776
777                 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
778
779                 if (ret != MS_MEDIA_ERR_NONE) {
780                         media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
781
782                         *folder_list = NULL;
783                         *count = 0;
784
785                         return ret;
786                 }
787         }
788
789         *folder_list = malloc(sizeof(char *) * cnt);
790
791         while (1) {
792                 (*folder_list)[idx] = strdup((char *)sqlite3_column_text(sql_stmt, 0));
793
794                 if (sqlite3_step(sql_stmt) != SQLITE_ROW)
795                         break;
796                 idx++;
797         }
798
799         if (cnt == idx + 1) {
800                 *count = cnt;
801                 media_svc_debug("OK");
802         } else {
803                 /* free all data */
804                 int i = 0;
805                 for (i  = 0; i < idx; i++) {
806                         SAFE_FREE((*folder_list)[i]);
807                 }
808                 SAFE_FREE(*folder_list);
809                 *count = 0;
810                 ret = MS_MEDIA_ERR_INTERNAL;
811         }
812
813         SQLITE3_FINALIZE(sql_stmt);
814
815         return ret;
816 }
817
818 int _media_svc_delete_invalid_folder_by_path(sqlite3 *handle, const char *storage_id, const char *folder_path, uid_t uid, int *delete_count)
819 {
820         int ret = MS_MEDIA_ERR_NONE;
821         char *sql = NULL;
822         int del_count = 0;
823         sqlite3_stmt *sql_stmt = NULL;
824
825         if (folder_path == NULL)
826                 return MS_MEDIA_ERR_INVALID_PARAMETER;
827
828         /*check the number of the deleted folder*/
829         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);
830
831         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
832         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
833
834         del_count = sqlite3_column_int(sql_stmt, 0);
835
836         SQLITE3_FINALIZE(sql_stmt);
837         sql = NULL;
838
839         /*delete invalid folder*/
840         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);
841         ret = _media_svc_sql_query(sql, uid);
842
843         sqlite3_free(sql);
844
845         *delete_count = del_count;
846
847         return ret;
848 }
849
850 int _media_svc_count_folder_with_path(sqlite3 *handle, const char *storage_id, const char *path, int *count)
851 {
852         int ret = MS_MEDIA_ERR_NONE;
853         sqlite3_stmt *sql_stmt = NULL;
854
855         char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE (storage_uuid='%q' AND path='%q')", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
856
857         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
858
859         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
860
861         *count = sqlite3_column_int(sql_stmt, 0);
862
863         SQLITE3_FINALIZE(sql_stmt);
864
865         return MS_MEDIA_ERR_NONE;
866 }
867
868 int _media_svc_count_subfolder_with_path(sqlite3 *handle, const char *storage_id, const char *path, int *count)
869 {
870         int ret = MS_MEDIA_ERR_NONE;
871         sqlite3_stmt *sql_stmt = NULL;
872
873         char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE (storage_uuid='%q' AND path LIKE'%q/%%')", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
874
875         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
876
877         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
878
879         *count = sqlite3_column_int(sql_stmt, 0);
880
881         SQLITE3_FINALIZE(sql_stmt);
882
883         return MS_MEDIA_ERR_NONE;
884 }
885
886 int _media_svc_get_folder_uuid(sqlite3 *handle, const char *storage_id, const char *path, char *folder_id)
887 {
888         int ret = MS_MEDIA_ERR_NONE;
889         sqlite3_stmt *sql_stmt = NULL;
890         char *sql = NULL;
891
892         sql = sqlite3_mprintf("SELECT folder_uuid FROM '%s' WHERE (storage_uuid='%q' AND path='%q')", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
893
894         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
895
896         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
897
898         _strncpy_safe(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE+1);
899
900         SQLITE3_FINALIZE(sql_stmt);
901
902         if (!STRING_VALID(folder_id)) {
903                 media_svc_error("Not found valid storage id [%s]", path);
904                 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
905         }
906
907         return ret;
908 }
909