#ifdef _WIN32
#include <windows.h>
#endif
#include "lib/fs/files.h"
#include "lib/fs/path.h"
#include "lib/container/smartlist.h"
#include "lib/log/log.h"
#include "lib/log/util_bug.h"
#include "lib/log/escape.h"
#include "lib/err/torerr.h"
#include "lib/malloc/malloc.h"
#include "lib/sandbox/sandbox.h"
#include "lib/string/printf.h"
#include "lib/string/util_string.h"
#include "lib/fdio/fdio.h"
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_UTIME_H
#include <utime.h>
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <errno.h>
#include <stdio.h>
#include <string.h>
int
tor_open_cloexec(const char *path, int flags, unsigned mode)
{
int fd;
const char *p = sandbox_intern_string(path);
#ifdef O_CLOEXEC
fd = open(p, flags|O_CLOEXEC, mode);
if (fd >= 0)
return fd;
if (errno != EINVAL)
return -1;
#endif
log_debug(LD_FS, "Opening %s with flags %x", p, flags);
fd = open(p, flags, mode);
#ifdef FD_CLOEXEC
if (fd >= 0) {
if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
log_warn(LD_FS,"Couldn't set FD_CLOEXEC: %s", strerror(errno));
close(fd);
return -1;
}
}
#endif
return fd;
}
FILE *
tor_fopen_cloexec(const char *path, const char *mode)
{
FILE *result = fopen(path, mode);
#ifdef FD_CLOEXEC
if (result != NULL) {
if (fcntl(fileno(result), F_SETFD, FD_CLOEXEC) == -1) {
log_warn(LD_FS,"Couldn't set FD_CLOEXEC: %s", strerror(errno));
fclose(result);
return NULL;
}
}
#endif
return result;
}
int
tor_rename(const char *path_old, const char *path_new)
{
log_debug(LD_FS, "Renaming %s to %s", path_old, path_new);
return rename(sandbox_intern_string(path_old),
sandbox_intern_string(path_new));
}
int
replace_file(const char *from, const char *to)
{
#ifndef _WIN32
return tor_rename(from, to);
#else
switch (file_status(to))
{
case FN_NOENT:
break;
case FN_FILE:
case FN_EMPTY:
if (unlink(to)) return -1;
break;
case FN_ERROR:
return -1;
case FN_DIR:
errno = EISDIR;
return -1;
}
return tor_rename(from,to);
#endif
}
int
touch_file(const char *fname)
{
if (utime(fname, NULL)!=0)
return -1;
return 0;
}
MOCK_IMPL(int,
tor_unlink,(const char *pathname))
{
return unlink(pathname);
}
ssize_t
write_all_to_fd(int fd, const char *buf, size_t count)
{
size_t written = 0;
ssize_t result;
raw_assert(count < SSIZE_MAX);
while (written != count) {
result = write(fd, buf+written, count-written);
if (result<0)
return -1;
written += result;
}
return (ssize_t)count;
}
ssize_t
read_all_from_fd(int fd, char *buf, size_t count)
{
size_t numread = 0;
ssize_t result;
if (count > SIZE_T_CEILING || count > SSIZE_MAX) {
errno = EINVAL;
return -1;
}
while (numread < count) {
result = read(fd, buf+numread, count-numread);
if (result<0)
return -1;
else if (result == 0)
break;
numread += result;
}
return (ssize_t)numread;
}
file_status_t
file_status(const char *fname)
{
struct stat st;
char *f;
int r;
if (!fname || strlen(fname) == 0) {
return FN_ERROR;
}
f = tor_strdup(fname);
clean_fname_for_stat(f);
log_debug(LD_FS, "stat()ing %s", f);
r = stat(sandbox_intern_string(f), &st);
tor_free(f);
if (r) {
if (errno == ENOENT) {
return FN_NOENT;
}
return FN_ERROR;
}
if (st.st_mode & S_IFDIR) {
return FN_DIR;
} else if (st.st_mode & S_IFREG) {
if (st.st_size > 0) {
return FN_FILE;
} else if (st.st_size == 0) {
return FN_EMPTY;
} else {
return FN_ERROR;
}
#ifndef _WIN32
} else if (st.st_mode & S_IFIFO) {
return FN_FILE;
#endif
} else {
return FN_ERROR;
}
}
bool
is_file(file_status_t file_type)
{
return file_type != FN_ERROR && file_type != FN_NOENT && file_type != FN_DIR;
}
bool
is_dir(file_status_t file_type)
{
return file_type == FN_DIR;
}
MOCK_IMPL(int,
write_str_to_file,(const char *fname, const char *str, int bin))
{
#ifdef _WIN32
if (!bin && strchr(str, '\r')) {
log_warn(LD_BUG,
"We're writing a text string that already contains a CR to %s",
escaped(fname));
}
#endif
return write_bytes_to_file(fname, str, strlen(str), bin);
}
struct open_file_t {
char *tempname;
char *filename;
unsigned rename_on_close:1;
unsigned binary:1;
int fd;
FILE *stdio_file;
};
int
start_writing_to_file(const char *fname, int open_flags, int mode,
open_file_t **data_out)
{
open_file_t *new_file = tor_malloc_zero(sizeof(open_file_t));
const char *open_name;
int append = 0;
tor_assert(fname);
tor_assert(data_out);
#if (O_BINARY != 0 && O_TEXT != 0)
tor_assert((open_flags & (O_BINARY|O_TEXT)) != 0);
#endif
new_file->fd = -1;
new_file->filename = tor_strdup(fname);
if (open_flags & O_APPEND) {
open_name = fname;
new_file->rename_on_close = 0;
append = 1;
open_flags &= ~O_APPEND;
} else {
tor_asprintf(&new_file->tempname, "%s.tmp", fname);
open_name = new_file->tempname;
open_flags |= O_CREAT|O_TRUNC;
open_flags &= ~O_EXCL;
new_file->rename_on_close = 1;
}
#if O_BINARY != 0
if (open_flags & O_BINARY)
new_file->binary = 1;
#endif
new_file->fd = tor_open_cloexec(open_name, open_flags, mode);
if (new_file->fd < 0) {
log_warn(LD_FS, "Couldn't open \"%s\" (%s) for writing: %s",
open_name, fname, strerror(errno));
goto err;
}
if (append) {
if (tor_fd_seekend(new_file->fd) < 0) {
log_warn(LD_FS, "Couldn't seek to end of file \"%s\": %s", open_name,
strerror(errno));
goto err;
}
}
*data_out = new_file;
return new_file->fd;
err:
if (new_file->fd >= 0)
close(new_file->fd);
*data_out = NULL;
tor_free(new_file->filename);
tor_free(new_file->tempname);
tor_free(new_file);
return -1;
}
FILE *
fdopen_file(open_file_t *file_data)
{
tor_assert(file_data);
if (file_data->stdio_file)
return file_data->stdio_file;
tor_assert(file_data->fd >= 0);
if (!(file_data->stdio_file = fdopen(file_data->fd,
file_data->binary?"ab":"a"))) {
log_warn(LD_FS, "Couldn't fdopen \"%s\" [%d]: %s", file_data->filename,
file_data->fd, strerror(errno));
}
return file_data->stdio_file;
}
FILE *
start_writing_to_stdio_file(const char *fname, int open_flags, int mode,
open_file_t **data_out)
{
FILE *res;
if (start_writing_to_file(fname, open_flags, mode, data_out)<0)
return NULL;
if (!(res = fdopen_file(*data_out))) {
abort_writing_to_file(*data_out);
*data_out = NULL;
}
return res;
}
static int
finish_writing_to_file_impl(open_file_t *file_data, int abort_write)
{
int r = 0;
tor_assert(file_data && file_data->filename);
if (file_data->stdio_file) {
if (fclose(file_data->stdio_file)) {
log_warn(LD_FS, "Error closing \"%s\": %s", file_data->filename,
strerror(errno));
abort_write = r = -1;
}
} else if (file_data->fd >= 0 && close(file_data->fd) < 0) {
log_warn(LD_FS, "Error flushing \"%s\": %s", file_data->filename,
strerror(errno));
abort_write = r = -1;
}
if (file_data->rename_on_close) {
tor_assert(file_data->tempname && file_data->filename);
if (!abort_write) {
tor_assert(strcmp(file_data->filename, file_data->tempname));
if (replace_file(file_data->tempname, file_data->filename)) {
log_warn(LD_FS, "Error replacing \"%s\": %s", file_data->filename,
strerror(errno));
abort_write = r = -1;
}
}
if (abort_write) {
int res = unlink(file_data->tempname);
if (res != 0) {
log_warn(LD_FS, "Failed to unlink %s: %s",
file_data->tempname, strerror(errno));
r = -1;
}
}
}
tor_free(file_data->filename);
tor_free(file_data->tempname);
tor_free(file_data);
return r;
}
int
finish_writing_to_file(open_file_t *file_data)
{
return finish_writing_to_file_impl(file_data, 0);
}
int
abort_writing_to_file(open_file_t *file_data)
{
return finish_writing_to_file_impl(file_data, 1);
}
static int
write_chunks_to_file_impl(const char *fname, const smartlist_t *chunks,
int open_flags)
{
open_file_t *file = NULL;
int fd;
ssize_t result;
fd = start_writing_to_file(fname, open_flags, 0600, &file);
if (fd<0)
return -1;
SMARTLIST_FOREACH(chunks, sized_chunk_t *, chunk,
{
result = write_all_to_fd(fd, chunk->bytes, chunk->len);
if (result < 0) {
log_warn(LD_FS, "Error writing to \"%s\": %s", fname,
strerror(errno));
goto err;
}
tor_assert((size_t)result == chunk->len);
});
return finish_writing_to_file(file);
err:
abort_writing_to_file(file);
return -1;
}
int
write_chunks_to_file(const char *fname, const smartlist_t *chunks, int bin,
int no_tempfile)
{
int flags = OPEN_FLAGS_REPLACE|(bin?O_BINARY:O_TEXT);
if (no_tempfile) {
flags |= O_APPEND;
}
return write_chunks_to_file_impl(fname, chunks, flags);
}
static int
write_bytes_to_file_impl(const char *fname, const char *str, size_t len,
int flags)
{
int r;
sized_chunk_t c = { str, len };
smartlist_t *chunks = smartlist_new();
smartlist_add(chunks, &c);
r = write_chunks_to_file_impl(fname, chunks, flags);
smartlist_free(chunks);
return r;
}
MOCK_IMPL(int,
write_bytes_to_file,(const char *fname, const char *str, size_t len,
int bin))
{
return write_bytes_to_file_impl(fname, str, len,
OPEN_FLAGS_REPLACE|(bin?O_BINARY:O_TEXT));
}
int
append_bytes_to_file(const char *fname, const char *str, size_t len,
int bin)
{
return write_bytes_to_file_impl(fname, str, len,
OPEN_FLAGS_APPEND|(bin?O_BINARY:O_TEXT));
}
int
write_bytes_to_new_file(const char *fname, const char *str, size_t len,
int bin)
{
return write_bytes_to_file_impl(fname, str, len,
OPEN_FLAGS_DONT_REPLACE|
(bin?O_BINARY:O_TEXT));
}
char *
read_file_to_str_until_eof(int fd, size_t max_bytes_to_read, size_t *sz_out)
{
ssize_t r;
size_t pos = 0;
char *string = NULL;
size_t string_max = 0;
if (max_bytes_to_read+1 >= SIZE_T_CEILING) {
errno = EINVAL;
return NULL;
}
do {
string_max = pos + 1024;
if (string_max > max_bytes_to_read)
string_max = max_bytes_to_read + 1;
string = tor_realloc(string, string_max);
r = read(fd, string + pos, string_max - pos - 1);
if (r < 0) {
int save_errno = errno;
tor_free(string);
errno = save_errno;
return NULL;
}
pos += r;
} while (r > 0 && pos < max_bytes_to_read);
tor_assert(pos < string_max);
*sz_out = pos;
string[pos] = '\0';
return string;
}
MOCK_IMPL(char *,
read_file_to_str, (const char *filename, int flags, struct stat *stat_out))
{
int fd;
struct stat statbuf;
char *string;
ssize_t r;
int bin = flags & RFTS_BIN;
tor_assert(filename);
fd = tor_open_cloexec(filename,O_RDONLY|(bin?O_BINARY:O_TEXT),0);
if (fd<0) {
int severity = LOG_WARN;
int save_errno = errno;
if (errno == ENOENT && (flags & RFTS_IGNORE_MISSING))
severity = LOG_INFO;
log_fn(severity, LD_FS,"Could not open \"%s\": %s",filename,
strerror(errno));
errno = save_errno;
return NULL;
}
if (fstat(fd, &statbuf)<0) {
int save_errno = errno;
close(fd);
log_warn(LD_FS,"Could not fstat \"%s\".",filename);
errno = save_errno;
return NULL;
}
#ifndef _WIN32
#define FIFO_READ_MAX (1024*1024)
if (S_ISFIFO(statbuf.st_mode)) {
size_t sz = 0;
string = read_file_to_str_until_eof(fd, FIFO_READ_MAX, &sz);
int save_errno = errno;
if (string && stat_out) {
statbuf.st_size = sz;
memcpy(stat_out, &statbuf, sizeof(struct stat));
}
close(fd);
if (!string)
errno = save_errno;
return string;
}
#endif
if ((uint64_t)(statbuf.st_size)+1 >= SIZE_T_CEILING) {
close(fd);
errno = EINVAL;
return NULL;
}
string = tor_malloc((size_t)(statbuf.st_size+1));
r = read_all_from_fd(fd,string,(size_t)statbuf.st_size);
if (r<0) {
int save_errno = errno;
log_warn(LD_FS,"Error reading from file \"%s\": %s", filename,
strerror(errno));
tor_free(string);
close(fd);
errno = save_errno;
return NULL;
}
string[r] = '\0';
if (!bin && strchr(string, '\r')) {
log_debug(LD_FS, "We didn't convert CRLF to LF as well as we hoped "
"when reading %s. Coping.",
filename);
tor_strstrip(string, "\r");
r = strlen(string);
}
if (!bin) {
statbuf.st_size = (size_t) r;
} else {
if (r != statbuf.st_size) {
int save_errno = errno;
log_warn(LD_FS,"Could read only %d of %ld bytes of file \"%s\".",
(int)r, (long)statbuf.st_size,filename);
tor_free(string);
close(fd);
errno = save_errno;
return NULL;
}
}
close(fd);
if (stat_out) {
memcpy(stat_out, &statbuf, sizeof(struct stat));
}
return string;
}
int
write_str_to_file_if_not_equal(const char *fname, const char *str)
{
char *fstr = read_file_to_str(fname, RFTS_IGNORE_MISSING, NULL);
int rv;
if (!fstr || strcmp(str, fstr)) {
rv = write_str_to_file(fname, str, 0);
} else {
rv = 0;
}
tor_free(fstr);
return rv;
}
#if !defined(HAVE_GETDELIM) || defined(TOR_UNIT_TESTS)
#include "ext/getdelim.c"
#endif