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"
29 #include "storage-experimental.h"
32 Get compat path from origin Multi-user path
33 from TZ_USER_CONTENT/.. to /opt/usr/media/..
34 Input should be normalized path like /opt/usr/home/owner/media (TODO: internal normalization)
36 Why this API should be provided?
37 In multi-user environment, each user has own compat content direcotry.(/opt/usr/media)
38 However, although system daemon operates real path,
39 system daemon needs to provide compat path to App if the real path is converted.
42 #include <storage-internal.h>
45 if(storage_get_compat_internal_path(src, sizeof(dest), dest) < 0)
46 // cannot convert. use src path
48 // can convert. use dest path
50 API int storage_get_compat_internal_path(const char* origin, int len, char* compat)
56 if (!compat || !origin) {
57 _E("Invalid parameter");
61 // this API works on place where compat path is bind-mounted
62 if (!is_compat_bind_mount()) {
63 _E("No compat bind mount");
67 str = tzplatform_getenv(TZ_USER_CONTENT);
68 str_len = strlen(str);
69 if (strncmp(origin, str, str_len) != 0) {
70 _E("Failed to match TZ_USER_CONTENT");
74 r = snprintf(compat, len, "%s%s", COMPAT_DIR, origin + str_len);
76 _E("Failed to create new path");
84 Get Multi-user path from compat path
85 from /opt/usr/media/.. to TZ_USER_CONTENT/..
86 Input should be normalized path like /opt/usr/media (TODO: internal normalization)
88 Why this API should be provided?
89 In multi-user environment, each user has own compat content direcotry.(/opt/usr/media)
90 However, although some APIs send the compat path to system daemon,
91 system daemon should access real path.
94 #include <storage-internal.h>
97 if(storage_get_origin_internal_path(src, sizeof(dest), dest) < 0)
98 // cannot convert. use src path
100 // can convert. use dest path
102 API int storage_get_origin_internal_path(const char* compat, int len, char* origin)
107 if (!compat || !origin) {
108 _E("Invalid parameter");
112 // this API works on place where compat path is bind-mounted
113 if (!is_compat_bind_mount()) {
114 _E("no compat bind mount");
118 compat_len = strlen(COMPAT_DIR);
119 if (strncmp(compat, COMPAT_DIR, compat_len) != 0) {
120 _E("failed to match COMPAT_DIR");
124 r = snprintf(origin, len, "%s%s", tzplatform_getenv(TZ_USER_CONTENT), compat + compat_len);
126 _E("failed to create new path");
133 API int storage_get_primary_sdcard(int *storage_id, char **path)
136 storage_ext_device info;
138 if (!storage_id || !path)
139 return STORAGE_ERROR_INVALID_PARAMETER;
141 result = dbus_method_call_sync(STORAGE_EXT_BUS_NAME,
142 STORAGE_EXT_PATH_MANAGER,
143 STORAGE_EXT_IFACE_MANAGER,
147 _E("Failed to get primary sdcard partition"); //LCOV_EXCL_LINE
148 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
151 g_variant_get(result, "(issssssisibii)",
152 &info.type, &info.devnode, &info.syspath,
153 &info.fs_usage, &info.fs_type,
154 &info.fs_version, &info.fs_uuid,
155 &info.readonly, &info.mount_point,
156 &info.state, &info.primary,
157 &info.flags, &info.storage_id);
159 g_variant_unref(result);
161 if (info.storage_id < 0)
162 return STORAGE_ERROR_NO_DEVICE;
164 *path = strdup(info.mount_point);
166 return STORAGE_ERROR_OUT_OF_MEMORY;
168 *storage_id = info.storage_id;
170 return STORAGE_ERROR_NONE;
173 API int storage_get_type_dev(int storage_id, storage_type_e *type, storage_dev_e *dev)
175 storage_ext_device *ext_dev;
178 if (storage_id < 0) {
179 _E("Invalid parameger");
180 return STORAGE_ERROR_NOT_SUPPORTED;
184 _E("Invalid parameger");
185 return STORAGE_ERROR_INVALID_PARAMETER;
189 _E("Invalid parameger");
190 return STORAGE_ERROR_INVALID_PARAMETER;
193 ret = storage_get_type(storage_id, type);
194 if (ret != STORAGE_ERROR_NONE) {
195 _E("Failed to get storage type: %d", ret);
198 if (*type == STORAGE_TYPE_INTERNAL)
199 return STORAGE_ERROR_NONE;
201 ext_dev = calloc(1, sizeof(storage_ext_device));
203 //LCOV_EXCL_START System Error
205 return STORAGE_ERROR_OUT_OF_MEMORY;
209 ret = storage_ext_get_device_info(storage_id, ext_dev);
211 _E("Cannot get the storage with id (%d, ret:%d)", storage_id, ret); //LCOV_EXCL_LINE
212 ret = STORAGE_ERROR_NOT_SUPPORTED;
216 if (ext_dev->type == STORAGE_EXT_SCSI)
217 *dev = STORAGE_DEV_EXT_USB_MASS_STORAGE;
218 else if (ext_dev->type == STORAGE_EXT_MMC)
219 *dev = STORAGE_DEV_EXT_SDCARD;
220 ret = STORAGE_ERROR_NONE;
221 _I("type: %d(internal:0, external:1) dev: %d(sdcard: 1001, usb: 1002)", *type, *dev);
224 storage_ext_release_device(&ext_dev);