Make a new public api: storage_get_type_dev
[platform/core/system/libstorage.git] / src / storage-inhouse.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <errno.h>
22 #include <tzplatform_config.h>
23
24 #include "common.h"
25 #include "list.h"
26 #include "log.h"
27 #include "storage-internal.h"
28 #include "storage-external-dbus.h"
29
30 /*
31         Get compat path from origin Multi-user path
32         from TZ_USER_CONTENT/.. to /opt/usr/media/..
33         Input should be normalized path like /opt/usr/home/owner/media (TODO: internal normalization)
34
35         Why this API should be provided?
36         In multi-user environment, each user has own compat content direcotry.(/opt/usr/media)
37         However, although system daemon operates real path,
38                 system daemon needs to provide compat path to App if the real path is converted.
39
40         Usage:
41                 #include <storage-internal.h>
42
43                 char dest[100];
44                 if(storage_get_compat_internal_path(src, sizeof(dest), dest) < 0)
45                         // cannot convert. use src path
46                 else
47                         // can convert. use dest path
48  */
49 //LCOV_EXCL_START Untested function
50 API int storage_get_compat_internal_path(const char* origin, int len, char* compat)
51 {
52         int r = -1;
53         int str_len;
54         const char* str;
55
56         if (!compat || !origin) {
57                 _E("Invalid parameter");
58                 return -1;
59         }
60
61         // this API works on place where compat path is bind-mounted
62         if (!is_compat_bind_mount()) {
63                 //LCOV_EXCL_START System Error
64                 _E("No compat bind mount");
65                 return -1;
66                 //LCOV_EXCL_STOP
67         }
68
69         str = tzplatform_getenv(TZ_USER_CONTENT);
70         str_len = strlen(str);
71         if (strncmp(origin, str, str_len) != 0) {
72                 _E("Failed to match TZ_USER_CONTENT");
73                 return -1;
74         }
75
76         r = snprintf(compat, len, "%s%s", COMPAT_DIR, origin + str_len);
77         if (r < 0) {
78                 //LCOV_EXCL_START System Error
79                 _E("Failed to create new path");
80                 return -1;
81                 //LCOV_EXCL_STOP
82         }
83
84         return 0;
85 }
86 //LCOV_EXCL_STOP
87
88 /*
89         Get Multi-user path from compat path
90         from /opt/usr/media/.. to TZ_USER_CONTENT/..
91         Input should be normalized path like /opt/usr/media (TODO: internal normalization)
92
93         Why this API should be provided?
94         In multi-user environment, each user has own compat content direcotry.(/opt/usr/media)
95         However, although some APIs send the compat path to system daemon,
96                 system daemon should access real path.
97
98         Usage:
99                 #include <storage-internal.h>
100
101                 char dest[100];
102                 if(storage_get_origin_internal_path(src, sizeof(dest), dest) < 0)
103                         // cannot convert. use src path
104                 else
105                         // can convert. use dest path
106 */
107 API int storage_get_origin_internal_path(const char* compat, int len, char* origin)
108 {
109         int r;
110         int compat_len;
111
112         if (!compat || !origin) {
113                 _E("Invalid parameter");
114                 return -1;
115         }
116
117         // this API works on place where compat path is bind-mounted
118         if (!is_compat_bind_mount()) {
119                 //LCOV_EXCL_START System Error
120                 _E("no compat bind mount");
121                 return -1;
122                 //LCOV_EXCL_STOP
123         }
124
125         compat_len = strlen(COMPAT_DIR);
126         if (strncmp(compat, COMPAT_DIR, compat_len) != 0) {
127                 _E("failed to match COMPAT_DIR");
128                 return -1;
129         }
130
131         r = snprintf(origin, len, "%s%s", tzplatform_getenv(TZ_USER_CONTENT), compat + compat_len);
132         if (r < 0) {
133                 //LCOV_EXCL_START System Error
134                 _E("failed to create new path");
135                 return -1;
136                 //LCOV_EXCL_STOP
137         }
138
139         return 0;
140 }
141
142 API int storage_get_primary_sdcard(int *storage_id, char **path)
143 {
144         GVariant *result;
145         storage_ext_device info;
146
147         if (!storage_id || !path)
148                 return STORAGE_ERROR_INVALID_PARAMETER;
149
150         result = dbus_method_call_sync(STORAGE_EXT_BUS_NAME,
151                         STORAGE_EXT_PATH_MANAGER,
152                         STORAGE_EXT_IFACE_MANAGER,
153                         "GetMmcPrimary",
154                         NULL);
155         if (!result) {
156                 //LCOV_EXCL_START System Error
157                 _E("Failed to get primary sdcard partition"); //LCOV_EXCL_LINE
158                 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
159                 //LCOV_EXCL_STOP
160         }
161
162         g_variant_get(result, "(issssssisibii)",
163                         &info.type, &info.devnode, &info.syspath,
164                         &info.fs_usage, &info.fs_type,
165                         &info.fs_version, &info.fs_uuid,
166                         &info.readonly, &info.mount_point,
167                         &info.state, &info.primary,
168                         &info.flags, &info.storage_id);
169
170         g_variant_unref(result);
171
172         if (info.storage_id < 0)
173                 return STORAGE_ERROR_NO_DEVICE;
174
175         *path = strdup(info.mount_point);
176         if (*path == NULL)
177                 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
178
179         *storage_id = info.storage_id;
180
181         return STORAGE_ERROR_NONE;
182 }
183