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 */
9 * \brief Header for files.c
15 #include "lib/cc/compat_compiler.h"
16 #include "lib/cc/torint.h"
17 #include "lib/testsupport/testsupport.h"
23 /* We need these for struct stat to work */
24 #ifdef HAVE_SYS_TYPES_H
25 #include <sys/types.h>
27 #ifdef HAVE_SYS_STAT_H
30 #endif /* defined(_WIN32) */
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
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
,
79 MOCK_DECL(int, write_bytes_to_file
,(const char *fname
, const char *str
,
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
{
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
,
93 int write_bytes_to_new_file(const char *fname
, const char *str
, size_t len
,
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. */
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
),
106 char *read_file_to_str_until_eof(int fd
, size_t max_bytes_to_read
,
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) */
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) */
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) */