tor  0.4.2.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-2019, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
5 
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 
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 
59 int64_t tor_get_avail_disk_space(const char *path);
60 
61 ssize_t write_all_to_fd(int fd, const char *buf, size_t count);
62 ssize_t read_all_from_fd(int fd, char *buf, size_t count);
63 
64 #define OPEN_FLAGS_REPLACE (O_WRONLY|O_CREAT|O_TRUNC)
65 #define OPEN_FLAGS_APPEND (O_WRONLY|O_CREAT|O_APPEND)
66 #define OPEN_FLAGS_DONT_REPLACE (O_CREAT|O_EXCL|O_APPEND|O_WRONLY)
67 typedef struct open_file_t open_file_t;
68 int start_writing_to_file(const char *fname, int open_flags, int mode,
69  open_file_t **data_out);
70 FILE *start_writing_to_stdio_file(const char *fname, int open_flags, int mode,
71  open_file_t **data_out);
72 FILE *fdopen_file(open_file_t *file_data);
73 int finish_writing_to_file(open_file_t *file_data);
74 int abort_writing_to_file(open_file_t *file_data);
75 MOCK_DECL(int, write_str_to_file,(const char *fname, const char *str,
76  int bin));
77 MOCK_DECL(int, write_bytes_to_file,(const char *fname, const char *str,
78  size_t len,int bin));
79 
82 typedef struct sized_chunk_t {
83  const char *bytes;
84  size_t len;
86 struct smartlist_t;
87 int write_chunks_to_file(const char *fname, const struct smartlist_t *chunks,
88  int bin, int no_tempfile);
89 int append_bytes_to_file(const char *fname, const char *str, size_t len,
90  int bin);
91 int write_bytes_to_new_file(const char *fname, const char *str, size_t len,
92  int bin);
93 
95 #define RFTS_BIN 1
96 
97 #define RFTS_IGNORE_MISSING 2
98 
99 MOCK_DECL_ATTR(char *, read_file_to_str,(const char *filename, int flags,
100  struct stat *stat_out),
101  ATTR_MALLOC);
102 char *read_file_to_str_until_eof(int fd, size_t max_bytes_to_read,
103  size_t *sz_out)
104  ATTR_MALLOC;
105 
106 #if !defined(HAVE_GETDELIM) || defined(TOR_UNIT_TESTS)
107 
110 ssize_t compat_getdelim_(char **lineptr, size_t *n, int delim, FILE *stream);
111 #endif /* !defined(HAVE_GETDELIM) || defined(TOR_UNIT_TESTS) */
112 
113 #ifdef HAVE_GETDELIM
114 
124 #define tor_getdelim(lineptr, n, delim, stream) \
125  getdelim((lineptr), (n), (delim), (stream))
126 #else /* !(defined(HAVE_GETDELIM)) */
127 #define tor_getdelim(lineptr, n, delim, stream) \
128  compat_getdelim_((lineptr), (n), (delim), (stream))
129 #endif /* defined(HAVE_GETDELIM) */
130 
131 #ifdef HAVE_GETLINE
132 
138 #define tor_getline(lineptr, n, stream) \
139  getline((lineptr), (n), (stream))
140 #else /* !(defined(HAVE_GETLINE)) */
141 #define tor_getline(lineptr, n, stream) \
142  tor_getdelim((lineptr), (n), '\n', (stream))
143 #endif /* defined(HAVE_GETLINE) */
144 
145 #endif /* !defined(TOR_FS_H) */
int tor_open_cloexec(const char *path, int flags, unsigned mode)
Definition: files.c:54
int replace_file(const char *from, const char *to)
Definition: files.c:117
int write_bytes_to_new_file(const char *fname, const char *str, size_t len, int bin)
Definition: files.c:548
ssize_t write_all_to_fd(int fd, const char *buf, size_t count)
Definition: files.c:162
FILE * fdopen_file(open_file_t *file_data)
Definition: files.c:365
Integer definitions used throughout Tor.
FILE * start_writing_to_stdio_file(const char *fname, int open_flags, int mode, open_file_t **data_out)
Definition: files.c:382
int64_t tor_get_avail_disk_space(const char *path)
Definition: freespace.c:28
int append_bytes_to_file(const char *fname, const char *str, size_t len, int bin)
Definition: files.c:538
int finish_writing_to_file(open_file_t *file_data)
Definition: files.c:449
Utility macros to handle different features and behavior in different compilers.
int start_writing_to_file(const char *fname, int open_flags, int mode, open_file_t **data_out)
Definition: files.c:301
char * read_file_to_str_until_eof(int fd, size_t max_bytes_to_read, size_t *sz_out) ATTR_MALLOC
Definition: files.c:564
ssize_t read_all_from_fd(int fd, char *buf, size_t count)
Definition: files.c:181
file_status_t
Definition: files.h:55
Macros to implement mocking and selective exposure for the test code.
#define MOCK_DECL(rv, funcname, arglist)
Definition: testsupport.h:94
int abort_writing_to_file(open_file_t *file_data)
Definition: files.c:457
struct sized_chunk_t sized_chunk_t
int tor_rename(const char *path_old, const char *path_new)
Definition: files.c:103
ssize_t compat_getdelim_(char **lineptr, size_t *n, int delim, FILE *stream)
FILE * tor_fopen_cloexec(const char *path, const char *mode)
Definition: files.c:86
file_status_t file_status(const char *filename)
Definition: files.c:212
int touch_file(const char *fname)
Definition: files.c:142