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.
19 #include <sys/types.h>
24 #include <tzplatform_config.h>
30 #include "storage-internal.h"
31 #include "storage-external-dbus.h"
33 #define FORMAT_TIMEOUT (120*1000)
34 #define USER_PARTITION "user"
37 Get compat path from origin Multi-user path
38 from TZ_USER_CONTENT/.. to /opt/usr/media/..
39 Input should be normalized path like /opt/usr/home/owner/media (TODO: internal normalization)
41 Why this API should be provided?
42 In multi-user environment, each user has own compat content direcotry.(/opt/usr/media)
43 However, although system daemon operates real path,
44 system daemon needs to provide compat path to App if the real path is converted.
47 #include <storage-internal.h>
50 if(storage_get_compat_internal_path(src, sizeof(dest), dest) < 0)
51 // cannot convert. use src path
53 // can convert. use dest path
55 //LCOV_EXCL_START Untested function
56 API int storage_get_compat_internal_path(const char* origin, int len, char* compat)
62 if (!compat || !origin) {
63 _E("Invalid parameter");
67 if (getuid() <= USER_UID_START) {
68 //LCOV_EXCL_START System Error
69 _E("Only apps and user session daemons are allowed "
70 "to use storage_get_compat_internal_path()");
75 // this API works on place where compat path is bind-mounted
76 if (!is_compat_bind_mount()) {
77 //LCOV_EXCL_START System Error
78 _E("No compat bind mount");
83 str = tzplatform_uid_getenv(getuid(), TZ_USER_CONTENT);
84 str_len = strlen(str);
85 if (strncmp(origin, str, str_len) != 0) {
86 _E("Failed to match TZ_USER_CONTENT");
90 r = snprintf(compat, len, "%s%s", COMPAT_DIR, origin + str_len);
92 //LCOV_EXCL_START System Error
93 _E("Failed to create new path");
103 Get Multi-user path from compat path
104 from /opt/usr/media/.. to TZ_USER_CONTENT/..
105 Input should be normalized path like /opt/usr/media (TODO: internal normalization)
107 Why this API should be provided?
108 In multi-user environment, each user has own compat content direcotry.(/opt/usr/media)
109 However, although some APIs send the compat path to system daemon,
110 system daemon should access real path.
113 #include <storage-internal.h>
116 if(storage_get_origin_internal_path(src, sizeof(dest), dest) < 0)
117 // cannot convert. use src path
119 // can convert. use dest path
121 API int storage_get_origin_internal_path(const char* compat, int len, char* origin)
126 if (!compat || !origin) {
127 _E("Invalid parameter");
131 if (getuid() <= USER_UID_START) {
132 //LCOV_EXCL_START System Error
133 _E("Only apps and user session daemons are allowed "
134 "to use storage_get_origin_internal_path()");
139 // this API works on place where compat path is bind-mounted
140 if (!is_compat_bind_mount()) {
141 //LCOV_EXCL_START System Error
142 _E("no compat bind mount");
147 compat_len = strlen(COMPAT_DIR);
148 if (strncmp(compat, COMPAT_DIR, compat_len) != 0) {
149 _E("failed to match COMPAT_DIR");
153 r = snprintf(origin, len, "%s%s", tzplatform_uid_getenv(getuid(), TZ_USER_CONTENT), compat + compat_len);
155 //LCOV_EXCL_START System Error
156 _E("failed to create new path");
164 API int storage_get_primary_sdcard(int *storage_id, char **path)
167 storage_ext_device info;
169 if (!storage_id || !path)
170 return STORAGE_ERROR_INVALID_PARAMETER;
172 if (!storage_ext_is_supported())
173 return STORAGE_ERROR_NOT_SUPPORTED;
175 result = dbus_method_call_sync(STORAGE_EXT_BUS_NAME,
176 STORAGE_EXT_PATH_MANAGER,
177 STORAGE_EXT_IFACE_MANAGER,
181 //LCOV_EXCL_START System Error
182 _E("Failed to get primary sdcard partition"); //LCOV_EXCL_LINE
183 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
187 g_variant_get(result, "(issssssisibii)",
188 &info.type, &info.devnode, &info.syspath,
189 &info.fs_usage, &info.fs_type,
190 &info.fs_version, &info.fs_uuid,
191 &info.readonly, &info.mount_point,
192 &info.state, &info.primary,
193 &info.flags, &info.storage_id);
195 g_variant_unref(result);
197 if (info.storage_id < 0)
198 return STORAGE_ERROR_NO_DEVICE;
200 *path = strdup(info.mount_point);
202 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
204 *storage_id = info.storage_id;
206 return STORAGE_ERROR_NONE;
209 API int storage_get_storage_level(const char *path, char **level)
214 return STORAGE_ERROR_INVALID_PARAMETER;
216 ret = storage_ext_get_storage_level(path, level);
218 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
219 else if (ret == -EINVAL)
220 return STORAGE_ERROR_INVALID_PARAMETER;
222 return STORAGE_ERROR_OPERATION_FAILED;
224 return STORAGE_ERROR_NONE;
227 //LCOV_EXCL_START Not called callback
228 static void mount_mmc_cb(GVariant *var, void *user_data, GError *err)
230 struct mmc_contents *mmc_data = (struct mmc_contents*)user_data;
233 _D("mount_mmc_cb called");
236 _E("no message [%s]", err->message);
241 g_variant_get(var, "(i)", &mmc_ret);
243 _I("Mount State : %d", mmc_ret);
247 g_variant_unref(var);
248 (mmc_data->mmc_cb)(mmc_ret, mmc_data->user_data);
252 API int storage_request_mount_mmc(struct mmc_contents *mmc_data)
254 void (*mount_cb)(GVariant *, void *, GError *) = NULL;
260 if (mmc_data && mmc_data->mmc_cb) {
261 _I("Mount callback exists");
262 mount_cb = mount_mmc_cb;
266 ret = storage_get_primary_sdcard(&id, &path);
267 if (ret != STORAGE_ERROR_NONE)
270 ret = dbus_method_async_with_reply_var(STORAGE_EXT_BUS_NAME,
271 STORAGE_EXT_PATH_MANAGER,
272 STORAGE_EXT_IFACE_MANAGER,
274 g_variant_new("(is)", id, ""),
279 _I("Mount Request %s", ret == 0 ? "Success" : "Failed");
282 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
284 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
286 return STORAGE_ERROR_NONE;
289 //LCOV_EXCL_START Not called callback
290 static void unmount_mmc_cb(GVariant *var, void *user_data, GError *err)
292 struct mmc_contents *mmc_data = (struct mmc_contents*)user_data;
295 _D("unmount_mmc_cb called");
298 _E("no message [%s]", err->message);
303 g_variant_get(var, "(i)", &mmc_ret);
305 _I("Unmount State : %d", mmc_ret);
309 g_variant_unref(var);
310 (mmc_data->mmc_cb)(mmc_ret, mmc_data->user_data);
314 API int storage_request_unmount_mmc(struct mmc_contents *mmc_data, int option)
316 void (*unmount_cb)(GVariant *, void *, GError *) = NULL;
322 if (option < 0 || option > 1)
323 return STORAGE_ERROR_INVALID_PARAMETER;
325 if (mmc_data && mmc_data->mmc_cb) {
326 _I("Unmount callback exists");
327 unmount_cb = unmount_mmc_cb;
331 ret = storage_get_primary_sdcard(&id, &path);
332 if (ret != STORAGE_ERROR_NONE)
335 ret = dbus_method_async_with_reply_var(STORAGE_EXT_BUS_NAME,
336 STORAGE_EXT_PATH_MANAGER,
337 STORAGE_EXT_IFACE_MANAGER,
339 g_variant_new("(ii)", id, option),
344 _I("Unmount Request %s", ret == 0 ? "Success" : "Failed");
347 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
349 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
351 return STORAGE_ERROR_NONE;
354 //LCOV_EXCL_START Not called callback
355 static void format_mmc_cb(GVariant *var, void *user_data, GError *err)
357 struct mmc_contents *mmc_data = (struct mmc_contents*)user_data;
360 _D("format_mmc_cb called");
363 _E("no message [%s]", err->message);
368 g_variant_get(var, "(i)", &mmc_ret);
370 _I("Format State : %d", mmc_ret);
374 g_variant_unref(var);
375 (mmc_data->mmc_cb)(mmc_ret, mmc_data->user_data);
379 API int storage_request_format_mmc(struct mmc_contents *mmc_data)
381 return storage_format_mmc(mmc_data, 1);
384 API int storage_format_mmc(struct mmc_contents *mmc_data, int option)
386 void (*format_cb)(GVariant *, void *, GError *) = NULL;
392 if (option < 0 || option > 1)
393 return STORAGE_ERROR_INVALID_PARAMETER;
395 if (mmc_data && mmc_data->mmc_cb) {
396 _I("Format callback exists");
397 format_cb = format_mmc_cb;
401 ret = storage_get_primary_sdcard(&id, &path);
402 if (ret != STORAGE_ERROR_NONE)
405 ret = dbus_method_async_with_reply_var(STORAGE_EXT_BUS_NAME,
406 STORAGE_EXT_PATH_MANAGER,
407 STORAGE_EXT_IFACE_MANAGER,
409 g_variant_new("(ii)", id, option),
414 _I("Format Request %s", ret == 0 ? "Success" : "Failed");
417 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
419 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
421 return STORAGE_ERROR_NONE;
424 API int storage_is_mounted_opt_usr(storage_part_mount_e *mounted)
426 blkid_cache cache = NULL;
427 blkid_dev_iterate iter;
433 return STORAGE_ERROR_INVALID_PARAMETER;
435 ret = blkid_get_cache(&cache, NULL);
437 _E("Failed to get cache"); //LCOV_EXCL_LINE
438 *mounted = STORAGE_PART_ERROR; //LCOV_EXCL_LINE
439 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
442 ret = blkid_probe_all(cache);
444 _E("Failed to probe all block devices"); //LCOV_EXCL_LINE
445 *mounted = STORAGE_PART_ERROR; //LCOV_EXCL_LINE
446 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
449 iter = blkid_dev_iterate_begin(cache);
451 _E("Failed to get iterate"); //LCOV_EXCL_LINE
452 *mounted = STORAGE_PART_ERROR; //LCOV_EXCL_LINE
453 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
456 ret = blkid_dev_set_search(iter, "LABEL", USER_PARTITION);
457 if (blkid_dev_next(iter, &dev) == 0) {
458 dev = blkid_verify(cache, dev);
461 _D("Partition for user data is found(LABEL=user)");
464 blkid_dev_iterate_end(iter);
467 iter = blkid_dev_iterate_begin(cache);
469 _E("Failed to get iterate"); //LCOV_EXCL_LINE
470 *mounted = STORAGE_PART_ERROR; //LCOV_EXCL_LINE
471 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
474 ret = blkid_dev_set_search(iter, "PARTLABEL", USER_PARTITION);
475 if (blkid_dev_next(iter, &dev) == 0) {
476 dev = blkid_verify(cache, dev);
479 _D("Partition for user data is found(PARTLABEL=user)");
482 blkid_dev_iterate_end(iter);
485 blkid_put_cache(cache);
488 ret = mount_check(tzplatform_getenv(TZ_SYS_USER));
490 *mounted = STORAGE_PART_MOUNTED;
492 *mounted = STORAGE_PART_NOT_MOUNTED;
494 *mounted = STORAGE_PART_NOT_SUPPORTED;
496 return STORAGE_ERROR_NONE;