f1f38252f5e4283dc901972a7f09c2b0eef16a45
[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  * 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-err.h>
22 #include "media-svc-media-folder.h"
23 #include "media-svc-debug.h"
24 #include "media-svc-env.h"
25 #include "media-svc-util.h"
26 #include "media-svc-db-utils.h"
27
28 static int __media_svc_get_folder_id(sqlite3 *handle, const char *path, long long int *folder_id)
29 {
30         int ret = MS_MEDIA_ERR_NONE;
31         sqlite3_stmt *sql_stmt = NULL;
32         char *sql = NULL;
33
34         sql = sqlite3_mprintf("SELECT folder_id FROM %q WHERE folder_path=%Q", DB_TABLE_FOLDER, path);
35
36         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
37         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_sql_prepare_to_step failed [%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         int folder_modified_date = 0;
51
52         folder_name = g_path_get_basename(folder_path);
53         folder_modified_date = _media_svc_get_file_time(folder_path);
54
55         /* Sometime SQLITE3 returns NO_RECORD, so need to consider conflict case.. */
56         char *sql = sqlite3_mprintf("INSERT OR IGNORE INTO %q (folder_path, folder_name, storage_uuid, folder_modified_time) VALUES (%Q, %Q, %Q, '%d');",
57                                 DB_TABLE_FOLDER, folder_path, folder_name, storage_id, folder_modified_date);
58
59         if (is_direct)
60                 ret = _media_svc_sql_query_direct(sql, uid);
61         else
62                 ret = _media_svc_sql_query(sql, uid);
63
64         SQLITE3_SAFE_FREE(sql);
65
66         g_free(folder_name);
67
68         return ret;
69 }
70
71 int _media_svc_update_folder_modified_time(const char *folder_path, uid_t uid)
72 {
73         int ret = MS_MEDIA_ERR_NONE;
74         int time = 0;
75         char *query = NULL;
76
77         time = _media_svc_get_file_time(folder_path);
78         query = sqlite3_mprintf("UPDATE %q SET folder_modified_time=%d WHERE folder_path=%Q", DB_TABLE_FOLDER, time, folder_path);
79
80         ret = _media_svc_sql_query(query, uid);
81         SQLITE3_SAFE_FREE(query);
82
83         return ret;
84 }
85
86 static int __media_svc_append_parent_folder(sqlite3 *handle, bool is_direct, const char *storage_id, const char *path, uid_t uid)
87 {
88         int ret = MS_MEDIA_ERR_NONE;
89         unsigned int next_pos = 0;
90         char *next = NULL;
91         char *dir_path = NULL;
92         char *internal_path = NULL;
93
94         if (g_str_has_prefix(path, tzplatform_getenv(TZ_SYS_STORAGE))) {
95                 next_pos = strlen(tzplatform_getenv(TZ_SYS_STORAGE)) + 1;
96         } else {
97                 ret = ms_user_get_internal_root_path(uid, &internal_path);
98                 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Fail to get root path");
99
100                 next_pos = strlen(internal_path);
101                 g_free(internal_path);
102         }
103
104         do {
105                 next = strstr(path + next_pos, "/");
106                 if (next) {
107                         next_pos = (next - path);
108                         dir_path = g_strndup(path, next_pos);
109                         next_pos++;
110                 } else {
111                         dir_path = g_strdup(path);
112                 }
113
114                 ret = _media_svc_check_folder_by_path(handle, dir_path);
115                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
116                         ret = __media_svc_append_folder(is_direct, storage_id, dir_path, uid);
117                         if (ret != MS_MEDIA_ERR_NONE)
118                                 media_svc_error("__media_svc_append_folder is failed");
119                         else
120                                 media_svc_sec_debug("Append new folder path[%s]", dir_path);
121                 }
122
123                 g_free(dir_path);
124         } while (next);
125
126         return MS_MEDIA_ERR_NONE;
127 }
128
129 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)
130 {
131         char *dir_path = NULL;
132         int ret = MS_MEDIA_ERR_NONE;
133
134         dir_path = g_path_get_dirname(path);
135
136         ret = __media_svc_get_folder_id(handle, dir_path, folder_id);
137         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
138                 ret = __media_svc_append_parent_folder(handle, is_direct, storage_id, dir_path, uid);
139                 if (ret != MS_MEDIA_ERR_NONE) {
140                         media_svc_error("__media_svc_append_parent_folder failed");
141                         goto FINALIZE;
142                 }
143
144                 ret = __media_svc_get_folder_id(handle, dir_path, folder_id);
145         }
146 FINALIZE:
147         g_free(dir_path);
148
149         return ret;
150 }
151
152 int _media_svc_append_by_folder_path(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
153 {
154         int ret = MS_MEDIA_ERR_NONE;
155
156         ret = _media_svc_check_folder_by_path(handle, path);
157         if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
158                 ret = __media_svc_append_parent_folder(handle, true, storage_id, path, uid);
159         else
160                 ret = _media_svc_set_folder_validity(true, path, 1, false, uid);
161
162         return ret;
163 }
164
165 int _media_svc_set_folder_validity(bool is_direct, const char *start_path, int validity, bool is_recursive, uid_t uid)
166 {
167         int ret = MS_MEDIA_ERR_NONE;
168         char *sql = NULL;
169
170         if (is_recursive) {
171                 sql = sqlite3_mprintf("UPDATE %q SET validity=%d WHERE folder_path LIKE '%q/%%' OR folder_path=%Q",
172                         DB_TABLE_FOLDER, validity, start_path, start_path);
173         } else {
174                 sql = sqlite3_mprintf("UPDATE %q SET validity=%d WHERE folder_path=%Q", DB_TABLE_FOLDER, validity, start_path);
175         }
176
177         if (is_direct)
178                 ret = _media_svc_sql_query_direct(sql, uid);
179         else
180                 ret = _media_svc_sql_query(sql, uid);
181
182         SQLITE3_SAFE_FREE(sql);
183
184         return ret;
185 }
186
187 int _media_svc_check_folder_by_path(sqlite3 *handle, const char *path)
188 {
189         int ret = MS_MEDIA_ERR_NONE;
190         sqlite3_stmt *sql_stmt = NULL;
191         char *sql = NULL;
192
193         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
194
195         sql = sqlite3_mprintf("SELECT 1 FROM %q WHERE folder_path=%Q", DB_TABLE_FOLDER, path);
196         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
197         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
198
199         SQLITE3_FINALIZE(sql_stmt);
200
201         return MS_MEDIA_ERR_NONE;
202 }