2019-03-28 17:43:33 +00:00
|
|
|
/*
|
2019-04-08 02:00:49 +00:00
|
|
|
* Copyright (c) 2018-2019 Atmosphère-NX
|
2019-03-28 17:43:33 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#include <switch.h>
|
|
|
|
|
|
|
|
#include "fs_path_utils.hpp"
|
|
|
|
#include "fs_ifilesystem.hpp"
|
|
|
|
|
|
|
|
class FsDirUtils {
|
|
|
|
private:
|
|
|
|
template<typename OnEnterDir, typename OnExitDir, typename OnFile>
|
|
|
|
static Result IterateDirectoryRecursivelyInternal(IFileSystem *fs, FsPath &work_path, FsDirectoryEntry *ent_buf, OnEnterDir on_enter_dir, OnExitDir on_exit_dir, OnFile on_file) {
|
|
|
|
std::unique_ptr<IDirectory> dir;
|
|
|
|
|
|
|
|
/* Open the directory. */
|
2019-06-20 11:04:33 +00:00
|
|
|
R_TRY(fs->OpenDirectory(dir, work_path, DirectoryOpenMode_All));
|
2019-03-28 17:43:33 +00:00
|
|
|
|
2019-04-05 03:04:26 +00:00
|
|
|
const size_t parent_len = strnlen(work_path.str, sizeof(work_path.str) - 1);
|
2019-03-28 17:43:33 +00:00
|
|
|
|
|
|
|
/* Read and handle entries. */
|
|
|
|
while (true) {
|
|
|
|
/* Read a single entry. */
|
|
|
|
u64 read_count;
|
2019-06-20 11:04:33 +00:00
|
|
|
R_TRY(dir->Read(&read_count, ent_buf, 1));
|
2019-03-28 17:43:33 +00:00
|
|
|
|
|
|
|
/* If we're out of entries, we're done. */
|
|
|
|
if (read_count == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
const size_t child_name_len = strnlen(ent_buf->name, sizeof(ent_buf->name) - 1);
|
|
|
|
const bool is_dir = ent_buf->type == ENTRYTYPE_DIR;
|
|
|
|
const size_t child_path_len = parent_len + child_name_len + (is_dir ? 1 : 0);
|
|
|
|
|
|
|
|
/* Validate child path size. */
|
|
|
|
if (child_path_len >= sizeof(work_path.str)) {
|
|
|
|
return ResultFsTooLongPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
strncat(work_path.str, ent_buf->name, sizeof(work_path.str) - 1 - parent_len);
|
|
|
|
if (is_dir) {
|
|
|
|
/* Enter directory. */
|
2019-06-20 11:04:33 +00:00
|
|
|
R_TRY(on_enter_dir(work_path, ent_buf));
|
2019-03-28 17:43:33 +00:00
|
|
|
|
|
|
|
/* Append separator, recurse. */
|
|
|
|
strncat(work_path.str, "/", sizeof(work_path.str) - 1 - parent_len - child_name_len);
|
2019-06-20 11:04:33 +00:00
|
|
|
R_TRY(IterateDirectoryRecursivelyInternal(fs, work_path, ent_buf, on_enter_dir, on_exit_dir, on_file));
|
2019-03-28 17:43:33 +00:00
|
|
|
|
|
|
|
/* Exit directory. */
|
2019-06-20 11:04:33 +00:00
|
|
|
R_TRY(on_exit_dir(work_path, ent_buf));
|
2019-03-28 17:43:33 +00:00
|
|
|
} else {
|
|
|
|
/* Call file handler. */
|
2019-06-20 11:04:33 +00:00
|
|
|
R_TRY(on_file(work_path, ent_buf));
|
2019-03-28 17:43:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Restore parent path. */
|
|
|
|
work_path.str[parent_len] = 0;
|
|
|
|
}
|
|
|
|
|
2019-10-24 08:40:44 +00:00
|
|
|
return ResultSuccess();
|
2019-03-28 17:43:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
template<typename OnEnterDir, typename OnExitDir, typename OnFile>
|
|
|
|
static Result IterateDirectoryRecursively(IFileSystem *fs, const FsPath &root_path, FsPath &work_path, FsDirectoryEntry *ent_buf, OnEnterDir on_enter_dir, OnExitDir on_exit_dir, OnFile on_file) {
|
|
|
|
/* Ensure valid root path. */
|
|
|
|
size_t root_path_len = strnlen(root_path.str, sizeof(root_path.str));
|
|
|
|
if (root_path_len > FS_MAX_PATH - 1 || ((root_path_len == FS_MAX_PATH - 1) && root_path.str[root_path_len-1] != '/')) {
|
|
|
|
return ResultFsTooLongPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy path, ensure terminating separator. */
|
|
|
|
memcpy(work_path.str, root_path.str, root_path_len);
|
|
|
|
if (work_path.str[root_path_len-1] != '/') {
|
|
|
|
root_path_len++;
|
|
|
|
work_path.str[root_path_len-1] = '/';
|
|
|
|
}
|
|
|
|
work_path.str[root_path_len] = 0;
|
|
|
|
|
|
|
|
/* Actually iterate. */
|
|
|
|
return IterateDirectoryRecursivelyInternal(fs, work_path, ent_buf, on_enter_dir, on_exit_dir, on_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Helper for not specifying work path/entry buffer. */
|
|
|
|
template<typename OnEnterDir, typename OnExitDir, typename OnFile>
|
|
|
|
static Result IterateDirectoryRecursively(IFileSystem *fs, const FsPath &root_path, OnEnterDir on_enter_dir, OnExitDir on_exit_dir, OnFile on_file) {
|
|
|
|
FsDirectoryEntry dir_ent = {0};
|
|
|
|
FsPath work_path = {0};
|
|
|
|
return IterateDirectoryRecursively(fs, root_path, work_path, &dir_ent, on_enter_dir, on_exit_dir, on_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Helper for iterating over the filesystem root. */
|
|
|
|
template<typename OnEnterDir, typename OnExitDir, typename OnFile>
|
|
|
|
static Result IterateDirectoryRecursively(IFileSystem *fs, OnEnterDir on_enter_dir, OnExitDir on_exit_dir, OnFile on_file) {
|
|
|
|
return IterateDirectoryRecursively(fs, FsPathUtils::RootPath, on_enter_dir, on_exit_dir, on_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy API. */
|
|
|
|
static Result CopyFile(IFileSystem *dst_fs, IFileSystem *src_fs, const FsPath &dst_parent_path, const FsPath &src_path, const FsDirectoryEntry *dir_ent, void *work_buf, size_t work_buf_size);
|
|
|
|
static Result CopyFile(IFileSystem *fs, const FsPath &dst_parent_path, const FsPath &src_path, const FsDirectoryEntry *dir_ent, void *work_buf, size_t work_buf_size) {
|
|
|
|
return CopyFile(fs, fs, dst_parent_path, src_path, dir_ent, work_buf, work_buf_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Result CopyDirectoryRecursively(IFileSystem *dst_fs, IFileSystem *src_fs, const FsPath &dst_path, const FsPath &src_path, void *work_buf, size_t work_buf_size);
|
|
|
|
static Result CopyDirectoryRecursively(IFileSystem *fs, const FsPath &dst_path, const FsPath &src_path, void *work_buf, size_t work_buf_size) {
|
|
|
|
return CopyDirectoryRecursively(fs, fs, dst_path, src_path, work_buf, work_buf_size);
|
|
|
|
}
|
2019-06-20 11:04:33 +00:00
|
|
|
|
2019-04-05 20:36:38 +00:00
|
|
|
/* Ensure directory existence. */
|
|
|
|
static Result EnsureDirectoryExists(IFileSystem *fs, const FsPath &path);
|
2019-03-28 17:43:33 +00:00
|
|
|
|
|
|
|
/* Other Utility. */
|
|
|
|
template<typename F>
|
|
|
|
static Result RetryUntilTargetNotLocked(F f) {
|
|
|
|
const size_t MaxRetries = 10;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < MaxRetries; i++) {
|
2019-06-20 11:04:33 +00:00
|
|
|
R_TRY_CATCH(f()) {
|
|
|
|
R_CATCH(ResultFsTargetLocked) {
|
|
|
|
/* If target is locked, wait 100ms and try again. */
|
|
|
|
svcSleepThread(100'000'000ul);
|
|
|
|
}
|
|
|
|
} R_END_TRY_CATCH;
|
2019-03-28 17:43:33 +00:00
|
|
|
}
|
|
|
|
|
2019-10-24 08:40:44 +00:00
|
|
|
return ResultSuccess();
|
2019-03-28 17:43:33 +00:00
|
|
|
}
|
|
|
|
};
|