Tor 0.4.9.2-alpha-dev
|
Wrappers for reading and writing data to files on disk. More...
#include "lib/cc/compat_compiler.h"
#include "lib/cc/torint.h"
#include "lib/testsupport/testsupport.h"
#include <stddef.h>
#include <stdio.h>
Go to the source code of this file.
Data Structures | |
struct | sized_chunk_t |
Macros | |
#define | O_BINARY 0 |
#define | O_TEXT 0 |
#define | O_NOFOLLOW 0 |
#define | OPEN_FLAGS_REPLACE (O_WRONLY|O_CREAT|O_TRUNC) |
#define | OPEN_FLAGS_APPEND (O_WRONLY|O_CREAT|O_APPEND) |
#define | OPEN_FLAGS_DONT_REPLACE (O_CREAT|O_EXCL|O_APPEND|O_WRONLY) |
#define | RFTS_BIN 1 |
#define | RFTS_IGNORE_MISSING 2 |
#define | tor_getdelim(lineptr, n, delim, stream) compat_getdelim_((lineptr), (n), (delim), (stream)) |
#define | tor_getline(lineptr, n, stream) tor_getdelim((lineptr), (n), '\n', (stream)) |
Enumerations | |
enum | file_status_t { FN_ERROR , FN_NOENT , FN_FILE , FN_DIR , FN_EMPTY } |
Functions | |
int | tor_open_cloexec (const char *path, int flags, unsigned mode) |
FILE * | tor_fopen_cloexec (const char *path, const char *mode) |
int | tor_rename (const char *path_old, const char *path_new) |
int | replace_file (const char *from, const char *to) |
int | touch_file (const char *fname) |
int | tor_unlink (const char *pathname) |
file_status_t | file_status (const char *filename) |
bool | is_file (file_status_t file_type) |
bool | is_dir (file_status_t file_type) |
int64_t | tor_get_avail_disk_space (const char *path) |
ssize_t | write_all_to_fd (int fd, const char *buf, size_t count) |
ssize_t | read_all_from_fd (int fd, char *buf, size_t count) |
int | start_writing_to_file (const char *fname, int open_flags, int mode, open_file_t **data_out) |
FILE * | start_writing_to_stdio_file (const char *fname, int open_flags, int mode, open_file_t **data_out) |
FILE * | fdopen_file (open_file_t *file_data) |
int | finish_writing_to_file (open_file_t *file_data) |
int | abort_writing_to_file (open_file_t *file_data) |
int | write_str_to_file (const char *fname, const char *str, int bin) |
int | write_bytes_to_file (const char *fname, const char *str, size_t len, int bin) |
int | write_chunks_to_file (const char *fname, const struct smartlist_t *chunks, int bin, int no_tempfile) |
int | append_bytes_to_file (const char *fname, const char *str, size_t len, int bin) |
int | write_bytes_to_new_file (const char *fname, const char *str, size_t len, int bin) |
int | write_str_to_file_if_not_equal (const char *fname, const char *str) |
MOCK_DECL_ATTR (char *, read_file_to_str,(const char *filename, int flags, struct stat *stat_out), ATTR_MALLOC) | |
char * | read_file_to_str_until_eof (int fd, size_t max_bytes_to_read, size_t *sz_out) ATTR_MALLOC |
ssize_t | compat_getdelim_ (char **lineptr, size_t *n, int delim, FILE *stream) |
Wrappers for reading and writing data to files on disk.
Header for files.c.
Definition in file files.h.
#define OPEN_FLAGS_DONT_REPLACE (O_CREAT|O_EXCL|O_APPEND|O_WRONLY) |
#define RFTS_BIN 1 |
#define RFTS_IGNORE_MISSING 2 |
#define tor_getdelim | ( | lineptr, | |
n, | |||
delim, | |||
stream | |||
) | compat_getdelim_((lineptr), (n), (delim), (stream)) |
#define tor_getline | ( | lineptr, | |
n, | |||
stream | |||
) | tor_getdelim((lineptr), (n), '\n', (stream)) |
enum file_status_t |
Return values from file_status(); see that function's documentation for details.
int abort_writing_to_file | ( | open_file_t * | file_data | ) |
int append_bytes_to_file | ( | const char * | fname, |
const char * | str, | ||
size_t | len, | ||
int | bin | ||
) |
ssize_t compat_getdelim_ | ( | char ** | lineptr, |
size_t * | n, | ||
int | delim, | ||
FILE * | stream | ||
) |
Internal back-end function to implement getdelim(): only exists when Tor is built for unit tests, or when Tor is built on an operating system without its own getdelim().
Definition at line 38 of file getdelim.c.
FILE * fdopen_file | ( | open_file_t * | file_data | ) |
file_status_t file_status | ( | const char * | fname | ) |
Return: FN_ERROR if filename can't be read, is NULL, or is zero-length, FN_NOENT if it doesn't exist, FN_FILE if it is a non-empty regular file, or a FIFO on unix-like systems, FN_EMPTY for zero-byte regular files, FN_DIR if it's a directory, and FN_ERROR for any other file type. On FN_ERROR and FN_NOENT, sets errno. (errno is not set when FN_ERROR is returned due to an unhandled file type.)
int finish_writing_to_file | ( | open_file_t * | file_data | ) |
bool is_dir | ( | file_status_t | file_type | ) |
bool is_file | ( | file_status_t | file_type | ) |
ssize_t read_all_from_fd | ( | int | fd, |
char * | buf, | ||
size_t | count | ||
) |
char * read_file_to_str_until_eof | ( | int | fd, |
size_t | max_bytes_to_read, | ||
size_t * | sz_out | ||
) |
Read the contents of the open file fd presuming it is a FIFO (or similar) file descriptor for which the size of the file isn't known ahead of time. Return NULL on failure, and a NUL-terminated string on success. On success, set sz_out to the number of bytes read (not including the final NULL, which wasn't read from fd).
int replace_file | ( | const char * | from, |
const char * | to | ||
) |
int start_writing_to_file | ( | const char * | fname, |
int | open_flags, | ||
int | mode, | ||
open_file_t ** | data_out | ||
) |
Try to start writing to the file in fname, passing the flags open_flags to the open() syscall, creating the file (if needed) with access value mode. If the O_APPEND flag is set, we append to the original file. Otherwise, we open a new temporary file in the same directory, and either replace the original or remove the temporary file when we're done.
Return the fd for the newly opened file, and store working data in *data_out. The caller should not close the fd manually: instead, call finish_writing_to_file() or abort_writing_to_file(). Returns -1 on failure.
NOTE: When not appending, the flags O_CREAT and O_TRUNC are treated as true and the flag O_EXCL is treated as false.
NOTE: Ordinarily, O_APPEND means "seek to the end of the file before each write()". We don't do that.
FILE * start_writing_to_stdio_file | ( | const char * | fname, |
int | open_flags, | ||
int | mode, | ||
open_file_t ** | data_out | ||
) |
FILE * tor_fopen_cloexec | ( | const char * | path, |
const char * | mode | ||
) |
int64_t tor_get_avail_disk_space | ( | const char * | path | ) |
Return the amount of free disk space we have permission to use, in bytes. Return -1 if the amount of free space can't be determined.
Definition at line 28 of file freespace.c.
int tor_open_cloexec | ( | const char * | path, |
int | flags, | ||
unsigned | mode | ||
) |
int tor_rename | ( | const char * | path_old, |
const char * | path_new | ||
) |
int tor_unlink | ( | const char * | pathname | ) |
int touch_file | ( | const char * | fname | ) |
ssize_t write_all_to_fd | ( | int | fd, |
const char * | buf, | ||
size_t | count | ||
) |
int write_bytes_to_file | ( | const char * | fname, |
const char * | str, | ||
size_t | len, | ||
int | bin | ||
) |
int write_bytes_to_new_file | ( | const char * | fname, |
const char * | str, | ||
size_t | len, | ||
int | bin | ||
) |
int write_str_to_file | ( | const char * | fname, |
const char * | str, | ||
int | bin | ||
) |
Create a file named fname with the contents str. Overwrite the previous fname if possible. Return 0 on success, -1 on failure.
This function replaces the old file atomically, if possible. This function, and all other functions in util.c that create files, create them with mode 0600.
int write_str_to_file_if_not_equal | ( | const char * | fname, |
const char * | str | ||
) |