summaryrefslogtreecommitdiff
path: root/src/common/media-svc-storage.c
blob: 9903a71410a4645d7c480c60a5a836f9d3779028 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
 * libmedia-service
 *
 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@samsung.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include "media-util-err.h"
#include "media-svc-debug.h"
#include "media-svc-env.h"
#include "media-svc-db-utils.h"
#include "media-svc-util.h"
#include "media-svc-storage.h"

int _media_svc_check_storage(sqlite3 *handle, const char *storage_id, char **storage_path, int *validity)
{
	int ret = MS_MEDIA_ERR_NONE;
	sqlite3_stmt *sql_stmt = NULL;
	char *sql = NULL;

	media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
	media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
	media_svc_retvm_if(validity == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "validity is NULL");

	*storage_path = NULL;
	*validity = 0;

	sql = sqlite3_mprintf("SELECT storage_path, validity FROM %q WHERE storage_id=%Q", MEDIA_SVC_DB_TABLE_STORAGE, storage_id);
	ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
	media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);

	*storage_path = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
	*validity = sqlite3_column_int(sql_stmt, 1);

	SQLITE3_FINALIZE(sql_stmt);

	return MS_MEDIA_ERR_NONE;
}

int _media_svc_append_storage(const char *storage_id, const char *storage_path, ms_user_storage_type_e storage_type, uid_t uid)
{
	int ret = MS_MEDIA_ERR_NONE;
	char *sql = sqlite3_mprintf("INSERT INTO %q (storage_id, storage_path, storage_type) values (%Q, %Q, %d);",
						MEDIA_SVC_DB_TABLE_STORAGE, storage_id, storage_path, storage_type);

	ret = _media_svc_sql_query_direct(sql, uid);
	SQLITE3_SAFE_FREE(sql);

	return ret;
}

int _media_svc_update_storage_path(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
{
	int ret = MS_MEDIA_ERR_NONE;
	char *sql = NULL;
	char *old_storage_path = NULL;
	int validity = 0;

	media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
	media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");

	/*Get old path*/
	ret = _media_svc_check_storage(handle, storage_id, &old_storage_path, &validity);
	media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);

	/*Storage table update*/
	sql = sqlite3_mprintf("UPDATE %q SET storage_path=%Q WHERE storage_id=%Q", MEDIA_SVC_DB_TABLE_STORAGE, path, storage_id);
	ret = _media_svc_sql_query_direct(sql, uid);
	SQLITE3_SAFE_FREE(sql);
	if (ret != MS_MEDIA_ERR_NONE) {
		g_free(old_storage_path);
		return ret;
	}

	/*Folder table update*/
	sql = sqlite3_mprintf("UPDATE %q SET folder_path=REPLACE(folder_path, %Q, %Q) WHERE storage_uuid=%Q", MEDIA_SVC_DB_TABLE_FOLDER, old_storage_path, path, storage_id);
	ret = _media_svc_sql_query_direct(sql, uid);
	SQLITE3_SAFE_FREE(sql);
	if (ret != MS_MEDIA_ERR_NONE) {
		g_free(old_storage_path);
		return ret;
	}

	/*Media table update*/
	sql = sqlite3_mprintf("UPDATE %q SET media_path=REPLACE(media_path, %Q, %Q) WHERE storage_uuid=%Q", MEDIA_SVC_DB_TABLE_MEDIA, old_storage_path, path, storage_id);
	ret = _media_svc_sql_query_direct(sql, uid);
	SQLITE3_SAFE_FREE(sql);
	g_free(old_storage_path);
	media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);

	return ret;
}

int __media_svc_delete_thumbnail(sqlite3 *handle)
{
	int ret = MS_MEDIA_ERR_NONE;
	char *sql = NULL;
	sqlite3_stmt *sql_stmt = NULL;

	sql = sqlite3_mprintf("SELECT media_thumbnail_path FROM %q WHERE validity=0 AND media_thumbnail_path is not null", MEDIA_SVC_DB_TABLE_MEDIA);
	ret = _media_svc_sql_prepare_to_step_simple(handle, sql, &sql_stmt);
	media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);

	while (sqlite3_step(sql_stmt) == SQLITE_ROW)
		_media_svc_remove_file((const char *)sqlite3_column_text(sql_stmt, 0));

	SQLITE3_FINALIZE(sql_stmt);

	return ret;
}

