Tor 0.4.9.2-alpha-dev
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
files.h
Go to the documentation of this file.
1/* Copyright (c) 2003-2004, Roger Dingledine
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2021, The Tor Project, Inc. */
4/* See LICENSE for licensing information */
5
6/**
7 * \file files.h
8 *
9 * \brief Header for files.c
10 **/
11
12#ifndef TOR_FS_H
13#define TOR_FS_H
14
16#include "lib/cc/torint.h"
18
19#include <stddef.h>
20#include <stdio.h>
21
22#ifdef _WIN32
23/* We need these for struct stat to work */
24#ifdef HAVE_SYS_TYPES_H
25#include <sys/types.h>
26#endif
27#ifdef HAVE_SYS_STAT_H
28#include <sys/stat.h>
29#endif
30#endif /* defined(_WIN32) */
31
32#if HAVE_FCNTL_H
33#include <fcntl.h>
34#endif
35
36#ifndef O_BINARY
37#define O_BINARY 0
38#endif
39#ifndef O_TEXT
40#define O_TEXT 0
41#endif
42#ifndef O_NOFOLLOW
43#define O_NOFOLLOW 0
44#endif
45
46struct stat;
47
48int tor_open_cloexec(const char *path, int flags, unsigned mode);
49FILE *tor_fopen_cloexec(const char *path, const char *mode);
50int tor_rename(const char *path_old, const char *path_new);
51
52int replace_file(const char *from, const char *to);
53int touch_file(const char *fname);
54
55MOCK_DECL(int,tor_unlink,(const char *pathname));
56
57/** Return values from file_status(); see that function's documentation
58 * for details. */
59typedef enum { FN_ERROR, FN_NOENT, FN_FILE, FN_DIR, FN_EMPTY } file_status_t;
60
61file_status_t file_status(const char *filename);
62bool is_file(file_status_t file_type);
63bool is_dir(file_status_t file_type);
64
65int64_t tor_get_avail_disk_space(const char *path);
66
67ssize_t write_all_to_fd(int fd, const char *buf, size_t count);
68ssize_t read_all_from_fd(int fd, char *buf, size_t count);
69
70#define OPEN_FLAGS_REPLACE (O_WRONLY|O_CREAT|O_TRUNC)
71#define OPEN_FLAGS_APPEND (O_WRONLY|O_CREAT|O_APPEND)
72#define OPEN_FLAGS_DONT_REPLACE (O_CREAT|O_EXCL|O_APPEND|O_WRONLY)
73typedef struct open_file_t open_file_t;
74int start_writing_to_file(const char *fname, int open_flags, int mode,
75 open_file_t **data_out);
76FILE *start_writing_to_stdio_file(const char *fname, int open_flags, int mode,
77 open_file_t **data_out);
78FILE *fdopen_file(open_file_t *file_data);
81MOCK_DECL(int, write_str_to_file,(const char *fname, const char *str,
82 int bin));
83MOCK_DECL(int, write_bytes_to_file,(const char *fname, const char *str,
84 size_t len,int bin));
85
86/** An ad-hoc type to hold a string of characters and a count; used by
87 * write_chunks_to_file. */
88typedef struct sized_chunk_t {
89 const char *bytes;
90 size_t len;
92struct smartlist_t;
93int write_chunks_to_file(const char *fname, const struct smartlist_t *chunks,
94 int bin, int no_tempfile);
95int append_bytes_to_file(const char *fname, const char *str, size_t len,
96 int bin);
97int write_bytes_to_new_file(const char *fname, const char *str, size_t len,
98 int bin);
99
100int write_str_to_file_if_not_equal(const char *fname, const char *str);
101
102/** Flag for read_file_to_str: open the file in binary mode. */
103#define RFTS_BIN 1
104/** Flag for read_file_to_str: it's okay if the file doesn't exist. */
105#define RFTS_IGNORE_MISSING 2
106
107MOCK_DECL_ATTR(char *, read_file_to_str,(const char *filename, int flags,
108 struct stat *stat_out),
109 ATTR_MALLOC);
110char *read_file_to_str_until_eof(int fd, size_t max_bytes_to_read,
111 size_t *sz_out)
112 ATTR_MALLOC;
113
114#if !defined(HAVE_GETDELIM) || defined(TOR_UNIT_TESTS)
115/** Internal back-end function to implement getdelim(): only exists when
116 * Tor is built for unit tests, or when Tor is built on an operating system
117 * without its own getdelim(). */
118ssize_t compat_getdelim_(char **lineptr, size_t *n, int delim, FILE *stream);
119#endif /* !defined(HAVE_GETDELIM) || defined(TOR_UNIT_TESTS) */
120
121#ifdef HAVE_GETDELIM
122/**
123 * Cross-platform wrapper for getdelim(): behaves as the POSIX-standard
124 * getdelim() function.
125 *
126 * See `getdelim(3)` for more information.
127 *
128 * Note that this function will use the libc memory allocator -- so any memory
129 * passed to this function must come from raw_malloc(), and must be freed by
130 * raw_free() -- don't use tor_malloc() and tor_free() with this.
131 */
132#define tor_getdelim(lineptr, n, delim, stream) \
133 getdelim((lineptr), (n), (delim), (stream))
134#else /* !defined(HAVE_GETDELIM) */
135#define tor_getdelim(lineptr, n, delim, stream) \
136 compat_getdelim_((lineptr), (n), (delim), (stream))
137#endif /* defined(HAVE_GETDELIM) */
138
139#ifdef HAVE_GETLINE
140/**
141 * Cross-platform wrapper for getline(): behaves as the POSIX-standard
142 * getline() function.
143 *
144 * See tor_getdelim() for usage notes.
145 */
146#define tor_getline(lineptr, n, stream) \
147 getline((lineptr), (n), (stream))
148#else /* !defined(HAVE_GETLINE) */
149#define tor_getline(lineptr, n, stream) \
150 tor_getdelim((lineptr), (n), '\n', (stream))
151#endif /* defined(HAVE_GETLINE) */
152
153#endif /* !defined(TOR_FS_H) */
Utility macros to handle different features and behavior in different compilers.
char * read_file_to_str_until_eof(int fd, size_t max_bytes_to_read, size_t *sz_out) ATTR_MALLOC
Definition: files.c:582
int write_str_to_file(const char *fname, const char *str, int bin)
Definition: files.c:275
ssize_t read_all_from_fd(int fd, char *buf, size_t count)
Definition: files.c:182
int tor_unlink(const char *pathname)
Definition: files.c:155
file_status_t file_status(const char *filename)
Definition: files.c:213
int finish_writing_to_file(open_file_t *file_data)
Definition: files.c:466
int write_bytes_to_new_file(const char *fname, const char *str, size_t len, int bin)
Definition: files.c:565
ssize_t compat_getdelim_(char **lineptr, size_t *n, int delim, FILE *stream)
Definition: getdelim.c:38
int start_writing_to_file(const char *fname, int open_flags, int mode, open_file_t **data_out)
Definition: files.c:318
int tor_open_cloexec(const char *path, int flags, unsigned mode)
Definition: files.c:54
int touch_file(const char *fname)
Definition: files.c:143
ssize_t write_all_to_fd(int fd, const char *buf, size_t count)
Definition: files.c:163
int write_str_to_file_if_not_equal(const char *fname, const char *str)
Definition: files.c:745
int64_t tor_get_avail_disk_space(const char *path)
Definition: freespace.c:28
file_status_t
Definition: files.h:59
int append_bytes_to_file(const char *fname, const char *str, size_t len, int bin)
Definition: files.c:555
FILE * start_writing_to_stdio_file(const char *fname, int open_flags, int mode, open_file_t **data_out)
Definition: files.c:399
int replace_file(const char *from, const char *to)
Definition: files.c:118
int abort_writing_to_file(open_file_t *file_data)
Definition: files.c:474
int write_bytes_to_file(const char *fname, const char *str, size_t len, int bin)
Definition: files.c:546
FILE * tor_fopen_cloexec(const char *path, const char *mode)
Definition: files.c:86
int tor_rename(const char *path_old, const char *path_new)
Definition: files.c:104
bool is_file(file_status_t file_type)
Definition: files.c:254
FILE * fdopen_file(open_file_t *file_data)
Definition: files.c:382
bool is_dir(file_status_t file_type)
Definition: files.c:262
Macros to implement mocking and selective exposure for the test code.
#define MOCK_DECL(rv, funcname, arglist)
Definition: testsupport.h:127
#define MOCK_DECL_ATTR(rv, funcname, arglist, attr)
Definition: testsupport.h:130
Integer definitions used throughout Tor.