2014-07-03 21:58:39 +00:00
|
|
|
/*
|
2017-11-27 22:22:44 +00:00
|
|
|
* This file Copyright (C) 2013-2017 Mnemosyne LLC
|
2014-07-03 21:58:39 +00:00
|
|
|
*
|
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-09-21 20:56:03 +00:00
|
|
|
#undef _GNU_SOURCE
|
|
|
|
#define _GNU_SOURCE
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2014-09-21 17:52:36 +00:00
|
|
|
#include <dirent.h>
|
2014-07-03 21:58:39 +00:00
|
|
|
#include <errno.h>
|
2019-07-13 19:53:04 +00:00
|
|
|
#include <fcntl.h> /* O_LARGEFILE, posix_fadvise(), [posix_]fallocate(), fcntl() */
|
2017-04-21 07:40:57 +00:00
|
|
|
#include <libgen.h> /* basename(), dirname() */
|
2014-07-03 21:58:39 +00:00
|
|
|
#include <limits.h> /* PATH_MAX */
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2014-07-08 00:15:12 +00:00
|
|
|
#include <string.h>
|
2014-07-03 21:58:39 +00:00
|
|
|
#include <sys/types.h>
|
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>
|
2017-04-21 07:40:57 +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
|
|
|
|
|
|
|
#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"
|
2014-07-08 00:15:12 +00:00
|
|
|
#include "platform.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.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
|
|
|
|
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (error == NULL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_error_set_literal(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)
|
|
|
|
{
|
|
|
|
tr_error_set(error, ENOTDIR, _("File \"%s\" is in the way"), path);
|
|
|
|
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 */
|
2017-10-27 13:12:55 +00:00
|
|
|
char* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* pp;
|
|
|
|
bool ret = false;
|
|
|
|
tr_error* my_error = NULL;
|
|
|
|
|
|
|
|
/* Go one level up on each iteration and attempt to create */
|
|
|
|
for (pp = path_end; pp != NULL; pp = strrchr(p, TR_PATH_DELIMITER))
|
|
|
|
{
|
|
|
|
*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
|
|
|
|
|
|
|
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);
|
|
|
|
goto failure;
|
2014-09-21 17:52:36 +00:00
|
|
|
}
|
2017-10-27 13:12:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ret && pp == path_end)
|
|
|
|
{
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Go one level down on each iteration and attempt to create */
|
2017-10-31 07:01:06 +00:00
|
|
|
for (pp = pp == NULL ? 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)
|
|
|
|
{
|
|
|
|
goto cleanup;
|
2014-09-21 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
2017-10-27 13:12:55 +00:00
|
|
|
failure:
|
|
|
|
|
|
|
|
TR_ASSERT(!ret);
|
|
|
|
TR_ASSERT(my_error != NULL);
|
|
|
|
|
|
|
|
tr_logAddError(_("Couldn't create \"%1$s\": %2$s"), path, my_error->message);
|
|
|
|
tr_error_propagate(error, &my_error);
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
|
|
|
TR_ASSERT(my_error == NULL);
|
|
|
|
|
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
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(path != NULL);
|
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
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(path != NULL);
|
|
|
|
TR_ASSERT(info != NULL);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
bool ret;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sys_path_is_relative(char const* path)
|
2015-10-20 21:22:19 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(path != NULL);
|
2015-10-20 21:22:19 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return path[0] != '/';
|
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
|
|
|
{
|
2017-06-13 02:24:09 +00:00
|
|
|
TR_ASSERT(path1 != NULL);
|
|
|
|
TR_ASSERT(path2 != NULL);
|
|
|
|
|
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
|
|
|
{
|
2017-06-13 02:24:09 +00:00
|
|
|
TR_ASSERT(path != NULL);
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
char* ret = NULL;
|
|
|
|
char* tmp = NULL;
|
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
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
#if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200809L
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
/* Better safe than sorry: realpath() officially supports NULL as destination
|
2017-04-19 12:04:45 +00:00
|
|
|
starting off POSIX.1-2008. */
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (ret == NULL)
|
|
|
|
{
|
|
|
|
ret = realpath(path, NULL);
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (ret == NULL)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tmp = tr_new(char, PATH_MAX);
|
|
|
|
ret = realpath(path, tmp);
|
|
|
|
|
|
|
|
if (ret != NULL)
|
|
|
|
{
|
|
|
|
ret = tr_strdup(ret);
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (ret == NULL)
|
|
|
|
{
|
|
|
|
set_system_error(error, errno);
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(tmp);
|
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_basename(char const* path, tr_error** error)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2017-06-13 02:24:09 +00:00
|
|
|
TR_ASSERT(path != NULL);
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
char* ret = NULL;
|
|
|
|
char* tmp;
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tmp = tr_strdup(path);
|
|
|
|
ret = basename(tmp);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (ret != NULL)
|
|
|
|
{
|
|
|
|
ret = tr_strdup(ret);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
set_system_error(error, errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_free(tmp);
|
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_dirname(char const* path, tr_error** error)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(path != NULL);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
char* tmp = tr_strdup(path);
|
|
|
|
char* ret = dirname(tmp);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (ret != NULL)
|
|
|
|
{
|
|
|
|
ret = tr_strdup(ret);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
set_system_error(error, errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_free(tmp);
|
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_rename(char const* src_path, char const* dst_path, tr_error** error)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(src_path != NULL);
|
|
|
|
TR_ASSERT(dst_path != NULL);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
bool 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
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(path != NULL);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
bool 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:
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT_MSG(false, "unknown standard file %d", (int)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
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(path != NULL);
|
|
|
|
TR_ASSERT((flags & (TR_SYS_FILE_READ | TR_SYS_FILE_WRITE)) != 0);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
tr_sys_file_t ret;
|
|
|
|
int native_flags = 0;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if ((flags & (TR_SYS_FILE_READ | TR_SYS_FILE_WRITE)) == (TR_SYS_FILE_READ | TR_SYS_FILE_WRITE))
|
|
|
|
{
|
|
|
|
native_flags |= O_RDWR;
|
|
|
|
}
|
2019-07-14 12:40:41 +00:00
|
|
|
else if ((flags & TR_SYS_FILE_READ) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
native_flags |= O_RDONLY;
|
|
|
|
}
|
2019-07-14 12:40:41 +00:00
|
|
|
else if ((flags & TR_SYS_FILE_WRITE) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
native_flags |= O_WRONLY;
|
|
|
|
}
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
native_flags |=
|
2019-07-14 12:40:41 +00:00
|
|
|
((flags & TR_SYS_FILE_CREATE) != 0 ? O_CREAT : 0) |
|
|
|
|
((flags & TR_SYS_FILE_CREATE_NEW) != 0 ? O_CREAT | O_EXCL : 0) |
|
|
|
|
((flags & TR_SYS_FILE_APPEND) != 0 ? O_APPEND : 0) |
|
|
|
|
((flags & TR_SYS_FILE_TRUNCATE) != 0 ? O_TRUNC : 0) |
|
|
|
|
((flags & TR_SYS_FILE_SEQUENTIAL) != 0 ? O_SEQUENTIAL : 0) |
|
2017-04-19 12:04:45 +00:00
|
|
|
O_BINARY | O_LARGEFILE | O_CLOEXEC;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
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
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(path_template != NULL);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
tr_sys_file_t 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
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
bool 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);
|
|
|
|
TR_ASSERT(info != NULL);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
struct stat sb;
|
|
|
|
bool 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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
TR_STATIC_ASSERT(TR_SEEK_SET == SEEK_SET, "values should match");
|
|
|
|
TR_STATIC_ASSERT(TR_SEEK_CUR == SEEK_CUR, "values should match");
|
|
|
|
TR_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;
|
|
|
|
off_t my_new_offset;
|
|
|
|
|
|
|
|
TR_STATIC_ASSERT(sizeof(*new_offset) >= sizeof(my_new_offset), "");
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
my_new_offset = lseek(handle, offset, origin);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (my_new_offset != -1)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (new_offset != NULL)
|
|
|
|
{
|
|
|
|
*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);
|
|
|
|
TR_ASSERT(buffer != NULL || size == 0);
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool ret = false;
|
|
|
|
ssize_t my_bytes_read;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
TR_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
|
|
|
my_bytes_read = read(handle, buffer, size);
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (bytes_read != NULL)
|
|
|
|
{
|
|
|
|
*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
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +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,
|
|
|
|
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(buffer != NULL || 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;
|
|
|
|
ssize_t my_bytes_read;
|
|
|
|
|
|
|
|
TR_STATIC_ASSERT(sizeof(*bytes_read) >= sizeof(my_bytes_read), "");
|
|
|
|
|
2014-07-08 00:15:12 +00:00
|
|
|
#ifdef HAVE_PREAD
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
my_bytes_read = pread(handle, buffer, size, offset);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (lseek(handle, offset, SEEK_SET) != -1)
|
|
|
|
{
|
|
|
|
my_bytes_read = read(handle, buffer, size);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
my_bytes_read = -1;
|
|
|
|
}
|
2014-07-08 00:15:12 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (my_bytes_read != -1)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (bytes_read != NULL)
|
|
|
|
{
|
|
|
|
*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
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
TR_ASSERT(buffer != NULL || size == 0);
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool ret = false;
|
|
|
|
ssize_t my_bytes_written;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
TR_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
|
|
|
my_bytes_written = write(handle, buffer, size);
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (bytes_written != NULL)
|
|
|
|
{
|
|
|
|
*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-20 16:02:19 +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);
|
|
|
|
TR_ASSERT(buffer != NULL || 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;
|
|
|
|
ssize_t my_bytes_written;
|
|
|
|
|
|
|
|
TR_STATIC_ASSERT(sizeof(*bytes_written) >= sizeof(my_bytes_written), "");
|
|
|
|
|
2014-07-08 00:15:12 +00:00
|
|
|
#ifdef HAVE_PWRITE
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
my_bytes_written = pwrite(handle, buffer, size, offset);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (lseek(handle, offset, SEEK_SET) != -1)
|
|
|
|
{
|
|
|
|
my_bytes_written = write(handle, buffer, size);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
my_bytes_written = -1;
|
|
|
|
}
|
2014-07-08 00:15:12 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (my_bytes_written != -1)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (bytes_written != NULL)
|
|
|
|
{
|
|
|
|
*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
|
|
|
}
|
|
|
|
|
2017-07-08 13:09:37 +00:00
|
|
|
bool tr_sys_file_advise(tr_sys_file_t handle, uint64_t offset, uint64_t size, tr_sys_file_advice_t advice, 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
|
|
|
|
2017-07-08 13:09:37 +00:00
|
|
|
int const native_advice = advice == TR_SYS_FILE_ADVICE_WILL_NEED ? POSIX_FADV_WILLNEED :
|
|
|
|
(advice == TR_SYS_FILE_ADVICE_DONT_NEED ? POSIX_FADV_DONTNEED : POSIX_FADV_NORMAL);
|
|
|
|
|
|
|
|
TR_ASSERT(native_advice != POSIX_FADV_NORMAL);
|
|
|
|
|
|
|
|
int const code = posix_fadvise(handle, offset, size, native_advice);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-07-08 09:51:36 +00:00
|
|
|
if (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
|
|
|
|
2017-07-08 13:09:37 +00:00
|
|
|
if (advice != TR_SYS_FILE_ADVICE_WILL_NEED)
|
|
|
|
{
|
|
|
|
goto skip_darwin_fcntl;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct radvisory const radv =
|
|
|
|
{
|
|
|
|
.ra_offset = offset,
|
|
|
|
.ra_count = size
|
|
|
|
};
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = fcntl(handle, F_RDADVISE, &radv) != -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-07-08 13:09:37 +00:00
|
|
|
skip_darwin_fcntl:
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2020-08-18 11:09:30 +00:00
|
|
|
TR_UNUSED(handle);
|
|
|
|
TR_UNUSED(offset);
|
|
|
|
TR_UNUSED(size);
|
|
|
|
TR_UNUSED(advice);
|
|
|
|
TR_UNUSED(error);
|
2017-07-08 13:09:37 +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
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool tr_sys_file_preallocate(tr_sys_file_t handle, uint64_t size, int flags, tr_error** error)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2020-08-18 11:09:30 +00:00
|
|
|
TR_UNUSED(size);
|
2020-08-11 18:11:55 +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 = false;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
errno = 0;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_FALLOCATE64
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* fallocate64 is always preferred, so try it first */
|
|
|
|
ret = fallocate64(handle, 0, 0, size) != -1;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (ret || errno == ENOSPC)
|
|
|
|
{
|
|
|
|
goto out;
|
|
|
|
}
|
2014-12-10 18:23:11 +00:00
|
|
|
|
2014-07-08 00:15:12 +00:00
|
|
|
#endif
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if ((flags & TR_SYS_FILE_PREALLOC_SPARSE) == 0)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int code = errno;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_XFS_XFS_H
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (platform_test_xfs_fd(handle))
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
xfs_flock64_t fl;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
fl.l_whence = 0;
|
|
|
|
fl.l_start = 0;
|
|
|
|
fl.l_len = size;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = xfsctl(NULL, handle, XFS_IOC_RESVSP64, &fl) != -1;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
ret = ftruncate(handle, size) != -1;
|
|
|
|
}
|
2014-12-10 18:23:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
code = errno;
|
2014-12-10 18:23:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (ret || code == ENOSPC)
|
|
|
|
{
|
|
|
|
goto non_sparse_out;
|
|
|
|
}
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
fstore_t fst;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +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-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = fcntl(handle, F_PREALLOCATE, &fst) != -1;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
ret = ftruncate(handle, size) != -1;
|
|
|
|
}
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
code = errno;
|
2014-12-10 18:23:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (ret || code == ENOSPC)
|
|
|
|
{
|
|
|
|
goto non_sparse_out;
|
|
|
|
}
|
|
|
|
}
|
2014-07-08 00:15:12 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_POSIX_FALLOCATE
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
code = posix_fallocate(handle, 0, size);
|
|
|
|
ret = code == 0;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2015-12-06 22:24:35 +00:00
|
|
|
#if defined(HAVE_XFS_XFS_H) || defined(__APPLE__)
|
2014-12-10 18:23:11 +00:00
|
|
|
non_sparse_out:
|
2015-12-06 22:24:35 +00:00
|
|
|
#endif
|
2017-04-19 12:04:45 +00:00
|
|
|
errno = code;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2015-12-28 23:53:55 +00:00
|
|
|
#ifdef HAVE_FALLOCATE64
|
2014-12-10 18:23:11 +00:00
|
|
|
out:
|
2015-12-28 23:53:55 +00:00
|
|
|
#endif
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
set_system_error(error, errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
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
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
void* ret = mmap(NULL, size, PROT_READ, MAP_SHARED, handle, offset);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (ret == MAP_FAILED)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
set_system_error(error, errno);
|
|
|
|
ret = NULL;
|
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
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(address != NULL);
|
|
|
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool tr_sys_file_lock(tr_sys_file_t handle, 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);
|
|
|
|
TR_ASSERT(!!(operation & TR_SYS_FILE_LOCK_SH) + !!(operation & TR_SYS_FILE_LOCK_EX) +
|
2017-04-19 12:04:45 +00:00
|
|
|
!!(operation & TR_SYS_FILE_LOCK_UN) == 1);
|
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
bool ret;
|
2019-07-13 19:53:04 +00:00
|
|
|
|
|
|
|
#if defined(F_OFD_SETLK)
|
|
|
|
|
2019-07-13 20:02:27 +00:00
|
|
|
struct flock fl = { 0 };
|
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;
|
|
|
|
}
|
|
|
|
while (!ret && errno == EINTR);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
while (!ret && errno == EINTR);
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2020-08-18 11:09:30 +00:00
|
|
|
TR_UNUSED(handle);
|
|
|
|
TR_UNUSED(operation);
|
2019-07-13 19:53:04 +00:00
|
|
|
|
|
|
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* ret;
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = getcwd(NULL, 0);
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (ret == NULL && (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;
|
|
|
|
char* tmp = NULL;
|
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);
|
|
|
|
|
|
|
|
if (tmp == NULL)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = getcwd(tmp, size);
|
|
|
|
size += 2048;
|
2014-09-21 17:52:36 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
while (ret == NULL && errno == ERANGE);
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (ret == NULL)
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (ret == NULL)
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2017-06-13 02:24:09 +00:00
|
|
|
TR_ASSERT(path != NULL);
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool ret;
|
|
|
|
tr_error* my_error = NULL;
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (my_error != NULL)
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(path_template != NULL);
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
bool ret;
|
|
|
|
|
2014-09-21 17:52:36 +00:00
|
|
|
#ifdef HAVE_MKDTEMP
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = mkdtemp(path_template) != NULL;
|
2014-09-21 17:52:36 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = mktemp(path_template) != NULL && 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
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(path != NULL);
|
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
|
|
|
|
2019-03-10 16:52:27 +00:00
|
|
|
if (ret == NULL)
|
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
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
char const* ret = NULL;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
errno = 0;
|
2019-03-10 16:52:27 +00:00
|
|
|
struct dirent* entry = readdir((DIR*)handle);
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (entry != NULL)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|