Fix sign-compare
[platform/core/multimedia/libmedia-service.git] / src / media-svc-media-folder.c
1 /*
2  * libmedia-service
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <glib/gstdio.h>
21 #include <media-util-user.h>
22
23 #include "media-svc-media-folder.h"
24 #include "media-svc-debug.h"
25 #include "media-svc-env.h"
26 #include "media-svc-util.h"
27 #include "media-svc-db-utils.h"
28
29 static int __media_svc_get_folder_id(sqlite3 *handle, const char *path, long long int *folder_id)
30 {
31         int ret = MS_MEDIA_ERR_NONE;
32         sqlite3_stmt *sql_stmt = NULL;
33         char *sql = sqlite3_mprintf("SELECT folder_id FROM %q WHERE folder_path=%Q", DB_TABLE_FOLDER, path);
34
35         ret = _media_svc_get_result_with_check_record(handle, sql, &sql_stmt);
36         SQLITE3_SAFE_FREE(sql);
37         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "failed to get folder id[%d]", ret);
38
39         *folder_id = sqlite3_column_int64(sql_stmt, 0);
40
41         SQLITE3_FINALIZE(sql_stmt);
42
43         return ret;
44 }
45
46 static int __media_svc_append_folder(bool is_direct, const char *storage_id, const char *folder_path, uid_t uid)
47 {
48         int ret = MS_MEDIA_ERR_NONE;
49         char *folder_name = NULL;
50
51         folder_name = g_path_get_basename(folder_path);
52
53         /* Sometime SQLITE3 returns NO_RECORD, so need to consider conflict case.. */
54         char *sql = sqlite3_mprintf("INSERT OR IGNORE INTO %q(folder_path, folder_name, storage_uuid) VALUES (%Q, %Q, %Q);",
55                                 DB_TABLE_FOLDER, folder_path, folder_name, storage_id);
56
57         if (is_direct)
58                 ret = _media_svc_sql_query_direct(sql, uid);
59         else
60                 ret = _media_svc_sql_query(sql, uid);
61
62         SQLITE3_SAFE_FREE(sql);
63
64         g_free(folder_name);
65
66         return ret;
67 }
68
69 static int __media_svc_append_parent_folder(sqlite3 *handle, bool is_direct, const char *storage_id, const char *path, uid_t uid)
70 {
71         int ret = MS_MEDIA_ERR_NONE;
72         size_t next_pos = ms_user_get_root_length(path, uid);
73         char *next = NULL;
74         char *dir_path = NULL;
75
76         do {
77                 next = strstr(path + next_pos, "/");
78                 if (next) {
79                         next_pos = (next - path);
80                         dir_path = g_strndup(path, next_pos);
81                         next_pos++;
82                 } else {
83                         dir_path = g_strdup(path);
84                 }
85
86                 ret = _media_svc_check_folder_by_path(handle, dir_path);
87                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
88                         ret = __media_svc_append_folder(is_direct, storage_id, dir_path, uid);
89                         if (ret != MS_MEDIA_ERR_NONE)
90                                 media_svc_error("__media_svc_append_folder is failed");
91                         else
92                                 media_svc_sec_debug("Append new folder path[%s]", dir_path);
93                 }
94
95                 g_free(dir_path);
96         } while (next);
97
98         return MS_MEDIA_ERR_NONE;
99 }
100
101 int _media_svc_get_and_append_folder_id_by_path(sqlite3 *handle, bool is_direct, const char *storage_id, const char *path, long long int *folder_id, uid_t uid)
102 {
103         char *dir_path = NULL;
104         int ret = MS_MEDIA_ERR_NONE;
105
106         dir_path = g_path_get_dirname(path);
107
108         ret = __media_svc_get_folder_id(handle, dir_path, folder_id);
109         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
110                 ret = __media_svc_append_parent_folder(handle, is_direct, storage_id, dir_path, uid);
111                 if (ret != MS_MEDIA_ERR_NONE) {
112                         media_svc_error("__media_svc_append_parent_folder failed");
113                         goto FINALIZE;
114                 }
115
116                 ret = __media_svc_get_folder_id(handle, dir_path, folder_id);
117         }
118 FINALIZE:
119         g_free(dir_path);
120
121         return ret;
122 }
123
124 int _media_svc_append_by_folder_path(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
125 {
126         int ret = MS_MEDIA_ERR_NONE;
127
128         ret = _media_svc_check_folder_by_path(handle, path);
129         if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
130                 ret = __media_svc_append_parent_folder(handle, true, storage_id, path, uid);
131         else
132                 ret = _media_svc_set_folder_validity(true, path, 1, false, uid);
133
134         return ret;
135 }
136
137 int _media_svc_set_folder_validity(bool is_direct, const char *start_path, int validity, bool is_recursive, uid_t uid)
138 {
139         int ret = MS_MEDIA_ERR_NONE;
140         char *sql = NULL;
141
142         if (is_recursive) {
143                 sql = sqlite3_mprintf("UPDATE %q SET validity=%d WHERE folder_path LIKE '%q/%%' OR folder_path=%Q",
144                         DB_TABLE_FOLDER, validity, start_path, start_path);
145         } else {
146                 sql = sqlite3_mprintf("UPDATE %q SET validity=%d WHERE folder_path=%Q", DB_TABLE_FOLDER, validity, start_path);
147         }
148
149         if (is_direct)
150                 ret = _media_svc_sql_query_direct(sql, uid);
151         else
152                 ret = _media_svc_sql_query(sql, uid);
153
154         SQLITE3_SAFE_FREE(sql);
155
156         return ret;
157 }
158
159 int _media_svc_check_folder_by_path(sqlite3 *handle, const char *path)
160 {
161         int ret = MS_MEDIA_ERR_NONE;
162         sqlite3_stmt *sql_stmt = NULL;
163         char *sql = NULL;
164
165         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
166
167         sql = sqlite3_mprintf("SELECT 1 FROM %q WHERE folder_path=%Q", DB_TABLE_FOLDER, path);
168         ret = _media_svc_get_result_with_check_record(handle, sql, &sql_stmt);
169         SQLITE3_SAFE_FREE(sql);
170         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
171
172         SQLITE3_FINALIZE(sql_stmt);
173
174         return MS_MEDIA_ERR_NONE;
175 }