Code refactoring
[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 <sqlite3.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 an iterator to the begin element in the range that compares equal to option
45  * @param[in] begin element
46  * @param[in] end elment
47  * return return true when elements match
48  */
49 bool cmdOptionExists(char** begin, char** end, const std::string& option);
50
51 /**
52  * @brief get current executable path
53  * return std::string path
54  */
55 std::string readSelfPath();
56
57 /**
58  * @brief concat path with PATH_SEPARATOR
59  * @param[in] destination path
60  * @param[in] source path
61  * return std::string result path
62  */
63 std::string concatPath(const std::string& path1, const std::string& path2);
64
65 /**
66  * @brief get absolute Path
67  * @param[in] source path
68  * return std::string result path
69  */
70 std::string absolutePath(const std::string& path);
71
72 /**
73  * @brief get the directory of file
74  * @param[in] source path
75  * return std::string result path
76  */
77 std::string baseName(const std::string& path);
78
79 /**
80  * @brief replaces all matching substrings of the regular expression with a given replacement
81  * @param[in] original string
82  * @param[in] pattern to match
83  * @param[in] replacement string
84  * return the modified string
85  */
86 std::string replaceAll(const std::string &str, const std::string &pattern, const std::string &replace);
87
88 /**
89  * @brief get root path
90  * @param[in] package id
91  * @param[out] root path
92  */
93 int getRootPath(std::string pkgId, std::string& rootPath);
94
95 /**
96  * @brief get exec name
97  * @param[in] package id
98  * @param[out] exec name
99  */
100 int getExecName(std::string pkgId, std::string& execName);
101
102 /**
103  * @brief get metadata value
104  * @param[in] package id
105  * @param[in] metadata key
106  * @param[out] metadata value
107  */
108 int getMetadataValue(std::string pkgId, std::string metadataKey, std::string& metadataValue);
109
110 /**
111  * @brief split path with ":" delimiter and put that in the vector
112  * @param[in] source path
113  * @param[out] string vector
114  */
115 void splitPath(const std::string& path, std::vector<std::string>& out);
116
117 /**
118  * @brief check file is exist
119  * @param[in] source path
120  * @return bool
121  */
122 bool isFileExist(const std::string& path);
123
124 /**
125  * @brief get file size
126  * @param[in] source path
127  * @return size of file
128  */
129 uintptr_t getFileSize(const std::string& path);
130
131 /**
132  * @brief check the file is managed assembly or not.
133  * @param[in] file path
134  * @return return true when the file is managed assembly.
135  *         otherwise return false including native image case.
136  */
137 bool isManagedAssembly(const std::string& filePath);
138
139 /**
140  * @brief check the file is native image or not.
141  * @param[in] file path
142  * @return return true when the file is native image.
143  */
144 bool isNativeImage(const std::string& filePath);
145
146 /**
147  * @brief find assembly files in the directories
148  * @param[in] directories
149  * @param[out] ":" seperated assembly path list
150  */
151 void assembliesInDirectory(const std::vector<std::string>& directories, std::string& tpaList);
152
153 /**
154  * @brief function pointer for file reader
155  */
156 typedef std::function<void (const std::string&, const char*)> FileReader;
157
158 /**
159  * @brief scan files in the given directory and run file reader function for that
160  * @param[in] directory
161  * @param[in] file reader function
162  * @param[in] scan depth
163  */
164 void scanFilesInDir(const std::string& directory, FileReader reader, unsigned int depth);
165
166 /**
167  * @brief update assembly file info.
168  * @param[in] get path
169  * @param[in] set path
170  * @param[in] symbolic link
171  */
172 void updateAssemblyInfo(const std::string& getPath, const std::string& setPath, bool isSymlink = false);
173
174 /**
175  * @brief create the new directory.
176  * @param[in] source path
177  * @return return true when the directory was created.
178  */
179 bool createDir(const bf::path& path);
180
181 /**
182  * @brief copy the directory.
183  * @param[in] path to the source directory
184  * @param[in] path to the target directory
185  * @param[in] filesystem flag
186  * @return return true when the directory was copied.
187  */
188 bool copyDir(const bf::path& path1, const bf::path& path2, FSFlag flags = FS_NONE);
189
190 /**
191  * @brief copy the file.
192  * @param[in] path to the source file
193  * @param[in] path to the target file
194  * @return return true when the file was copied.
195  */
196 bool copyFile(const bf::path& path1, const bf::path& path2);
197
198 /**
199  * @brief moves of renames the file or directory.
200  * @param[in] path to the source file
201  * @param[in] path to the target file
202  * @return return true when the file was moved.
203  */
204 bool moveFile(const bf::path& path1, const bf::path& path2);
205
206 /**
207  * @brief removes the file or empty directory.
208  * @param[in] source path
209  * @return return true when the file was deleted.
210  */
211 bool removeFile(const bf::path& path);
212
213 /**
214  * @brief removes the file or directory and all its contents.
215  * @param[in] source path
216  * @return return true when the file or directory was deleted.
217  */
218 bool removeAll(const bf::path& path);
219
220 /**
221  * @brief .deps.json file parser
222  * @param[in] package id
223  * @param[in] root path
224  * @param[in] exec name
225  * @param[in] tpa list
226  * @param[in] dotnettool
227  * @param[in] sqlite3
228  * return std::vector<std::string> parser data
229  */
230 std::vector<std::string> depsJsonParser(std::string pkgId, std::string rootPath, std::string execName, std::string tpaList, bool isTool = false, sqlite3 *tac_db = NULL);
231
232 #endif /* __UTILS_H__ */