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>
26 #include <launcher_env.h>
28 #ifndef PATH_SEPARATOR
29 #define PATH_SEPARATOR '/'
32 namespace bf = boost::filesystem;
33 namespace bs = boost::system;
37 FS_MERGE_SKIP = (1 << 0),
38 FS_MERGE_OVERWRITE = (1 << 1),
39 FS_COMMIT_COPY_FILE = (1 << 2),
40 FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS = (1 << 3)
44 * @brief an iterator to the begin element in the range that compares equal to option
45 * @param[in] begin element
46 * @param[in] end elment
47 * return return true when elements match
49 bool cmdOptionExists(char** begin, char** end, const std::string& option);
52 * @brief get current executable path
53 * return std::string path
55 std::string readSelfPath();
58 * @brief concat path with PATH_SEPARATOR
59 * @param[in] destination path
60 * @param[in] source path
61 * return std::string result path
63 std::string concatPath(const std::string& path1, const std::string& path2);
66 * @brief get absolute Path
67 * @param[in] source path
68 * return std::string result path
70 std::string absolutePath(const std::string& path);
73 * @brief get the directory of file
74 * @param[in] source path
75 * return std::string result path
77 std::string baseName(const std::string& path);
80 * @brief replaces all matching substrings of the regular expression with a given replacement
81 * @param[in] original string
82 * @param[in] pattern to match
83 * @param[in] replacement string
84 * return the modified string
86 std::string replaceAll(const std::string &str, const std::string &pattern, const std::string &replace);
89 * @brief get root path
90 * @param[in] package id
91 * @param[out] root path
93 int getRootPath(std::string pkgId, std::string& rootPath);
96 * @brief get exec name
97 * @param[in] package id
98 * @param[out] exec name
100 int getExecName(std::string pkgId, std::string& execName);
103 * @brief get metadata value
104 * @param[in] package id
105 * @param[in] metadata key
106 * @param[out] metadata value
108 int getMetadataValue(std::string pkgId, std::string metadataKey, std::string& metadataValue);
111 * @brief split path with ":" delimiter and put that in the vector
112 * @param[in] source path
113 * @param[out] string vector
115 void splitPath(const std::string& path, std::vector<std::string>& out);
118 * @brief check file is exist
119 * @param[in] source path
122 bool isFileExist(const std::string& path);
125 * @brief get file size
126 * @param[in] source path
127 * @return size of file
129 uintptr_t getFileSize(const std::string& path);
132 * @brief check the file is managed assembly or not.
133 * @param[in] file path
134 * @return return true when the file is managed assembly.
135 * otherwise return false including native image case.
137 bool isManagedAssembly(const std::string& filePath);
140 * @brief check the file is native image or not.
141 * @param[in] file path
142 * @return return true when the file is native image.
144 bool isNativeImage(const std::string& filePath);
147 * @brief find assembly files in the directories
148 * @param[in] directories
149 * @param[out] ":" seperated assembly path list
151 void assembliesInDirectory(const std::vector<std::string>& directories, std::string& tpaList);
154 * @brief function pointer for file reader
156 typedef std::function<void (const std::string&, const char*)> FileReader;
159 * @brief scan files in the given directory and run file reader function for that
160 * @param[in] directory
161 * @param[in] file reader function
162 * @param[in] scan depth
164 void scanFilesInDir(const std::string& directory, FileReader reader, unsigned int depth);
167 * @brief update assembly file info.
168 * @param[in] get path
169 * @param[in] set path
170 * @param[in] symbolic link
172 void updateAssemblyInfo(const std::string& getPath, const std::string& setPath, bool isSymlink = false);
175 * @brief create the new directory.
176 * @param[in] source path
177 * @return return true when the directory was created.
179 bool createDir(const bf::path& path);
182 * @brief copy the directory.
183 * @param[in] path to the source directory
184 * @param[in] path to the target directory
185 * @param[in] filesystem flag
186 * @return return true when the directory was copied.
188 bool copyDir(const bf::path& path1, const bf::path& path2, FSFlag flags = FS_NONE);
191 * @brief copy the file.
192 * @param[in] path to the source file
193 * @param[in] path to the target file
194 * @return return true when the file was copied.
196 bool copyFile(const bf::path& path1, const bf::path& path2);
199 * @brief moves of renames the file or directory.
200 * @param[in] path to the source file
201 * @param[in] path to the target file
202 * @return return true when the file was moved.
204 bool moveFile(const bf::path& path1, const bf::path& path2);
207 * @brief removes the file or empty directory.
208 * @param[in] source path
209 * @return return true when the file was deleted.
211 bool removeFile(const bf::path& path);
214 * @brief removes the file or directory and all its contents.
215 * @param[in] source path
216 * @return return true when the file or directory was deleted.
218 bool removeAll(const bf::path& path);
221 * @brief .deps.json file parser
222 * @param[in] package id
223 * @param[in] root path
224 * @param[in] exec name
225 * @param[in] tpa list
226 * @param[in] dotnettool
228 * return std::vector<std::string> parser data
230 std::vector<std::string> depsJsonParser(std::string pkgId, std::string rootPath, std::string execName, std::string tpaList, bool isTool = false, sqlite3 *tac_db = NULL);
232 #endif /* __UTILS_H__ */