Remove parent_folder_uuid related code
[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 int _media_svc_get_folder_id_by_foldername(sqlite3 *handle, const char *storage_id, const char *folder_name, char *folder_id, uid_t uid)
36 {
37         int ret = MS_MEDIA_ERR_NONE;
38         sqlite3_stmt *sql_stmt = NULL;
39         char *sql = NULL;
40
41         sql = sqlite3_mprintf("SELECT folder_uuid FROM '%s' WHERE storage_uuid = '%q' AND path = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, folder_name);
42
43         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
44         if (ret != MS_MEDIA_ERR_NONE) {
45                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
46                         media_svc_debug("there is no folder.");
47                 else
48                         media_svc_error("error when _media_svc_get_folder_id_by_foldername. err = [%d]", ret);
49
50                 return ret;
51         }
52
53         _strncpy_safe(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE + 1);
54
55         SQLITE3_FINALIZE(sql_stmt);
56
57         return ret;
58 }
59
60 static int __media_svc_append_folder(const char *storage_id, media_svc_storage_type_e storage_type,
61                         const char *folder_id, const char *folder_path, bool stack_query, uid_t uid)
62 {
63         int ret = MS_MEDIA_ERR_NONE;
64         char *folder_name = NULL;
65         int folder_modified_date = 0;
66
67         folder_name = g_path_get_basename(folder_path);
68         folder_modified_date = _media_svc_get_file_time(folder_path);
69
70         /*Update Pinyin If Support Pinyin*/
71         char *folder_name_pinyin = NULL;
72         if (_media_svc_check_pinyin_support())
73                 _media_svc_get_pinyin_str(folder_name, &folder_name_pinyin);
74         /* Sometime SQLITE3 returns NO_RECORD, so need to consider conflict case.. */
75         media_svc_debug("UNIQUE:path[%s], storage_uuid[%s]", folder_path, storage_id);
76         char *sql = sqlite3_mprintf("INSERT OR IGNORE INTO %s (folder_uuid, path, name, storage_uuid, storage_type, modified_time, name_pinyin) values (%Q, %Q, %Q, %Q, '%d', '%d', %Q); ",
77                                 MEDIA_SVC_DB_TABLE_FOLDER, folder_id, folder_path, folder_name, storage_id, storage_type, folder_modified_date, folder_name_pinyin);
78
79         if (!stack_query) {
80                 ret = _media_svc_sql_query(sql, uid);
81                 SQLITE3_SAFE_FREE(sql);
82         } else {
83                 _media_svc_sql_query_add(&g_media_svc_insert_folder_query_list, &sql);
84         }
85
86         SAFE_FREE(folder_name);
87         SAFE_FREE(folder_name_pinyin);
88
89         return ret;
90 }
91
92 int _media_svc_update_folder_modified_time_by_folder_uuid(const char *folder_uuid, const char *folder_path, bool stack_query, uid_t uid)
93 {
94         int ret = MS_MEDIA_ERR_NONE;
95         int modified_time = 0;
96
97         modified_time = _media_svc_get_file_time(folder_path);
98
99         char *sql = sqlite3_mprintf("UPDATE %s SET modified_time=%d WHERE folder_uuid=%Q;", MEDIA_SVC_DB_TABLE_FOLDER, modified_time, folder_uuid);
100
101         if (!stack_query) {
102                 ret = _media_svc_sql_query(sql, uid);
103                 SQLITE3_SAFE_FREE(sql);
104         } else {
105                 _media_svc_sql_query_add(&g_media_svc_move_item_query_list, &sql);
106         }
107
108         return ret;
109 }
110
111 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)
112 {
113         int ret = MS_MEDIA_ERR_NONE;
114         unsigned int next_pos = 0;
115         char *next = NULL;
116         char *dir_path = NULL;
117         const char *token = "/";
118         char *folder_uuid = NULL;
119         char tmp_folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
120         bool folder_search_end = FALSE;
121         char *internal_path = NULL;
122
123         memset(tmp_folder_uuid, 0, sizeof(tmp_folder_uuid));
124         ret = ms_user_get_internal_root_path(uid, &internal_path);
125         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Fail to get root path");
126
127         if (STRING_VALID(internal_path) && (strncmp(path, internal_path, strlen(internal_path)) == 0))
128                 next_pos = strlen(internal_path);
129         else if (STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0)
130                 next_pos = strlen(MEDIA_ROOT_PATH_SDCARD);
131         else if (STRING_VALID(MEDIA_ROOT_PATH_EXTERNAL) && strncmp(path, MEDIA_ROOT_PATH_EXTERNAL, strlen(MEDIA_ROOT_PATH_EXTERNAL)) == 0)
132                 next_pos = strlen(MEDIA_ROOT_PATH_EXTERNAL);
133         else if (STRING_VALID(MEDIA_ROOT_PATH_DISC) && strncmp(path, MEDIA_ROOT_PATH_DISC, strlen(MEDIA_ROOT_PATH_DISC)) == 0)
134                 next_pos = strlen(MEDIA_ROOT_PATH_DISC);
135         else {
136                 media_svc_error("Invalid Path");
137                 media_svc_sec_error("Invalid Path [%s], internal_path [%s]", path, internal_path);
138                 SAFE_FREE(internal_path);
139                 return MS_MEDIA_ERR_INTERNAL;
140         }
141
142         SAFE_FREE(internal_path);
143
144         while (!folder_search_end) {
145                 next = strstr(path + next_pos, token);
146                 if (next != NULL) {
147                         next_pos = (next - path);
148                         dir_path = strndup(path, next_pos);
149                         next_pos++;
150                 } else {
151                         dir_path = strndup(path, strlen(path));
152                         folder_search_end = TRUE;
153                         media_svc_error("[No-Error] End Path [%s]", dir_path);
154                 }
155
156                 if (STRING_VALID(MEDIA_ROOT_PATH_EXTERNAL) && (g_strcmp0(dir_path, MEDIA_ROOT_PATH_EXTERNAL) == 0)) {
157                         /*To avoid insert MEDIA_ROOT_PATH_EXTERNAL path*/
158                         SAFE_FREE(dir_path);
159                         continue;
160                 }
161
162                 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, dir_path, tmp_folder_uuid, uid);
163                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
164                         folder_uuid = _media_info_generate_uuid();
165                         if (folder_uuid == NULL) {
166                                 media_svc_error("Invalid UUID");
167                                 SAFE_FREE(dir_path);
168                                 return MS_MEDIA_ERR_INTERNAL;
169                         }
170
171                         ret = __media_svc_append_folder(storage_id, storage_type, folder_uuid, dir_path, FALSE, uid);
172                         if (ret != MS_MEDIA_ERR_NONE)
173                                 media_svc_error("__media_svc_append_folder is failed");
174
175                         media_svc_error("[No-Error] New Appended folder path [%s], folder_uuid [%s]", dir_path, folder_uuid);
176                 } else {
177                         media_svc_error("EXIST dir path : %s\n", dir_path);
178                 }
179
180                 SAFE_FREE(dir_path);
181         }
182
183         if (STRING_VALID(folder_uuid)) {
184                 _strncpy_safe(folder_id, folder_uuid, MEDIA_SVC_UUID_SIZE + 1);
185         } else {
186                 media_svc_error("Fail to get folder_uuid");
187                 return MS_MEDIA_ERR_INTERNAL;
188         }
189
190         return MS_MEDIA_ERR_NONE;
191 }
192
193 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)
194 {
195         int ret = MS_MEDIA_ERR_NONE;
196
197         ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, path, folder_id, uid);
198
199         if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
200                 ret = __media_svc_get_and_append_parent_folder(handle, storage_id, path, storage_type, folder_id, uid);
201
202         return ret;
203 }
204
205 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)
206 {
207         char *dir_path = NULL;
208         int ret = MS_MEDIA_ERR_NONE;
209
210         dir_path = g_path_get_dirname(path);
211
212         ret = _media_svc_get_and_append_folder(handle, storage_id, dir_path, storage_type, folder_id, uid);
213
214         SAFE_FREE(dir_path);
215
216         return ret;
217 }
218
219 int _media_svc_update_folder_table(const char *storage_id, uid_t uid)
220 {
221         int ret = MS_MEDIA_ERR_NONE;
222         char *sql = NULL;
223
224         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'));",
225                 MEDIA_SVC_DB_TABLE_FOLDER, MEDIA_SVC_DB_TABLE_FOLDER, storage_id);
226
227         ret = _media_svc_sql_query(sql, uid);
228         SQLITE3_SAFE_FREE(sql);
229
230         return ret;
231 }
232
233 static int __media_svc_count_all_folders(sqlite3 *handle, char *start_path, int *count)
234 {
235         int ret = MS_MEDIA_ERR_NONE;
236         sqlite3_stmt *sql_stmt = NULL;
237         char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE path LIKE '%q%%'", MEDIA_SVC_DB_TABLE_FOLDER, start_path);
238
239         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
240         if (ret != MS_MEDIA_ERR_NONE) {
241                 media_svc_error("error when _media_svc_sql_prepare_to_step. err = [%d]", ret);
242                 return ret;
243         }
244
245         *count = sqlite3_column_int(sql_stmt, 0);
246
247         SQLITE3_FINALIZE(sql_stmt);
248
249         return MS_MEDIA_ERR_NONE;
250 }
251
252 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)
253 {
254         int ret = MS_MEDIA_ERR_NONE;
255         int idx = 0;
256         sqlite3_stmt *sql_stmt = NULL;
257         char *sql = NULL;
258         int cnt = 0;
259         char **folder_uuid = NULL;
260         int i = 0;
261
262         ret = __media_svc_count_all_folders(handle, start_path, &cnt);
263         if (ret != MS_MEDIA_ERR_NONE) {
264                 media_svc_error("error when __media_svc_count_all_folders. err = [%d]", ret);
265                 return ret;
266         }
267
268         if (cnt > 0) {
269                 sql = sqlite3_mprintf("SELECT path, modified_time, folder_uuid FROM '%s' WHERE path LIKE '%q%%'", MEDIA_SVC_DB_TABLE_FOLDER, start_path);
270         } else {
271                 *folder_list = NULL;
272                 *modified_time_list = NULL;
273                 *item_num_list = NULL;
274                 return MS_MEDIA_ERR_NONE;
275         }
276
277         *folder_list = malloc(sizeof(char *) * cnt);
278         *modified_time_list = malloc(sizeof(int) * cnt);
279         *item_num_list = malloc(sizeof(int) * cnt);
280         folder_uuid = malloc(sizeof(char *) * cnt);
281
282         if ((*folder_list == NULL) || (*modified_time_list == NULL) || (*item_num_list == NULL) || (folder_uuid == NULL)) {
283                 media_svc_error("Out of memory");
284                 goto ERROR;
285         }
286         memset(folder_uuid, 0x0, sizeof(char *) * cnt);
287         memset(*folder_list, 0x0, sizeof(char *) * cnt);
288
289         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
290         if (ret != MS_MEDIA_ERR_NONE) {
291                 media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
292                 goto ERROR;
293         }
294
295         media_svc_debug("QEURY OK");
296
297         while (idx < cnt) {
298                 (*folder_list)[idx] = g_strdup((char *)sqlite3_column_text(sql_stmt, 0));
299                 (*modified_time_list)[idx] = (int)sqlite3_column_int(sql_stmt, 1);
300
301                 /* get the folder's id */
302                 folder_uuid[idx] = g_strdup((char *)sqlite3_column_text(sql_stmt, 2));
303
304                 idx++;
305
306                 if (sqlite3_step(sql_stmt) != SQLITE_ROW)
307                         break;
308         }
309         SQLITE3_FINALIZE(sql_stmt);
310
311         /*get the numbder of item in the folder by using folder's id */
312         for (i = 0; i < idx; i++) {
313                 if (STRING_VALID(folder_uuid[i])) {
314                         sql = sqlite3_mprintf("SELECT COUNT(*) FROM %s WHERE (folder_uuid='%q' AND validity = 1)", MEDIA_SVC_DB_TABLE_MEDIA, folder_uuid[i]);
315                         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
316                         if (ret != MS_MEDIA_ERR_NONE) {
317                                 media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
318                                 goto ERROR;
319                         }
320
321                         (*item_num_list)[i] = (int)sqlite3_column_int(sql_stmt, 0);
322
323                         SQLITE3_FINALIZE(sql_stmt);
324                 } else {
325                         media_svc_error("Invalid Folder Id");
326                 }
327         }
328
329         if (cnt == idx) {
330                 *count = cnt;
331                 media_svc_debug("Get Folder is OK");
332         } else {
333                 media_svc_error("Fail to get folder");
334                 ret = MS_MEDIA_ERR_INTERNAL;
335                 goto ERROR;
336         }
337
338         /* free all data */
339         for (i = 0; i < idx; i++)
340                 SAFE_FREE(folder_uuid[i]);
341
342         SAFE_FREE(folder_uuid);
343
344         return ret;
345
346 ERROR:
347
348         /* free all data */
349         for (i = 0; i < idx; i++) {
350                 SAFE_FREE((*folder_list)[i]);
351                 SAFE_FREE(folder_uuid[i]);
352         }
353         SAFE_FREE(*folder_list);
354         SAFE_FREE(*modified_time_list);
355         SAFE_FREE(*item_num_list);
356         SAFE_FREE(folder_uuid);
357
358         *count = 0;
359
360         return ret;
361 }
362
363 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)
364 {
365         int ret = MS_MEDIA_ERR_NONE;
366         sqlite3_stmt *sql_stmt = NULL;
367
368         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);
369
370         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
371
372         if (ret != MS_MEDIA_ERR_NONE) {
373                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
374                         media_svc_debug("there is no folder.");
375                 else
376                         media_svc_error("error when _media_svc_get_folder_id_by_foldername. err = [%d]", ret);
377
378                 return ret;
379         }
380
381         _strncpy_safe(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE + 1);
382         *modified_time = (int)sqlite3_column_int(sql_stmt, 1);
383
384         SQLITE3_FINALIZE(sql_stmt);
385
386         return ret;
387 }
388
389 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)
390 {
391         char *path_name = NULL;
392         int ret = MS_MEDIA_ERR_NONE;
393         char *sql = NULL;
394
395         path_name = strdup(path);
396         if (path_name == NULL) {
397                 media_svc_error("out of memory");
398                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
399         }
400
401         ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, path_name, folder_id, uid);
402         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
403                 ret = __media_svc_get_and_append_parent_folder(handle, storage_id, path_name, storage_type, folder_id, uid);
404
405         } else {
406                 sql = sqlite3_mprintf("UPDATE '%s' SET validity=1 WHERE storage_uuid = '%q' AND path = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
407                 if (!stack_query) {
408                         ret = _media_svc_sql_query(sql, uid);
409                         SQLITE3_SAFE_FREE(sql);
410                 } else {
411                         _media_svc_sql_query_add(&g_media_svc_insert_folder_query_list, &sql);
412                 }
413         }
414
415         SAFE_FREE(path_name);
416
417         return ret;
418 }
419
420 int _media_svc_delete_invalid_folder(const char *storage_id, int storage_type, uid_t uid)
421 {
422         int ret = MS_MEDIA_ERR_NONE;
423         char *sql = NULL;
424
425         sql = sqlite3_mprintf("DELETE FROM '%s' WHERE storage_uuid = '%q' AND storage_type = %d AND validity = 0;", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, storage_type);
426         ret = _media_svc_sql_query(sql, uid);
427
428         SQLITE3_SAFE_FREE(sql);
429
430         return ret;
431 }
432
433 int _media_svc_set_folder_validity(sqlite3 *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
434 {
435         int ret = MS_MEDIA_ERR_NONE;
436         char *sql = NULL;
437         char start_path_id[MEDIA_SVC_UUID_SIZE+1] = {0,};
438
439         if (is_recursive) {
440                 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, start_path, start_path_id, uid);
441                 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_folder_id_by_foldername fail");
442                 media_svc_retvm_if(!STRING_VALID(start_path_id), MS_MEDIA_ERR_INVALID_PARAMETER, "start_path_id is NULL");
443
444                 sql = sqlite3_mprintf("UPDATE '%s' SET validity = %d WHERE storage_uuid = '%q' AND (path LIKE '%q/%%' OR folder_uuid ='%q');",
445                                                 MEDIA_SVC_DB_TABLE_FOLDER, validity, storage_id, start_path, start_path_id);
446         } else {
447                 sql = sqlite3_mprintf("UPDATE '%s' SET validity = %d WHERE storage_uuid = '%q' AND path = '%q';",
448                                                 MEDIA_SVC_DB_TABLE_FOLDER, validity, storage_id, start_path);
449         }
450
451         ret = _media_svc_sql_query(sql, uid);
452
453         SQLITE3_SAFE_FREE(sql);
454
455         return ret;
456 }
457
458 int _media_svc_delete_folder_by_storage_id(const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
459 {
460         int ret = MS_MEDIA_ERR_NONE;
461         char *sql = NULL;
462
463         sql = sqlite3_mprintf("DELETE FROM '%s' WHERE storage_uuid = '%q' AND storage_type = %d;", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, storage_type);
464         ret = _media_svc_sql_query(sql, uid);
465
466         SQLITE3_SAFE_FREE(sql);
467
468         return ret;
469 }
470
471 GList **_media_svc_get_folder_list_ptr(void)
472 {
473         return &g_media_svc_insert_folder_query_list;
474 }
475
476 int _media_svc_get_folder_scan_status(sqlite3 *handle, const char *storage_id, const char *path, int *scan_status)
477 {
478         int ret = MS_MEDIA_ERR_NONE;
479         sqlite3_stmt *sql_stmt = NULL;
480         char *sql = NULL;
481
482         if (!STRING_VALID(path)) {
483                 media_svc_error("Not found valid path");
484                 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
485         }
486
487         sql = sqlite3_mprintf("SELECT scan_status FROM '%s' WHERE path = '%q' AND storage_uuid = '%q'", MEDIA_SVC_DB_TABLE_FOLDER, path, storage_id);
488
489         ret = _media_svc_sql_prepare_to_step_simple(handle, sql, &sql_stmt);
490
491         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
492
493         while (sqlite3_step(sql_stmt) == SQLITE_ROW)
494                 *scan_status = sqlite3_column_int(sql_stmt, 0);
495
496         SQLITE3_FINALIZE(sql_stmt);
497
498         return ret;
499 }
500
501 int _media_svc_set_folder_scan_status(const char *storage_id, const char *path, int scan_status, uid_t uid)
502 {
503         int ret = MS_MEDIA_ERR_NONE;
504         char *sql = NULL;
505
506         if (path)
507                 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);
508         else
509                 sql = sqlite3_mprintf("UPDATE '%s' SET scan_status=%d WHERE storage_uuid = '%q'", MEDIA_SVC_DB_TABLE_FOLDER, scan_status, storage_id);
510
511         ret = _media_svc_sql_query(sql, uid);
512         SQLITE3_SAFE_FREE(sql);
513
514         return ret;
515 }
516
517 int _media_svc_get_folder_modified_time_by_path(sqlite3 *handle, const char *path, const char *storage_id, time_t *modified_time)
518 {
519         int ret = MS_MEDIA_ERR_NONE;
520         sqlite3_stmt *sql_stmt = NULL;
521
522         char *sql = sqlite3_mprintf("SELECT modified_time FROM '%s' WHERE path = '%q' AND storage_uuid = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, path, storage_id);
523
524         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
525
526         if (ret != MS_MEDIA_ERR_NONE) {
527                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
528                         media_svc_debug("there is no folder.");
529                 else
530                         media_svc_error("error when _media_svc_get_folder_modified_time_by_path. err = [%d]", ret);
531
532                 return ret;
533         }
534
535         *modified_time = (int)sqlite3_column_int(sql_stmt, 0);
536
537         SQLITE3_FINALIZE(sql_stmt);
538
539         return ret;
540 }
541
542 int _media_svc_get_null_scan_folder_list(sqlite3 *handle, const char *storage_id, const char *path, char ***folder_list, int *count)
543 {
544         int ret = MS_MEDIA_ERR_NONE;
545         int idx = 0;
546         int cnt = 0;
547         char *sql = NULL;
548         sqlite3_stmt *sql_stmt = NULL;
549
550         if (path == NULL) {
551                 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);
552                 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
553
554                 if (ret != MS_MEDIA_ERR_NONE) {
555                         if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
556                                 media_svc_debug("there is no folder.");
557                         else
558                                 media_svc_error("error when get folder_id by path. err = [%d]", ret);
559
560                         *folder_list = NULL;
561                         *count = 0;
562
563                         return ret;
564                 }
565
566                 cnt = sqlite3_column_int(sql_stmt, 0);
567                 SQLITE3_FINALIZE(sql_stmt);
568                 if (cnt > 0) {
569                         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);
570                 } else {
571                         *folder_list = NULL;
572                         *count = 0;
573
574                         return MS_MEDIA_ERR_INTERNAL;
575                 }
576
577                 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
578
579                 if (ret != MS_MEDIA_ERR_NONE) {
580                         media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
581
582                         *folder_list = NULL;
583                         *count = 0;
584
585                         return ret;
586                 }
587         } else {
588                 sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE path LIKE '%q/%%' AND name IN (select REPLACE(path,'%q/','') from '%s' WHERE path LIKE '%q/%%'); ",
589                         MEDIA_SVC_DB_TABLE_FOLDER, path, path, MEDIA_SVC_DB_TABLE_FOLDER, path);
590                 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
591
592                 if (ret != MS_MEDIA_ERR_NONE) {
593                         media_svc_error("error when _media_svc_sql_prepare_to_step. err = [%d]", ret);
594                         return ret;
595                 }
596
597                 cnt = sqlite3_column_int(sql_stmt, 0);
598                 SQLITE3_FINALIZE(sql_stmt);
599                 if (cnt > 0) {
600                         sql = sqlite3_mprintf("SELECT path FROM '%s' WHERE path LIKE '%q/%%' AND name IN (select REPLACE(path,'%q/','') from '%s' WHERE path LIKE '%q/%%'); ",
601                         MEDIA_SVC_DB_TABLE_FOLDER, path, path, MEDIA_SVC_DB_TABLE_FOLDER, path);
602                 } else {
603                         *folder_list = NULL;
604                         *count = 0;
605
606                         return MS_MEDIA_ERR_NONE;
607                 }
608
609                 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
610
611                 if (ret != MS_MEDIA_ERR_NONE) {
612                         media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
613
614                         *folder_list = NULL;
615                         *count = 0;
616
617                         return ret;
618                 }
619         }
620
621         *folder_list = malloc(sizeof(char *) * cnt);
622
623         while (1) {
624                 (*folder_list)[idx] = strdup((char *)sqlite3_column_text(sql_stmt, 0));
625
626                 if (sqlite3_step(sql_stmt) != SQLITE_ROW)
627                         break;
628                 idx++;
629         }
630
631         if (cnt == idx + 1) {
632                 *count = cnt;
633                 media_svc_debug("OK");
634         } else {
635                 /* free all data */
636                 int i = 0;
637                 for (i  = 0; i < idx; i++)
638                         SAFE_FREE((*folder_list)[i]);
639
640                 SAFE_FREE(*folder_list);
641                 *count = 0;
642                 ret = MS_MEDIA_ERR_INTERNAL;
643         }
644
645         SQLITE3_FINALIZE(sql_stmt);
646
647         return ret;
648 }
649
650 int _media_svc_delete_invalid_folder_by_path(sqlite3 *handle, const char *storage_id, const char *folder_path, uid_t uid, int *delete_count)
651 {
652         int ret = MS_MEDIA_ERR_NONE;
653         char *sql = NULL;
654         int del_count = 0;
655         sqlite3_stmt *sql_stmt = NULL;
656
657         if (folder_path == NULL)
658                 return MS_MEDIA_ERR_INVALID_PARAMETER;
659
660         /*check the number of the deleted folder*/
661         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);
662
663         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
664         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
665
666         del_count = sqlite3_column_int(sql_stmt, 0);
667
668         SQLITE3_FINALIZE(sql_stmt);
669         sql = NULL;
670
671         /*delete invalid folder*/
672         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);
673         ret = _media_svc_sql_query(sql, uid);
674
675         SQLITE3_SAFE_FREE(sql);
676
677         *delete_count = del_count;
678
679         return ret;
680 }
681
682 int _media_svc_count_folder_with_path(sqlite3 *handle, const char *storage_id, const char *path, int *count)
683 {
684         int ret = MS_MEDIA_ERR_NONE;
685         sqlite3_stmt *sql_stmt = NULL;
686
687         char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE (storage_uuid='%q' AND path='%q')", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
688
689         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
690
691         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
692
693         *count = sqlite3_column_int(sql_stmt, 0);
694
695         SQLITE3_FINALIZE(sql_stmt);
696
697         return MS_MEDIA_ERR_NONE;
698 }
699
700 int _media_svc_count_subfolder_with_path(sqlite3 *handle, const char *storage_id, const char *path, int *count)
701 {
702         int ret = MS_MEDIA_ERR_NONE;
703         sqlite3_stmt *sql_stmt = NULL;
704
705         char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE (storage_uuid='%q' AND path LIKE'%q/%%')", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
706
707         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
708
709         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
710
711         *count = sqlite3_column_int(sql_stmt, 0);
712
713         SQLITE3_FINALIZE(sql_stmt);
714
715         return MS_MEDIA_ERR_NONE;
716 }
717
718 int _media_svc_get_folder_uuid(sqlite3 *handle, const char *storage_id, const char *path, char *folder_id)
719 {
720         int ret = MS_MEDIA_ERR_NONE;
721         sqlite3_stmt *sql_stmt = NULL;
722         char *sql = NULL;
723
724         sql = sqlite3_mprintf("SELECT folder_uuid FROM '%s' WHERE (storage_uuid='%q' AND path='%q')", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
725
726         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
727
728         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
729
730         _strncpy_safe(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE+1);
731
732         SQLITE3_FINALIZE(sql_stmt);
733
734         if (!STRING_VALID(folder_id)) {
735                 media_svc_error("Not found valid storage id [%s]", path);
736                 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
737         }
738
739         return ret;
740 }
741