Add 'Tizen Assembly Cache' feature
[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
25 #ifndef PATH_SEPARATOR
26 #define PATH_SEPARATOR '/'
27 #endif
28
29 namespace bf = boost::filesystem;
30 namespace bs = boost::system;
31
32 enum FSFlag : int {
33   FS_NONE              = 0,
34   FS_MERGE_SKIP        = (1 << 0),
35   FS_MERGE_OVERWRITE   = (1 << 1),
36   FS_COMMIT_COPY_FILE  = (1 << 2),
37   FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS = (1 << 3)
38 };
39
40 bool cmdOptionExists(char** begin, char** end, const std::string& option);
41
42 /**
43  * @brief get current executable path
44  * return std::string path
45  */
46 std::string readSelfPath();
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 absolute Path
58  * @param[in] source path
59  * return std::string result path
60  */
61 std::string absolutePath(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 baseName(const std::string& path);
69
70 /**
71  * @brief get root path
72  * @param[in] package id
73  * @param[out] root path
74  */
75 int getRootPath(std::string pkgId, std::string& rootPath);
76
77 /**
78  * @brief split path with ":" delimiter and put that in the vector
79  * @param[in] source path
80  * @param[out] string vector
81  */
82 void splitPath(const std::string& path, std::vector<std::string>& out);
83
84 /**
85  * @brief check file is exist
86  * @param[in] source path
87  * @return bool
88  */
89 bool isFileExist(const std::string& path);
90
91 /**
92  * @brief check the file is managed assembly or not.
93  * @param[in] file path
94  * @return return true when the file is managed assembly.
95  *         otherwise return false including native image case.
96  */
97 bool isManagedAssembly(const std::string& filePath);
98
99 /**
100  * @brief check the file is native image or not.
101  * @param[in] file path
102  * @return return true when the file is native image.
103  */
104 bool isNativeImage(const std::string& filePath);
105
106 /**
107  * @brief find assembly files in the directories
108  * @param[in] directories
109  * @param[out] ":" seperated assembly path list
110  */
111 void assembliesInDirectory(const std::vector<std::string>& directories, std::string& tpaList);
112
113 /**
114  * @brief function pointer for file reader
115  */
116 typedef std::function<void (const std::string&, const char*)> FileReader;
117
118 /**
119  * @brief scan files in the given directory and run file reader function for that
120  * @param[in] directory
121  * @param[in] file reader function
122  * @param[in] scan depth
123  */
124 void scanFilesInDir(const std::string& directory, FileReader reader, unsigned int depth);
125
126 /**
127  * @brief create the new directory.
128  * @param[in] source path
129  * @return return true when the directory was created.
130  */
131 bool createDir(const bf::path& path);
132
133 /**
134  * @brief copy the directory.
135  * @param[in] path to the source directory
136  * @param[in] path to the target directory
137  * @param[in] filesystem flag
138  * @return return true when the directory was copied.
139  */
140 bool copyDir(const bf::path& path1, const bf::path& path2, FSFlag flags = FS_NONE);
141
142 /**
143  * @brief copy the file.
144  * @param[in] path to the source file
145  * @param[in] path to the target file
146  * @return return true when the file was copied.
147  */
148 bool copyFile(const bf::path& path1, const bf::path& path2);
149
150 /**
151  * @brief moves of renames the file or directory.
152  * @param[in] path to the source file
153  * @param[in] path to the target file
154  * @return return true when the file was moved.
155  */
156 bool moveFile(const bf::path& path1, const bf::path& path2);
157
158 /**
159  * @brief removes the file or empty directory.
160  * @param[in] source path
161  * @return return true when the file was deleted.
162  */
163 bool removeFile(const bf::path& path);
164
165 /**
166  * @brief removes the file or directory and all its contents.
167  * @param[in] source path
168  * @return return true when the file or directory was deleted.
169  */
170 bool removeAll(const bf::path& path);
171
172 #endif /* __UTILS_H__ */