summaryrefslogtreecommitdiff
path: root/src/media-svc-media-folder.c
blob: f6798265787b746c100ef45066f1ad41cf1daa95 (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
/*
 * libmedia-service
 *
 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * 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 <glib/gstdio.h>
#include <media-util-user.h>

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

static int __media_svc_get_folder_id(sqlite3 *handle, const char *path, long long int *folder_id)
{
	int ret = MS_MEDIA_ERR_NONE;
	sqlite3_stmt *sql_stmt = NULL;
	char *sql = sqlite3_mprintf("SELECT folder_id FROM %q WHERE folder_path=%Q", DB_TABLE_FOLDER, path);

	ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
	SQLITE3_SAFE_FREE(sql);
	media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_sql_prepare_to_step failed [%d]", ret);

	*folder_id = sqlite3_column_int64(sql_stmt, 0);

	SQLITE3_FINALIZE(sql_stmt);

	return ret;
}

static int __media_svc_append_folder(bool is_direct, const char *storage_id, const char *folder_path, uid_t uid)
{
	int ret = MS_MEDIA_ERR_NONE;
	char *folder_name = NULL;
	int folder_modified_date = 0;

	folder_name = g_path_get_basename(folder_path);
	folder_modified_date = _media_svc_get_file_time(folder_path);

	/* Sometime SQLITE3 returns NO_RECORD, so need to consider conflict case.. */
	char *sql = sqlite3_mprintf("INSERT OR IGNORE INTO %q(folder_path, folder_name, storage_uuid, folder_modified_time) VALUES (%Q, %Q, %Q, %d);",
				DB_TABLE_FOLDER, folder_path, folder_name, storage_id, folder_modified_date);

	if (is_direct)
		ret = _media_svc_sql_query_direct(sql, uid);
	else
		ret = _media_svc_sql_query(sql, uid);

	SQLITE3_SAFE_FREE(sql);

	g_free(folder_name);

	return ret;
}

int _media_svc_update_folder_modified_time(const char *folder_path, uid_t uid)
{
	int ret = MS_MEDIA_ERR_NONE;
	int time = 0;
	char *query = NULL;

	time = _media_svc_get_file_time(folder_path);
	query = sqlite3_mprintf("UPDATE %q SET folder_modified_time=%d WHERE folder_path=%Q", DB_TABLE_FOLDER, time, folder_path);

	ret = _media_svc_sql_query(query, uid);
	SQLITE3_SAFE_FREE(query);

	return ret;
}

static int __media_svc_append_parent_folder(sqlite3 *handle, bool is_direct, const char *storage_id, const char *path, uid_t uid)
{
	int ret = MS_MEDIA_ERR_NONE;
	size_t next_pos = ms_user_get_root_length(path, uid);
	char *next = NULL;
	char *dir_path = NULL;

	do {
		next = strstr(path + next_pos, "/");
		if (next) {
			next_pos = (next - path);
			dir_path = g_strndup(path, next_pos);
			next_pos++;
		} else {
			dir_path = g_strdup(path);
		}

		ret = _media_svc_check_folder_by_path(handle, dir_path);
		if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
			ret = __media_svc_append_folder(is_direct, storage_id, dir_path, uid);
			if (ret != MS_MEDIA_ERR_NONE)
				media_svc_error("__media_svc_append_folder is failed");
			else
				media_svc_sec_debug("Append new folder path[%s]", dir_path);
		}

		g_free(dir_path);
	} while (next);

	return MS_MEDIA_ERR_NONE;
}

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)
{
	char *dir_path = NULL;
	int ret = MS_MEDIA_ERR_NONE;

	dir_path = g_path_get_dirname(path);

	ret = __media_svc_get_folder_id(handle, dir_path, folder_id);
	if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
		ret = __media_svc_append_parent_folder(handle, is_direct, storage_id, dir_path, uid);
		if (ret != MS_MEDIA_ERR_NONE) {
			media_svc_error("__media_svc_append_parent_folder failed");
			goto FINALIZE;
		}

		ret = __media_svc_get_folder_id(handle, dir_path, folder_id);
	}
FINALIZE:
	g_free(dir_path);

	return ret;
}

int _media_svc_append_by_folder_path(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
{
	int ret = MS_MEDIA_ERR_NONE;

	ret = _media_svc_check_folder_by_path(handle, path);
	if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
		ret = __media_svc_append_parent_folder(handle, true, storage_id, path, uid);
	else
		ret = _media_svc_set_folder_validity(true, path, 1, false, uid);

	return ret;
}

int _media_svc_set_folder_validity(bool is_direct, const char *start_path, int validity, bool is_recursive, uid_t uid)
{
	int ret = MS_MEDIA_ERR_NONE;
	char *sql = NULL;

	if (is_recursive) {
		sql = sqlite3_mprintf("UPDATE %q SET validity=%d WHERE folder_path LIKE '%q/%%' OR folder_path=%Q",
			DB_TABLE_FOLDER, validity, start_path, start_path);
	} else {
		sql = sqlite3_mprintf("UPDATE %q SET validity=%d WHERE folder_path=%Q", DB_TABLE_FOLDER, validity, start_path);
	}

	if (is_direct)
		ret = _media_svc_sql_query_direct(sql, uid);
	else
		ret = _media_svc_sql_query(sql, uid);

	SQLITE3_SAFE_FREE(sql);

	return ret;
}

int _media_svc_check_folder_by_path(sqlite3 *handle, const char *path)
{
	int ret = MS_MEDIA_ERR_NONE;
	sqlite3_stmt *sql_stmt = NULL;
	char *sql = NULL;

	media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");

	sql = sqlite3_mprintf("SELECT 1 FROM %q WHERE folder_path=%Q", DB_TABLE_FOLDER, path);
	ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
	SQLITE3_SAFE_FREE(sql);
	media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);

	SQLITE3_FINALIZE(sql_stmt);

	return MS_MEDIA_ERR_NONE;
}