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