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