2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include <boost/filesystem.hpp>
25 #include <launcher_env.h>
27 #ifndef PATH_SEPARATOR
28 #define PATH_SEPARATOR '/'
31 namespace bf = boost::filesystem;
32 namespace bs = boost::system;
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)
43 * @brief concat path with PATH_SEPARATOR
44 * @param[in] destination path
45 * @param[in] source path
46 * return std::string result path
48 std::string concatPath(const std::string& path1, const std::string& path2);
51 * @brief get canonicalized absolute Path
52 * @param[in] source path
53 * return std::string result path
55 std::string getAbsolutePath(const std::string& path);
58 * @brief get the directory of file
59 * @param[in] source path
60 * return std::string result path
62 std::string getBaseName(const std::string& path);
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
71 std::string replaceAll(const std::string& str, const std::string& pattern, const std::string& replace);
74 * @brief get root path
75 * @param[in] package id
76 * return std::string root path
78 std::string getRootPath(const std::string& pkgId);
81 * @brief get exec name
82 * @param[in] package id
83 * return std::string exec name
85 std::string getExecName(const std::string& pkgId);
89 * @param[in] package id
90 * return std::string app type
92 std::string getAppType(const std::string& pkgId);
95 * @brief get metadata value
96 * @param[in] package id
97 * @param[in] metadata key
98 * return std::string metadata value
100 std::string getMetadataValue(const std::string& pkgId, const std::string& key);
103 * @brief split path with ":" delimiter and put that in the vector
104 * @param[in] source path
105 * @param[out] string vector
107 void splitPath(const std::string& path, std::vector<std::string>& out);
110 * @brief check file exists
111 * in case of symlink file, check both input file and link reference file.
112 * @param[in] source path
115 bool isFile(const std::string& path);
118 * @brief check directory exists
119 * @param[in] source path
122 bool isDirectory(const std::string& path);
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.
130 bool isManagedAssembly(const std::string& filePath);
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.
137 bool isNativeImage(const std::string& filePath);
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
146 void addAssembliesFromDirectories(const std::vector<std::string>& directories, std::string& list);
149 * @brief File search callback
150 * @param[in] path full path to a resolved file
151 * @param[in] filename file name
153 typedef std::function<void (const std::string& path, const std::string& filename)> FileReader;
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
161 void scanFilesInDirectory(const std::string& directory, FileReader reader, unsigned int depth);
164 * @brief copy smack and ownership.
165 * @param[in] get path
166 * @param[in] set path
167 * @param[in] symbolic link
169 void copySmackAndOwnership(const std::string& fromPath, const std::string& toPath, bool isSymlink = false);
172 * @brief check whether the path exists or not.
173 * @param[in] source path
174 * @return return true when the path exist.
176 bool exist(const bf::path& path);
179 * @brief create the new directory.
180 * @param[in] source path
181 * @return return true when the directory was created.
183 bool createDir(const bf::path& path);
186 * @brief copy the directory.
187 * @param[in] path to the source directory
188 * @param[in] path to the target directory
189 * @param[in] filesystem flag
190 * @return return true when the directory was copied.
192 bool copyDir(const bf::path& path1, const bf::path& path2, FSFlag flags = FS_NONE);
195 * @brief copy the file.
196 * @param[in] path to the source file
197 * @param[in] path to the target file
198 * @return return true when the file was copied.
200 bool copyFile(const bf::path& path1, const bf::path& path2);
203 * @brief moves of renames the file or directory.
204 * @param[in] path to the source file
205 * @param[in] path to the target file
206 * @return return true when the file was moved.
208 bool moveFile(const bf::path& path1, const bf::path& path2);
211 * @brief removes the file or empty directory.
212 * @param[in] source path
213 * @return return true when the file was deleted.
215 bool removeFile(const bf::path& path);
218 * @brief removes the file or directory and all its contents.
219 * @param[in] source path
220 * @return return true when the file or directory was deleted.
222 bool removeAll(const bf::path& path);
225 * @brief change command name of the current process for access via ps command
228 void setCmdName(const std::string& name);
231 * @brief Get the file name from the specified path.
232 * @remark An empty string will be returned if the path string ends with a path separator character.
233 * @param[in] path path to a file
234 * @return a substring after the path separator character
236 std::string getFileName(const std::string& path);
239 * @brief Generates a representation called a message digest
240 * @param[in] file path
241 * @return message digest
243 std::string SHA256(const std::string& path);
245 #endif /* __UTILS_H__ */