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