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>
29 #include "storage-internal.h"
30 #include "storage-external-dbus.h"
31 #include "storage-experimental.h"
34 Get compat path from origin Multi-user path
35 from TZ_USER_CONTENT/.. to /opt/usr/media/..
36 Input should be normalized path like /opt/usr/home/owner/media (TODO: internal normalization)
38 Why this API should be provided?
39 In multi-user environment, each user has own compat content direcotry.(/opt/usr/media)
40 However, although system daemon operates real path,
41 system daemon needs to provide compat path to App if the real path is converted.
44 #include <storage-internal.h>
47 if(storage_get_compat_internal_path(src, sizeof(dest), dest) < 0)
48 // cannot convert. use src path
50 // can convert. use dest path
52 //LCOV_EXCL_START Untested function
53 API int storage_get_compat_internal_path(const char* origin, int len, char* compat)
59 if (!compat || !origin) {
60 _E("Invalid parameter");
64 if (getuid() <= USER_UID_START) {
65 //LCOV_EXCL_START System Error
66 _E("Only apps and user session daemons are allowed "
67 "to use storage_get_compat_internal_path()");
72 // this API works on place where compat path is bind-mounted
73 if (!is_compat_bind_mount()) {
74 //LCOV_EXCL_START System Error
75 _E("No compat bind mount");
80 str = tzplatform_uid_getenv(getuid(), TZ_USER_CONTENT);
81 str_len = strlen(str);
82 if (strncmp(origin, str, str_len) != 0) {
83 _E("Failed to match TZ_USER_CONTENT");
87 r = snprintf(compat, len, "%s%s", COMPAT_DIR, origin + str_len);
89 //LCOV_EXCL_START System Error
90 _E("Failed to create new path");
100 Get Multi-user path from compat path
101 from /opt/usr/media/.. to TZ_USER_CONTENT/..
102 Input should be normalized path like /opt/usr/media (TODO: internal normalization)
104 Why this API should be provided?
105 In multi-user environment, each user has own compat content direcotry.(/opt/usr/media)
106 However, although some APIs send the compat path to system daemon,
107 system daemon should access real path.
110 #include <storage-internal.h>
113 if(storage_get_origin_internal_path(src, sizeof(dest), dest) < 0)
114 // cannot convert. use src path
116 // can convert. use dest path
118 API int storage_get_origin_internal_path(const char* compat, int len, char* origin)
123 if (!compat || !origin) {
124 _E("Invalid parameter");
128 if (getuid() <= USER_UID_START) {
129 //LCOV_EXCL_START System Error
130 _E("Only apps and user session daemons are allowed "
131 "to use storage_get_origin_internal_path()");
136 // this API works on place where compat path is bind-mounted
137 if (!is_compat_bind_mount()) {
138 //LCOV_EXCL_START System Error
139 _E("no compat bind mount");
144 compat_len = strlen(COMPAT_DIR);
145 if (strncmp(compat, COMPAT_DIR, compat_len) != 0) {
146 _E("failed to match COMPAT_DIR");
150 r = snprintf(origin, len, "%s%s", tzplatform_uid_getenv(getuid(), TZ_USER_CONTENT), compat + compat_len);
152 //LCOV_EXCL_START System Error
153 _E("failed to create new path");
161 API int storage_get_primary_sdcard(int *storage_id, char **path)
164 storage_ext_device info;
166 if (!storage_id || !path)
167 return STORAGE_ERROR_INVALID_PARAMETER;
169 if (!storage_ext_is_supported())
170 return STORAGE_ERROR_NO_DEVICE;
172 result = dbus_method_call_sync(STORAGE_EXT_BUS_NAME,
173 STORAGE_EXT_PATH_MANAGER,
174 STORAGE_EXT_IFACE_MANAGER,
178 //LCOV_EXCL_START System Error
179 _E("Failed to get primary sdcard partition"); //LCOV_EXCL_LINE
180 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
184 g_variant_get(result, "(issssssisibii)",
185 &info.type, &info.devnode, &info.syspath,
186 &info.fs_usage, &info.fs_type,
187 &info.fs_version, &info.fs_uuid,
188 &info.readonly, &info.mount_point,
189 &info.state, &info.primary,
190 &info.flags, &info.storage_id);
192 g_variant_unref(result);
194 if (info.storage_id < 0)
195 return STORAGE_ERROR_NO_DEVICE;
197 *path = strdup(info.mount_point);
199 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
201 *storage_id = info.storage_id;
203 return STORAGE_ERROR_NONE;
206 API int storage_get_type_dev(int storage_id, storage_type_e *type, storage_dev_e *dev)
208 storage_ext_device *ext_dev;
211 if (storage_id < 0) {
212 _E("Invalid parameger");
213 return STORAGE_ERROR_NO_DEVICE;
217 _E("Invalid parameger");
218 return STORAGE_ERROR_INVALID_PARAMETER;
222 _E("Invalid parameger");
223 return STORAGE_ERROR_INVALID_PARAMETER;
226 ret = storage_get_type(storage_id, type);
227 if (ret != STORAGE_ERROR_NONE) {
228 _E("Failed to get storage type: %d", ret);
231 if (*type == STORAGE_TYPE_INTERNAL)
232 return STORAGE_ERROR_NONE;
234 ext_dev = calloc(1, sizeof(storage_ext_device));
236 //LCOV_EXCL_START System Error
238 return STORAGE_ERROR_OUT_OF_MEMORY;
242 ret = storage_ext_get_device_info(storage_id, ext_dev);
244 _E("Cannot get the storage with id (%d, ret:%d)", storage_id, ret); //LCOV_EXCL_LINE
245 ret = STORAGE_ERROR_NO_DEVICE;
249 if (ext_dev->type == STORAGE_EXT_SCSI)
250 *dev = STORAGE_DEV_EXT_USB_MASS_STORAGE;
251 else if (ext_dev->type == STORAGE_EXT_MMC)
252 *dev = STORAGE_DEV_EXT_SDCARD;
253 ret = STORAGE_ERROR_NONE;
254 _I("type: %d(internal:0, external:1) dev: %d(sdcard: 1001, usb: 1002)", *type, *dev);
257 storage_ext_release_device(&ext_dev);