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 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
48 bool cmdOptionExists(char** begin, char** end, const std::string& option);
51 * @brief get current executable path
52 * return std::string path
54 std::string readSelfPath();
57 * @brief concat path with PATH_SEPARATOR
58 * @param[in] destination path
59 * @param[in] source path
60 * return std::string result path
62 std::string concatPath(const std::string& path1, const std::string& path2);
65 * @brief get canonicalized absolute Path
66 * @param[in] source path
67 * return std::string result path
69 std::string getAbsolutePath(const std::string& path);
72 * @brief get the directory of file
73 * @param[in] source path
74 * return std::string result path
76 std::string getBaseName(const std::string& path);
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
85 std::string replaceAll(const std::string &str, const std::string &pattern, const std::string &replace);
88 * @brief get root path
89 * @param[in] package id
90 * @param[out] root path
92 int getRootPath(std::string pkgId, std::string& rootPath);
95 * @brief get exec name
96 * @param[in] package id
97 * @param[out] exec name
99 int getExecName(std::string pkgId, std::string& execName);
102 * @brief get metadata value
103 * @param[in] package id
104 * @param[in] metadata key
105 * @param[out] metadata value
107 int getMetadataValue(std::string pkgId, std::string metadataKey, std::string& metadataValue);
110 * @brief split path with ":" delimiter and put that in the vector
111 * @param[in] source path
112 * @param[out] string vector
114 void splitPath(const std::string& path, std::vector<std::string>& out);
117 * @brief check file exists
118 * @param[in] source path
121 bool isFile(const std::string& path);
124 * @brief check directory exists
125 * @param[in] source path
128 bool isDirectory(const std::string& path);
131 * @brief get file size
132 * @param[in] source path
133 * @return size of file
135 uintptr_t getFileSize(const std::string& path);
138 * @brief check the file is managed assembly or not.
139 * @param[in] file path
140 * @return return true when the file is managed assembly.
141 * otherwise return false including native image case.
143 bool isManagedAssembly(const std::string& filePath);
146 * @brief check the file is native image or not.
147 * @param[in] file path
148 * @return return true when the file is native image.
150 bool isNativeImage(const std::string& filePath);
153 * @brief Resolve assembly files from directories and append their paths to the given list.
154 * @remark If a native image exists for an assembly in the same directory, it will be used.
155 * If multiple assemblies of the same name exist, the first one will be used.
156 * @param[in] directories list of directories
157 * @param[out] list colon-separated string of assembly paths
159 void addAssembliesFromDirectories(const std::vector<std::string>& directories, std::string& list);
162 * @brief File search callback
163 * @param[in] path full path to a resolved file
164 * @param[in] filename file name
166 typedef std::function<void (const std::string& path, const std::string& filename)> FileReader;
169 * @brief Scan all files in the given directory.
170 * @param[in] directory path to a root directory
171 * @param[in] reader callback for iteration
172 * @param[in] depth recursive search depth
174 void scanFilesInDirectory(const std::string& directory, FileReader reader, unsigned int depth);
177 * @brief copy smack and ownership.
178 * @param[in] get path
179 * @param[in] set path
180 * @param[in] symbolic link
182 void copySmackAndOwnership(const std::string& fromPath, const std::string& toPath, bool isSymlink = false);
185 * @brief create the new directory.
186 * @param[in] source path
187 * @return return true when the directory was created.
189 bool createDir(const bf::path& path);
192 * @brief copy the directory.
193 * @param[in] path to the source directory
194 * @param[in] path to the target directory
195 * @param[in] filesystem flag
196 * @return return true when the directory was copied.
198 bool copyDir(const bf::path& path1, const bf::path& path2, FSFlag flags = FS_NONE);
201 * @brief copy the file.
202 * @param[in] path to the source file
203 * @param[in] path to the target file
204 * @return return true when the file was copied.
206 bool copyFile(const bf::path& path1, const bf::path& path2);
209 * @brief moves of renames the file or directory.
210 * @param[in] path to the source file
211 * @param[in] path to the target file
212 * @return return true when the file was moved.
214 bool moveFile(const bf::path& path1, const bf::path& path2);
217 * @brief removes the file or empty directory.
218 * @param[in] source path
219 * @return return true when the file was deleted.
221 bool removeFile(const bf::path& path);
224 * @brief removes the file or directory and all its contents.
225 * @param[in] source path
226 * @return return true when the file or directory was deleted.
228 bool removeAll(const bf::path& path);
231 * @brief change command name of the current process for access via ps command
234 void setCmdName(const std::string& name);
237 * @brief Get the file name from the specified path.
238 * @remark An empty string will be returned if the path string ends with a path separator character.
239 * @param[in] path path to a file
240 * @return a substring after the path separator character
242 std::string getFileName(const std::string& path);
244 #endif /* __UTILS_H__ */