Support for TLC(Tizen Library cache) (#260)
[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 concat path with PATH_SEPARATOR
44  * @param[in] destination path
45  * @param[in] source path
46  * return std::string result path
47  */
48 std::string concatPath(const std::string& path1, const std::string& path2);
49
50 /**
51  * @brief get canonicalized absolute Path
52  * @param[in] source path
53  * return std::string result path
54  */
55 std::string getAbsolutePath(const std::string& path);
56
57 /**
58  * @brief get the directory of file
59  * @param[in] source path
60  * return std::string result path
61  */
62 std::string getBaseName(const std::string& path);
63
64 /**
65  * @brief replaces all matching substrings of the regular expression with a given replacement
66  * @param[in] original string
67  * @param[in] pattern to match
68  * @param[in] replacement string
69  * return std::string the modified string
70  */
71 std::string replaceAll(const std::string& str, const std::string& pattern, const std::string& replace);
72
73 /**
74  * @brief get root path
75  * @param[in] package id
76  * return std::string root path
77  */
78 std::string getRootPath(const std::string& pkgId);
79
80 /**
81  * @brief get exec name
82  * @param[in] package id
83  * return std::string exec name
84  */
85 std::string getExecName(const std::string& pkgId);
86
87 /**
88  * @brief get app type
89  * @param[in] package id
90  * return std::string app type
91  */
92 std::string getAppType(const std::string& pkgId);
93
94 /**
95  * @brief get metadata value
96  * @param[in] package id
97  * @param[in] metadata key
98  * return std::string metadata value
99  */
100 std::string getMetadataValue(const std::string& pkgId, const std::string& key);
101
102 /**
103  * @brief split path with ":" delimiter and put that in the vector
104  * @param[in] source path
105  * @param[out] string vector
106  */
107 void splitPath(const std::string& path, std::vector<std::string>& out);
108
109 /**
110  * @brief check file exists
111  *        in case of symlink file, check both input file and link reference file.
112  * @param[in] source path
113  * @return bool
114  */
115 bool isFile(const std::string& path);
116
117 /**
118  * @brief check directory exists
119  * @param[in] source path
120  * @return bool
121  */
122 bool isDirectory(const std::string& path);
123
124 /**
125  * @brief check the file is managed assembly or not.
126  * @param[in] file path
127  * @return return true when the file is managed assembly.
128  *         otherwise return false including native image case.
129  */
130 bool isManagedAssembly(const std::string& filePath);
131
132 /**
133  * @brief check the file is native image or not.
134  * @param[in] file path
135  * @return return true when the file is native image.
136  */
137 bool isNativeImage(const std::string& filePath);
138
139 /**
140  * @brief Resolve assembly files from directories and append their paths to the given list.
141  * @remark If a native image exists for an assembly in the same directory, it will be used.
142  *         If multiple assemblies of the same name exist, the first one will be used.
143  * @param[in]  directories  list of directories
144  * @param[out] list         colon-separated string of assembly paths
145  */
146 void addAssembliesFromDirectories(const std::vector<std::string>& directories, std::string& list);
147
148 /**
149  * @brief File search callback
150  * @param[in] path      full path to a resolved file
151  * @param[in] filename  file name
152  */
153 typedef std::function<void (const std::string& path, const std::string& filename)> FileReader;
154
155 /**
156  * @brief Scan all files in the given directory.
157  * @param[in] directory path to a root directory
158  * @param[in] reader    callback for iteration
159  * @param[in] depth     recursive search depth
160  */
161 void scanFilesInDirectory(const std::string& directory, FileReader reader, unsigned int depth);
162
163 /**
164  * @brief copy smack and ownership.
165  * @param[in] get path
166  * @param[in] set path
167  * @param[in] symbolic link
168  */
169 void copySmackAndOwnership(const std::string& fromPath, const std::string& toPath, bool isSymlink = false);
170
171 /**
172  * @brief create the new directory.
173  * @param[in] source path
174  * @return return true when the directory was created.
175  */
176 bool createDir(const bf::path& path);
177
178 /**
179  * @brief copy the directory.
180  * @param[in] path to the source directory
181  * @param[in] path to the target directory
182  * @param[in] filesystem flag
183  * @return return true when the directory was copied.
184  */
185 bool copyDir(const bf::path& path1, const bf::path& path2, FSFlag flags = FS_NONE);
186
187 /**
188  * @brief copy the file.
189  * @param[in] path to the source file
190  * @param[in] path to the target file
191  * @return return true when the file was copied.
192  */
193 bool copyFile(const bf::path& path1, const bf::path& path2);
194
195 /**
196  * @brief moves of renames the file or directory.
197  * @param[in] path to the source file
198  * @param[in] path to the target file
199  * @return return true when the file was moved.
200  */
201 bool moveFile(const bf::path& path1, const bf::path& path2);
202
203 /**
204  * @brief removes the file or empty directory.
205  * @param[in] source path
206  * @return return true when the file was deleted.
207  */
208 bool removeFile(const bf::path& path);
209
210 /**
211  * @brief removes the file or directory and all its contents.
212  * @param[in] source path
213  * @return return true when the file or directory was deleted.
214  */
215 bool removeAll(const bf::path& path);
216
217 /**
218  * @brief change command name of the current process for access via ps command
219  * @param[in] name
220  */
221 void setCmdName(const std::string& name);
222
223 /**
224  * @brief Get the file name from the specified path.
225  * @remark An empty string will be returned if the path string ends with a path separator character.
226  * @param[in] path  path to a file
227  * @return a substring after the path separator character
228  */
229 std::string getFileName(const std::string& path);
230
231 /**
232  * @brief Generates a representation called a message digest
233  * @param[in] file path
234  * @return message digest
235  */
236 std::string SHA256(const std::string& path);
237
238 #endif /* __UTILS_H__ */