2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright 2013-2022 Mnemosyne LLC.
|
2022-08-08 18:05:39 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2016-03-29 16:37:21 +00:00
|
|
|
#pragma once
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2021-12-16 09:43:51 +00:00
|
|
|
#include <cstddef> // size_t
|
2022-04-08 01:50:26 +00:00
|
|
|
#include <cstdint> // uint64_t
|
|
|
|
#include <ctime> // time_t
|
2022-08-02 17:41:04 +00:00
|
|
|
#include <optional>
|
2022-03-21 20:22:50 +00:00
|
|
|
#include <string>
|
2021-11-13 16:08:50 +00:00
|
|
|
#include <string_view>
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2014-11-30 20:08:20 +00:00
|
|
|
#ifdef _WIN32
|
2017-04-19 12:04:45 +00:00
|
|
|
#include <windows.h>
|
2014-07-03 21:58:39 +00:00
|
|
|
#endif
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
#include "tr-macros.h"
|
|
|
|
|
2015-04-11 14:54:01 +00:00
|
|
|
struct tr_error;
|
|
|
|
|
2014-07-03 21:58:39 +00:00
|
|
|
/**
|
|
|
|
* @addtogroup file_io File IO
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2014-07-08 00:15:12 +00:00
|
|
|
#ifndef _WIN32
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/** @brief Platform-specific file descriptor type. */
|
2021-10-06 14:26:07 +00:00
|
|
|
using tr_sys_file_t = int;
|
2017-04-19 12:04:45 +00:00
|
|
|
/** @brief Platform-specific invalid file descriptor constant. */
|
|
|
|
#define TR_BAD_SYS_FILE (-1)
|
|
|
|
/** @brief Platform-specific directory descriptor type. */
|
2021-10-06 14:26:07 +00:00
|
|
|
using tr_sys_dir_t = void*;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2014-07-08 00:15:12 +00:00
|
|
|
#else
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-06 14:26:07 +00:00
|
|
|
using tr_sys_file_t = HANDLE;
|
2017-04-19 12:04:45 +00:00
|
|
|
#define TR_BAD_SYS_FILE INVALID_HANDLE_VALUE
|
2021-10-06 14:26:07 +00:00
|
|
|
struct tr_sys_dir_win32;
|
|
|
|
using tr_sys_dir_t = tr_sys_dir_win32*;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2014-07-08 00:15:12 +00:00
|
|
|
#endif
|
|
|
|
|
2014-09-21 17:52:36 +00:00
|
|
|
/** @brief Platform-specific invalid directory descriptor constant. */
|
2021-09-15 00:18:09 +00:00
|
|
|
#define TR_BAD_SYS_DIR ((tr_sys_dir_t) nullptr)
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2021-10-06 14:26:07 +00:00
|
|
|
enum tr_std_sys_file_t
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
TR_STD_SYS_FILE_IN,
|
|
|
|
TR_STD_SYS_FILE_OUT,
|
|
|
|
TR_STD_SYS_FILE_ERR
|
2021-10-06 14:26:07 +00:00
|
|
|
};
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2021-10-06 14:26:07 +00:00
|
|
|
enum tr_sys_file_open_flags_t
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
TR_SYS_FILE_READ = (1 << 0),
|
|
|
|
TR_SYS_FILE_WRITE = (1 << 1),
|
|
|
|
TR_SYS_FILE_CREATE = (1 << 2),
|
2022-08-28 19:10:00 +00:00
|
|
|
TR_SYS_FILE_APPEND = (1 << 3),
|
|
|
|
TR_SYS_FILE_TRUNCATE = (1 << 4),
|
|
|
|
TR_SYS_FILE_SEQUENTIAL = (1 << 5)
|
2021-10-06 14:26:07 +00:00
|
|
|
};
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2021-10-06 14:26:07 +00:00
|
|
|
enum tr_sys_file_lock_flags_t
|
2016-09-21 20:56:03 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
TR_SYS_FILE_LOCK_SH = (1 << 0),
|
|
|
|
TR_SYS_FILE_LOCK_EX = (1 << 1),
|
|
|
|
TR_SYS_FILE_LOCK_NB = (1 << 2),
|
|
|
|
TR_SYS_FILE_LOCK_UN = (1 << 3)
|
2021-10-06 14:26:07 +00:00
|
|
|
};
|
2016-09-21 20:56:03 +00:00
|
|
|
|
2021-10-06 14:26:07 +00:00
|
|
|
enum tr_sys_path_get_info_flags_t
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
TR_SYS_PATH_NO_FOLLOW = (1 << 0)
|
2021-10-06 14:26:07 +00:00
|
|
|
};
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2021-10-06 14:26:07 +00:00
|
|
|
enum tr_sys_file_advice_t
|
2017-07-08 13:09:37 +00:00
|
|
|
{
|
|
|
|
TR_SYS_FILE_ADVICE_WILL_NEED,
|
|
|
|
TR_SYS_FILE_ADVICE_DONT_NEED
|
2021-10-06 14:26:07 +00:00
|
|
|
};
|
2017-07-08 13:09:37 +00:00
|
|
|
|
2021-10-06 14:26:07 +00:00
|
|
|
enum tr_sys_file_preallocate_flags_t
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
TR_SYS_FILE_PREALLOC_SPARSE = (1 << 0)
|
2021-10-06 14:26:07 +00:00
|
|
|
};
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2021-10-06 14:26:07 +00:00
|
|
|
enum tr_sys_dir_create_flags_t
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
TR_SYS_DIR_CREATE_PARENTS = (1 << 0)
|
2021-10-06 14:26:07 +00:00
|
|
|
};
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2021-10-06 14:26:07 +00:00
|
|
|
enum tr_sys_path_type_t
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
TR_SYS_PATH_IS_FILE,
|
|
|
|
TR_SYS_PATH_IS_DIRECTORY,
|
|
|
|
TR_SYS_PATH_IS_OTHER
|
2021-10-06 14:26:07 +00:00
|
|
|
};
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2021-10-06 14:26:07 +00:00
|
|
|
struct tr_sys_path_info
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2021-10-06 14:26:07 +00:00
|
|
|
tr_sys_path_type_t type = {};
|
2022-08-02 17:41:04 +00:00
|
|
|
uint64_t size = {};
|
|
|
|
time_t last_modified_at = {};
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto isFile() const noexcept
|
|
|
|
{
|
|
|
|
return type == TR_SYS_PATH_IS_FILE;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto isFolder() const noexcept
|
|
|
|
{
|
|
|
|
return type == TR_SYS_PATH_IS_DIRECTORY;
|
|
|
|
}
|
2021-10-06 14:26:07 +00:00
|
|
|
};
|
2014-07-03 21:58:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Platform-specific wrapper functions
|
|
|
|
*
|
|
|
|
* Following functions accept paths in UTF-8 encoding and convert them to native
|
|
|
|
* encoding internally if needed.
|
2014-09-21 17:52:36 +00:00
|
|
|
* Descriptors returned (@ref tr_sys_file_t and @ref tr_sys_dir_t) may have
|
|
|
|
* different type depending on platform and should generally not be passed to
|
|
|
|
* native functions, but to wrapper functions instead.
|
2014-07-03 21:58:39 +00:00
|
|
|
*
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Path-related wrappers */
|
|
|
|
|
Add in-kernel file copying for several platforms. (#1092)
* Add in-kernel copying support for Linux (sendfile64(2), copy_file_range(2)), FreeBSD 13 (copy_file_range(2)), MacOS (copyfile(2)), and Windows (CopyFileExA).
* Fix macro name USE_COPY_FILE_RANGE.
* Minor bugfixes for userspace fallback.
* Fix linux sendfile64 bugs.
* Remove some overzealous asserts.
* Allow transmission-test-copy to take an optional argument for an external reference file.
* Fix return value error of tr_sys_path_copy.
* Use COPYFILE_ALL for Macs without COPYFILE_CLONE.
* Add in-kernel file copying for several platforms.
Numerous operating systems now have support for copying files directly in the
kernel, which is generally more efficient than copying in a userspace read(2)/
write(2) loop. (This becomes particularly relevant for 4th gen PCI-E storage,
which approaches the latency of DRAM.) For Linux I use sendfile64(2), and, for
later kernels, copy_file_range(2). FreeBSD 13 will also support
copy_file_range(2). MacOS has copyfile(2), and Windows has CopyFileExA.
Operating systems lacking such a syscall continue to use the existing
read(2)/write(2) loop.
* Appease uncrustify.
* Appease uncrustify.
* copy-test: generate random content at run time.
* copy-test: Stylistic changes and more check()s.
* copy-test: files_are_identical should follow test idioms
* tr_sys_path_copy: numerous tweaks as requested by review.
* s/old file/source file; s/new file/destination file.
* tr_sys_path_copy: handle win32 wide characters in paths.
* Uncrustify.
* test-copy: Use non-string create_file_with_contents.
* tr_sys_path_copy: numerous fixes.
Per review: generate test file content at runtime; tidy use of check();
fix style; re-measure file sizes in the copy; define a macro when the
system does not provide it; use Unicode APIs on Windows; and fix
documentation.
* Updated as per comments.
* Rebase kernel-copy changes onto 3.0 with gtest.
* Undo irrelevant comment change.
* Fix syntax error.
* Use tr_malloc() instead of tr_valloc().
* Use EXPECT instead of TR_ASSERT in gtest.
* Add error handling.
* Acceptable coding style has changed again.
Now it's camelCase. Also use nullptr instead of NULL, etc.
* Fix east/west const.
Co-authored-by: Mike Gelfand <mikedld@users.noreply.github.com>
2021-08-07 09:04:03 +00:00
|
|
|
/**
|
|
|
|
* @brief Portability wrapper for various in-kernel file copy functions, with a
|
|
|
|
* fallback to a userspace read/write loop.
|
|
|
|
*
|
|
|
|
* @param[in] src_path Path to source file.
|
|
|
|
* @param[in] dst_path Path to destination file.
|
2021-10-06 16:32:17 +00:00
|
|
|
* @param[out] error Pointer to error object. Optional, pass `nullptr` if
|
|
|
|
* you are not interested in error details.
|
Add in-kernel file copying for several platforms. (#1092)
* Add in-kernel copying support for Linux (sendfile64(2), copy_file_range(2)), FreeBSD 13 (copy_file_range(2)), MacOS (copyfile(2)), and Windows (CopyFileExA).
* Fix macro name USE_COPY_FILE_RANGE.
* Minor bugfixes for userspace fallback.
* Fix linux sendfile64 bugs.
* Remove some overzealous asserts.
* Allow transmission-test-copy to take an optional argument for an external reference file.
* Fix return value error of tr_sys_path_copy.
* Use COPYFILE_ALL for Macs without COPYFILE_CLONE.
* Add in-kernel file copying for several platforms.
Numerous operating systems now have support for copying files directly in the
kernel, which is generally more efficient than copying in a userspace read(2)/
write(2) loop. (This becomes particularly relevant for 4th gen PCI-E storage,
which approaches the latency of DRAM.) For Linux I use sendfile64(2), and, for
later kernels, copy_file_range(2). FreeBSD 13 will also support
copy_file_range(2). MacOS has copyfile(2), and Windows has CopyFileExA.
Operating systems lacking such a syscall continue to use the existing
read(2)/write(2) loop.
* Appease uncrustify.
* Appease uncrustify.
* copy-test: generate random content at run time.
* copy-test: Stylistic changes and more check()s.
* copy-test: files_are_identical should follow test idioms
* tr_sys_path_copy: numerous tweaks as requested by review.
* s/old file/source file; s/new file/destination file.
* tr_sys_path_copy: handle win32 wide characters in paths.
* Uncrustify.
* test-copy: Use non-string create_file_with_contents.
* tr_sys_path_copy: numerous fixes.
Per review: generate test file content at runtime; tidy use of check();
fix style; re-measure file sizes in the copy; define a macro when the
system does not provide it; use Unicode APIs on Windows; and fix
documentation.
* Updated as per comments.
* Rebase kernel-copy changes onto 3.0 with gtest.
* Undo irrelevant comment change.
* Fix syntax error.
* Use tr_malloc() instead of tr_valloc().
* Use EXPECT instead of TR_ASSERT in gtest.
* Add error handling.
* Acceptable coding style has changed again.
Now it's camelCase. Also use nullptr instead of NULL, etc.
* Fix east/west const.
Co-authored-by: Mike Gelfand <mikedld@users.noreply.github.com>
2021-08-07 09:04:03 +00:00
|
|
|
*
|
|
|
|
* @return `True` on success, `false` otherwise (with `error` set accordingly).
|
|
|
|
*/
|
2022-03-27 17:37:29 +00:00
|
|
|
bool tr_sys_path_copy(char const* src_path, char const* dst_path, struct tr_error** error = nullptr);
|
Add in-kernel file copying for several platforms. (#1092)
* Add in-kernel copying support for Linux (sendfile64(2), copy_file_range(2)), FreeBSD 13 (copy_file_range(2)), MacOS (copyfile(2)), and Windows (CopyFileExA).
* Fix macro name USE_COPY_FILE_RANGE.
* Minor bugfixes for userspace fallback.
* Fix linux sendfile64 bugs.
* Remove some overzealous asserts.
* Allow transmission-test-copy to take an optional argument for an external reference file.
* Fix return value error of tr_sys_path_copy.
* Use COPYFILE_ALL for Macs without COPYFILE_CLONE.
* Add in-kernel file copying for several platforms.
Numerous operating systems now have support for copying files directly in the
kernel, which is generally more efficient than copying in a userspace read(2)/
write(2) loop. (This becomes particularly relevant for 4th gen PCI-E storage,
which approaches the latency of DRAM.) For Linux I use sendfile64(2), and, for
later kernels, copy_file_range(2). FreeBSD 13 will also support
copy_file_range(2). MacOS has copyfile(2), and Windows has CopyFileExA.
Operating systems lacking such a syscall continue to use the existing
read(2)/write(2) loop.
* Appease uncrustify.
* Appease uncrustify.
* copy-test: generate random content at run time.
* copy-test: Stylistic changes and more check()s.
* copy-test: files_are_identical should follow test idioms
* tr_sys_path_copy: numerous tweaks as requested by review.
* s/old file/source file; s/new file/destination file.
* tr_sys_path_copy: handle win32 wide characters in paths.
* Uncrustify.
* test-copy: Use non-string create_file_with_contents.
* tr_sys_path_copy: numerous fixes.
Per review: generate test file content at runtime; tidy use of check();
fix style; re-measure file sizes in the copy; define a macro when the
system does not provide it; use Unicode APIs on Windows; and fix
documentation.
* Updated as per comments.
* Rebase kernel-copy changes onto 3.0 with gtest.
* Undo irrelevant comment change.
* Fix syntax error.
* Use tr_malloc() instead of tr_valloc().
* Use EXPECT instead of TR_ASSERT in gtest.
* Add error handling.
* Acceptable coding style has changed again.
Now it's camelCase. Also use nullptr instead of NULL, etc.
* Fix east/west const.
Co-authored-by: Mike Gelfand <mikedld@users.noreply.github.com>
2021-08-07 09:04:03 +00:00
|
|
|
|
2014-07-03 21:58:39 +00:00
|
|
|
/**
|
2017-04-21 07:40:57 +00:00
|
|
|
* @brief Portability wrapper for `stat()`.
|
2014-07-03 21:58:39 +00:00
|
|
|
*
|
|
|
|
* @param[in] path Path to file or directory.
|
|
|
|
* @param[in] flags Combination of @ref tr_sys_path_get_info_flags_t values.
|
2021-10-06 16:32:17 +00:00
|
|
|
* @param[out] error Pointer to error object. Optional, pass `nullptr` if you
|
|
|
|
* are not interested in error details.
|
2014-07-03 21:58:39 +00:00
|
|
|
*
|
2022-08-02 17:41:04 +00:00
|
|
|
* @return info on success, or nullopt with `error` set accordingly.
|
2014-07-03 21:58:39 +00:00
|
|
|
*/
|
2022-08-02 17:41:04 +00:00
|
|
|
[[nodiscard]] std::optional<tr_sys_path_info> tr_sys_path_get_info(
|
|
|
|
std::string_view path,
|
|
|
|
int flags = 0,
|
|
|
|
tr_error** error = nullptr);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
|
|
|
/**
|
2017-04-21 07:40:57 +00:00
|
|
|
* @brief Portability wrapper for `access()`.
|
2014-07-03 21:58:39 +00:00
|
|
|
*
|
|
|
|
* @param[in] path Path to file or directory.
|
2021-10-06 16:32:17 +00:00
|
|
|
* @param[out] error Pointer to error object. Optional, pass `nullptr` if you
|
|
|
|
* are not interested in error details.
|
2014-07-03 21:58:39 +00:00
|
|
|
*
|
|
|
|
* @return `True` if path exists, `false` otherwise. Note that `false` will also
|
|
|
|
* be returned in case of error; if you need to distinguish the two,
|
2021-10-06 16:32:17 +00:00
|
|
|
* check if `error` is `nullptr` afterwards.
|
2014-07-03 21:58:39 +00:00
|
|
|
*/
|
2022-03-27 17:37:29 +00:00
|
|
|
bool tr_sys_path_exists(char const* path, struct tr_error** error = nullptr);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2022-11-11 17:18:17 +00:00
|
|
|
template<typename T, typename = decltype(&T::c_str)>
|
2022-05-22 01:17:00 +00:00
|
|
|
bool tr_sys_path_exists(T const& path, struct tr_error** error = nullptr)
|
|
|
|
{
|
|
|
|
return tr_sys_path_exists(path.c_str(), error);
|
|
|
|
}
|
|
|
|
|
2015-10-20 21:22:19 +00:00
|
|
|
/**
|
|
|
|
* @brief Check whether path is relative.
|
|
|
|
*
|
|
|
|
* This function only analyzes the string, so no error reporting is needed.
|
|
|
|
*
|
|
|
|
* @param[in] path Path to file or directory.
|
|
|
|
*
|
|
|
|
* @return `True` if path is relative, `false` otherwise
|
|
|
|
*/
|
2021-11-13 16:08:50 +00:00
|
|
|
bool tr_sys_path_is_relative(std::string_view path);
|
2015-10-20 21:22:19 +00:00
|
|
|
|
2014-07-03 21:58:39 +00:00
|
|
|
/**
|
|
|
|
* @brief Test to see if the two filenames point to the same file.
|
|
|
|
*
|
2021-10-06 16:32:17 +00:00
|
|
|
* @param[in] path1 Path to first file or directory.
|
|
|
|
* @param[in] path2 Path to second file or directory.
|
|
|
|
* @param[out] error Pointer to error object. Optional, pass `nullptr` if
|
|
|
|
* you are not interested in error details.
|
2014-07-03 21:58:39 +00:00
|
|
|
*
|
|
|
|
* @return `True` if two paths point to the same file or directory, `false`
|
|
|
|
* otherwise. Note that `false` will also be returned in case of error;
|
2021-10-06 16:32:17 +00:00
|
|
|
* if you need to distinguish the two, check if `error` is `nullptr`
|
2014-07-03 21:58:39 +00:00
|
|
|
* afterwards.
|
|
|
|
*/
|
2022-03-27 17:37:29 +00:00
|
|
|
bool tr_sys_path_is_same(char const* path1, char const* path2, struct tr_error** error = nullptr);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2022-11-11 17:18:17 +00:00
|
|
|
template<typename T, typename U, typename = decltype(&T::c_str), typename = decltype(&U::c_str)>
|
2022-05-22 01:17:00 +00:00
|
|
|
bool tr_sys_path_is_same(T const& path1, U const& path2, struct tr_error** error = nullptr)
|
|
|
|
{
|
|
|
|
return tr_sys_path_is_same(path1.c_str(), path2.c_str(), error);
|
|
|
|
}
|
|
|
|
|
2014-07-03 21:58:39 +00:00
|
|
|
/**
|
2017-04-21 07:40:57 +00:00
|
|
|
* @brief Portability wrapper for `realpath()`.
|
2014-07-03 21:58:39 +00:00
|
|
|
*
|
|
|
|
* @param[in] path Path to file or directory.
|
2021-10-06 16:32:17 +00:00
|
|
|
* @param[out] error Pointer to error object. Optional, pass `nullptr` if you
|
|
|
|
* are not interested in error details.
|
2014-07-03 21:58:39 +00:00
|
|
|
*
|
2022-08-05 16:36:01 +00:00
|
|
|
* @return Full path with symbolic links, `.` and `..` resolved on success,
|
|
|
|
* or an empty string otherwise (with `error` set accordingly).
|
2014-07-03 21:58:39 +00:00
|
|
|
*/
|
2022-08-05 16:36:01 +00:00
|
|
|
std::string tr_sys_path_resolve(std::string_view path, struct tr_error** error = nullptr);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
|
|
|
/**
|
2017-04-21 07:40:57 +00:00
|
|
|
* @brief Portability wrapper for `basename()`.
|
2014-07-03 21:58:39 +00:00
|
|
|
*
|
|
|
|
* @param[in] path Path to file or directory.
|
2021-10-06 16:32:17 +00:00
|
|
|
* @param[out] error Pointer to error object. Optional, pass `nullptr` if you
|
|
|
|
* are not interested in error details.
|
2014-07-03 21:58:39 +00:00
|
|
|
*
|
2022-08-04 13:44:18 +00:00
|
|
|
* @return base name (last path component; parent path removed) on success,
|
|
|
|
* or empty string otherwise (with `error` set accordingly).
|
2014-07-03 21:58:39 +00:00
|
|
|
*/
|
2022-11-15 14:29:00 +00:00
|
|
|
std::string_view tr_sys_path_basename(std::string_view path, struct tr_error** error = nullptr);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
|
|
|
/**
|
2017-04-21 07:40:57 +00:00
|
|
|
* @brief Portability wrapper for `dirname()`.
|
2014-07-03 21:58:39 +00:00
|
|
|
*
|
|
|
|
* @param[in] path Path to file or directory.
|
|
|
|
*
|
2022-08-04 13:44:18 +00:00
|
|
|
* @return parent path substring of `path` (last path component removed) on
|
|
|
|
* success, or empty string otherwise with `error` set accordingly).
|
2014-07-03 21:58:39 +00:00
|
|
|
*/
|
2022-05-21 16:10:58 +00:00
|
|
|
std::string_view tr_sys_path_dirname(std::string_view path);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
|
|
|
/**
|
2017-04-21 07:40:57 +00:00
|
|
|
* @brief Portability wrapper for `rename()`.
|
2014-07-03 21:58:39 +00:00
|
|
|
*
|
|
|
|
* @param[in] src_path Path to source file or directory.
|
|
|
|
* @param[in] dst_path Path to destination file or directory.
|
2021-10-06 16:32:17 +00:00
|
|
|
* @param[out] error Pointer to error object. Optional, pass `nullptr` if you
|
2014-07-03 21:58:39 +00:00
|
|
|
* are not interested in error details.
|
|
|
|
*
|
|
|
|
* @return `True` on success, `false` otherwise (with `error` set accordingly).
|
|
|
|
* Rename will generally only succeed if both source and destination are
|
|
|
|
* on the same partition.
|
|
|
|
*/
|
2022-03-27 17:37:29 +00:00
|
|
|
bool tr_sys_path_rename(char const* src_path, char const* dst_path, struct tr_error** error = nullptr);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2022-11-11 17:18:17 +00:00
|
|
|
template<typename T, typename U, typename = decltype(&T::c_str), typename = decltype(&U::c_str)>
|
2022-05-22 01:17:00 +00:00
|
|
|
bool tr_sys_path_rename(T const& src_path, U const& dst_path, struct tr_error** error = nullptr)
|
|
|
|
{
|
|
|
|
return tr_sys_path_rename(src_path.c_str(), dst_path.c_str(), error);
|
|
|
|
}
|
|
|
|
|
2014-07-03 21:58:39 +00:00
|
|
|
/**
|
2017-04-21 07:40:57 +00:00
|
|
|
* @brief Portability wrapper for `remove()`.
|
2014-07-03 21:58:39 +00:00
|
|
|
*
|
|
|
|
* @param[in] path Path to file or directory.
|
2021-10-06 16:32:17 +00:00
|
|
|
* @param[out] error Pointer to error object. Optional, pass `nullptr` if you
|
|
|
|
* are not interested in error details.
|
2014-07-03 21:58:39 +00:00
|
|
|
*
|
|
|
|
* @return `True` on success, `false` otherwise (with `error` set accordingly).
|
|
|
|
* Directory removal will only succeed if it is empty (contains no other
|
|
|
|
* files and directories).
|
|
|
|
*/
|
2022-03-27 17:37:29 +00:00
|
|
|
bool tr_sys_path_remove(char const* path, struct tr_error** error = nullptr);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2022-11-11 17:18:17 +00:00
|
|
|
template<typename T, typename = decltype(&T::c_str)>
|
2022-05-22 01:17:00 +00:00
|
|
|
bool tr_sys_path_remove(T const& path, struct tr_error** error = nullptr)
|
|
|
|
{
|
|
|
|
return tr_sys_path_remove(path.c_str(), error);
|
|
|
|
}
|
|
|
|
|
2017-11-27 22:22:44 +00:00
|
|
|
/**
|
|
|
|
* @brief Transform path separators to native ones, in-place.
|
|
|
|
*
|
|
|
|
* @param[in,out] path Path to transform.
|
|
|
|
*
|
|
|
|
* @return Same path but with native (and uniform) separators.
|
|
|
|
*/
|
|
|
|
char* tr_sys_path_native_separators(char* path);
|
|
|
|
|
2014-07-08 00:15:12 +00:00
|
|
|
/* File-related wrappers */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get handle to one of standard I/O files.
|
|
|
|
*
|
|
|
|
* @param[in] std_file Standard file identifier.
|
2021-10-06 16:32:17 +00:00
|
|
|
* @param[out] error Pointer to error object. Optional, pass `nullptr` if you
|
2014-07-08 00:15:12 +00:00
|
|
|
* are not interested in error details.
|
|
|
|
*
|
|
|
|
* @return Opened file descriptor on success, `TR_BAD_SYS_FILE` otherwise (with
|
|
|
|
* `error` set accordingly). DO NOT pass this descriptor to
|
|
|
|
* @ref tr_sys_file_close (unless you know what you are doing).
|
|
|
|
*/
|
2022-03-27 17:37:29 +00:00
|
|
|
tr_sys_file_t tr_sys_file_get_std(tr_std_sys_file_t std_file, struct tr_error** error = nullptr);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
|
|
|
/**
|
2017-04-21 07:40:57 +00:00
|
|
|
* @brief Portability wrapper for `open()`.
|
2014-07-08 00:15:12 +00:00
|
|
|
*
|
|
|
|
* @param[in] path Path to file.
|
|
|
|
* @param[in] flags Combination of @ref tr_sys_file_open_flags_t values.
|
|
|
|
* @param[in] permissions Permissions to create file with (in case
|
|
|
|
@ref TR_SYS_FILE_CREATE is used). Not used on Windows.
|
2021-10-06 16:32:17 +00:00
|
|
|
* @param[out] error Pointer to error object. Optional, pass `nullptr` if
|
|
|
|
* you are not interested in error details.
|
2014-07-08 00:15:12 +00:00
|
|
|
*
|
|
|
|
* @return Opened file descriptor on success, `TR_BAD_SYS_FILE` otherwise (with
|
|
|
|
* `error` set accordingly).
|
|
|
|
*/
|
2022-03-27 17:37:29 +00:00
|
|
|
tr_sys_file_t tr_sys_file_open(char const* path, int flags, int permissions, struct tr_error** error = nullptr);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
|
|
|
/**
|
2017-04-21 07:40:57 +00:00
|
|
|
* @brief Portability wrapper for `mkstemp()`.
|
2014-07-08 00:15:12 +00:00
|
|
|
*
|
|
|
|
* @param[in,out] path_template Template path to file. Should end with at least
|
|
|
|
* six 'X' characters. Upon success, trailing 'X'
|
|
|
|
* characters are replaced with actual random
|
|
|
|
* characters used to form a unique path to
|
|
|
|
* temporary file.
|
2021-10-06 16:32:17 +00:00
|
|
|
* @param[out] error Pointer to error object. Optional, pass `nullptr`
|
2014-07-08 00:15:12 +00:00
|
|
|
* if you are not interested in error details.
|
|
|
|
*
|
|
|
|
* @return Opened file descriptor on success, `TR_BAD_SYS_FILE` otherwise (with
|
|
|
|
* `error` set accordingly).
|
|
|
|
*/
|
2022-03-27 17:37:29 +00:00
|
|
|
tr_sys_file_t tr_sys_file_open_temp(char* path_template, struct tr_error** error = nullptr);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
|
|
|
/**
|
2017-04-21 07:40:57 +00:00
|
|
|
* @brief Portability wrapper for `close()`.
|
2014-07-08 00:15:12 +00:00
|
|
|
*
|
|
|
|
* @param[in] handle Valid file descriptor.
|
2021-10-06 16:32:17 +00:00
|
|
|
* @param[out] error Pointer to error object. Optional, pass `nullptr` if you
|
|
|
|
* are not interested in error details.
|
2014-07-08 00:15:12 +00:00
|
|
|
*
|
|
|
|
* @return `True` on success, `false` otherwise (with `error` set accordingly).
|
|
|
|
*/
|
2022-03-27 17:37:29 +00:00
|
|
|
bool tr_sys_file_close(tr_sys_file_t handle, struct tr_error** error = nullptr);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
|
|
|
/**
|
2017-04-21 07:40:57 +00:00
|
|
|
* @brief Portability wrapper for `read()`.
|
2014-07-08 00:15:12 +00:00
|
|
|
*
|
|
|
|
* @param[in] handle Valid file descriptor.
|
|
|
|
* @param[out] buffer Buffer to store read data to.
|
|
|
|
* @param[in] size Number of bytes to read.
|
2021-10-06 16:32:17 +00:00
|
|
|
* @param[out] bytes_read Number of bytes actually read. Optional, pass `nullptr`
|
2014-07-08 00:15:12 +00:00
|
|
|
* if you are not interested.
|
2021-10-06 16:32:17 +00:00
|
|
|
* @param[out] error Pointer to error object. Optional, pass `nullptr` if
|
|
|
|
* you are not interested in error details.
|
2014-07-08 00:15:12 +00:00
|
|
|
*
|
|
|
|
* @return `True` on success, `false` otherwise (with `error` set accordingly).
|
|
|
|
*/
|
2022-03-27 17:37:29 +00:00
|
|
|
bool tr_sys_file_read(
|
|
|
|
tr_sys_file_t handle,
|
|
|
|
void* buffer,
|
|
|
|
uint64_t size,
|
|
|
|
uint64_t* bytes_read,
|
|
|
|
struct tr_error** error = nullptr);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
|
|
|
/**
|
2017-04-21 07:40:57 +00:00
|
|
|
* @brief Like `pread()`, except that the position is undefined afterwards.
|
2014-07-08 00:15:12 +00:00
|
|
|
* Not thread-safe.
|
|
|
|
*
|
|
|
|
* @param[in] handle Valid file descriptor.
|
|
|
|
* @param[out] buffer Buffer to store read data to.
|
|
|
|
* @param[in] size Number of bytes to read.
|
|
|
|
* @param[in] offset File offset in bytes to start reading from.
|
2021-10-06 16:32:17 +00:00
|
|
|
* @param[out] bytes_read Number of bytes actually read. Optional, pass `nullptr`
|
2014-07-08 00:15:12 +00:00
|
|
|
* if you are not interested.
|
2021-10-06 16:32:17 +00:00
|
|
|
* @param[out] error Pointer to error object. Optional, pass `nullptr` if
|
|
|
|
* you are not interested in error details.
|
2014-07-08 00:15:12 +00:00
|
|
|
*
|
|
|
|
* @return `True` on success, `false` otherwise (with `error` set accordingly).
|
|
|
|
*/
|
2021-08-15 09:41:48 +00:00
|
|
|
bool tr_sys_file_read_at(
|
|
|
|
tr_sys_file_t handle,
|
|
|
|
void* buffer,
|
|
|
|
uint64_t size,
|
|
|
|
uint64_t offset,
|
|
|
|
uint64_t* bytes_read,
|
2022-03-27 17:37:29 +00:00
|
|
|
struct tr_error** error = nullptr);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
|
|
|
/**
|
2017-04-21 07:40:57 +00:00
|
|
|
* @brief Portability wrapper for `write()`.
|
2014-07-08 00:15:12 +00:00
|
|
|
*
|
|
|
|
* @param[in] handle Valid file descriptor.
|
|
|
|
* @param[in] buffer Buffer to get data being written from.
|
|
|
|
* @param[in] size Number of bytes to write.
|
|
|
|
* @param[out] bytes_written Number of bytes actually written. Optional, pass
|
2021-10-06 16:32:17 +00:00
|
|
|
* `nullptr` if you are not interested.
|
|
|
|
* @param[out] error Pointer to error object. Optional, pass `nullptr` if
|
2014-07-08 00:15:12 +00:00
|
|
|
* you are not interested in error details.
|
|
|
|
*
|
|
|
|
* @return `True` on success, `false` otherwise (with `error` set accordingly).
|
|
|
|
*/
|
2021-08-15 09:41:48 +00:00
|
|
|
bool tr_sys_file_write(
|
|
|
|
tr_sys_file_t handle,
|
|
|
|
void const* buffer,
|
|
|
|
uint64_t size,
|
|
|
|
uint64_t* bytes_written,
|
2022-03-27 17:37:29 +00:00
|
|
|
struct tr_error** error = nullptr);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
|
|
|
/**
|
2017-04-21 07:40:57 +00:00
|
|
|
* @brief Like `pwrite()`, except that the position is undefined afterwards.
|
2014-07-08 00:15:12 +00:00
|
|
|
* Not thread-safe.
|
|
|
|
*
|
|
|
|
* @param[in] handle Valid file descriptor.
|
|
|
|
* @param[in] buffer Buffer to get data being written from.
|
|
|
|
* @param[in] size Number of bytes to write.
|
|
|
|
* @param[in] offset File offset in bytes to start writing from.
|
|
|
|
* @param[out] bytes_written Number of bytes actually written. Optional, pass
|
2021-10-06 16:32:17 +00:00
|
|
|
* `nullptr` if you are not interested.
|
|
|
|
* @param[out] error Pointer to error object. Optional, pass `nullptr`
|
|
|
|
* if you are not interested in error details.
|
2014-07-08 00:15:12 +00:00
|
|
|
*
|
|
|
|
* @return `True` on success, `false` otherwise (with `error` set accordingly).
|
|
|
|
*/
|
2021-08-15 09:41:48 +00:00
|
|
|
bool tr_sys_file_write_at(
|
|
|
|
tr_sys_file_t handle,
|
|
|
|
void const* buffer,
|
|
|
|
uint64_t size,
|
|
|
|
uint64_t offset,
|
|
|
|
uint64_t* bytes_written,
|
2022-03-27 17:37:29 +00:00
|
|
|
struct tr_error** error = nullptr);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
|
|
|
/**
|
2017-04-21 07:40:57 +00:00
|
|
|
* @brief Portability wrapper for `fsync()`.
|
2014-07-08 00:15:12 +00:00
|
|
|
*
|
|
|
|
* @param[in] handle Valid file descriptor.
|
2021-10-06 16:32:17 +00:00
|
|
|
* @param[out] error Pointer to error object. Optional, pass `nullptr` if you
|
|
|
|
* are not interested in error details.
|
2014-07-08 00:15:12 +00:00
|
|
|
*
|
|
|
|
* @return `True` on success, `false` otherwise (with `error` set accordingly).
|
|
|
|
*/
|
2022-03-27 17:37:29 +00:00
|
|
|
bool tr_sys_file_flush(tr_sys_file_t handle, struct tr_error** error = nullptr);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2023-01-18 18:46:57 +00:00
|
|
|
/* @brief Check whether `handle` may be flushed via `tr_sys_file_flush()`. */
|
|
|
|
bool tr_sys_file_flush_possible(tr_sys_file_t handle, struct tr_error** error = nullptr);
|
|
|
|
|
2014-07-08 00:15:12 +00:00
|
|
|
/**
|
2017-04-21 07:40:57 +00:00
|
|
|
* @brief Portability wrapper for `ftruncate()`.
|
2014-07-08 00:15:12 +00:00
|
|
|
*
|
|
|
|
* @param[in] handle Valid file descriptor.
|
|
|
|
* @param[in] size Number of bytes to truncate (or extend) file to.
|
2021-10-06 16:32:17 +00:00
|
|
|
* @param[out] error Pointer to error object. Optional, pass `nullptr` if you
|
|
|
|
* are not interested in error details.
|
2014-07-08 00:15:12 +00:00
|
|
|
*
|
|
|
|
* @return `True` on success, `false` otherwise (with `error` set accordingly).
|
|
|
|
*/
|
2022-03-27 17:37:29 +00:00
|
|
|
bool tr_sys_file_truncate(tr_sys_file_t handle, uint64_t size, struct tr_error** error = nullptr);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
|
|
|
/**
|
2017-07-08 13:09:37 +00:00
|
|
|
* @brief Tell system to prefetch or discard some part of file which is [not] to be read soon.
|
2014-07-08 00:15:12 +00:00
|
|
|
*
|
|
|
|
* @param[in] handle Valid file descriptor.
|
|
|
|
* @param[in] offset Offset in file to prefetch from.
|
|
|
|
* @param[in] size Number of bytes to prefetch.
|
2021-10-06 16:32:17 +00:00
|
|
|
* @param[out] error Pointer to error object. Optional, pass `nullptr` if you
|
|
|
|
* are not interested in error details.
|
2014-07-08 00:15:12 +00:00
|
|
|
*
|
|
|
|
* @return `True` on success, `false` otherwise (with `error` set accordingly).
|
|
|
|
*/
|
2021-08-15 09:41:48 +00:00
|
|
|
bool tr_sys_file_advise(
|
|
|
|
tr_sys_file_t handle,
|
|
|
|
uint64_t offset,
|
|
|
|
uint64_t size,
|
|
|
|
tr_sys_file_advice_t advice,
|
2022-03-27 17:37:29 +00:00
|
|
|
struct tr_error** error = nullptr);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Preallocate file to specified size in full or sparse mode.
|
|
|
|
*
|
|
|
|
* @param[in] handle Valid file descriptor.
|
|
|
|
* @param[in] size Number of bytes to preallocate file to.
|
|
|
|
* @param[in] flags Combination of @ref tr_sys_file_preallocate_flags_t values.
|
2021-10-06 16:32:17 +00:00
|
|
|
* @param[out] error Pointer to error object. Optional, pass `nullptr` if you
|
|
|
|
* are not interested in error details.
|
2014-07-08 00:15:12 +00:00
|
|
|
*
|
|
|
|
* @return `True` on success, `false` otherwise (with `error` set accordingly).
|
|
|
|
*/
|
2022-03-27 17:37:29 +00:00
|
|
|
bool tr_sys_file_preallocate(tr_sys_file_t handle, uint64_t size, int flags, struct tr_error** error = nullptr);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2016-09-21 20:56:03 +00:00
|
|
|
/**
|
2017-04-21 07:40:57 +00:00
|
|
|
* @brief Portability wrapper for `flock()`.
|
2016-09-21 20:56:03 +00:00
|
|
|
*
|
|
|
|
* Don't try to upgrade or downgrade the lock unless you know what you are
|
|
|
|
* doing, as behavior varies a bit between platforms.
|
|
|
|
*
|
|
|
|
* @param[in] handle Valid file descriptor.
|
|
|
|
* @param[in] operation Combination of @ref tr_sys_file_lock_flags_t values.
|
2021-10-06 16:32:17 +00:00
|
|
|
* @param[out] error Pointer to error object. Optional, pass `nullptr` if you
|
2016-09-21 20:56:03 +00:00
|
|
|
* are not interested in error details.
|
|
|
|
*
|
|
|
|
* @return `True` on success, `false` otherwise (with `error` set accordingly).
|
|
|
|
*/
|
2022-03-27 17:37:29 +00:00
|
|
|
bool tr_sys_file_lock(tr_sys_file_t handle, int operation, struct tr_error** error = nullptr);
|
2016-09-21 20:56:03 +00:00
|
|
|
|
2014-09-21 17:57:45 +00:00
|
|
|
/* File-related wrappers (utility) */
|
|
|
|
|
|
|
|
/**
|
2017-04-21 07:40:57 +00:00
|
|
|
* @brief Portability wrapper for `fputs()`, appending EOL internally.
|
2014-09-21 17:57:45 +00:00
|
|
|
*
|
|
|
|
* Special care should be taken when writing to one of standard output streams
|
|
|
|
* (@ref tr_std_sys_file_t) since no UTF-8 conversion is currently being made.
|
|
|
|
*
|
|
|
|
* Writing to other streams (files, pipes) also leaves data untouched, so it
|
|
|
|
* should already be in UTF-8 encoding, or whichever else you expect.
|
|
|
|
*
|
|
|
|
* @param[in] handle Valid file descriptor.
|
2021-12-27 22:47:25 +00:00
|
|
|
* @param[in] buffer String to write.
|
2021-10-06 16:32:17 +00:00
|
|
|
* @param[out] error Pointer to error object. Optional, pass `nullptr` if you
|
|
|
|
* are not interested in error details.
|
2014-09-21 17:57:45 +00:00
|
|
|
*
|
|
|
|
* @return `True` on success, `false` otherwise (with `error` set accordingly).
|
|
|
|
*/
|
2022-03-27 17:37:29 +00:00
|
|
|
bool tr_sys_file_write_line(tr_sys_file_t handle, std::string_view buffer, struct tr_error** error = nullptr);
|
2014-09-21 17:57:45 +00:00
|
|
|
|
2014-09-21 17:52:36 +00:00
|
|
|
/* Directory-related wrappers */
|
|
|
|
|
|
|
|
/**
|
2017-04-21 07:40:57 +00:00
|
|
|
* @brief Portability wrapper for `getcwd()`.
|
2014-09-21 17:52:36 +00:00
|
|
|
*
|
2021-10-06 16:32:17 +00:00
|
|
|
* @param[out] error Pointer to error object. Optional, pass `nullptr` if you are
|
2014-09-21 17:52:36 +00:00
|
|
|
* not interested in error details.
|
|
|
|
*
|
2022-08-04 13:44:18 +00:00
|
|
|
* @return current directory on success, or an empty string otherwise
|
|
|
|
* (with `error` set accordingly).
|
2014-09-21 17:52:36 +00:00
|
|
|
*/
|
2022-08-04 04:59:41 +00:00
|
|
|
std::string tr_sys_dir_get_current(struct tr_error** error = nullptr);
|
2014-09-21 17:52:36 +00:00
|
|
|
|
|
|
|
/**
|
2017-04-21 07:40:57 +00:00
|
|
|
* @brief Like `mkdir()`, but makes parent directories if needed.
|
2014-09-21 17:52:36 +00:00
|
|
|
*
|
|
|
|
* @param[in] path Path to directory.
|
|
|
|
* @param[in] flags Combination of @ref tr_sys_dir_create_flags_t values.
|
|
|
|
* @param[in] permissions Permissions to create directory with. Not used on
|
|
|
|
Windows.
|
2021-10-06 16:32:17 +00:00
|
|
|
* @param[out] error Pointer to error object. Optional, pass `nullptr` if
|
|
|
|
* you are not interested in error details.
|
2014-09-21 17:52:36 +00:00
|
|
|
*
|
|
|
|
* @return `True` on success, `false` otherwise (with `error` set accordingly).
|
|
|
|
*/
|
2022-03-27 17:37:29 +00:00
|
|
|
bool tr_sys_dir_create(char const* path, int flags, int permissions, struct tr_error** error = nullptr);
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2022-11-11 17:18:17 +00:00
|
|
|
template<typename T, typename = decltype(&T::c_str)>
|
2022-05-22 01:17:00 +00:00
|
|
|
bool tr_sys_dir_create(T const& path, int flags, int permissions, struct tr_error** error = nullptr)
|
|
|
|
{
|
|
|
|
return tr_sys_dir_create(path.c_str(), flags, permissions, error);
|
|
|
|
}
|
|
|
|
|
2014-09-21 17:52:36 +00:00
|
|
|
/**
|
2017-04-21 07:40:57 +00:00
|
|
|
* @brief Portability wrapper for `mkdtemp()`.
|
2014-09-21 17:52:36 +00:00
|
|
|
*
|
|
|
|
* @param[in,out] path_template Template path to directory. Should end with at
|
|
|
|
* least six 'X' characters. Upon success, trailing
|
|
|
|
* 'X' characters are replaced with actual random
|
|
|
|
* characters used to form a unique path to
|
|
|
|
* temporary directory.
|
2021-10-06 16:32:17 +00:00
|
|
|
* @param[out] error Pointer to error object. Optional, pass `nullptr`
|
2014-09-21 17:52:36 +00:00
|
|
|
* if you are not interested in error details.
|
|
|
|
*
|
|
|
|
* @return `True` on success, `false` otherwise (with `error` set accordingly).
|
|
|
|
*/
|
2022-03-27 17:37:29 +00:00
|
|
|
bool tr_sys_dir_create_temp(char* path_template, struct tr_error** error = nullptr);
|
2014-09-21 17:52:36 +00:00
|
|
|
|
|
|
|
/**
|
2017-04-21 07:40:57 +00:00
|
|
|
* @brief Portability wrapper for `opendir()`.
|
2014-09-21 17:52:36 +00:00
|
|
|
*
|
|
|
|
* @param[in] path Path to directory.
|
2021-10-06 16:32:17 +00:00
|
|
|
* @param[out] error Pointer to error object. Optional, pass `nullptr` if you are
|
2014-09-21 17:52:36 +00:00
|
|
|
* not interested in error details.
|
|
|
|
*
|
|
|
|
* @return Opened directory descriptor on success, `TR_BAD_SYS_DIR` otherwise
|
|
|
|
* (with `error` set accordingly).
|
|
|
|
*/
|
2022-03-27 17:37:29 +00:00
|
|
|
tr_sys_dir_t tr_sys_dir_open(char const* path, struct tr_error** error = nullptr);
|
2014-09-21 17:52:36 +00:00
|
|
|
|
|
|
|
/**
|
2017-04-21 07:40:57 +00:00
|
|
|
* @brief Portability wrapper for `readdir()`.
|
2014-09-21 17:52:36 +00:00
|
|
|
*
|
|
|
|
* @param[in] handle Valid directory descriptor.
|
2021-10-06 16:32:17 +00:00
|
|
|
* @param[out] error Pointer to error object. Optional, pass `nullptr` if you
|
|
|
|
* are not interested in error details.
|
2014-09-21 17:52:36 +00:00
|
|
|
*
|
2022-08-04 13:44:18 +00:00
|
|
|
* @return Pointer to next directory entry name (stored internally, DO NOT free
|
|
|
|
* it) on success, `nullptr` otherwise (with `error` set accordingly).
|
|
|
|
* Note that `nullptr` will also be returned in case of end of directory.
|
|
|
|
* If you need to distinguish the two, check if `error` is `nullptr`
|
|
|
|
* afterwards.
|
2014-09-21 17:52:36 +00:00
|
|
|
*/
|
2022-03-27 17:37:29 +00:00
|
|
|
char const* tr_sys_dir_read_name(tr_sys_dir_t handle, struct tr_error** error = nullptr);
|
2014-09-21 17:52:36 +00:00
|
|
|
|
|
|
|
/**
|
2017-04-21 07:40:57 +00:00
|
|
|
* @brief Portability wrapper for `closedir()`.
|
2014-09-21 17:52:36 +00:00
|
|
|
*
|
|
|
|
* @param[in] handle Valid directory descriptor.
|
2021-10-06 16:32:17 +00:00
|
|
|
* @param[out] error Pointer to error object. Optional, pass `nullptr` if you
|
|
|
|
* are not interested in error details.
|
2014-09-21 17:52:36 +00:00
|
|
|
*
|
|
|
|
* @return `True` on success, `false` otherwise (with `error` set accordingly).
|
|
|
|
*/
|
2022-03-27 17:37:29 +00:00
|
|
|
bool tr_sys_dir_close(tr_sys_dir_t handle, struct tr_error** error = nullptr);
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2014-07-03 21:58:39 +00:00
|
|
|
/** @} */
|
|
|
|
/** @} */
|