Fixed build error (#359)
[platform/core/dotnet/launcher.git] / NativeLauncher / inc / utils.h
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 #ifndef __UTILS_H__
18 #define __UTILS_H__
19
20 #include <algorithm>
21 #include <string>
22 #include <vector>
23 #include <functional>
24 #include <boost/filesystem.hpp>
25 #include <pkgmgr-info.h>
26
27 #include <launcher_env.h>
28
29 #ifndef PATH_SEPARATOR
30 #define PATH_SEPARATOR '/'
31 #endif
32
33 #ifndef PROFILE_BASENAME
34 #define PROFILE_BASENAME ".__tizen_specific_profile_data"
35 #endif
36
37 namespace bf = boost::filesystem;
38 namespace bs = boost::system;
39
40 enum FSFlag : int {
41   FS_NONE              = 0,
42   FS_MERGE_SKIP        = (1 << 0),
43   FS_MERGE_OVERWRITE   = (1 << 1),
44   FS_COMMIT_COPY_FILE  = (1 << 2),
45   FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS = (1 << 3)
46 };
47
48 /**
49  * @brief concat path with PATH_SEPARATOR
50  * @param[in] destination path
51  * @param[in] source path
52  * @return std::string result path
53  */
54 std::string concatPath(const std::string& path1, const std::string& path2);
55
56 /**
57  * @brief get canonicalized absolute Path
58  * @param[in] source path
59  * @return std::string result path
60  */
61 std::string getAbsolutePath(const std::string& path);
62
63 /**
64  * @brief get the directory of file
65  * @param[in] source path
66  * @return std::string result path
67  */
68 std::string getBaseName(const std::string& path);
69
70 /**
71  * @brief replaces all matching substrings of the regular expression with a given replacement
72  * @param[in] original string
73  * @param[in] pattern to match
74  * @param[in] replacement string
75  * @return std::string the modified string
76  */
77 std::string replaceAll(const std::string& str, const std::string& pattern, const std::string& replace);
78
79 /**
80  * @brief get root path
81  * @param[in] package id
82  * @return std::string root path
83  */
84 std::string getRootPath(const std::string& pkgId);
85
86 /**
87  * @brief get exec name
88  * @param[in] package id
89  * @return std::string exec name
90  */
91 std::string getExecName(const std::string& pkgId);
92
93 /**
94  * @brief get app type
95  * @param[in] package id
96  * @return std::string app type
97  */
98 std::string getAppType(const std::string& pkgId);
99
100 /**
101  * @brief get metadata value
102  * @param[in] package id
103  * @param[in] metadata key
104  * @return std::string metadata value
105  */
106 std::string getMetadataValue(const std::string& pkgId, const std::string& key);
107
108 /**
109  * @brief change the extension of a path or file
110  * @param[in] source path or file
111  * @param[in] from extension
112  * @param[in] to extension
113  * @return std::string path or file with changed extension
114  */
115 std::string changeExtension(const std::string& path, const std::string& from, const std::string& to);
116
117 /**
118  * @brief check the path is 'readonly' or not
119  * @param[in] path
120  * @return bool package readonly value
121  */
122 bool isReadOnlyArea(const std::string& path);
123
124 /**
125  * @brief split path with ":" delimiter and put that in the vector
126  * @param[in] source path
127  * @param[out] string vector
128  */
129 void splitPath(const std::string& path, std::vector<std::string>& out);
130
131 /**
132  * @brief check file exists
133  *        in case of symlink file, check both input file and link reference file.
134  * @param[in] source path
135  * @return bool
136  */
137 bool isFile(const std::string& path);
138
139 /**
140  * @brief check symlink file
141  * @param[in] source path
142  * @return bool
143  */
144 bool isSymlinkFile(const std::string& path);
145
146 /**
147  * @brief check directory exists
148  * @param[in] source path
149  * @return bool
150  */
151 bool isDirectory(const std::string& path);
152
153 /**
154  * @brief check the file is managed assembly or not.
155  * @param[in] file path
156  * @return return true when the file is managed assembly.
157  *         otherwise return false including native image case.
158  */
159 bool isManagedAssembly(const std::string& filePath);
160
161 /**
162  * @brief check the file is native image or not.
163  * @param[in] file path
164  * @return return true when the file is native image.
165  */
166 bool isNativeImage(const std::string& filePath);
167
168 /**
169  * @brief Resolve assembly files from directories and append their paths to the given list.
170  * @remark If a native image exists for an assembly in the same directory, it will be used.
171  *         If multiple assemblies of the same name exist, the first one will be used.
172  * @param[in]  directories  list of directories
173  * @param[out] list         colon-separated string of assembly paths
174  */
175 void addAssembliesFromDirectories(const std::vector<std::string>& directories, std::string& list);
176
177 /**
178  * @brief File search callback
179  * @param[in] path      full path to a resolved file
180  * @param[in] filename  file name
181  */
182 typedef std::function<void (const std::string& path, const std::string& filename)> FileReader;
183
184 /**
185  * @brief Scan all files in the given directory.
186  * @param[in] directory path to a root directory
187  * @param[in] reader    callback for iteration
188  * @param[in] depth     recursive search depth
189  */
190 void scanFilesInDirectory(const std::string& directory, FileReader reader, unsigned int depth);
191
192 /**
193  * @brief copy smack and ownership.
194  * @param[in] get path
195  * @param[in] set path
196  * @param[in] symbolic link
197  */
198 void copySmackAndOwnership(const std::string& fromPath, const std::string& toPath, bool isSymlink = false);
199
200 /**
201  * @brief check whether the path exists or not.
202  * @param[in] source path
203  * @return return true when the path exist.
204  */
205 bool exist(const bf::path& path);
206
207 /**
208  * @brief create the new directory.
209  * @param[in] source path
210  * @return return true when the directory was created.
211  */
212 bool createDir(const bf::path& path);
213
214 /**
215  * @brief copy the directory.
216  * @param[in] path to the source directory
217  * @param[in] path to the target directory
218  * @param[in] filesystem flag
219  * @return return true when the directory was copied.
220  */
221 bool copyDir(const bf::path& path1, const bf::path& path2, FSFlag flags = FS_NONE);
222
223 /**
224  * @brief copy the file.
225  * @param[in] path to the source file
226  * @param[in] path to the target file
227  * @return return true when the file was copied.
228  */
229 bool copyFile(const bf::path& path1, const bf::path& path2);
230
231 /**
232  * @brief moves of renames the file or directory.
233  * @param[in] path to the source file
234  * @param[in] path to the target file
235  * @return return true when the file was moved.
236  */
237 bool moveFile(const bf::path& path1, const bf::path& path2);
238
239 /**
240  * @brief removes the file or empty directory.
241  * @param[in] source path
242  * @return return true when the file was deleted.
243  */
244 bool removeFile(const bf::path& path);
245
246 /**
247  * @brief removes the file or directory and all its contents.
248  * @param[in] source path
249  * @return return true when the file or directory was deleted.
250  */
251 bool removeAll(const bf::path& path);
252
253 /**
254  * @brief change command name of the current process for access via ps command
255  * @param[in] name
256  */
257 void setCmdName(const std::string& name);
258
259 /**
260  * @brief Get the file name from the specified path.
261  * @remark An empty string will be returned if the path string ends with a path separator character.
262  * @param[in] path  path to a file
263  * @return a substring after the path separator character
264  */
265 std::string getFileName(const std::string& path);
266
267 /**
268  * @brief Generates a representation called a message digest
269  * @param[in] file path
270  * @return message digest
271  */
272 std::string SHA256(const std::string& path);
273
274 /**
275  * @brief Creates the package information handle from db which is not disabled.
276  *        This function is a wrapper of pkgmgrinfo_pkginfo_get_pkginfo() to handle multi-user case
277  * @param[in] pkg id
278  * @param[out] pkginfo handle
279  * @return 0 if success, otherwise -1
280  */
281 int pkgmgrGetPkgInfo(const std::string& pkgId, pkgmgrinfo_pkginfo_h* handle);
282
283 /**
284  * @brief Creates the application information handle from db.
285  *        This function is a wrapper of pkgmgrinfo_appinfo_get_appinfo() to handle multi-user case
286  * @param[in] app id
287  * @param[out] appinfo handle
288  * @return 0 if success, otherwise -1
289  */
290 int pkgmgrGetAppInfo(const std::string& appId, pkgmgrinfo_appinfo_h* handle);
291
292 /**
293  * @brief Executes the metadata filter query for all the installed packages.
294  *        This function is a wrapper of pkgmgrinfo_appinfo_metadata_filter_foreach() to handle multi-user case
295  * @param[in] metadata filter handle
296  * @param[in] callback function
297  * @param[in] user data
298  * @return 0 if success, otherwise -1
299  */
300 int pkgmgrMDFilterForeach(pkgmgrinfo_appinfo_metadata_filter_h handle,
301                                          pkgmgrinfo_app_list_cb app_cb,
302                                          void *user_data);
303
304 #endif /* __UTILS_H__ */