Update copyrights to 2021, using "make update-copyright"
[tor.git] / src / lib / fs / files.h
blobe02365db521aa97fb6f2c5799b60137b84d34eb3
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 */
6 /**
7 * \file files.h
9 * \brief Header for files.c
10 **/
12 #ifndef TOR_FS_H
13 #define TOR_FS_H
15 #include "lib/cc/compat_compiler.h"
16 #include "lib/cc/torint.h"
17 #include "lib/testsupport/testsupport.h"
19 #include <stddef.h>
20 #include <stdio.h>
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) */
32 #ifndef O_BINARY
33 #define O_BINARY 0
34 #endif
35 #ifndef O_TEXT
36 #define O_TEXT 0
37 #endif
38 #ifndef O_NOFOLLOW
39 #define O_NOFOLLOW 0
40 #endif
42 struct stat;
44 int tor_open_cloexec(const char *path, int flags, unsigned mode);
45 FILE *tor_fopen_cloexec(const char *path, const char *mode);
46 int tor_rename(const char *path_old, const char *path_new);
48 int replace_file(const char *from, const char *to);
49 int touch_file(const char *fname);
51 MOCK_DECL(int,tor_unlink,(const char *pathname));
53 /** Return values from file_status(); see that function's documentation
54 * for details. */
55 typedef enum { FN_ERROR, FN_NOENT, FN_FILE, FN_DIR, FN_EMPTY } file_status_t;
57 file_status_t file_status(const char *filename);
58 bool is_file(file_status_t file_type);
59 bool is_dir(file_status_t file_type);
61 int64_t tor_get_avail_disk_space(const char *path);
63 ssize_t write_all_to_fd(int fd, const char *buf, size_t count);
64 ssize_t read_all_from_fd(int fd, char *buf, size_t count);
66 #define OPEN_FLAGS_REPLACE (O_WRONLY|O_CREAT|O_TRUNC)
67 #define OPEN_FLAGS_APPEND (O_WRONLY|O_CREAT|O_APPEND)
68 #define OPEN_FLAGS_DONT_REPLACE (O_CREAT|O_EXCL|O_APPEND|O_WRONLY)
69 typedef struct open_file_t open_file_t;
70 int start_writing_to_file(const char *fname, int open_flags, int mode,
71 open_file_t **data_out);
72 FILE *start_writing_to_stdio_file(const char *fname, int open_flags, int mode,
73 open_file_t **data_out);
74 FILE *fdopen_file(open_file_t *file_data);
75 int finish_writing_to_file(open_file_t *file_data);
76 int abort_writing_to_file(open_file_t *file_data);
77 MOCK_DECL(int, write_str_to_file,(const char *fname, const char *str,
78 int bin));
79 MOCK_DECL(int, write_bytes_to_file,(const char *fname, const char *str,
80 size_t len,int bin));
82 /** An ad-hoc type to hold a string of characters and a count; used by
83 * write_chunks_to_file. */
84 typedef struct sized_chunk_t {
85 const char *bytes;
86 size_t len;
87 } sized_chunk_t;
88 struct smartlist_t;
89 int write_chunks_to_file(const char *fname, const struct smartlist_t *chunks,
90 int bin, int no_tempfile);
91 int append_bytes_to_file(const char *fname, const char *str, size_t len,
92 int bin);
93 int write_bytes_to_new_file(const char *fname, const char *str, size_t len,
94 int bin);
96 int write_str_to_file_if_not_equal(const char *fname, const char *str);
98 /** Flag for read_file_to_str: open the file in binary mode. */
99 #define RFTS_BIN 1
100 /** Flag for read_file_to_str: it's okay if the file doesn't exist. */
101 #define RFTS_IGNORE_MISSING 2
103 MOCK_DECL_ATTR(char *, read_file_to_str,(const char *filename, int flags,
104 struct stat *stat_out),
105 ATTR_MALLOC);
106 char *read_file_to_str_until_eof(int fd, size_t max_bytes_to_read,
107 size_t *sz_out)
108 ATTR_MALLOC;
110 #if !defined(HAVE_GETDELIM) || defined(TOR_UNIT_TESTS)
111 /** Internal back-end function to implement getdelim(): only exists when
112 * Tor is built for unit tests, or when Tor is built on an operating system
113 * without its own getdelim(). */
114 ssize_t compat_getdelim_(char **lineptr, size_t *n, int delim, FILE *stream);
115 #endif /* !defined(HAVE_GETDELIM) || defined(TOR_UNIT_TESTS) */
117 #ifdef HAVE_GETDELIM
119 * Cross-platform wrapper for getdelim(): behaves as the POSIX-standard
120 * getdelim() function.
122 * See `getdelim(3)` for more information.
124 * Note that this function will use the libc memory allocator -- so any memory
125 * passed to this function must come from raw_malloc(), and must be freed by
126 * raw_free() -- don't use tor_malloc() and tor_free() with this.
128 #define tor_getdelim(lineptr, n, delim, stream) \
129 getdelim((lineptr), (n), (delim), (stream))
130 #else /* !defined(HAVE_GETDELIM) */
131 #define tor_getdelim(lineptr, n, delim, stream) \
132 compat_getdelim_((lineptr), (n), (delim), (stream))
133 #endif /* defined(HAVE_GETDELIM) */
135 #ifdef HAVE_GETLINE
137 * Cross-platform wrapper for getline(): behaves as the POSIX-standard
138 * getline() function.
140 * See tor_getdelim() for usage notes.
142 #define tor_getline(lineptr, n, stream) \
143 getline((lineptr), (n), (stream))
144 #else /* !defined(HAVE_GETLINE) */
145 #define tor_getline(lineptr, n, stream) \
146 tor_getdelim((lineptr), (n), '\n', (stream))
147 #endif /* defined(HAVE_GETLINE) */
149 #endif /* !defined(TOR_FS_H) */