Refactoring the dotnettool (#231)
[platform/core/dotnet/launcher.git] / NativeLauncher / inc / utils.h
index ac3816e..90cc23d 100644 (file)
 #include <string>
 #include <vector>
 #include <functional>
-#include <map>
+#include <boost/filesystem.hpp>
+
+#include <launcher_env.h>
 
 #ifndef PATH_SEPARATOR
 #define PATH_SEPARATOR '/'
 #endif
 
-bool iCompare(const std::string& a, const std::string& b);
-bool iCompare(const std::string& a, int aOffset, const std::string& b, int bOffset, int length);
-bool isManagedAssembly(const std::string& fileName);
-bool isNativeImage(const std::string& fileName);
+namespace bf = boost::filesystem;
+namespace bs = boost::system;
+
+enum FSFlag : int {
+  FS_NONE              = 0,
+  FS_MERGE_SKIP        = (1 << 0),
+  FS_MERGE_OVERWRITE   = (1 << 1),
+  FS_COMMIT_COPY_FILE  = (1 << 2),
+  FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS = (1 << 3)
+};
+
+/**
+ * @brief an iterator to the begin element in the range that compares equal to option
+ * @param[in] begin element
+ * @param[in] end elment
+ * return return true when elements match
+ */
+bool cmdOptionExists(char** begin, char** end, const std::string& option);
+
+/**
+ * @brief get current executable path
+ * return std::string path
+ */
 std::string readSelfPath();
+
+/**
+ * @brief concat path with PATH_SEPARATOR
+ * @param[in] destination path
+ * @param[in] source path
+ * return std::string result path
+ */
 std::string concatPath(const std::string& path1, const std::string& path2);
-void appendPath(std::string& path1, const std::string& path2);
+
+/**
+ * @brief get absolute Path
+ * @param[in] source path
+ * return std::string result path
+ */
 std::string absolutePath(const std::string& path);
+
+/**
+ * @brief get the directory of file
+ * @param[in] source path
+ * return std::string result path
+ */
 std::string baseName(const std::string& path);
-bool endWithIgnoreCase(const std::string& str1, const std::string& str2, std::string& fileName);
-void assembliesInDirectory(const std::vector<std::string>& directories, std::string& tpaList);
 
-bool fileNotExist(const std::string& path);
-std::string joinStrings(const std::vector<std::string>& strings, const char* const delimeter);
+/**
+ * @brief replaces all matching substrings of the regular expression with a given replacement
+ * @param[in] original string
+ * @param[in] pattern to match
+ * @param[in] replacement string
+ * return the modified string
+ */
+std::string replaceAll(const std::string &str, const std::string &pattern, const std::string &replace);
+
+/**
+ * @brief get root path
+ * @param[in] package id
+ * @param[out] root path
+ */
+int getRootPath(std::string pkgId, std::string& rootPath);
+
+/**
+ * @brief get exec name
+ * @param[in] package id
+ * @param[out] exec name
+ */
+int getExecName(std::string pkgId, std::string& execName);
+
+/**
+ * @brief get metadata value
+ * @param[in] package id
+ * @param[in] metadata key
+ * @param[out] metadata value
+ */
+int getMetadataValue(std::string pkgId, std::string metadataKey, std::string& metadataValue);
+
+/**
+ * @brief split path with ":" delimiter and put that in the vector
+ * @param[in] source path
+ * @param[out] string vector
+ */
+void splitPath(const std::string& path, std::vector<std::string>& out);
+
+/**
+ * @brief check file is exist
+ * @param[in] source path
+ * @return bool
+ */
+bool isFileExist(const std::string& path);
+
+/**
+ * @brief check directory is exist
+ * @param[in] source path
+ * @return bool
+ */
+bool isDirectoryExist(const std::string& path);
+
+/**
+ * @brief get file size
+ * @param[in] source path
+ * @return size of file
+ */
+uintptr_t getFileSize(const std::string& path);
+
+/**
+ * @brief check the file is managed assembly or not.
+ * @param[in] file path
+ * @return return true when the file is managed assembly.
+ *         otherwise return false including native image case.
+ */
+bool isManagedAssembly(const std::string& filePath);
+
+/**
+ * @brief check the file is native image or not.
+ * @param[in] file path
+ * @return return true when the file is native image.
+ */
+bool isNativeImage(const std::string& filePath);
+
+/**
+ * @brief Resolve assembly files from directories and append their paths to the given list.
+ * @remark If a native image exists for an assembly in the same directory, it will be used.
+ *         If multiple assemblies of the same name exist, the first one will be used.
+ * @param[in]  directories  list of directories
+ * @param[out] list         colon-separated string of assembly paths
+ */
+void addAssembliesFromDirectories(const std::vector<std::string>& directories, std::string& list);
+
+/**
+ * @brief File search callback
+ * @param[in] path      full path to a resolved file
+ * @param[in] filename  file name
+ */
+typedef std::function<void (const std::string& path, const std::string& filename)> FileReader;
+
+/**
+ * @brief Scan all files in the given directory.
+ * @param[in] directory path to a root directory
+ * @param[in] reader    callback for iteration
+ * @param[in] depth     recursive search depth
+ */
+void scanFilesInDirectory(const std::string& directory, FileReader reader, unsigned int depth);
+
+/**
+ * @brief copy smack and ownership.
+ * @param[in] get path
+ * @param[in] set path
+ * @param[in] symbolic link
+ */
+void copySmackAndOwnership(const std::string& fromPath, const std::string& toPath, bool isSymlink = false);
+
+/**
+ * @brief create the new directory.
+ * @param[in] source path
+ * @return return true when the directory was created.
+ */
+bool createDir(const bf::path& path);
 
-typedef std::function<void (const char*, const char*)> FileReader;
-void scanFilesInDir(const char* directory, FileReader reader, unsigned int depth);
+/**
+ * @brief copy the directory.
+ * @param[in] path to the source directory
+ * @param[in] path to the target directory
+ * @param[in] filesystem flag
+ * @return return true when the directory was copied.
+ */
+bool copyDir(const bf::path& path1, const bf::path& path2, FSFlag flags = FS_NONE);
+
+/**
+ * @brief copy the file.
+ * @param[in] path to the source file
+ * @param[in] path to the target file
+ * @return return true when the file was copied.
+ */
+bool copyFile(const bf::path& path1, const bf::path& path2);
+
+/**
+ * @brief moves of renames the file or directory.
+ * @param[in] path to the source file
+ * @param[in] path to the target file
+ * @return return true when the file was moved.
+ */
+bool moveFile(const bf::path& path1, const bf::path& path2);
+
+/**
+ * @brief removes the file or empty directory.
+ * @param[in] source path
+ * @return return true when the file was deleted.
+ */
+bool removeFile(const bf::path& path);
+
+/**
+ * @brief removes the file or directory and all its contents.
+ * @param[in] source path
+ * @return return true when the file or directory was deleted.
+ */
+bool removeAll(const bf::path& path);
+
+/**
+ * @brief change command name of the current process for access via ps command
+ * @param[in] name
+ */
+void setCmdName(const std::string& name);
+
+/**
+ * @brief Get the file name from the specified path.
+ * @remark An empty string will be returned if the path string ends with a path separator character.
+ * @param[in] path  path to a file
+ * @return a substring after the path separator character
+ */
+std::string getFileName(const std::string& path);
 
-int runLoggingThread();
 #endif /* __UTILS_H__ */