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