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