int _media_svc_delete_invalid_storage(sqlite3 *handle, uid_t uid)
{
	int ret = MS_MEDIA_ERR_NONE;
	char *sql = NULL;

	ret = __media_svc_delete_thumbnail(handle);
	media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Fail to remove thumbnail");

	sql = sqlite3_mprintf("DELETE FROM %q WHERE validity=0;DELETE FROM %q WHERE validity=0;DELETE FROM %q WHERE validity=0;",
			MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_DB_TABLE_STORAGE, MEDIA_SVC_DB_TABLE_FOLDER);

	ret = _media_svc_sql_query_direct(sql, uid);
	SQLITE3_SAFE_FREE(sql);

	return ret;
}

int _media_svc_update_storage_validity(const char *storage_id, int validity, uid_t uid)
{
	int ret = MS_MEDIA_ERR_NONE;
	char *sql = NULL;

	if (storage_id == NULL)
		sql = sqlite3_mprintf("UPDATE %q SET validity=%d", MEDIA_SVC_DB_TABLE_STORAGE, validity);
	else
		sql = sqlite3_mprintf("UPDATE %q SET validity=%d WHERE storage_id=%Q", MEDIA_SVC_DB_TABLE_STORAGE, validity, storage_id);

	ret = _media_svc_sql_query_direct(sql, uid);
	SQLITE3_SAFE_FREE(sql);

	return ret;
}

int _media_svc_get_storage_uuid(sqlite3 *handle, const char *path, char *storage_id, uid_t uid)
{
	int ret = MS_MEDIA_ERR_NONE;
	sqlite3_stmt *sql_stmt = NULL;
	char *sql = NULL;
	char *storage_path = NULL;
	char *remain_path = NULL;
	int remain_len = 0;
	char *internal_path = NULL;

	media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");

	ret = ms_user_get_internal_root_path(uid, &internal_path);
	media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Fail to get root path");

	if (STRING_VALID(internal_path) && strncmp(path, internal_path, strlen(internal_path)) == 0) {
		SAFE_STRLCPY(storage_id, MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_UUID_SIZE+1);
		SAFE_FREE(internal_path);
		return MS_MEDIA_ERR_NONE;
	}

	SAFE_FREE(internal_path);

	remain_path = strstr(path + (STRING_VALID(MEDIA_ROOT_PATH_USB) ? strlen(MEDIA_ROOT_PATH_USB) : 0) + 1, "/");
	if (remain_path != NULL)
		remain_len = strlen(remain_path);

	storage_path = strndup(path, strlen(path) - remain_len);

	sql = sqlite3_mprintf("SELECT storage_id FROM '%q' WHERE validity=1 AND storage_path = '%q'", MEDIA_SVC_DB_TABLE_STORAGE, storage_path);

	ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
	SAFE_FREE(storage_path);
	media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);

	if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 0)))
		SAFE_STRLCPY(storage_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE+1);

	SQLITE3_FINALIZE(sql_stmt);

	if (!STRING_VALID(storage_id)) {
		media_svc_error("Not found valid storage id [%s]", path);
		ret = MS_MEDIA_ERR_INVALID_PARAMETER;
	}

	return ret;
}

int _media_svc_get_storage_path(sqlite3 *handle, GPtrArray **storage_path)
{
	int ret = MS_MEDIA_ERR_NONE;
	sqlite3_stmt *sql_stmt = NULL;
	char *sql = NULL;
	char *root_path = NULL;

	media_svc_retvm_if(!storage_path, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");

	sql = sqlite3_mprintf("SELECT storage_path FROM %q WHERE validity=1", MEDIA_SVC_DB_TABLE_STORAGE);

	ret = _media_svc_sql_prepare_to_step_simple(handle, sql, &sql_stmt);
	media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);

	while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
		root_path = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
		g_ptr_array_add(*storage_path, root_path);
	}

	SQLITE3_FINALIZE(sql_stmt);

	return ret;
}