Modify return value for invalid storage id
[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 #include "storage-experimental.h"
30
31 /*
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)
35
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.
40
41         Usage:
42                 #include <storage-internal.h>
43
44                 char dest[100];
45                 if(storage_get_compat_internal_path(src, sizeof(dest), dest) < 0)
46                         // cannot convert. use src path
47                 else
48                         // can convert. use dest path
49  */
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                 _E("No compat bind mount");
64                 return -1;
65         }
66
67         str = tzplatform_getenv(TZ_USER_CONTENT);
68         str_len = strlen(str);
69         if (strncmp(origin, str, str_len) != 0) {
70                 _E("Failed to match TZ_USER_CONTENT");
71                 return -1;
72         }
73
74         r = snprintf(compat, len, "%s%s", COMPAT_DIR, origin + str_len);
75         if (r < 0) {
76                 _E("Failed to create new path");
77                 return -1;
78         }
79
80         return 0;
81 }
82
83 /*
84         Get Multi-user path from compat path
85         from /opt/usr/media/.. to TZ_USER_CONTENT/..
86         Input should be normalized path like /opt/usr/media (TODO: internal normalization)
87
88         Why this API should be provided?
89         In multi-user environment, each user has own compat content direcotry.(/opt/usr/media)
90         However, although some APIs send the compat path to system daemon,
91                 system daemon should access real path.
92
93         Usage:
94                 #include <storage-internal.h>
95
96                 char dest[100];
97                 if(storage_get_origin_internal_path(src, sizeof(dest), dest) < 0)
98                         // cannot convert. use src path
99                 else
100                         // can convert. use dest path
101 */
102 API int storage_get_origin_internal_path(const char* compat, int len, char* origin)
103 {
104         int r;
105         int compat_len;
106
107         if (!compat || !origin) {
108                 _E("Invalid parameter");
109                 return -1;
110         }
111
112         // this API works on place where compat path is bind-mounted
113         if (!is_compat_bind_mount()) {
114                 _E("no compat bind mount");
115                 return -1;
116         }
117
118         compat_len = strlen(COMPAT_DIR);
119         if (strncmp(compat, COMPAT_DIR, compat_len) != 0) {
120                 _E("failed to match COMPAT_DIR");
121                 return -1;
122         }
123
124         r = snprintf(origin, len, "%s%s", tzplatform_getenv(TZ_USER_CONTENT), compat + compat_len);
125         if (r < 0) {
126                 _E("failed to create new path");
127                 return -1;
128         }
129
130         return 0;
131 }
132
133 API int storage_get_primary_sdcard(int *storage_id, char **path)
134 {
135         GVariant *result;
136         storage_ext_device info;
137
138         if (!storage_id || !path)
139                 return STORAGE_ERROR_INVALID_PARAMETER;
140
141         result = dbus_method_call_sync(STORAGE_EXT_BUS_NAME,
142                         STORAGE_EXT_PATH_MANAGER,
143                         STORAGE_EXT_IFACE_MANAGER,
144                         "GetMmcPrimary",
145                         NULL);
146         if (!result) {
147                 _E("Failed to get primary sdcard partition"); //LCOV_EXCL_LINE
148                 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
149         }
150
151         g_variant_get(result, "(issssssisibii)",
152                         &info.type, &info.devnode, &info.syspath,
153                         &info.fs_usage, &info.fs_type,
154                         &info.fs_version, &info.fs_uuid,
155                         &info.readonly, &info.mount_point,
156                         &info.state, &info.primary,
157                         &info.flags, &info.storage_id);
158
159         g_variant_unref(result);
160
161         if (info.storage_id < 0)
162                 return STORAGE_ERROR_NO_DEVICE;
163
164         *path = strdup(info.mount_point);
165         if (*path == NULL)
166                 return STORAGE_ERROR_OUT_OF_MEMORY;
167
168         *storage_id = info.storage_id;
169
170         return STORAGE_ERROR_NONE;
171 }
172
173 API int storage_get_type_dev(int storage_id, storage_type_e *type, storage_dev_e *dev)
174 {
175         storage_ext_device *ext_dev;
176         int ret;
177
178         if (storage_id < 0) {
179                 _E("Invalid parameger");
180                 return STORAGE_ERROR_NO_DEVICE;
181         }
182
183         if (!type) {
184                 _E("Invalid parameger");
185                 return STORAGE_ERROR_INVALID_PARAMETER;
186         }
187
188         if (!dev) {
189                 _E("Invalid parameger");
190                 return STORAGE_ERROR_INVALID_PARAMETER;
191         }
192
193         ret = storage_get_type(storage_id, type);
194         if (ret != STORAGE_ERROR_NONE) {
195                 _E("Failed to get storage type: %d", ret);
196                 return ret;
197         }
198         if (*type == STORAGE_TYPE_INTERNAL)
199                 return STORAGE_ERROR_NONE;
200
201         ext_dev = calloc(1, sizeof(storage_ext_device));
202         if (!ext_dev) {
203 //LCOV_EXCL_START System Error
204                 _E("calloc failed");
205                 return STORAGE_ERROR_OUT_OF_MEMORY;
206 //LCOV_EXCL_STOP
207         }
208
209         ret = storage_ext_get_device_info(storage_id, ext_dev);
210         if (ret < 0) {
211                 _E("Cannot get the storage with id (%d, ret:%d)", storage_id, ret); //LCOV_EXCL_LINE
212                 ret = STORAGE_ERROR_NO_DEVICE;
213                 goto out;
214         }
215
216         if (ext_dev->type == STORAGE_EXT_SCSI)
217                 *dev = STORAGE_DEV_EXT_USB_MASS_STORAGE;
218         else if (ext_dev->type == STORAGE_EXT_MMC)
219                 *dev = STORAGE_DEV_EXT_SDCARD;
220         ret = STORAGE_ERROR_NONE;
221         _I("type: %d(internal:0, external:1) dev: %d(sdcard: 1001, usb: 1002)", *type, *dev);
222
223 out:
224         storage_ext_release_device(&ext_dev);
225         return ret;
226 }