* limitations under the License.
*/
-#include <stdio.h>
#include <dirent.h>
+#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <limits.h>
#include <strings.h>
-#include <pthread.h>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <iterator>
#include <sstream>
+#include <map>
+#include "log.h"
#include "utils.h"
#include "path_manager.h"
-#include "log.h"
-
-static pthread_t loggingThread;
static bool iCompare(const std::string& a, int aOffset, const std::string& b, int bOffset, int length)
{
struct dirent* entry;
bool isDir;
+ if (strstr(directory.c_str(), "TAC.Release") != NULL)
+ return; // skip nitool --regen-all-app (--r2r)
+
dir = opendir(directory.c_str());
if (dir == nullptr)
if (depth != 0)
for (auto& d : innerDirectories)
- scanFilesInDir(d.c_str(), reader, depth-1);
+ scanFilesInDir(d.c_str(), reader, depth - 1);
closedir(dir);
}
-static int __pfd[2];
-
-static void *stdlog(void*)
-{
- ssize_t readSize;
- char buf[1024];
-
- while ((readSize = read(__pfd[0], buf, sizeof buf - 1)) > 0) {
- if (buf[readSize - 1] == '\n') {
- --readSize;
- }
-
- buf[readSize] = 0;
+static bool setOwnership(const bf::path& path, uid_t uid, gid_t gid) {
+ int fd = open(path.c_str(), O_RDONLY);
+ if (fd < 0) {
+ _ERR("Can't open directory: %s", path.c_str());
+ return false;
+ }
+ int ret = fchown(fd, uid, gid);
+ close(fd);
+ if (ret != 0) {
+ _ERR("Failed to change owner of: %s", path.c_str());
+ return false;
+ }
+ return true;
+}
- _LOGX("%s", buf);
- }
+static bool setDirPermissions(const bf::path& path, bf::perms permissions) {
+ bs::error_code error;
+ bf::permissions(path, permissions, error);
+ if (error) {
+ _ERR("Failed to set permissions for directory: %s, %s", path.c_str(), error.message().c_str());
+ return false;
+ }
+ return true;
+}
- close(__pfd[0]);
+static bool setDirOwnershipAndPermissions(const bf::path& path, bf::perms permissions, uid_t uid, gid_t gid) {
+ if (!setOwnership(path, uid, gid)) {
+ _ERR("Failed to change owner: %s, (uid: %d, gid: %d)", path.c_str(), uid, gid);
+ return false;
+ }
+ if (!setDirPermissions(path, permissions)) {
+ _ERR("Failed to change permission: %s, (%d)", path.c_str(), permissions);
+ return false;
+ }
+ return true;
+}
- return 0;
+static bool copyOwnershipAndPermissions(const bf::path& path, const bf::path& path2) {
+ if (!bf::exists(path)) {
+ _ERR("Failed to copy ownership and permissions from %s to %s", path.c_str(), path2.c_str());
+ return false;
+ }
+ bf::perms permissions = bf::status(path).permissions();
+ struct stat stats;
+ if (stat(path.c_str(), &stats) != 0) {
+ return false;
+ }
+ if (!setDirOwnershipAndPermissions(path2, permissions, stats.st_uid, stats.st_gid)) {
+ _ERR("Failed to copy ownership and permissions from %s to %s", path.c_str(), path2.c_str());
+ return false;
+ }
+ return true;
}
-int runLoggingThread()
-{
- if (setvbuf(stdout, NULL, _IOLBF, 0) < 0) {
- _DBG("fail to make stdout line-buffered");
- return 0;
- }
-
- if (setvbuf(stderr, NULL, _IONBF, 0) < 0) {
- _DBG("make stderr unbuffered");
- return 0;
+bool createDir(const bf::path& path) {
+ if (bf::exists(path)) {
+ return true;
}
+ bs::error_code error;
+ bf::create_directories(path, error);
+ if (error) {
+ _ERR("Failed to create directory: %s", error.message().c_str());
+ return false;
+ }
+ return true;
+}
- /* create the pipe and redirect stdout and stderr */
- if (pipe(__pfd) < 0) {
- _DBG("fail to create pipe for logging");
- return 0;
- }
-
- // stdout
- if (dup2(__pfd[1], 1) == -1) {
- _DBG("fail to duplicate fd to stdout");
- return 0;
- }
-
- // stderr
- if (dup2(__pfd[1], 2) == -1) {
- _DBG("fail to duplicate fd to stderr");
- return 0;
+bool copyDir(const bf::path& path1, const bf::path& path2, FSFlag flags) {
+ try {
+ // Check whether the function call is valid
+ if (!bf::exists(path1) || !bf::is_directory(path1)) {
+ _ERR("Source directory %s does not exist or is not a directory", path1.c_str());
+ return false;
+ }
+ if (!bf::exists(path2)) {
+ // Create the destination directory
+ if (!createDir(path2)) {
+ _ERR("Unable to create destination directory %s", path2.c_str());
+ return false;
+ }
+ if (flags & FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS) {
+ copyOwnershipAndPermissions(path1, path2);
+ }
+ } else {
+ if (!(flags & (FS_MERGE_SKIP | FS_MERGE_OVERWRITE))) {
+ _ERR("Destination directory %s already exists", path2.c_str());
+ return false;
+ }
+ if (flags & (FS_MERGE_OVERWRITE | FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS)) {
+ copyOwnershipAndPermissions(path1, path2);
+ }
+ }
+ } catch (const bf::filesystem_error& error) {
+ _ERR("Failed to copy directory: %s", error.what());
+ return false;
}
- close(__pfd[1]);
+ // Iterate through the source directory
+ for (bf::directory_iterator file(path1); file != bf::directory_iterator(); ++file) {
+ try {
+ bf::path current(file->path());
+ bf::path target = path2 / current.filename();
+ if (bf::is_symlink(symlink_status(current))) {
+ if ((flags & (FS_MERGE_SKIP | FS_MERGE_OVERWRITE)) && bf::exists(target)) {
+ continue;
+ }
+ bs::error_code error;
+ bf::copy_symlink(current, target, error);
+ if (error) {
+ _ERR("Failed to copy symlink: %s, %s", current.c_str(), error.message().c_str());
+ return false;
+ }
+ } else if (bf::is_directory(current)) {
+ // Found directory: Recursion
+ if (!copyDir(current, target, flags)) {
+ return false;
+ }
+ } else {
+ if ((flags & FS_MERGE_SKIP) && bf::exists(target)) {
+ continue;
+ }
+ bf::path destination = target;
+ if (flags & FS_COMMIT_COPY_FILE) {
+ destination = bf::unique_path(target.parent_path() / "%%%%-%%%%-%%%%-%%%%");
+ }
+ if (flags & FS_MERGE_OVERWRITE) {
+ bf::copy_file(current, destination, bf::copy_option::overwrite_if_exists);
+ } else {
+ bf::copy_file(current, destination);
+ }
+ if (flags & FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS) {
+ copyOwnershipAndPermissions(current, destination);
+ }
+ if (flags & FS_COMMIT_COPY_FILE) {
+ if (flags & FS_MERGE_OVERWRITE) {
+ bf::remove(target);
+ }
+ bf::rename(destination, target);
+ }
+ }
+ } catch (const bf::filesystem_error& error) {
+ _ERR("Failed to copy directory: %s", error.what());
+ return false;
+ }
+ }
+ return true;
+}
- /* spawn the logging thread */
- if (pthread_create(&loggingThread, 0, stdlog, 0) != 0) {
- _DBG("fail to create pthread");
- return -1;
- }
+bool copyFile(const bf::path& path1, const bf::path& path2) {
+ bs::error_code error;
+ bf::copy_file(path1, path2, bf::copy_option::overwrite_if_exists, error);
+ if (error) {
+ _ERR("copy file %s due to error [%s]", path1.c_str(), error.message().c_str());
+ return false;
+ }
+ return true;
+}
- if (pthread_detach(loggingThread) != 0) {
- _DBG("fail to detach pthread");
- return -1;
+bool moveFile(const bf::path& path1, const bf::path& path2) {
+ if (bf::exists(path2)) {
+ return false;
+ }
+ bs::error_code error;
+ bf::rename(path1, path2, error);
+ if (error) {
+ _ERR("Cannot move file: %s. Will copy/remove... with error [%s]", path1.c_str(), error.message().c_str());
+ bf::copy_file(path1, path2, bf::copy_option::overwrite_if_exists, error);
+ if (error) {
+ _ERR("Cannot copy file %s due to error [%s]", path1.c_str(), error.message().c_str());
+ return false;
+ }
+ bf::remove_all(path1, error);
+ if (error) {
+ _ERR("Cannot remove old file when coping: %s with error [%s]", path1.c_str(), error.message().c_str());
+ return false;
+ }
}
+ return true;
+}
- return 0;
+bool removeFile(const bf::path& path) {
+ if (!bf::exists(path)) {
+ return true;
+ }
+ bs::error_code error;
+ bf::remove(path, error);
+ if (error) {
+ _ERR("Cannot remove: %s, %s", path.c_str(), error.message().c_str());
+ return false;
+ }
+ return true;
}
+bool removeAll(const bf::path& path) {
+ if (!exists(path)) {
+ return true;
+ }
+ bs::error_code error;
+ bf::remove_all(path, error);
+ if (error) {
+ _ERR("Cannot remove: %s, %s", path.c_str(), error.message().c_str());
+ return false;
+ }
+ return true;
+}
\ No newline at end of file