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 dbus_handle_h dbus_handle = gdbus_get_connection(G_BUS_TYPE_SYSTEM, true);
180 if (dbus_handle == NULL) {
181 _E("Failed to get dbus connection");
182 return STORAGE_ERROR_OPERATION_FAILED;
185 ret_dbus = gdbus_priv_call_sync_with_reply(dbus_handle,
186 STORAGE_EXT_BUS_NAME,
187 STORAGE_EXT_PATH_MANAGER,
188 STORAGE_EXT_IFACE_MANAGER,
193 gdbus_free_connection(dbus_handle);
196 //LCOV_EXCL_START System Error
197 _E("Failed to get primary sdcard partition"); //LCOV_EXCL_LINE
198 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
202 if (!g_variant_get_safe(reply, "(issssssisibii)",
206 NULL, &reply_mount_point,
209 g_variant_unref(reply);
210 return STORAGE_ERROR_OPERATION_FAILED;
213 g_variant_unref(reply);
216 ret = STORAGE_ERROR_NO_DEVICE;
220 *path = strdup(reply_mount_point);
222 ret = STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
226 *storage_id = reply_id;
228 ret = STORAGE_ERROR_NONE;
230 g_free(reply_mount_point);
235 API int storage_get_storage_level(const char *path, char **level)
240 return STORAGE_ERROR_INVALID_PARAMETER;
242 ret_level = storage_ext_get_storage_level(path, level);
243 if (ret_level == -ENOMEM)
244 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
245 else if (ret_level == -EINVAL)
246 return STORAGE_ERROR_INVALID_PARAMETER;
247 else if (ret_level < 0)
248 return STORAGE_ERROR_OPERATION_FAILED;
250 return STORAGE_ERROR_NONE;
253 //LCOV_EXCL_START Not called callback
254 static void mount_mmc_cb(GVariant *var, void *user_data, GError *err)
256 struct mmc_contents *mmc_data = (struct mmc_contents*)user_data;
259 _D("mount_mmc_cb called");
262 _E("no message [%s]", err->message);
267 g_variant_get(var, "(i)", &mmc_ret);
269 _I("Mount State : %d", mmc_ret);
273 g_variant_unref(var);
274 (mmc_data->mmc_cb)(mmc_ret, mmc_data->user_data);
278 API int storage_request_mount_mmc(struct mmc_contents *mmc_data)
280 void (*mount_cb)(GVariant *, void *, GError *) = NULL;
286 if (mmc_data && mmc_data->mmc_cb) {
287 _I("Mount callback exists");
288 mount_cb = mount_mmc_cb;
292 ret_val = storage_get_primary_sdcard(&id, &path);
293 if (ret_val != STORAGE_ERROR_NONE)
295 //LCOV_EXCL_START System Error
300 ret_val = gdbus_call_async_with_reply(STORAGE_EXT_BUS_NAME,
301 STORAGE_EXT_PATH_MANAGER,
302 STORAGE_EXT_IFACE_MANAGER,
304 g_variant_new("(is)", id, ""),
309 _I("Mount Request %s", ret_val == 0 ? "Success" : "Failed");
311 if (ret_val == -ENOMEM)
312 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
314 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
316 return STORAGE_ERROR_NONE;
319 //LCOV_EXCL_START Not called callback
320 static void unmount_mmc_cb(GVariant *var, void *user_data, GError *err)
322 struct mmc_contents *mmc_data = (struct mmc_contents*)user_data;
325 _D("unmount_mmc_cb called");
328 _E("no message [%s]", err->message);
333 g_variant_get(var, "(i)", &mmc_ret);
335 _I("Unmount State : %d", mmc_ret);
339 g_variant_unref(var);
340 (mmc_data->mmc_cb)(mmc_ret, mmc_data->user_data);
344 API int storage_request_unmount_mmc(struct mmc_contents *mmc_data, int option)
346 void (*unmount_cb)(GVariant *, void *, GError *) = NULL;
352 if (option < 0 || option > 1)
353 return STORAGE_ERROR_INVALID_PARAMETER;
355 if (mmc_data && mmc_data->mmc_cb) {
356 _I("Unmount callback exists");
357 unmount_cb = unmount_mmc_cb;
361 ret_val = storage_get_primary_sdcard(&id, &path);
362 if (ret_val != STORAGE_ERROR_NONE)
364 //LCOV_EXCL_START System Error
369 ret_val = gdbus_call_async_with_reply(STORAGE_EXT_BUS_NAME,
370 STORAGE_EXT_PATH_MANAGER,
371 STORAGE_EXT_IFACE_MANAGER,
373 g_variant_new("(ii)", id, option),
378 _I("Unmount Request %s", ret_val == 0 ? "Success" : "Failed");
380 if (ret_val == -ENOMEM)
381 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
383 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
385 return STORAGE_ERROR_NONE;
388 //LCOV_EXCL_START Not called callback
389 static void format_mmc_cb(GVariant *var, void *user_data, GError *err)
391 struct mmc_contents *mmc_data = (struct mmc_contents*)user_data;
394 _D("format_mmc_cb called");
397 _E("no message [%s]", err->message);
402 g_variant_get(var, "(i)", &mmc_ret);
404 _I("Format State : %d", mmc_ret);
408 g_variant_unref(var);
409 (mmc_data->mmc_cb)(mmc_ret, mmc_data->user_data);
413 API int storage_request_format_mmc(struct mmc_contents *mmc_data)
415 return storage_format_mmc(mmc_data, 1);
418 API int storage_format_mmc(struct mmc_contents *mmc_data, int option)
420 void (*format_cb)(GVariant *, void *, GError *) = NULL;
426 if (option < 0 || option > 1)
427 return STORAGE_ERROR_INVALID_PARAMETER;
429 if (mmc_data && mmc_data->mmc_cb) {
430 _I("Format callback exists");
431 format_cb = format_mmc_cb;
435 ret_val = storage_get_primary_sdcard(&id, &path);
436 if (ret_val != STORAGE_ERROR_NONE)
438 //LCOV_EXCL_START System Error
443 ret_val = gdbus_call_async_with_reply(STORAGE_EXT_BUS_NAME,
444 STORAGE_EXT_PATH_MANAGER,
445 STORAGE_EXT_IFACE_MANAGER,
447 g_variant_new("(ii)", id, option),
452 _I("Format Request %s", ret_val == 0 ? "Success" : "Failed");
454 if (ret_val == -ENOMEM)
455 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
457 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
459 return STORAGE_ERROR_NONE;
462 API int storage_is_mounted_opt_usr(storage_part_mount_e *mounted)
464 blkid_cache cache = NULL;
465 blkid_dev_iterate iter;
469 char *user_label = libsys_is_container()? CONTAINER_USER_PARTITION: USER_PARTITION;
472 return STORAGE_ERROR_INVALID_PARAMETER;
474 _D("Find user partition label: %s", user_label);
475 ret_val = blkid_get_cache(&cache, NULL);
477 _E("Failed to get cache"); //LCOV_EXCL_LINE
478 *mounted = STORAGE_PART_ERROR; //LCOV_EXCL_LINE
479 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
482 ret_val = blkid_probe_all(cache);
484 _E("Failed to probe all block devices"); //LCOV_EXCL_LINE
485 *mounted = STORAGE_PART_ERROR; //LCOV_EXCL_LINE
486 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
489 iter = blkid_dev_iterate_begin(cache);
491 _E("Failed to get iterate"); //LCOV_EXCL_LINE
492 *mounted = STORAGE_PART_ERROR; //LCOV_EXCL_LINE
493 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
496 ret_val = blkid_dev_set_search(iter, "LABEL", user_label);
497 if (blkid_dev_next(iter, &dev) == 0) {
498 dev = blkid_verify(cache, dev);
501 _D("Partition for user data is found(LABEL=%s)", user_label);
504 blkid_dev_iterate_end(iter);
507 iter = blkid_dev_iterate_begin(cache);
509 _E("Failed to get iterate"); //LCOV_EXCL_LINE
510 *mounted = STORAGE_PART_ERROR; //LCOV_EXCL_LINE
511 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
514 ret_val = blkid_dev_set_search(iter, "PARTLABEL", user_label);
515 if (blkid_dev_next(iter, &dev) == 0) {
516 dev = blkid_verify(cache, dev);
519 _D("Partition for user data is found(PARTLABEL=%s)", user_label);
522 blkid_dev_iterate_end(iter);
525 blkid_put_cache(cache);
528 ret_val = mount_check(tzplatform_getenv(TZ_SYS_USER));
530 *mounted = STORAGE_PART_MOUNTED;
532 *mounted = STORAGE_PART_NOT_MOUNTED;
534 *mounted = STORAGE_PART_NOT_SUPPORTED;
536 return STORAGE_ERROR_NONE;