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"
30 #define FORMAT_TIMEOUT (120*1000)
33 Get compat path from origin Multi-user path
34 from TZ_USER_CONTENT/.. to /opt/usr/media/..
35 Input should be normalized path like /opt/usr/home/owner/media (TODO: internal normalization)
37 Why this API should be provided?
38 In multi-user environment, each user has own compat content direcotry.(/opt/usr/media)
39 However, although system daemon operates real path,
40 system daemon needs to provide compat path to App if the real path is converted.
43 #include <storage-internal.h>
46 if(storage_get_compat_internal_path(src, sizeof(dest), dest) < 0)
47 // cannot convert. use src path
49 // can convert. use dest path
51 //LCOV_EXCL_START Untested function
52 API int storage_get_compat_internal_path(const char* origin, int len, char* compat)
58 if (!compat || !origin) {
59 _E("Invalid parameter");
63 // this API works on place where compat path is bind-mounted
64 if (!is_compat_bind_mount()) {
65 //LCOV_EXCL_START System Error
66 _E("No compat bind mount");
71 str = tzplatform_getenv(TZ_USER_CONTENT);
72 str_len = strlen(str);
73 if (strncmp(origin, str, str_len) != 0) {
74 _E("Failed to match TZ_USER_CONTENT");
78 r = snprintf(compat, len, "%s%s", COMPAT_DIR, origin + str_len);
80 //LCOV_EXCL_START System Error
81 _E("Failed to create new path");
91 Get Multi-user path from compat path
92 from /opt/usr/media/.. to TZ_USER_CONTENT/..
93 Input should be normalized path like /opt/usr/media (TODO: internal normalization)
95 Why this API should be provided?
96 In multi-user environment, each user has own compat content direcotry.(/opt/usr/media)
97 However, although some APIs send the compat path to system daemon,
98 system daemon should access real path.
101 #include <storage-internal.h>
104 if(storage_get_origin_internal_path(src, sizeof(dest), dest) < 0)
105 // cannot convert. use src path
107 // can convert. use dest path
109 API int storage_get_origin_internal_path(const char* compat, int len, char* origin)
114 if (!compat || !origin) {
115 _E("Invalid parameter");
119 // this API works on place where compat path is bind-mounted
120 if (!is_compat_bind_mount()) {
121 //LCOV_EXCL_START System Error
122 _E("no compat bind mount");
127 compat_len = strlen(COMPAT_DIR);
128 if (strncmp(compat, COMPAT_DIR, compat_len) != 0) {
129 _E("failed to match COMPAT_DIR");
133 r = snprintf(origin, len, "%s%s", tzplatform_getenv(TZ_USER_CONTENT), compat + compat_len);
135 //LCOV_EXCL_START System Error
136 _E("failed to create new path");
144 API int storage_get_primary_sdcard(int *storage_id, char **path)
147 storage_ext_device info;
149 if (!storage_id || !path)
150 return STORAGE_ERROR_INVALID_PARAMETER;
152 result = dbus_method_call_sync(STORAGE_EXT_BUS_NAME,
153 STORAGE_EXT_PATH_MANAGER,
154 STORAGE_EXT_IFACE_MANAGER,
158 //LCOV_EXCL_START System Error
159 _E("Failed to get primary sdcard partition"); //LCOV_EXCL_LINE
160 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
164 g_variant_get(result, "(issssssisibii)",
165 &info.type, &info.devnode, &info.syspath,
166 &info.fs_usage, &info.fs_type,
167 &info.fs_version, &info.fs_uuid,
168 &info.readonly, &info.mount_point,
169 &info.state, &info.primary,
170 &info.flags, &info.storage_id);
172 g_variant_unref(result);
174 if (info.storage_id < 0)
175 return STORAGE_ERROR_NO_DEVICE;
177 *path = strdup(info.mount_point);
179 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
181 *storage_id = info.storage_id;
183 return STORAGE_ERROR_NONE;
186 API int storage_get_storage_level(const char *path, char **level)
191 return STORAGE_ERROR_INVALID_PARAMETER;
193 ret = storage_ext_get_storage_level(path, level);
195 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
196 else if (ret == -EINVAL)
197 return STORAGE_ERROR_INVALID_PARAMETER;
199 return STORAGE_ERROR_OPERATION_FAILED;
201 return STORAGE_ERROR_NONE;
204 static void mount_mmc_cb(GVariant *var, void *user_data, GError *err)
206 struct mmc_contents *mmc_data = (struct mmc_contents*)user_data;
209 _D("mount_mmc_cb called");
212 _E("no message [%s]", err->message);
217 g_variant_get(var, "(i)", &mmc_ret);
219 _I("Mount State : %d", mmc_ret);
223 g_variant_unref(var);
224 (mmc_data->mmc_cb)(mmc_ret, mmc_data->user_data);
227 API int storage_request_mount_mmc(struct mmc_contents *mmc_data)
229 void (*mount_cb)(GVariant *, void *, GError *) = NULL;
235 if (mmc_data && mmc_data->mmc_cb) {
236 _I("Mount callback exists");
237 mount_cb = mount_mmc_cb;
241 ret = storage_get_primary_sdcard(&id, &path);
242 if (ret != STORAGE_ERROR_NONE)
245 ret = dbus_method_async_with_reply_var(STORAGE_EXT_BUS_NAME,
246 STORAGE_EXT_PATH_MANAGER,
247 STORAGE_EXT_IFACE_MANAGER,
249 g_variant_new("(is)", id, ""),
254 _I("Mount Request %s", ret == 0 ? "Success" : "Failed");
257 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
259 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
261 return STORAGE_ERROR_NONE;
264 static void unmount_mmc_cb(GVariant *var, void *user_data, GError *err)
266 struct mmc_contents *mmc_data = (struct mmc_contents*)user_data;
269 _D("unmount_mmc_cb called");
272 _E("no message [%s]", err->message);
277 g_variant_get(var, "(i)", &mmc_ret);
279 _I("Unmount State : %d", mmc_ret);
283 g_variant_unref(var);
284 (mmc_data->mmc_cb)(mmc_ret, mmc_data->user_data);
287 API int storage_request_unmount_mmc(struct mmc_contents *mmc_data, int option)
289 void (*unmount_cb)(GVariant *, void *, GError *) = NULL;
295 if (option < 0 || option > 1)
296 return STORAGE_ERROR_INVALID_PARAMETER;
298 if (mmc_data && mmc_data->mmc_cb) {
299 _I("Unmount callback exists");
300 unmount_cb = unmount_mmc_cb;
304 ret = storage_get_primary_sdcard(&id, &path);
305 if (ret != STORAGE_ERROR_NONE)
308 ret = dbus_method_async_with_reply_var(STORAGE_EXT_BUS_NAME,
309 STORAGE_EXT_PATH_MANAGER,
310 STORAGE_EXT_IFACE_MANAGER,
312 g_variant_new("(ii)", id, option),
317 _I("Unmount Request %s", ret == 0 ? "Success" : "Failed");
320 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
322 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
324 return STORAGE_ERROR_NONE;
327 static void format_mmc_cb(GVariant *var, void *user_data, GError *err)
329 struct mmc_contents *mmc_data = (struct mmc_contents*)user_data;
332 _D("format_mmc_cb called");
335 _E("no message [%s]", err->message);
340 g_variant_get(var, "(i)", &mmc_ret);
342 _I("Format State : %d", mmc_ret);
346 g_variant_unref(var);
347 (mmc_data->mmc_cb)(mmc_ret, mmc_data->user_data);
350 API int storage_request_format_mmc(struct mmc_contents *mmc_data)
352 return storage_format_mmc(mmc_data, 1);
355 API int storage_format_mmc(struct mmc_contents *mmc_data, int option)
357 void (*format_cb)(GVariant *, void *, GError *) = NULL;
363 if (option < 0 || option > 1)
364 return STORAGE_ERROR_INVALID_PARAMETER;
366 if (mmc_data && mmc_data->mmc_cb) {
367 _I("Format callback exists");
368 format_cb = format_mmc_cb;
372 ret = storage_get_primary_sdcard(&id, &path);
373 if (ret != STORAGE_ERROR_NONE)
376 ret = dbus_method_async_with_reply_var(STORAGE_EXT_BUS_NAME,
377 STORAGE_EXT_PATH_MANAGER,
378 STORAGE_EXT_IFACE_MANAGER,
380 g_variant_new("(ii)", id, option),
385 _I("Format Request %s", ret == 0 ? "Success" : "Failed");
388 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
390 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
392 return STORAGE_ERROR_NONE;