Code cleanup (#251)
[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  * @param[in] source path
112  * @return bool
113  */
114 bool isFile(const std::string& path);
115
116 /**
117  * @brief check directory exists
118  * @param[in] source path
119  * @return bool
120  */
121 bool isDirectory(const std::string& path);
122
123 /**
124  * @brief check the file is managed assembly or not.
125  * @param[in] file path
126  * @return return true when the file is managed assembly.
127  *         otherwise return false including native image case.
128  */
129 bool isManagedAssembly(const std::string& filePath);
130
131 /**
132  * @brief check the file is native image or not.
133  * @param[in] file path
134  * @return return true when the file is native image.
135  */
136 bool isNativeImage(const std::string& filePath);
137
138 /**
139  * @brief Resolve assembly files from directories and append their paths to the given list.
140  * @remark If a native image exists for an assembly in the same directory, it will be used.
141  *         If multiple assemblies of the same name exist, the first one will be used.
142  * @param[in]  directories  list of directories
143  * @param[out] list         colon-separated string of assembly paths
144  */
145 void addAssembliesFromDirectories(const std::vector<std::string>& directories, std::string& list);
146
147 /**
148  * @brief File search callback
149  * @param[in] path      full path to a resolved file
150  * @param[in] filename  file name
151  */
152 typedef std::function<void (const std::string& path, const std::string& filename)> FileReader;
153
154 /**
155  * @brief Scan all files in the given directory.
156  * @param[in] directory path to a root directory
157  * @param[in] reader    callback for iteration
158  * @param[in] depth     recursive search depth
159  */
160 void scanFilesInDirectory(const std::string& directory, FileReader reader, unsigned int depth);
161
162 /**
163  * @brief copy smack and ownership.
164  * @param[in] get path
165  * @param[in] set path
166  * @param[in] symbolic link
167  */
168 void copySmackAndOwnership(const std::string& fromPath, const std::string& toPath, bool isSymlink = false);
169
170 /**
171  * @brief create the new directory.
172  * @param[in] source path
173  * @return return true when the directory was created.
174  */
175 bool createDir(const bf::path& path);
176
177 /**
178  * @brief copy the directory.
179  * @param[in] path to the source directory
180  * @param[in] path to the target directory
181  * @param[in] filesystem flag
182  * @return return true when the directory was copied.
183  */
184 bool copyDir(const bf::path& path1, const bf::path& path2, FSFlag flags = FS_NONE);
185
186 /**
187  * @brief copy the file.
188  * @param[in] path to the source file
189  * @param[in] path to the target file
190  * @return return true when the file was copied.
191  */
192 bool copyFile(const bf::path& path1, const bf::path& path2);
193
194 /**
195  * @brief moves of renames the file or directory.
196  * @param[in] path to the source file
197  * @param[in] path to the target file
198  * @return return true when the file was moved.
199  */
200 bool moveFile(const bf::path& path1, const bf::path& path2);
201
202 /**
203  * @brief removes the file or empty directory.
204  * @param[in] source path
205  * @return return true when the file was deleted.
206  */
207 bool removeFile(const bf::path& path);
208
209 /**
210  * @brief removes the file or directory and all its contents.
211  * @param[in] source path
212  * @return return true when the file or directory was deleted.
213  */
214 bool removeAll(const bf::path& path);
215
216 /**
217  * @brief change command name of the current process for access via ps command
218  * @param[in] name
219  */
220 void setCmdName(const std::string& name);
221
222 /**
223  * @brief Get the file name from the specified path.
224  * @remark An empty string will be returned if the path string ends with a path separator character.
225  * @param[in] path  path to a file
226  * @return a substring after the path separator character
227  */
228 std::string getFileName(const std::string& path);
229
230 #endif /* __UTILS_H__ */