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 //LCOV_EXCL_START Untested function
51 API int storage_get_compat_internal_path(const char* origin, int len, char* compat)
57 if (!compat || !origin) {
58 _E("Invalid parameter");
62 // this API works on place where compat path is bind-mounted
63 if (!is_compat_bind_mount()) {
64 //LCOV_EXCL_START System Error
65 _E("No compat bind mount");
70 str = tzplatform_getenv(TZ_USER_CONTENT);
71 str_len = strlen(str);
72 if (strncmp(origin, str, str_len) != 0) {
73 _E("Failed to match TZ_USER_CONTENT");
77 r = snprintf(compat, len, "%s%s", COMPAT_DIR, origin + str_len);
79 //LCOV_EXCL_START System Error
80 _E("Failed to create new path");
90 Get Multi-user path from compat path
91 from /opt/usr/media/.. to TZ_USER_CONTENT/..
92 Input should be normalized path like /opt/usr/media (TODO: internal normalization)
94 Why this API should be provided?
95 In multi-user environment, each user has own compat content direcotry.(/opt/usr/media)
96 However, although some APIs send the compat path to system daemon,
97 system daemon should access real path.
100 #include <storage-internal.h>
103 if(storage_get_origin_internal_path(src, sizeof(dest), dest) < 0)
104 // cannot convert. use src path
106 // can convert. use dest path
108 API int storage_get_origin_internal_path(const char* compat, int len, char* origin)
113 if (!compat || !origin) {
114 _E("Invalid parameter");
118 // this API works on place where compat path is bind-mounted
119 if (!is_compat_bind_mount()) {
120 //LCOV_EXCL_START System Error
121 _E("no compat bind mount");
126 compat_len = strlen(COMPAT_DIR);
127 if (strncmp(compat, COMPAT_DIR, compat_len) != 0) {
128 _E("failed to match COMPAT_DIR");
132 r = snprintf(origin, len, "%s%s", tzplatform_getenv(TZ_USER_CONTENT), compat + compat_len);
134 //LCOV_EXCL_START System Error
135 _E("failed to create new path");
143 API int storage_get_primary_sdcard(int *storage_id, char **path)
146 storage_ext_device info;
148 if (!storage_id || !path)
149 return STORAGE_ERROR_INVALID_PARAMETER;
151 result = dbus_method_call_sync(STORAGE_EXT_BUS_NAME,
152 STORAGE_EXT_PATH_MANAGER,
153 STORAGE_EXT_IFACE_MANAGER,
157 //LCOV_EXCL_START System Error
158 _E("Failed to get primary sdcard partition"); //LCOV_EXCL_LINE
159 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
163 g_variant_get(result, "(issssssisibii)",
164 &info.type, &info.devnode, &info.syspath,
165 &info.fs_usage, &info.fs_type,
166 &info.fs_version, &info.fs_uuid,
167 &info.readonly, &info.mount_point,
168 &info.state, &info.primary,
169 &info.flags, &info.storage_id);
171 g_variant_unref(result);
173 if (info.storage_id < 0)
174 return STORAGE_ERROR_NO_DEVICE;
176 *path = strdup(info.mount_point);
178 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
180 *storage_id = info.storage_id;
182 return STORAGE_ERROR_NONE;
185 API int storage_get_type_dev(int storage_id, storage_type_e *type, storage_dev_e *dev)
187 storage_ext_device *ext_dev;
190 if (storage_id < 0) {
191 _E("Invalid parameger");
192 return STORAGE_ERROR_NO_DEVICE;
196 _E("Invalid parameger");
197 return STORAGE_ERROR_INVALID_PARAMETER;
201 _E("Invalid parameger");
202 return STORAGE_ERROR_INVALID_PARAMETER;
205 ret = storage_get_type(storage_id, type);
206 if (ret != STORAGE_ERROR_NONE) {
207 _E("Failed to get storage type: %d", ret);
210 if (*type == STORAGE_TYPE_INTERNAL)
211 return STORAGE_ERROR_NONE;
213 ext_dev = calloc(1, sizeof(storage_ext_device));
215 //LCOV_EXCL_START System Error
217 return STORAGE_ERROR_OUT_OF_MEMORY;
221 ret = storage_ext_get_device_info(storage_id, ext_dev);
223 _E("Cannot get the storage with id (%d, ret:%d)", storage_id, ret); //LCOV_EXCL_LINE
224 ret = STORAGE_ERROR_NO_DEVICE;
228 if (ext_dev->type == STORAGE_EXT_SCSI)
229 *dev = STORAGE_DEV_EXT_USB_MASS_STORAGE;
230 else if (ext_dev->type == STORAGE_EXT_MMC)
231 *dev = STORAGE_DEV_EXT_SDCARD;
232 ret = STORAGE_ERROR_NONE;
233 _I("type: %d(internal:0, external:1) dev: %d(sdcard: 1001, usb: 1002)", *type, *dev);
236 storage_ext_release_device(&ext_dev);