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