2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2013-2022 Mnemosyne LLC.
|
2022-02-07 16:25:02 +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-09-21 20:56:03 +00:00
|
|
|
#undef _GNU_SOURCE
|
2022-02-07 04:28:36 +00:00
|
|
|
#define _GNU_SOURCE // NOLINT
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2021-09-19 20:41:35 +00:00
|
|
|
#include <algorithm>
|
2021-10-06 13:09:16 +00:00
|
|
|
#include <array>
|
2021-09-19 20:41:35 +00:00
|
|
|
#include <cerrno>
|
|
|
|
#include <climits> /* PATH_MAX */
|
|
|
|
#include <cstdint> /* SIZE_MAX */
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <string_view>
|
2022-04-08 01:50:26 +00:00
|
|
|
#include <string>
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2022-02-07 04:28:36 +00:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <fcntl.h> /* O_LARGEFILE, posix_fadvise(), [posix_]fallocate(), fcntl() */
|
|
|
|
#include <libgen.h> /* basename(), dirname() */
|
2017-04-21 07:40:57 +00:00
|
|
|
#include <sys/file.h> /* flock() */
|
|
|
|
#include <sys/mman.h> /* mmap(), munmap() */
|
2014-07-03 21:58:39 +00:00
|
|
|
#include <sys/stat.h>
|
2021-09-19 20:41:35 +00:00
|
|
|
#include <sys/types.h>
|
2022-02-07 04:28:36 +00:00
|
|
|
#include <unistd.h> /* lseek(), write(), ftruncate(), pread(), pwrite(), pathconf(), etc */
|
2014-07-08 00:15:12 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_XFS_XFS_H
|
2017-04-19 12:04:45 +00:00
|
|
|
#include <xfs/xfs.h>
|
2014-07-08 00:15:12 +00:00
|
|
|
#endif
|
2014-07-03 21:58:39 +00:00
|
|
|
|
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
|
|
|
/* OS-specific file copy (copy_file_range, sendfile64, or copyfile). */
|
|
|
|
#if defined(__linux__)
|
2021-08-15 09:41:48 +00:00
|
|
|
#include <linux/version.h>
|
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
|
|
|
/* Linux's copy_file_range(2) is buggy prior to 5.3. */
|
2021-08-15 09:41:48 +00:00
|
|
|
#if defined(HAVE_COPY_FILE_RANGE) && LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0)
|
|
|
|
#define USE_COPY_FILE_RANGE
|
|
|
|
#elif defined(HAVE_SENDFILE64)
|
|
|
|
#include <sys/sendfile.h>
|
|
|
|
#define USE_SENDFILE64
|
|
|
|
#endif
|
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
|
|
|
#elif defined(__APPLE__) && defined(HAVE_COPYFILE)
|
2021-08-15 09:41:48 +00:00
|
|
|
#include <copyfile.h>
|
|
|
|
#ifndef COPYFILE_CLONE /* macos < 10.12 */
|
|
|
|
#define COPYFILE_CLONE 0
|
|
|
|
#endif
|
|
|
|
#define USE_COPYFILE
|
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
|
|
|
#elif defined(HAVE_COPY_FILE_RANGE)
|
|
|
|
/* Presently this is only FreeBSD 13+. */
|
2021-08-15 09:41:48 +00:00
|
|
|
#define USE_COPY_FILE_RANGE
|
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
|
|
|
#endif /* __linux__ */
|
|
|
|
|
2022-03-30 19:59:13 +00:00
|
|
|
#include <fmt/format.h>
|
2022-03-14 04:43:35 +00:00
|
|
|
|
2014-07-03 21:58:39 +00:00
|
|
|
#include "transmission.h"
|
2015-04-11 14:54:01 +00:00
|
|
|
#include "error.h"
|
2014-07-03 21:58:39 +00:00
|
|
|
#include "file.h"
|
2014-09-21 17:52:36 +00:00
|
|
|
#include "log.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2022-04-30 21:57:11 +00:00
|
|
|
#include "tr-strbuf.h"
|
2014-07-03 21:58:39 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
2014-07-08 00:15:12 +00:00
|
|
|
#ifndef O_LARGEFILE
|
2017-04-19 12:04:45 +00:00
|
|
|
#define O_LARGEFILE 0
|
2014-07-08 00:15:12 +00:00
|
|
|
#endif
|
|
|
|
#ifndef O_BINARY
|
2017-04-19 12:04:45 +00:00
|
|
|
#define O_BINARY 0
|
2014-07-08 00:15:12 +00:00
|
|
|
#endif
|
|
|
|
#ifndef O_SEQUENTIAL
|
2017-04-19 12:04:45 +00:00
|
|
|
#define O_SEQUENTIAL 0
|
2014-07-08 00:15:12 +00:00
|
|
|
#endif
|
|
|
|
#ifndef O_CLOEXEC
|
2017-04-19 12:04:45 +00:00
|
|
|
#define O_CLOEXEC 0
|
2014-07-08 00:15:12 +00:00
|
|
|
#endif
|
|
|
|
|
2014-07-03 21:58:39 +00:00
|
|
|
#ifndef PATH_MAX
|
2017-04-19 12:04:45 +00:00
|
|
|
#define PATH_MAX 4096
|
2014-07-03 21:58:39 +00:00
|
|
|
#endif
|
|
|
|
|
2014-07-08 00:15:12 +00:00
|
|
|
/* don't use pread/pwrite on old versions of uClibc because they're buggy.
|
|
|
|
* https://trac.transmissionbt.com/ticket/3826 */
|
2017-04-19 12:04:45 +00:00
|
|
|
#if defined(__UCLIBC__) && !TR_UCLIBC_CHECK_VERSION(0, 9, 28)
|
|
|
|
#undef HAVE_PREAD
|
|
|
|
#undef HAVE_PWRITE
|
2014-07-08 00:15:12 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __APPLE__
|
2017-04-19 12:04:45 +00:00
|
|
|
#ifndef HAVE_PREAD
|
|
|
|
#define HAVE_PREAD
|
|
|
|
#endif
|
|
|
|
#ifndef HAVE_PWRITE
|
|
|
|
#define HAVE_PWRITE
|
|
|
|
#endif
|
|
|
|
#ifndef HAVE_MKDTEMP
|
|
|
|
#define HAVE_MKDTEMP
|
|
|
|
#endif
|
2014-07-08 00:15:12 +00:00
|
|
|
#endif
|
|
|
|
|
2021-12-28 02:32:22 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void set_system_error(tr_error** error, int code)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (error == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2021-12-28 02:32:22 +00:00
|
|
|
tr_error_set(error, code, tr_strerror(code));
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void set_system_error_if_file_found(tr_error** error, int code)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (code != ENOENT)
|
|
|
|
{
|
|
|
|
set_system_error(error, code);
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void stat_to_sys_path_info(struct stat const* sb, tr_sys_path_info* info)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (S_ISREG(sb->st_mode))
|
|
|
|
{
|
|
|
|
info->type = TR_SYS_PATH_IS_FILE;
|
|
|
|
}
|
|
|
|
else if (S_ISDIR(sb->st_mode))
|
|
|
|
{
|
|
|
|
info->type = TR_SYS_PATH_IS_DIRECTORY;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
info->type = TR_SYS_PATH_IS_OTHER;
|
|
|
|
}
|
|
|
|
|
|
|
|
info->size = (uint64_t)sb->st_size;
|
|
|
|
info->last_modified_at = sb->st_mtime;
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void set_file_for_single_pass(tr_sys_file_t handle)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
/* Set hints about the lookahead buffer and caching. It's okay
|
|
|
|
for these to fail silently, so don't let them affect errno */
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
int const err = errno;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (handle == TR_BAD_SYS_FILE)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-07-08 00:15:12 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_POSIX_FADVISE
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
(void)posix_fadvise(handle, 0, 0, POSIX_FADV_SEQUENTIAL);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
(void)fcntl(handle, F_RDAHEAD, 1);
|
|
|
|
(void)fcntl(handle, F_NOCACHE, 1);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
errno = err;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2014-09-21 17:52:36 +00:00
|
|
|
#ifndef HAVE_MKDIRP
|
|
|
|
|
2017-10-27 13:12:55 +00:00
|
|
|
static bool create_path_require_dir(char const* path, tr_error** error)
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
struct stat sb;
|
|
|
|
|
2017-10-27 13:12:55 +00:00
|
|
|
if (stat(path, &sb) == -1)
|
|
|
|
{
|
|
|
|
set_system_error(error, errno);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((sb.st_mode & S_IFMT) != S_IFDIR)
|
|
|
|
{
|
2022-03-30 19:59:13 +00:00
|
|
|
tr_error_set(error, ENOTDIR, fmt::format(FMT_STRING("File is in the way: {:s}"), path));
|
2017-10-27 13:12:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool create_path(char const* path_in, int permissions, tr_error** error)
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
/* make a temporary copy of path */
|
2021-10-29 22:43:25 +00:00
|
|
|
char* const path = tr_strdup(path_in);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* walk past the root */
|
2017-10-27 13:12:55 +00:00
|
|
|
char* p = path;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
while (*p == TR_PATH_DELIMITER)
|
|
|
|
{
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
|
2017-10-27 13:12:55 +00:00
|
|
|
char* path_end = p + strlen(p);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-10-27 13:12:55 +00:00
|
|
|
while (path_end > path && *path_end == TR_PATH_DELIMITER)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-10-27 13:12:55 +00:00
|
|
|
--path_end;
|
|
|
|
}
|
|
|
|
|
2021-10-29 22:43:25 +00:00
|
|
|
char* pp = nullptr;
|
2017-10-27 13:12:55 +00:00
|
|
|
bool ret = false;
|
2021-09-15 00:18:09 +00:00
|
|
|
tr_error* my_error = nullptr;
|
2017-10-27 13:12:55 +00:00
|
|
|
|
|
|
|
/* Go one level up on each iteration and attempt to create */
|
2021-09-15 00:18:09 +00:00
|
|
|
for (pp = path_end; pp != nullptr; pp = strrchr(p, TR_PATH_DELIMITER))
|
2017-10-27 13:12:55 +00:00
|
|
|
{
|
|
|
|
*pp = '\0';
|
|
|
|
|
|
|
|
ret = mkdir(path, permissions) != -1;
|
|
|
|
|
|
|
|
if (ret)
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2017-10-27 13:12:55 +00:00
|
|
|
break;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2017-10-27 13:12:55 +00:00
|
|
|
if (errno == EEXIST)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-10-27 13:12:55 +00:00
|
|
|
ret = create_path_require_dir(path, &my_error);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-10-27 13:12:55 +00:00
|
|
|
if (ret)
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2017-10-27 13:12:55 +00:00
|
|
|
break;
|
2014-09-21 17:52:36 +00:00
|
|
|
}
|
2017-10-27 13:12:55 +00:00
|
|
|
|
2020-11-05 22:46:21 +00:00
|
|
|
goto FAILURE;
|
2014-09-21 17:52:36 +00:00
|
|
|
}
|
2017-10-27 13:12:55 +00:00
|
|
|
|
|
|
|
if (errno != ENOENT)
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2017-10-27 13:12:55 +00:00
|
|
|
set_system_error(&my_error, errno);
|
2020-11-05 22:46:21 +00:00
|
|
|
goto FAILURE;
|
2014-09-21 17:52:36 +00:00
|
|
|
}
|
2017-10-27 13:12:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ret && pp == path_end)
|
|
|
|
{
|
2020-11-05 22:46:21 +00:00
|
|
|
goto CLEANUP;
|
2017-10-27 13:12:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Go one level down on each iteration and attempt to create */
|
2021-09-15 00:18:09 +00:00
|
|
|
for (pp = pp == nullptr ? p + strlen(p) : pp; pp < path_end; pp += strlen(pp))
|
2017-10-27 13:12:55 +00:00
|
|
|
{
|
|
|
|
*pp = TR_PATH_DELIMITER;
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2017-10-27 13:12:55 +00:00
|
|
|
if (mkdir(path, permissions) == -1)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2017-10-27 13:12:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = create_path_require_dir(path, &my_error);
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2017-10-27 13:12:55 +00:00
|
|
|
if (ret)
|
|
|
|
{
|
2020-11-05 22:46:21 +00:00
|
|
|
goto CLEANUP;
|
2014-09-21 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
2020-11-05 22:46:21 +00:00
|
|
|
FAILURE:
|
2017-10-27 13:12:55 +00:00
|
|
|
|
|
|
|
TR_ASSERT(!ret);
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(my_error != nullptr);
|
2017-10-27 13:12:55 +00:00
|
|
|
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddError(fmt::format(
|
2022-03-16 00:51:36 +00:00
|
|
|
_("Couldn't create '{path}': {error} ({error_code})"),
|
2022-03-14 04:43:35 +00:00
|
|
|
fmt::arg("path", path),
|
2022-03-16 00:51:36 +00:00
|
|
|
fmt::arg("error", my_error->message),
|
|
|
|
fmt::arg("error_code", my_error->code)));
|
2017-10-27 13:12:55 +00:00
|
|
|
tr_error_propagate(error, &my_error);
|
|
|
|
|
2020-11-05 22:46:21 +00:00
|
|
|
CLEANUP:
|
2017-10-27 13:12:55 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(my_error == nullptr);
|
2017-10-27 13:12:55 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(path);
|
2017-10-27 13:12:55 +00:00
|
|
|
return ret;
|
2014-09-21 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sys_path_exists(char const* path, tr_error** error)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(path != nullptr);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
bool ret = access(path, F_OK) != -1;
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
set_system_error_if_file_found(error, errno);
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sys_path_get_info(char const* path, int flags, tr_sys_path_info* info, tr_error** error)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(path != nullptr);
|
|
|
|
TR_ASSERT(info != nullptr);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2021-10-29 22:43:25 +00:00
|
|
|
bool ret = false;
|
2017-06-13 02:24:09 +00:00
|
|
|
struct stat sb;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if ((flags & TR_SYS_PATH_NO_FOLLOW) == 0)
|
|
|
|
{
|
|
|
|
ret = stat(path, &sb) != -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = lstat(path, &sb) != -1;
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
stat_to_sys_path_info(&sb, info);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
set_system_error(error, errno);
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2021-11-13 16:08:50 +00:00
|
|
|
return std::empty(path) || path.front() != '/';
|
2015-10-20 21:22:19 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sys_path_is_same(char const* path1, char const* path2, tr_error** error)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(path1 != nullptr);
|
|
|
|
TR_ASSERT(path2 != nullptr);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool ret = false;
|
2017-05-01 15:46:41 +00:00
|
|
|
struct stat sb1;
|
|
|
|
struct stat sb2;
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (stat(path1, &sb1) != -1 && stat(path2, &sb2) != -1)
|
|
|
|
{
|
|
|
|
ret = sb1.st_dev == sb2.st_dev && sb1.st_ino == sb2.st_ino;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
set_system_error_if_file_found(error, errno);
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char* tr_sys_path_resolve(char const* path, tr_error** error)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(path != nullptr);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
char* ret = nullptr;
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
#if defined(HAVE_CANONICALIZE_FILE_NAME)
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = canonicalize_file_name(path);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (ret == nullptr)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2020-11-04 03:23:53 +00:00
|
|
|
char tmp[PATH_MAX];
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = realpath(path, tmp);
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (ret != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
ret = tr_strdup(ret);
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (ret == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
set_system_error(error, errno);
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 20:22:50 +00:00
|
|
|
std::string tr_sys_path_basename(std::string_view path, tr_error** error)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2022-04-30 21:57:11 +00:00
|
|
|
auto tmp = tr_pathbuf{ path };
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2022-03-25 05:24:04 +00:00
|
|
|
if (char const* ret = basename(std::data(tmp)); ret != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-03-21 20:22:50 +00:00
|
|
|
return ret;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2022-03-21 20:22:50 +00:00
|
|
|
set_system_error(error, errno);
|
|
|
|
return {};
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 20:22:50 +00:00
|
|
|
std::string tr_sys_path_dirname(std::string_view path, tr_error** error)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2022-04-30 21:57:11 +00:00
|
|
|
auto tmp = tr_pathbuf{ path };
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2022-03-25 05:24:04 +00:00
|
|
|
if (char const* ret = dirname(std::data(tmp)); ret != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-03-21 20:22:50 +00:00
|
|
|
return ret;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2022-03-21 20:22:50 +00:00
|
|
|
set_system_error(error, errno);
|
|
|
|
return {};
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sys_path_rename(char const* src_path, char const* dst_path, tr_error** error)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(src_path != nullptr);
|
|
|
|
TR_ASSERT(dst_path != nullptr);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2021-10-29 22:43:25 +00:00
|
|
|
bool const ret = rename(src_path, dst_path) != -1;
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
set_system_error(error, errno);
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
/* We try to do a fast (in-kernel) copy using a variety of non-portable system
|
|
|
|
* calls. If the current implementation does not support in-kernel copying, we
|
|
|
|
* use a user-space fallback instead. */
|
|
|
|
bool tr_sys_path_copy(char const* src_path, char const* dst_path, tr_error** error)
|
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(src_path != nullptr);
|
|
|
|
TR_ASSERT(dst_path != 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
|
|
|
|
|
|
|
#if defined(USE_COPYFILE)
|
2021-09-15 00:18:09 +00:00
|
|
|
if (copyfile(src_path, dst_path, nullptr, COPYFILE_CLONE | COPYFILE_ALL) < 0)
|
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
|
|
|
{
|
|
|
|
set_system_error(error, errno);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
#else /* USE_COPYFILE */
|
|
|
|
|
|
|
|
/* Other OSes require us to copy between file descriptors, so open them. */
|
|
|
|
tr_sys_file_t in = tr_sys_file_open(src_path, TR_SYS_FILE_READ | TR_SYS_FILE_SEQUENTIAL, 0, error);
|
|
|
|
if (in == TR_BAD_SYS_FILE)
|
|
|
|
{
|
|
|
|
tr_error_prefix(error, "Unable to open source file: ");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_sys_path_info info;
|
|
|
|
if (!tr_sys_file_get_info(in, &info, error))
|
|
|
|
{
|
|
|
|
tr_error_prefix(error, "Unable to get information on source file: ");
|
2022-03-27 17:37:29 +00:00
|
|
|
tr_sys_file_close(in);
|
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 false;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_sys_file_t out = tr_sys_file_open(dst_path, TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE | TR_SYS_FILE_TRUNCATE, 0666, error);
|
|
|
|
if (out == TR_BAD_SYS_FILE)
|
|
|
|
{
|
|
|
|
tr_error_prefix(error, "Unable to open destination file: ");
|
2022-03-27 17:37:29 +00:00
|
|
|
tr_sys_file_close(in);
|
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 false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t file_size = info.size;
|
|
|
|
|
|
|
|
#if defined(USE_COPY_FILE_RANGE) || defined(USE_SENDFILE64)
|
|
|
|
|
|
|
|
while (file_size > 0)
|
|
|
|
{
|
2021-09-19 20:41:35 +00:00
|
|
|
size_t const chunk_size = std::min(file_size, uint64_t{ SSIZE_MAX });
|
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
|
|
|
ssize_t const copied =
|
|
|
|
#ifdef USE_COPY_FILE_RANGE
|
2021-09-15 00:18:09 +00:00
|
|
|
copy_file_range(in, nullptr, out, nullptr, chunk_size, 0);
|
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
|
|
|
#elif defined(USE_SENDFILE64)
|
2021-09-15 00:18:09 +00:00
|
|
|
sendfile64(out, in, nullptr, chunk_size);
|
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
|
|
|
#else
|
|
|
|
#error File copy mechanism not implemented.
|
|
|
|
#endif
|
|
|
|
TR_ASSERT(copied == -1 || copied >= 0); /* -1 for error; some non-negative value otherwise. */
|
|
|
|
|
|
|
|
if (copied == -1)
|
|
|
|
{
|
|
|
|
set_system_error(error, errno);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
TR_ASSERT(copied >= 0 && ((uint64_t)copied) <= file_size);
|
|
|
|
TR_ASSERT(copied >= 0 && ((uint64_t)copied) <= chunk_size);
|
|
|
|
file_size -= copied;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* USE_COPY_FILE_RANGE || USE_SENDFILE64 */
|
|
|
|
|
|
|
|
/* Fallback to user-space copy. */
|
|
|
|
|
|
|
|
size_t const buflen = 1024 * 1024; /* 1024 KiB buffer */
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* buf = static_cast<char*>(tr_malloc(buflen));
|
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
|
|
|
|
|
|
|
while (file_size > 0)
|
|
|
|
{
|
2021-09-19 20:41:35 +00:00
|
|
|
uint64_t const chunk_size = std::min(file_size, uint64_t{ buflen });
|
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
|
|
|
uint64_t bytes_read;
|
|
|
|
uint64_t bytes_written;
|
|
|
|
|
|
|
|
if (!tr_sys_file_read(in, buf, chunk_size, &bytes_read, error))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!tr_sys_file_write(out, buf, bytes_read, &bytes_written, error))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
TR_ASSERT(bytes_read == bytes_written);
|
|
|
|
TR_ASSERT(bytes_written <= file_size);
|
|
|
|
file_size -= bytes_written;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
tr_free(buf);
|
|
|
|
|
|
|
|
#endif /* USE_COPY_FILE_RANGE || USE_SENDFILE64 */
|
|
|
|
|
|
|
|
/* cleanup */
|
2022-03-27 17:37:29 +00:00
|
|
|
tr_sys_file_close(out);
|
|
|
|
tr_sys_file_close(in);
|
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
|
|
|
|
|
|
|
if (file_size != 0)
|
|
|
|
{
|
|
|
|
tr_error_prefix(error, "Unable to read/write: ");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
#endif /* USE_COPYFILE */
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sys_path_remove(char const* path, tr_error** error)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(path != nullptr);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2021-10-29 22:43:25 +00:00
|
|
|
bool const ret = remove(path) != -1;
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
set_system_error(error, errno);
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-11-27 22:22:44 +00:00
|
|
|
char* tr_sys_path_native_separators(char* path)
|
|
|
|
{
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_sys_file_t tr_sys_file_get_std(tr_std_sys_file_t std_file, tr_error** error)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_sys_file_t ret = TR_BAD_SYS_FILE;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (std_file)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
|
|
|
case TR_STD_SYS_FILE_IN:
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = STDIN_FILENO;
|
|
|
|
break;
|
|
|
|
|
2014-07-08 00:15:12 +00:00
|
|
|
case TR_STD_SYS_FILE_OUT:
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = STDOUT_FILENO;
|
|
|
|
break;
|
|
|
|
|
2014-07-08 00:15:12 +00:00
|
|
|
case TR_STD_SYS_FILE_ERR:
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = STDERR_FILENO;
|
|
|
|
break;
|
|
|
|
|
2014-07-08 00:15:12 +00:00
|
|
|
default:
|
2022-04-01 23:00:51 +00:00
|
|
|
TR_ASSERT_MSG(false, fmt::format(FMT_STRING("unknown standard file {:d}"), std_file));
|
2017-04-19 12:04:45 +00:00
|
|
|
set_system_error(error, EINVAL);
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_sys_file_t tr_sys_file_open(char const* path, int flags, int permissions, tr_error** error)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(path != nullptr);
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT((flags & (TR_SYS_FILE_READ | TR_SYS_FILE_WRITE)) != 0);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2021-10-06 13:09:16 +00:00
|
|
|
struct native_map_item
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-06 13:09:16 +00:00
|
|
|
int symbolic_mask;
|
|
|
|
int symbolic_value;
|
|
|
|
int native_value;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto constexpr native_map = std::array<native_map_item, 8>{
|
|
|
|
{ { TR_SYS_FILE_READ | TR_SYS_FILE_WRITE, TR_SYS_FILE_READ | TR_SYS_FILE_WRITE, O_RDWR },
|
|
|
|
{ TR_SYS_FILE_READ | TR_SYS_FILE_WRITE, TR_SYS_FILE_READ, O_RDONLY },
|
|
|
|
{ TR_SYS_FILE_READ | TR_SYS_FILE_WRITE, TR_SYS_FILE_WRITE, O_WRONLY },
|
|
|
|
{ TR_SYS_FILE_CREATE, TR_SYS_FILE_CREATE, O_CREAT },
|
|
|
|
{ TR_SYS_FILE_CREATE_NEW, TR_SYS_FILE_CREATE_NEW, O_CREAT | O_EXCL },
|
|
|
|
{ TR_SYS_FILE_APPEND, TR_SYS_FILE_APPEND, O_APPEND },
|
|
|
|
{ TR_SYS_FILE_TRUNCATE, TR_SYS_FILE_TRUNCATE, O_TRUNC },
|
|
|
|
{ TR_SYS_FILE_SEQUENTIAL, TR_SYS_FILE_SEQUENTIAL, O_SEQUENTIAL } }
|
|
|
|
};
|
|
|
|
|
|
|
|
int native_flags = O_BINARY | O_LARGEFILE | O_CLOEXEC;
|
|
|
|
|
|
|
|
for (auto const& item : native_map)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-06 13:09:16 +00:00
|
|
|
if ((flags & item.symbolic_mask) == item.symbolic_value)
|
|
|
|
{
|
|
|
|
native_flags |= item.native_value;
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2021-10-29 22:43:25 +00:00
|
|
|
tr_sys_file_t const ret = open(path, native_flags, permissions);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (ret != TR_BAD_SYS_FILE)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2019-07-14 12:40:41 +00:00
|
|
|
if ((flags & TR_SYS_FILE_SEQUENTIAL) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
set_file_for_single_pass(ret);
|
|
|
|
}
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
set_system_error(error, errno);
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_sys_file_t tr_sys_file_open_temp(char* path_template, tr_error** error)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(path_template != nullptr);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2021-10-29 22:43:25 +00:00
|
|
|
tr_sys_file_t const ret = mkstemp(path_template);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (ret == TR_BAD_SYS_FILE)
|
|
|
|
{
|
|
|
|
set_system_error(error, errno);
|
|
|
|
}
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
set_file_for_single_pass(ret);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool tr_sys_file_close(tr_sys_file_t handle, tr_error** error)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(handle != TR_BAD_SYS_FILE);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2021-10-29 22:43:25 +00:00
|
|
|
bool const ret = close(handle) != -1;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
set_system_error(error, errno);
|
|
|
|
}
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool tr_sys_file_get_info(tr_sys_file_t handle, tr_sys_path_info* info, tr_error** error)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(handle != TR_BAD_SYS_FILE);
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(info != nullptr);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
struct stat sb;
|
2021-10-29 22:43:25 +00:00
|
|
|
bool const ret = fstat(handle, &sb) != -1;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
stat_to_sys_path_info(&sb, info);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
set_system_error(error, errno);
|
|
|
|
}
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool tr_sys_file_seek(tr_sys_file_t handle, int64_t offset, tr_seek_origin_t origin, uint64_t* new_offset, tr_error** error)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2021-10-14 20:40:35 +00:00
|
|
|
static_assert(TR_SEEK_SET == SEEK_SET, "values should match");
|
|
|
|
static_assert(TR_SEEK_CUR == SEEK_CUR, "values should match");
|
|
|
|
static_assert(TR_SEEK_END == SEEK_END, "values should match");
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(handle != TR_BAD_SYS_FILE);
|
|
|
|
TR_ASSERT(origin == TR_SEEK_SET || origin == TR_SEEK_CUR || origin == TR_SEEK_END);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
bool ret = false;
|
|
|
|
|
2021-10-29 22:43:25 +00:00
|
|
|
off_t const my_new_offset = lseek(handle, offset, origin);
|
2022-01-23 22:47:41 +00:00
|
|
|
static_assert(sizeof(*new_offset) >= sizeof(my_new_offset));
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (my_new_offset != -1)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (new_offset != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
*new_offset = my_new_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = true;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
set_system_error(error, errno);
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool tr_sys_file_read(tr_sys_file_t handle, void* buffer, uint64_t size, uint64_t* bytes_read, tr_error** error)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-06-13 02:24:09 +00:00
|
|
|
TR_ASSERT(handle != TR_BAD_SYS_FILE);
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(buffer != nullptr || size == 0);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool ret = false;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2021-10-29 22:43:25 +00:00
|
|
|
ssize_t const my_bytes_read = read(handle, buffer, size);
|
2022-01-23 22:47:41 +00:00
|
|
|
static_assert(sizeof(*bytes_read) >= sizeof(my_bytes_read));
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (my_bytes_read != -1)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (bytes_read != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
*bytes_read = my_bytes_read;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = true;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
set_system_error(error, errno);
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
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,
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_error** error)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(handle != TR_BAD_SYS_FILE);
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(buffer != nullptr || size == 0);
|
2017-04-19 12:04:45 +00:00
|
|
|
/* seek requires signed offset, so it should be in mod range */
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(offset < UINT64_MAX / 2);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
bool ret = false;
|
|
|
|
|
2014-07-08 00:15:12 +00:00
|
|
|
#ifdef HAVE_PREAD
|
|
|
|
|
2021-10-29 22:43:25 +00:00
|
|
|
ssize_t const my_bytes_read = pread(handle, buffer, size, offset);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2021-10-29 22:43:25 +00:00
|
|
|
ssize_t const my_bytes_read = lseek(handle, offset, SEEK_SET) == -1 ? -1 : read(handle, buffer, size);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2022-01-23 22:47:41 +00:00
|
|
|
static_assert(sizeof(*bytes_read) >= sizeof(my_bytes_read));
|
2021-10-29 22:43:25 +00:00
|
|
|
|
2022-03-17 15:41:02 +00:00
|
|
|
if (my_bytes_read > 0)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (bytes_read != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
*bytes_read = my_bytes_read;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = true;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
2022-04-30 21:57:11 +00:00
|
|
|
else if (my_bytes_read == -1)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
set_system_error(error, errno);
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sys_file_write(tr_sys_file_t handle, void const* buffer, uint64_t size, uint64_t* bytes_written, tr_error** error)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-06-13 02:24:09 +00:00
|
|
|
TR_ASSERT(handle != TR_BAD_SYS_FILE);
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(buffer != nullptr || size == 0);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool ret = false;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2021-10-29 22:43:25 +00:00
|
|
|
ssize_t const my_bytes_written = write(handle, buffer, size);
|
2022-01-23 22:47:41 +00:00
|
|
|
static_assert(sizeof(*bytes_written) >= sizeof(my_bytes_written));
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (my_bytes_written != -1)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (bytes_written != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
*bytes_written = my_bytes_written;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = true;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
set_system_error(error, errno);
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
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,
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_error** error)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(handle != TR_BAD_SYS_FILE);
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(buffer != nullptr || size == 0);
|
2017-04-19 12:04:45 +00:00
|
|
|
/* seek requires signed offset, so it should be in mod range */
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(offset < UINT64_MAX / 2);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
bool ret = false;
|
|
|
|
|
2014-07-08 00:15:12 +00:00
|
|
|
#ifdef HAVE_PWRITE
|
|
|
|
|
2021-10-29 22:43:25 +00:00
|
|
|
ssize_t const my_bytes_written = pwrite(handle, buffer, size, offset);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2021-10-29 22:43:25 +00:00
|
|
|
ssize_t const my_bytes_written = lseek(handle, offset, SEEK_SET) == -1 ? -1 : write(handle, buffer, size);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2022-01-23 22:47:41 +00:00
|
|
|
static_assert(sizeof(*bytes_written) >= sizeof(my_bytes_written));
|
2021-10-29 22:43:25 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (my_bytes_written != -1)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (bytes_written != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
*bytes_written = my_bytes_written;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = true;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
set_system_error(error, errno);
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool tr_sys_file_flush(tr_sys_file_t handle, tr_error** error)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(handle != TR_BAD_SYS_FILE);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
bool ret = fsync(handle) != -1;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
set_system_error(error, errno);
|
|
|
|
}
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool tr_sys_file_truncate(tr_sys_file_t handle, uint64_t size, tr_error** error)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(handle != TR_BAD_SYS_FILE);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
bool ret = ftruncate(handle, size) != -1;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
set_system_error(error, errno);
|
|
|
|
}
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2021-10-10 16:52:26 +00:00
|
|
|
bool tr_sys_file_advise(
|
|
|
|
[[maybe_unused]] tr_sys_file_t handle,
|
|
|
|
[[maybe_unused]] uint64_t offset,
|
|
|
|
[[maybe_unused]] uint64_t size,
|
|
|
|
[[maybe_unused]] tr_sys_file_advice_t advice,
|
|
|
|
[[maybe_unused]] tr_error** error)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-06-13 02:24:09 +00:00
|
|
|
TR_ASSERT(handle != TR_BAD_SYS_FILE);
|
|
|
|
TR_ASSERT(size > 0);
|
2017-07-08 13:09:37 +00:00
|
|
|
TR_ASSERT(advice == TR_SYS_FILE_ADVICE_WILL_NEED || advice == TR_SYS_FILE_ADVICE_DONT_NEED);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2017-07-08 09:51:36 +00:00
|
|
|
bool ret = true;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
#if defined(HAVE_POSIX_FADVISE)
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
int const native_advice = advice == TR_SYS_FILE_ADVICE_WILL_NEED ?
|
|
|
|
POSIX_FADV_WILLNEED :
|
2017-07-08 13:09:37 +00:00
|
|
|
(advice == TR_SYS_FILE_ADVICE_DONT_NEED ? POSIX_FADV_DONTNEED : POSIX_FADV_NORMAL);
|
|
|
|
|
|
|
|
TR_ASSERT(native_advice != POSIX_FADV_NORMAL);
|
|
|
|
|
2022-02-02 01:09:11 +00:00
|
|
|
if (int const code = posix_fadvise(handle, offset, size, native_advice); code != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
set_system_error(error, code);
|
2017-07-08 09:51:36 +00:00
|
|
|
ret = false;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
#elif defined(__APPLE__)
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
if (advice == TR_SYS_FILE_ADVICE_WILL_NEED)
|
2017-07-08 13:09:37 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto radv = radvisory{};
|
|
|
|
radv.ra_offset = offset;
|
|
|
|
radv.ra_count = size;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
ret = fcntl(handle, F_RDADVISE, &radv) != -1;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
set_system_error(error, errno);
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2014-07-08 00:15:12 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 16:55:46 +00:00
|
|
|
namespace
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
#ifdef HAVE_FALLOCATE64
|
2021-09-15 16:55:46 +00:00
|
|
|
bool preallocate_fallocate64(tr_sys_file_t handle, uint64_t size)
|
|
|
|
{
|
|
|
|
return fallocate64(handle, 0, 0, size) == 0;
|
|
|
|
}
|
|
|
|
#endif
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2021-09-15 16:55:46 +00:00
|
|
|
#ifdef HAVE_XFS_XFS_H
|
|
|
|
bool full_preallocate_xfs(tr_sys_file_t handle, uint64_t size)
|
|
|
|
{
|
2022-02-20 02:03:57 +00:00
|
|
|
if (platform_test_xfs_fd(handle) == 0) // true if on xfs filesystem
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-15 16:55:46 +00:00
|
|
|
return false;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2014-12-10 18:23:11 +00:00
|
|
|
|
2021-09-15 16:55:46 +00:00
|
|
|
xfs_flock64_t fl;
|
|
|
|
fl.l_whence = 0;
|
|
|
|
fl.l_start = 0;
|
|
|
|
fl.l_len = size;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2021-09-15 16:55:46 +00:00
|
|
|
// The blocks are allocated, but not zeroed, and the file size does not change
|
|
|
|
bool ok = xfsctl(nullptr, handle, XFS_IOC_RESVSP64, &fl) != -1;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2021-09-15 16:55:46 +00:00
|
|
|
if (ok)
|
|
|
|
{
|
|
|
|
ok = ftruncate(handle, size) == 0;
|
|
|
|
}
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2021-09-15 16:55:46 +00:00
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
#endif
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2021-09-15 16:55:46 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
bool full_preallocate_apple(tr_sys_file_t handle, uint64_t size)
|
|
|
|
{
|
|
|
|
fstore_t fst;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2021-09-15 16:55:46 +00:00
|
|
|
fst.fst_flags = F_ALLOCATEALL;
|
|
|
|
fst.fst_posmode = F_PEOFPOSMODE;
|
|
|
|
fst.fst_offset = 0;
|
|
|
|
fst.fst_length = size;
|
|
|
|
fst.fst_bytesalloc = 0;
|
2014-12-10 18:23:11 +00:00
|
|
|
|
2021-09-15 16:55:46 +00:00
|
|
|
bool ok = fcntl(handle, F_PREALLOCATE, &fst) != -1;
|
2014-12-10 18:23:11 +00:00
|
|
|
|
2021-09-15 16:55:46 +00:00
|
|
|
if (ok)
|
|
|
|
{
|
|
|
|
ok = ftruncate(handle, size) == 0;
|
|
|
|
}
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2021-09-15 16:55:46 +00:00
|
|
|
return ok;
|
|
|
|
}
|
2014-07-08 00:15:12 +00:00
|
|
|
#endif
|
|
|
|
|
2021-09-15 16:55:46 +00:00
|
|
|
#ifdef HAVE_POSIX_FALLOCATE
|
|
|
|
bool full_preallocate_posix(tr_sys_file_t handle, uint64_t size)
|
|
|
|
{
|
|
|
|
return posix_fallocate(handle, 0, size) == 0;
|
|
|
|
}
|
|
|
|
#endif
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2021-09-15 16:55:46 +00:00
|
|
|
} // unnamed namespace
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
bool tr_sys_file_preallocate(tr_sys_file_t handle, uint64_t size, int flags, tr_error** error)
|
2021-09-15 16:55:46 +00:00
|
|
|
{
|
|
|
|
TR_ASSERT(handle != TR_BAD_SYS_FILE);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2021-09-15 16:55:46 +00:00
|
|
|
using prealloc_func = bool (*)(tr_sys_file_t, uint64_t);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2021-09-15 16:55:46 +00:00
|
|
|
// these approaches are fast and should be tried first
|
|
|
|
auto approaches = std::vector<prealloc_func>{
|
|
|
|
#ifdef HAVE_FALLOCATE64
|
|
|
|
preallocate_fallocate64
|
|
|
|
#endif
|
|
|
|
};
|
2014-12-10 18:23:11 +00:00
|
|
|
|
2021-09-15 16:55:46 +00:00
|
|
|
// these approaches are sometimes slower in some settings (e.g.
|
|
|
|
// a slow zeroing of all the preallocated space) so only use them
|
|
|
|
// if specified by `flags`
|
|
|
|
if ((flags & TR_SYS_FILE_PREALLOC_SPARSE) == 0)
|
|
|
|
{
|
|
|
|
// TODO: these functions haven't been reviewed in awhile.
|
|
|
|
// It's possible that some are faster now & should be promoted
|
|
|
|
// to 'always try' and/or replaced with fresher platform API.
|
|
|
|
approaches.insert(
|
|
|
|
std::end(approaches),
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-15 16:55:46 +00:00
|
|
|
#ifdef HAVE_XFS_XFS_H
|
|
|
|
full_preallocate_xfs,
|
2014-07-08 00:15:12 +00:00
|
|
|
#endif
|
2021-09-15 16:55:46 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
full_preallocate_apple,
|
2014-07-08 00:15:12 +00:00
|
|
|
#endif
|
2021-09-15 16:55:46 +00:00
|
|
|
#ifdef HAVE_POSIX_FALLOCATE
|
|
|
|
full_preallocate_posix,
|
2015-12-06 22:24:35 +00:00
|
|
|
#endif
|
2021-09-15 16:55:46 +00:00
|
|
|
});
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 16:55:46 +00:00
|
|
|
for (auto& approach : approaches) // try until one of them works
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-15 16:55:46 +00:00
|
|
|
errno = 0;
|
|
|
|
|
2022-02-02 01:09:11 +00:00
|
|
|
if (auto const success = approach(handle, size); success)
|
2021-09-15 16:55:46 +00:00
|
|
|
{
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errno == ENOSPC) // disk full, so subsequent approaches will fail too
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 16:55:46 +00:00
|
|
|
set_system_error(error, errno);
|
|
|
|
return false;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void* tr_sys_file_map_for_reading(tr_sys_file_t handle, uint64_t offset, uint64_t size, tr_error** error)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(handle != TR_BAD_SYS_FILE);
|
|
|
|
TR_ASSERT(size > 0);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
void* ret = mmap(nullptr, size, PROT_READ, MAP_SHARED, handle, offset);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2021-10-17 21:09:58 +00:00
|
|
|
if (ret == MAP_FAILED) // NOLINT(performance-no-int-to-ptr)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
set_system_error(error, errno);
|
2021-09-15 00:18:09 +00:00
|
|
|
ret = nullptr;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sys_file_unmap(void const* address, uint64_t size, tr_error** error)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(address != nullptr);
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(size > 0);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
bool ret = munmap((void*)address, size) != -1;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
set_system_error(error, errno);
|
|
|
|
}
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2021-10-10 16:52:26 +00:00
|
|
|
bool tr_sys_file_lock([[maybe_unused]] tr_sys_file_t handle, [[maybe_unused]] int operation, tr_error** error)
|
2016-09-21 20:56:03 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(handle != TR_BAD_SYS_FILE);
|
|
|
|
TR_ASSERT((operation & ~(TR_SYS_FILE_LOCK_SH | TR_SYS_FILE_LOCK_EX | TR_SYS_FILE_LOCK_NB | TR_SYS_FILE_LOCK_UN)) == 0);
|
2021-08-15 09:41:48 +00:00
|
|
|
TR_ASSERT(
|
|
|
|
!!(operation & TR_SYS_FILE_LOCK_SH) + !!(operation & TR_SYS_FILE_LOCK_EX) + !!(operation & TR_SYS_FILE_LOCK_UN) == 1);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-29 22:43:25 +00:00
|
|
|
bool ret = false;
|
2019-07-13 19:53:04 +00:00
|
|
|
|
|
|
|
#if defined(F_OFD_SETLK)
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
struct flock fl = {};
|
2019-07-13 19:53:04 +00:00
|
|
|
|
|
|
|
switch (operation & (TR_SYS_FILE_LOCK_SH | TR_SYS_FILE_LOCK_EX | TR_SYS_FILE_LOCK_UN))
|
|
|
|
{
|
|
|
|
case TR_SYS_FILE_LOCK_SH:
|
|
|
|
fl.l_type = F_RDLCK;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_SYS_FILE_LOCK_EX:
|
|
|
|
fl.l_type = F_WRLCK;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_SYS_FILE_LOCK_UN:
|
|
|
|
fl.l_type = F_UNLCK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
fl.l_whence = SEEK_SET;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
ret = fcntl(handle, (operation & TR_SYS_FILE_LOCK_NB) != 0 ? F_OFD_SETLK : F_OFD_SETLKW, &fl) != -1;
|
2021-08-15 09:41:48 +00:00
|
|
|
} while (!ret && errno == EINTR);
|
2019-07-13 19:53:04 +00:00
|
|
|
|
|
|
|
if (!ret && errno == EAGAIN)
|
|
|
|
{
|
|
|
|
errno = EWOULDBLOCK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(HAVE_FLOCK)
|
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
int native_operation = 0;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if ((operation & TR_SYS_FILE_LOCK_SH) != 0)
|
|
|
|
{
|
|
|
|
native_operation |= LOCK_SH;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((operation & TR_SYS_FILE_LOCK_EX) != 0)
|
|
|
|
{
|
|
|
|
native_operation |= LOCK_EX;
|
|
|
|
}
|
2016-09-21 20:56:03 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if ((operation & TR_SYS_FILE_LOCK_NB) != 0)
|
|
|
|
{
|
|
|
|
native_operation |= LOCK_NB;
|
|
|
|
}
|
2016-09-21 20:56:03 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if ((operation & TR_SYS_FILE_LOCK_UN) != 0)
|
|
|
|
{
|
|
|
|
native_operation |= LOCK_UN;
|
|
|
|
}
|
2016-09-21 20:56:03 +00:00
|
|
|
|
2019-07-13 19:53:04 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
ret = flock(handle, native_operation) != -1;
|
2021-08-15 09:41:48 +00:00
|
|
|
} while (!ret && errno == EINTR);
|
2019-07-13 19:53:04 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
errno = ENOSYS;
|
|
|
|
ret = false;
|
|
|
|
|
|
|
|
#endif
|
2016-09-21 20:56:03 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
set_system_error(error, errno);
|
|
|
|
}
|
2016-09-21 20:56:03 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2016-09-21 20:56:03 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
char* tr_sys_dir_get_current(tr_error** error)
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2021-10-29 22:43:25 +00:00
|
|
|
char* ret = getcwd(nullptr, 0);
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (ret == nullptr && (errno == EINVAL || errno == ERANGE))
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t size = PATH_MAX;
|
2021-09-15 00:18:09 +00:00
|
|
|
char* tmp = nullptr;
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
do
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tmp = tr_renew(char, tmp, size);
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (tmp == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = getcwd(tmp, size);
|
|
|
|
size += 2048;
|
2021-09-15 00:18:09 +00:00
|
|
|
} while (ret == nullptr && errno == ERANGE);
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (ret == nullptr)
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
int const err = errno;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(tmp);
|
|
|
|
errno = err;
|
2014-09-21 17:52:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (ret == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
set_system_error(error, errno);
|
|
|
|
}
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-09-21 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sys_dir_create(char const* path, int flags, int permissions, tr_error** error)
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(path != nullptr);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2021-10-29 22:43:25 +00:00
|
|
|
bool ret = false;
|
2021-09-15 00:18:09 +00:00
|
|
|
tr_error* my_error = nullptr;
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if ((flags & TR_SYS_DIR_CREATE_PARENTS) != 0)
|
|
|
|
{
|
2014-09-21 17:52:36 +00:00
|
|
|
#ifdef HAVE_MKDIRP
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = mkdirp(path, permissions) != -1;
|
2014-09-21 17:52:36 +00:00
|
|
|
#else
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = create_path(path, permissions, &my_error);
|
2014-09-21 17:52:36 +00:00
|
|
|
#endif
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = mkdir(path, permissions) != -1;
|
|
|
|
}
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!ret && errno == EEXIST)
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
struct stat sb;
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (stat(path, &sb) != -1 && S_ISDIR(sb.st_mode))
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_error_clear(&my_error);
|
|
|
|
ret = true;
|
2014-09-21 17:52:36 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
errno = EEXIST;
|
2014-09-21 17:52:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!ret)
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (my_error != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_error_propagate(error, &my_error);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
set_system_error(error, errno);
|
|
|
|
}
|
2014-09-21 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-09-21 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool tr_sys_dir_create_temp(char* path_template, tr_error** error)
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(path_template != nullptr);
|
2014-09-21 17:52:36 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_MKDTEMP
|
|
|
|
|
2021-10-29 22:43:25 +00:00
|
|
|
bool const ret = mkdtemp(path_template) != nullptr;
|
2014-09-21 17:52:36 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2021-10-29 22:43:25 +00:00
|
|
|
bool const ret = mktemp(path_template) != nullptr && mkdir(path_template, 0700) != -1;
|
2014-09-21 17:52:36 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
set_system_error(error, errno);
|
|
|
|
}
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-09-21 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_sys_dir_t tr_sys_dir_open(char const* path, tr_error** error)
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(path != nullptr);
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2019-03-10 16:52:27 +00:00
|
|
|
DIR* ret = opendir(path);
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (ret == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
set_system_error(error, errno);
|
2019-03-10 16:52:27 +00:00
|
|
|
return TR_BAD_SYS_DIR;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2019-03-10 16:52:27 +00:00
|
|
|
return (tr_sys_dir_t)ret;
|
2014-09-21 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* tr_sys_dir_read_name(tr_sys_dir_t handle, tr_error** error)
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(handle != TR_BAD_SYS_DIR);
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
char const* ret = nullptr;
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
errno = 0;
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2022-02-02 01:09:11 +00:00
|
|
|
if (auto const* const entry = readdir((DIR*)handle); entry != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
ret = entry->d_name;
|
|
|
|
}
|
|
|
|
else if (errno != 0)
|
|
|
|
{
|
|
|
|
set_system_error(error, errno);
|
|
|
|
}
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-09-21 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool tr_sys_dir_close(tr_sys_dir_t handle, tr_error** error)
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(handle != TR_BAD_SYS_DIR);
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2019-03-10 16:52:27 +00:00
|
|
|
bool ret = closedir((DIR*)handle) != -1;
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
set_system_error(error, errno);
|
|
|
|
}
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-09-21 17:52:36 +00:00
|
|
|
}
|