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 * @param[in] source path
114 bool isFile(const std::string& path);
117 * @brief check directory exists
118 * @param[in] source path
121 bool isDirectory(const std::string& path);
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.
129 bool isManagedAssembly(const std::string& filePath);
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.
136 bool isNativeImage(const std::string& filePath);
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
145 void addAssembliesFromDirectories(const std::vector<std::string>& directories, std::string& list);
148 * @brief File search callback
149 * @param[in] path full path to a resolved file
150 * @param[in] filename file name
152 typedef std::function<void (const std::string& path, const std::string& filename)> FileReader;
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
160 void scanFilesInDirectory(const std::string& directory, FileReader reader, unsigned int depth);
163 * @brief copy smack and ownership.
164 * @param[in] get path
165 * @param[in] set path
166 * @param[in] symbolic link
168 void copySmackAndOwnership(const std::string& fromPath, const std::string& toPath, bool isSymlink = false);
171 * @brief create the new directory.
172 * @param[in] source path
173 * @return return true when the directory was created.
175 bool createDir(const bf::path& path);
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.
184 bool copyDir(const bf::path& path1, const bf::path& path2, FSFlag flags = FS_NONE);
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.
192 bool copyFile(const bf::path& path1, const bf::path& path2);
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.
200 bool moveFile(const bf::path& path1, const bf::path& path2);
203 * @brief removes the file or empty directory.
204 * @param[in] source path
205 * @return return true when the file was deleted.
207 bool removeFile(const bf::path& path);
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.
214 bool removeAll(const bf::path& path);
217 * @brief change command name of the current process for access via ps command
220 void setCmdName(const std::string& name);
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
228 std::string getFileName(const std::string& path);
230 #endif /* __UTILS_H__ */