Tor  0.4.7.0-alpha-dev
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 
15 #include "lib/cc/compat_compiler.h"
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 #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
41 
42 struct stat;
43 
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);
47 
48 int replace_file(const char *from, const char *to);
49 int touch_file(const char *fname);
50 
51 MOCK_DECL(int,tor_unlink,(const char *pathname));
52 
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;
56 
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);
60 
61 int64_t tor_get_avail_disk_space(const char *path);
62 
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);
65 
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));
81 
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;
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);
95 
96 int write_str_to_file_if_not_equal(const char *fname, const char *str);
97 
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
102 
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;
109 
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) */
116 
117 #ifdef HAVE_GETDELIM
118 /**
119  * Cross-platform wrapper for getdelim(): behaves as the POSIX-standard
120  * getdelim() function.
121  *
122  * See `getdelim(3)` for more information.
123  *
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.
127  */
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) */
134 
135 #ifdef HAVE_GETLINE
136 /**
137  * Cross-platform wrapper for getline(): behaves as the POSIX-standard
138  * getline() function.
139  *
140  * See tor_getdelim() for usage notes.
141  */
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) */
148 
149 #endif /* !defined(TOR_FS_H) */
Utility macros to handle different features and behavior in different compilers.
int write_str_to_file(const char *fname, const char *str, int bin)
Definition: files.c:274
ssize_t read_all_from_fd(int fd, char *buf, size_t count)
Definition: files.c:181
int tor_unlink(const char *pathname)
Definition: files.c:154
file_status_t file_status(const char *filename)
Definition: files.c:212
int finish_writing_to_file(open_file_t *file_data)
Definition: files.c:465
int write_bytes_to_new_file(const char *fname, const char *str, size_t len, int bin)
Definition: files.c:564
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:317
int tor_open_cloexec(const char *path, int flags, unsigned mode)
Definition: files.c:54
int touch_file(const char *fname)
Definition: files.c:142
ssize_t write_all_to_fd(int fd, const char *buf, size_t count)
Definition: files.c:162
char * read_file_to_str_until_eof(int fd, size_t max_bytes_to_read, size_t *sz_out) ATTR_MALLOC
Definition: files.c:580
int write_str_to_file_if_not_equal(const char *fname, const char *str)
Definition: files.c:743
int64_t tor_get_avail_disk_space(const char *path)
Definition: freespace.c:28
file_status_t
Definition: files.h:55
int append_bytes_to_file(const char *fname, const char *str, size_t len, int bin)
Definition: files.c:554
FILE * tor_fopen_cloexec(const char *path, const char *mode)
Definition: files.c:86
FILE * start_writing_to_stdio_file(const char *fname, int open_flags, int mode, open_file_t **data_out)
Definition: files.c:398
int replace_file(const char *from, const char *to)
Definition: files.c:117
FILE * fdopen_file(open_file_t *file_data)
Definition: files.c:381
int abort_writing_to_file(open_file_t *file_data)
Definition: files.c:473
int write_bytes_to_file(const char *fname, const char *str, size_t len, int bin)
Definition: files.c:545
int tor_rename(const char *path_old, const char *path_new)
Definition: files.c:103
bool is_file(file_status_t file_type)
Definition: files.c:253
bool is_dir(file_status_t file_type)
Definition: files.c:261
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.