2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #include <tzplatform_config.h>
27 #include "storage-internal.h"
28 #include "storage-external-dbus.h"
30 #define FORMAT_TIMEOUT (120*1000)
33 Get compat path from origin Multi-user path
34 from TZ_USER_CONTENT/.. to /opt/usr/media/..
35 Input should be normalized path like /opt/usr/home/owner/media (TODO: internal normalization)
37 Why this API should be provided?
38 In multi-user environment, each user has own compat content direcotry.(/opt/usr/media)
39 However, although system daemon operates real path,
40 system daemon needs to provide compat path to App if the real path is converted.
43 #include <storage-internal.h>
46 if(storage_get_compat_internal_path(src, sizeof(dest), dest) < 0)
47 // cannot convert. use src path
49 // can convert. use dest path
51 //LCOV_EXCL_START Untested function
52 API int storage_get_compat_internal_path(const char* origin, int len, char* compat)
58 if (!compat || !origin) {
59 _E("Invalid parameter");
63 // this API works on place where compat path is bind-mounted
64 if (!is_compat_bind_mount()) {
65 //LCOV_EXCL_START System Error
66 _E("No compat bind mount");
71 str = tzplatform_getenv(TZ_USER_CONTENT);
72 str_len = strlen(str);
73 if (strncmp(origin, str, str_len) != 0) {
74 _E("Failed to match TZ_USER_CONTENT");
78 r = snprintf(compat, len, "%s%s", COMPAT_DIR, origin + str_len);
80 //LCOV_EXCL_START System Error
81 _E("Failed to create new path");
91 Get Multi-user path from compat path
92 from /opt/usr/media/.. to TZ_USER_CONTENT/..
93 Input should be normalized path like /opt/usr/media (TODO: internal normalization)
95 Why this API should be provided?
96 In multi-user environment, each user has own compat content direcotry.(/opt/usr/media)
97 However, although some APIs send the compat path to system daemon,
98 system daemon should access real path.
101 #include <storage-internal.h>
104 if(storage_get_origin_internal_path(src, sizeof(dest), dest) < 0)
105 // cannot convert. use src path
107 // can convert. use dest path
109 API int storage_get_origin_internal_path(const char* compat, int len, char* origin)
114 if (!compat || !origin) {
115 _E("Invalid parameter");
119 // this API works on place where compat path is bind-mounted
120 if (!is_compat_bind_mount()) {
121 //LCOV_EXCL_START System Error
122 _E("no compat bind mount");
127 compat_len = strlen(COMPAT_DIR);
128 if (strncmp(compat, COMPAT_DIR, compat_len) != 0) {
129 _E("failed to match COMPAT_DIR");
133 r = snprintf(origin, len, "%s%s", tzplatform_getenv(TZ_USER_CONTENT), compat + compat_len);
135 //LCOV_EXCL_START System Error
136 _E("failed to create new path");
144 API int storage_get_primary_sdcard(int *storage_id, char **path)
147 storage_ext_device info;
149 if (!storage_id || !path)
150 return STORAGE_ERROR_INVALID_PARAMETER;
152 if (!storage_ext_is_supported())
153 return STORAGE_ERROR_NO_DEVICE;
155 result = dbus_method_call_sync(STORAGE_EXT_BUS_NAME,
156 STORAGE_EXT_PATH_MANAGER,
157 STORAGE_EXT_IFACE_MANAGER,
161 //LCOV_EXCL_START System Error
162 _E("Failed to get primary sdcard partition"); //LCOV_EXCL_LINE
163 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
167 g_variant_get(result, "(issssssisibii)",
168 &info.type, &info.devnode, &info.syspath,
169 &info.fs_usage, &info.fs_type,
170 &info.fs_version, &info.fs_uuid,
171 &info.readonly, &info.mount_point,
172 &info.state, &info.primary,
173 &info.flags, &info.storage_id);
175 g_variant_unref(result);
177 if (info.storage_id < 0)
178 return STORAGE_ERROR_NO_DEVICE;
180 *path = strdup(info.mount_point);
182 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
184 *storage_id = info.storage_id;
186 return STORAGE_ERROR_NONE;
189 API int storage_get_storage_level(const char *path, char **level)
194 return STORAGE_ERROR_INVALID_PARAMETER;
196 ret = storage_ext_get_storage_level(path, level);
198 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
199 else if (ret == -EINVAL)
200 return STORAGE_ERROR_INVALID_PARAMETER;
202 return STORAGE_ERROR_OPERATION_FAILED;
204 return STORAGE_ERROR_NONE;
207 static void mount_mmc_cb(GVariant *var, void *user_data, GError *err)
209 struct mmc_contents *mmc_data = (struct mmc_contents*)user_data;
212 _D("mount_mmc_cb called");
215 _E("no message [%s]", err->message);
220 g_variant_get(var, "(i)", &mmc_ret);
222 _I("Mount State : %d", mmc_ret);
226 g_variant_unref(var);
227 (mmc_data->mmc_cb)(mmc_ret, mmc_data->user_data);
230 API int storage_request_mount_mmc(struct mmc_contents *mmc_data)
232 void (*mount_cb)(GVariant *, void *, GError *) = NULL;
238 if (mmc_data && mmc_data->mmc_cb) {
239 _I("Mount callback exists");
240 mount_cb = mount_mmc_cb;
244 ret = storage_get_primary_sdcard(&id, &path);
245 if (ret != STORAGE_ERROR_NONE)
248 ret = dbus_method_async_with_reply_var(STORAGE_EXT_BUS_NAME,
249 STORAGE_EXT_PATH_MANAGER,
250 STORAGE_EXT_IFACE_MANAGER,
252 g_variant_new("(is)", id, ""),
257 _I("Mount Request %s", ret == 0 ? "Success" : "Failed");
260 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
262 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
264 return STORAGE_ERROR_NONE;
267 static void unmount_mmc_cb(GVariant *var, void *user_data, GError *err)
269 struct mmc_contents *mmc_data = (struct mmc_contents*)user_data;
272 _D("unmount_mmc_cb called");
275 _E("no message [%s]", err->message);
280 g_variant_get(var, "(i)", &mmc_ret);
282 _I("Unmount State : %d", mmc_ret);
286 g_variant_unref(var);
287 (mmc_data->mmc_cb)(mmc_ret, mmc_data->user_data);
290 API int storage_request_unmount_mmc(struct mmc_contents *mmc_data, int option)
292 void (*unmount_cb)(GVariant *, void *, GError *) = NULL;
298 if (option < 0 || option > 1)
299 return STORAGE_ERROR_INVALID_PARAMETER;
301 if (mmc_data && mmc_data->mmc_cb) {
302 _I("Unmount callback exists");
303 unmount_cb = unmount_mmc_cb;
307 ret = storage_get_primary_sdcard(&id, &path);
308 if (ret != STORAGE_ERROR_NONE)
311 ret = dbus_method_async_with_reply_var(STORAGE_EXT_BUS_NAME,
312 STORAGE_EXT_PATH_MANAGER,
313 STORAGE_EXT_IFACE_MANAGER,
315 g_variant_new("(ii)", id, option),
320 _I("Unmount Request %s", ret == 0 ? "Success" : "Failed");
323 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
325 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
327 return STORAGE_ERROR_NONE;
330 static void format_mmc_cb(GVariant *var, void *user_data, GError *err)
332 struct mmc_contents *mmc_data = (struct mmc_contents*)user_data;
335 _D("format_mmc_cb called");
338 _E("no message [%s]", err->message);
343 g_variant_get(var, "(i)", &mmc_ret);
345 _I("Format State : %d", mmc_ret);
349 g_variant_unref(var);
350 (mmc_data->mmc_cb)(mmc_ret, mmc_data->user_data);
353 API int storage_request_format_mmc(struct mmc_contents *mmc_data)
355 return storage_format_mmc(mmc_data, 1);
358 API int storage_format_mmc(struct mmc_contents *mmc_data, int option)
360 void (*format_cb)(GVariant *, void *, GError *) = NULL;
366 if (option < 0 || option > 1)
367 return STORAGE_ERROR_INVALID_PARAMETER;
369 if (mmc_data && mmc_data->mmc_cb) {
370 _I("Format callback exists");
371 format_cb = format_mmc_cb;
375 ret = storage_get_primary_sdcard(&id, &path);
376 if (ret != STORAGE_ERROR_NONE)
379 ret = dbus_method_async_with_reply_var(STORAGE_EXT_BUS_NAME,
380 STORAGE_EXT_PATH_MANAGER,
381 STORAGE_EXT_IFACE_MANAGER,
383 g_variant_new("(ii)", id, option),
388 _I("Format Request %s", ret == 0 ? "Success" : "Failed");
391 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
393 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
395 return STORAGE_ERROR_NONE;