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>
26 #include <libsyscommon/libgdbus.h>
27 #include <libsyscommon/common.h>
31 #include "storage-internal.h"
32 #include "storage-external-dbus.h"
34 #define FORMAT_TIMEOUT (120*1000)
35 #define USER_PARTITION "user"
36 #define CONTAINER_USER_PARTITION "contain-user"
39 Get compat path from origin Multi-user path
40 from TZ_USER_CONTENT/.. to /opt/usr/media/..
41 Input should be normalized path like /opt/usr/home/owner/media (TODO: internal normalization)
43 Why this API should be provided?
44 In multi-user environment, each user has own compat content direcotry.(/opt/usr/media)
45 However, although system daemon operates real path,
46 system daemon needs to provide compat path to App if the real path is converted.
49 #include <storage-internal.h>
52 if(storage_get_compat_internal_path(src, sizeof(dest), dest) < 0)
53 // cannot convert. use src path
55 // can convert. use dest path
57 //LCOV_EXCL_START Untested function
58 API int storage_get_compat_internal_path(const char* origin, int len, char* compat)
64 if (!compat || !origin) {
65 _E("Invalid parameter");
69 if (getuid() <= USER_UID_START) {
70 //LCOV_EXCL_START System Error
71 _E("Only apps and user session daemons are allowed "
72 "to use storage_get_compat_internal_path()");
77 // this API works on place where compat path is bind-mounted
78 if (!is_compat_bind_mount()) {
79 //LCOV_EXCL_START System Error
80 _E("No compat bind mount");
85 str = tzplatform_uid_getenv(getuid(), TZ_USER_CONTENT);
86 str_len = strlen(str);
87 if (strncmp(origin, str, str_len) != 0) {
88 _E("Failed to match TZ_USER_CONTENT");
92 r = snprintf(compat, len, "%s%s", COMPAT_DIR, origin + str_len);
94 //LCOV_EXCL_START System Error
95 _E("Failed to create new path");
105 Get Multi-user path from compat path
106 from /opt/usr/media/.. to TZ_USER_CONTENT/..
107 Input should be normalized path like /opt/usr/media (TODO: internal normalization)
109 Why this API should be provided?
110 In multi-user environment, each user has own compat content direcotry.(/opt/usr/media)
111 However, although some APIs send the compat path to system daemon,
112 system daemon should access real path.
115 #include <storage-internal.h>
118 if(storage_get_origin_internal_path(src, sizeof(dest), dest) < 0)
119 // cannot convert. use src path
121 // can convert. use dest path
123 API int storage_get_origin_internal_path(const char* compat, int len, char* origin)
128 if (!compat || !origin) {
129 _E("Invalid parameter");
133 if (getuid() <= USER_UID_START) {
134 //LCOV_EXCL_START System Error
135 _E("Only apps and user session daemons are allowed "
136 "to use storage_get_origin_internal_path()");
141 // this API works on place where compat path is bind-mounted
142 if (!is_compat_bind_mount()) {
143 //LCOV_EXCL_START System Error
144 _E("no compat bind mount");
149 compat_len = strlen(COMPAT_DIR);
150 if (strncmp(compat, COMPAT_DIR, compat_len) != 0) {
151 _E("failed to match COMPAT_DIR");
155 r = snprintf(origin, len, "%s%s", tzplatform_uid_getenv(getuid(), TZ_USER_CONTENT), compat + compat_len);
157 //LCOV_EXCL_START System Error
158 _E("failed to create new path");
166 API int storage_get_primary_sdcard(int *storage_id, char **path)
170 char *reply_mount_point = NULL;
173 if (!storage_id || !path)
174 return STORAGE_ERROR_INVALID_PARAMETER;
176 if (!storage_ext_is_supported())
177 return STORAGE_ERROR_NOT_SUPPORTED;
179 ret_dbus = gdbus_call_sync_with_reply(STORAGE_EXT_BUS_NAME,
180 STORAGE_EXT_PATH_MANAGER,
181 STORAGE_EXT_IFACE_MANAGER,
186 //LCOV_EXCL_START System Error
187 _E("Failed to get primary sdcard partition"); //LCOV_EXCL_LINE
188 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
192 if (!g_variant_get_safe(reply, "(issssssisibii)",
196 NULL, &reply_mount_point,
199 g_variant_unref(reply);
200 return STORAGE_ERROR_OPERATION_FAILED;
203 g_variant_unref(reply);
206 ret = STORAGE_ERROR_NO_DEVICE;
210 *path = strdup(reply_mount_point);
212 ret = STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
216 *storage_id = reply_id;
218 ret = STORAGE_ERROR_NONE;
220 g_free(reply_mount_point);
225 API int storage_get_storage_level(const char *path, char **level)
230 return STORAGE_ERROR_INVALID_PARAMETER;
232 ret_level = storage_ext_get_storage_level(path, level);
233 if (ret_level == -ENOMEM)
234 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
235 else if (ret_level == -EINVAL)
236 return STORAGE_ERROR_INVALID_PARAMETER;
237 else if (ret_level < 0)
238 return STORAGE_ERROR_OPERATION_FAILED;
240 return STORAGE_ERROR_NONE;
243 //LCOV_EXCL_START Not called callback
244 static void mount_mmc_cb(GVariant *var, void *user_data, GError *err)
246 struct mmc_contents *mmc_data = (struct mmc_contents*)user_data;
249 _D("mount_mmc_cb called");
252 _E("no message [%s]", err->message);
257 g_variant_get(var, "(i)", &mmc_ret);
259 _I("Mount State : %d", mmc_ret);
263 g_variant_unref(var);
264 (mmc_data->mmc_cb)(mmc_ret, mmc_data->user_data);
268 API int storage_request_mount_mmc(struct mmc_contents *mmc_data)
270 void (*mount_cb)(GVariant *, void *, GError *) = NULL;
276 if (mmc_data && mmc_data->mmc_cb) {
277 _I("Mount callback exists");
278 mount_cb = mount_mmc_cb;
282 ret_val = storage_get_primary_sdcard(&id, &path);
283 if (ret_val != STORAGE_ERROR_NONE)
285 //LCOV_EXCL_START System Error
290 ret_val = gdbus_call_async_with_reply(STORAGE_EXT_BUS_NAME,
291 STORAGE_EXT_PATH_MANAGER,
292 STORAGE_EXT_IFACE_MANAGER,
294 g_variant_new("(is)", id, ""),
299 _I("Mount Request %s", ret_val == 0 ? "Success" : "Failed");
301 if (ret_val == -ENOMEM)
302 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
304 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
306 return STORAGE_ERROR_NONE;
309 //LCOV_EXCL_START Not called callback
310 static void unmount_mmc_cb(GVariant *var, void *user_data, GError *err)
312 struct mmc_contents *mmc_data = (struct mmc_contents*)user_data;
315 _D("unmount_mmc_cb called");
318 _E("no message [%s]", err->message);
323 g_variant_get(var, "(i)", &mmc_ret);
325 _I("Unmount State : %d", mmc_ret);
329 g_variant_unref(var);
330 (mmc_data->mmc_cb)(mmc_ret, mmc_data->user_data);
334 API int storage_request_unmount_mmc(struct mmc_contents *mmc_data, int option)
336 void (*unmount_cb)(GVariant *, void *, GError *) = NULL;
342 if (option < 0 || option > 1)
343 return STORAGE_ERROR_INVALID_PARAMETER;
345 if (mmc_data && mmc_data->mmc_cb) {
346 _I("Unmount callback exists");
347 unmount_cb = unmount_mmc_cb;
351 ret_val = storage_get_primary_sdcard(&id, &path);
352 if (ret_val != STORAGE_ERROR_NONE)
354 //LCOV_EXCL_START System Error
359 ret_val = gdbus_call_async_with_reply(STORAGE_EXT_BUS_NAME,
360 STORAGE_EXT_PATH_MANAGER,
361 STORAGE_EXT_IFACE_MANAGER,
363 g_variant_new("(ii)", id, option),
368 _I("Unmount Request %s", ret_val == 0 ? "Success" : "Failed");
370 if (ret_val == -ENOMEM)
371 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
373 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
375 return STORAGE_ERROR_NONE;
378 //LCOV_EXCL_START Not called callback
379 static void format_mmc_cb(GVariant *var, void *user_data, GError *err)
381 struct mmc_contents *mmc_data = (struct mmc_contents*)user_data;
384 _D("format_mmc_cb called");
387 _E("no message [%s]", err->message);
392 g_variant_get(var, "(i)", &mmc_ret);
394 _I("Format State : %d", mmc_ret);
398 g_variant_unref(var);
399 (mmc_data->mmc_cb)(mmc_ret, mmc_data->user_data);
403 API int storage_request_format_mmc(struct mmc_contents *mmc_data)
405 return storage_format_mmc(mmc_data, 1);
408 API int storage_format_mmc(struct mmc_contents *mmc_data, int option)
410 void (*format_cb)(GVariant *, void *, GError *) = NULL;
416 if (option < 0 || option > 1)
417 return STORAGE_ERROR_INVALID_PARAMETER;
419 if (mmc_data && mmc_data->mmc_cb) {
420 _I("Format callback exists");
421 format_cb = format_mmc_cb;
425 ret_val = storage_get_primary_sdcard(&id, &path);
426 if (ret_val != STORAGE_ERROR_NONE)
428 //LCOV_EXCL_START System Error
433 ret_val = gdbus_call_async_with_reply(STORAGE_EXT_BUS_NAME,
434 STORAGE_EXT_PATH_MANAGER,
435 STORAGE_EXT_IFACE_MANAGER,
437 g_variant_new("(ii)", id, option),
442 _I("Format Request %s", ret_val == 0 ? "Success" : "Failed");
444 if (ret_val == -ENOMEM)
445 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
447 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
449 return STORAGE_ERROR_NONE;
452 API int storage_is_mounted_opt_usr(storage_part_mount_e *mounted)
454 blkid_cache cache = NULL;
455 blkid_dev_iterate iter;
459 char *user_label = is_container()? CONTAINER_USER_PARTITION: USER_PARTITION;
462 return STORAGE_ERROR_INVALID_PARAMETER;
464 _D("Find user partition label: %s", user_label);
465 ret_val = blkid_get_cache(&cache, NULL);
467 _E("Failed to get cache"); //LCOV_EXCL_LINE
468 *mounted = STORAGE_PART_ERROR; //LCOV_EXCL_LINE
469 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
472 ret_val = blkid_probe_all(cache);
474 _E("Failed to probe all block devices"); //LCOV_EXCL_LINE
475 *mounted = STORAGE_PART_ERROR; //LCOV_EXCL_LINE
476 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
479 iter = blkid_dev_iterate_begin(cache);
481 _E("Failed to get iterate"); //LCOV_EXCL_LINE
482 *mounted = STORAGE_PART_ERROR; //LCOV_EXCL_LINE
483 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
486 ret_val = blkid_dev_set_search(iter, "LABEL", user_label);
487 if (blkid_dev_next(iter, &dev) == 0) {
488 dev = blkid_verify(cache, dev);
491 _D("Partition for user data is found(LABEL=%s)", user_label);
494 blkid_dev_iterate_end(iter);
497 iter = blkid_dev_iterate_begin(cache);
499 _E("Failed to get iterate"); //LCOV_EXCL_LINE
500 *mounted = STORAGE_PART_ERROR; //LCOV_EXCL_LINE
501 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
504 ret_val = blkid_dev_set_search(iter, "PARTLABEL", user_label);
505 if (blkid_dev_next(iter, &dev) == 0) {
506 dev = blkid_verify(cache, dev);
509 _D("Partition for user data is found(PARTLABEL=%s)", user_label);
512 blkid_dev_iterate_end(iter);
515 blkid_put_cache(cache);
518 ret_val = mount_check(tzplatform_getenv(TZ_SYS_USER));
520 *mounted = STORAGE_PART_MOUNTED;
522 *mounted = STORAGE_PART_NOT_MOUNTED;
524 *mounted = STORAGE_PART_NOT_SUPPORTED;
526 return STORAGE_ERROR_NONE;