Provide internal APIs for converting compat path
[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 API int storage_get_compat_internal_path(const char* origin, int len, char* compat)
50 {
51         int r=-1;
52         const char* str;
53
54         // this API works on place where compat path is bind-mounted
55         if(!is_compat_bind_mount()){
56                 _E("no compat bind mount");
57                 return -1;
58         }
59
60         str = tzplatform_getenv(TZ_USER_CONTENT);
61         if(strncmp(origin,str,strlen(str))!=0){
62                 _E("failed to match TZ_USER_CONTENT");
63                 return -1;
64         }
65
66         r = snprintf(compat,len,"%s/%s",COMPAT_DIR,origin+strlen(str));
67         if(r < 0){
68                 _E("failed to create new path");
69                 return -1;
70         }
71
72         return 0;
73 }
74
75 /*
76    Get Multi-user path from compat path
77    from /opt/usr/media/.. to TZ_USER_CONTENT/..
78    Input should be normalized path like /opt/usr/media (TODO: internal normalization)
79
80    Why this API should be provided?
81    In multi-user environment, each user has own compat content direcotry.(/opt/usr/media)
82    However, although some APIs send the compat path to system daemon,
83             system daemon should access real path.
84
85    Usage:
86         #include <storage-internal.h>
87
88         char dest[100];
89         if(storage_get_origin_internal_path(src, sizeof(dest), dest) < 0)
90                 // cannot convert. use src path
91         else
92                 // can convert. use dest path
93 */
94 API int storage_get_origin_internal_path(const char* compat, int len, char* origin)
95 {
96         int r;
97
98         // this API works on place where compat path is bind-mounted
99         if(!is_compat_bind_mount()){
100                 _E("no compat bind mount");
101                 return -1;
102         }
103
104         if(strncmp(compat,COMPAT_DIR,strlen(COMPAT_DIR))!=0){
105                 _E("failed to match COMPAT_DIR");
106                 return -1;
107         }
108
109         r = snprintf(origin,len,"%s/%s",tzplatform_getenv(TZ_USER_CONTENT),compat+strlen(COMPAT_DIR));
110         if(r < 0){
111                 _E("failed to create new path");
112                 return -1;
113         }
114
115         return 0;
116 }
117
118 API int storage_get_primary_sdcard(int *storage_id, char **path)
119 {
120         GVariant *result;
121         storage_ext_device info;
122
123         if (!storage_id || !path)
124                 return STORAGE_ERROR_INVALID_PARAMETER;
125
126         result = dbus_method_call_sync(STORAGE_EXT_BUS_NAME,
127                         STORAGE_EXT_PATH_MANAGER,
128                         STORAGE_EXT_IFACE_MANAGER,
129                         "GetMmcPrimary",
130                         NULL);
131         if (!result) {
132                 _E("Failed to get primary sdcard partition"); //LCOV_EXCL_LINE
133                 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
134         }
135
136         g_variant_get(result, "(issssssisibii)",
137                         &info.type, &info.devnode, &info.syspath,
138                         &info.fs_usage, &info.fs_type,
139                         &info.fs_version, &info.fs_uuid,
140                         &info.readonly, &info.mount_point,
141                         &info.state, &info.primary,
142                         &info.flags, &info.storage_id);
143
144         g_variant_unref(result);
145
146         if (info.storage_id < 0)
147                 return STORAGE_ERROR_NO_DEVICE;
148
149         *path = strdup(info.mount_point);
150         if (*path == NULL)
151                 return STORAGE_ERROR_OUT_OF_MEMORY;
152
153         *storage_id = info.storage_id;
154
155         return STORAGE_ERROR_NONE;
156 }