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"
32 #define FORMAT_TIMEOUT (120*1000)
35 Get compat path from origin Multi-user path
36 from TZ_USER_CONTENT/.. to /opt/usr/media/..
37 Input should be normalized path like /opt/usr/home/owner/media (TODO: internal normalization)
39 Why this API should be provided?
40 In multi-user environment, each user has own compat content direcotry.(/opt/usr/media)
41 However, although system daemon operates real path,
42 system daemon needs to provide compat path to App if the real path is converted.
45 #include <storage-internal.h>
48 if(storage_get_compat_internal_path(src, sizeof(dest), dest) < 0)
49 // cannot convert. use src path
51 // can convert. use dest path
53 //LCOV_EXCL_START Untested function
54 API int storage_get_compat_internal_path(const char* origin, int len, char* compat)
60 if (!compat || !origin) {
61 _E("Invalid parameter");
65 if (getuid() <= USER_UID_START) {
66 //LCOV_EXCL_START System Error
67 _E("Only apps and user session daemons are allowed "
68 "to use storage_get_compat_internal_path()");
73 // this API works on place where compat path is bind-mounted
74 if (!is_compat_bind_mount()) {
75 //LCOV_EXCL_START System Error
76 _E("No compat bind mount");
81 str = tzplatform_uid_getenv(getuid(), TZ_USER_CONTENT);
82 str_len = strlen(str);
83 if (strncmp(origin, str, str_len) != 0) {
84 _E("Failed to match TZ_USER_CONTENT");
88 r = snprintf(compat, len, "%s%s", COMPAT_DIR, origin + str_len);
90 //LCOV_EXCL_START System Error
91 _E("Failed to create new path");
101 Get Multi-user path from compat path
102 from /opt/usr/media/.. to TZ_USER_CONTENT/..
103 Input should be normalized path like /opt/usr/media (TODO: internal normalization)
105 Why this API should be provided?
106 In multi-user environment, each user has own compat content direcotry.(/opt/usr/media)
107 However, although some APIs send the compat path to system daemon,
108 system daemon should access real path.
111 #include <storage-internal.h>
114 if(storage_get_origin_internal_path(src, sizeof(dest), dest) < 0)
115 // cannot convert. use src path
117 // can convert. use dest path
119 API int storage_get_origin_internal_path(const char* compat, int len, char* origin)
124 if (!compat || !origin) {
125 _E("Invalid parameter");
129 if (getuid() <= USER_UID_START) {
130 //LCOV_EXCL_START System Error
131 _E("Only apps and user session daemons are allowed "
132 "to use storage_get_origin_internal_path()");
137 // this API works on place where compat path is bind-mounted
138 if (!is_compat_bind_mount()) {
139 //LCOV_EXCL_START System Error
140 _E("no compat bind mount");
145 compat_len = strlen(COMPAT_DIR);
146 if (strncmp(compat, COMPAT_DIR, compat_len) != 0) {
147 _E("failed to match COMPAT_DIR");
151 r = snprintf(origin, len, "%s%s", tzplatform_uid_getenv(getuid(), TZ_USER_CONTENT), compat + compat_len);
153 //LCOV_EXCL_START System Error
154 _E("failed to create new path");
162 API int storage_get_primary_sdcard(int *storage_id, char **path)
165 storage_ext_device info;
167 if (!storage_id || !path)
168 return STORAGE_ERROR_INVALID_PARAMETER;
170 if (!storage_ext_is_supported())
171 return STORAGE_ERROR_NO_DEVICE;
173 result = dbus_method_call_sync(STORAGE_EXT_BUS_NAME,
174 STORAGE_EXT_PATH_MANAGER,
175 STORAGE_EXT_IFACE_MANAGER,
179 //LCOV_EXCL_START System Error
180 _E("Failed to get primary sdcard partition"); //LCOV_EXCL_LINE
181 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
185 g_variant_get(result, "(issssssisibii)",
186 &info.type, &info.devnode, &info.syspath,
187 &info.fs_usage, &info.fs_type,
188 &info.fs_version, &info.fs_uuid,
189 &info.readonly, &info.mount_point,
190 &info.state, &info.primary,
191 &info.flags, &info.storage_id);
193 g_variant_unref(result);
195 if (info.storage_id < 0)
196 return STORAGE_ERROR_NO_DEVICE;
198 *path = strdup(info.mount_point);
200 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
202 *storage_id = info.storage_id;
204 return STORAGE_ERROR_NONE;
207 API int storage_get_storage_level(const char *path, char **level)
212 return STORAGE_ERROR_INVALID_PARAMETER;
214 ret = storage_ext_get_storage_level(path, level);
216 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
217 else if (ret == -EINVAL)
218 return STORAGE_ERROR_INVALID_PARAMETER;
220 return STORAGE_ERROR_OPERATION_FAILED;
222 return STORAGE_ERROR_NONE;
225 //LCOV_EXCL_START Not called callback
226 static void mount_mmc_cb(GVariant *var, void *user_data, GError *err)
228 struct mmc_contents *mmc_data = (struct mmc_contents*)user_data;
231 _D("mount_mmc_cb called");
234 _E("no message [%s]", err->message);
239 g_variant_get(var, "(i)", &mmc_ret);
241 _I("Mount State : %d", mmc_ret);
245 g_variant_unref(var);
246 (mmc_data->mmc_cb)(mmc_ret, mmc_data->user_data);
250 API int storage_request_mount_mmc(struct mmc_contents *mmc_data)
252 void (*mount_cb)(GVariant *, void *, GError *) = NULL;
258 if (mmc_data && mmc_data->mmc_cb) {
259 _I("Mount callback exists");
260 mount_cb = mount_mmc_cb;
264 ret = storage_get_primary_sdcard(&id, &path);
265 if (ret != STORAGE_ERROR_NONE)
268 ret = dbus_method_async_with_reply_var(STORAGE_EXT_BUS_NAME,
269 STORAGE_EXT_PATH_MANAGER,
270 STORAGE_EXT_IFACE_MANAGER,
272 g_variant_new("(is)", id, ""),
277 _I("Mount Request %s", ret == 0 ? "Success" : "Failed");
280 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
282 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
284 return STORAGE_ERROR_NONE;
287 //LCOV_EXCL_START Not called callback
288 static void unmount_mmc_cb(GVariant *var, void *user_data, GError *err)
290 struct mmc_contents *mmc_data = (struct mmc_contents*)user_data;
293 _D("unmount_mmc_cb called");
296 _E("no message [%s]", err->message);
301 g_variant_get(var, "(i)", &mmc_ret);
303 _I("Unmount State : %d", mmc_ret);
307 g_variant_unref(var);
308 (mmc_data->mmc_cb)(mmc_ret, mmc_data->user_data);
312 API int storage_request_unmount_mmc(struct mmc_contents *mmc_data, int option)
314 void (*unmount_cb)(GVariant *, void *, GError *) = NULL;
320 if (option < 0 || option > 1)
321 return STORAGE_ERROR_INVALID_PARAMETER;
323 if (mmc_data && mmc_data->mmc_cb) {
324 _I("Unmount callback exists");
325 unmount_cb = unmount_mmc_cb;
329 ret = storage_get_primary_sdcard(&id, &path);
330 if (ret != STORAGE_ERROR_NONE)
333 ret = dbus_method_async_with_reply_var(STORAGE_EXT_BUS_NAME,
334 STORAGE_EXT_PATH_MANAGER,
335 STORAGE_EXT_IFACE_MANAGER,
337 g_variant_new("(ii)", id, option),
342 _I("Unmount Request %s", ret == 0 ? "Success" : "Failed");
345 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
347 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
349 return STORAGE_ERROR_NONE;
352 //LCOV_EXCL_START Not called callback
353 static void format_mmc_cb(GVariant *var, void *user_data, GError *err)
355 struct mmc_contents *mmc_data = (struct mmc_contents*)user_data;
358 _D("format_mmc_cb called");
361 _E("no message [%s]", err->message);
366 g_variant_get(var, "(i)", &mmc_ret);
368 _I("Format State : %d", mmc_ret);
372 g_variant_unref(var);
373 (mmc_data->mmc_cb)(mmc_ret, mmc_data->user_data);
377 API int storage_request_format_mmc(struct mmc_contents *mmc_data)
379 return storage_format_mmc(mmc_data, 1);
382 API int storage_format_mmc(struct mmc_contents *mmc_data, int option)
384 void (*format_cb)(GVariant *, void *, GError *) = NULL;
390 if (option < 0 || option > 1)
391 return STORAGE_ERROR_INVALID_PARAMETER;
393 if (mmc_data && mmc_data->mmc_cb) {
394 _I("Format callback exists");
395 format_cb = format_mmc_cb;
399 ret = storage_get_primary_sdcard(&id, &path);
400 if (ret != STORAGE_ERROR_NONE)
403 ret = dbus_method_async_with_reply_var(STORAGE_EXT_BUS_NAME,
404 STORAGE_EXT_PATH_MANAGER,
405 STORAGE_EXT_IFACE_MANAGER,
407 g_variant_new("(ii)", id, option),
412 _I("Format Request %s", ret == 0 ? "Success" : "Failed");
415 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
417 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
419 return STORAGE_ERROR_NONE;
422 int storage_is_mounted(char *path, bool *mounted)
426 ret = mount_check(path);
429 return STORAGE_ERROR_NONE;