tor

The Tor anonymity network
git clone https://git.dasho.dev/tor.git
Log | Files | Refs | README | LICENSE

files.h (5102B)


      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"
     17 #include "lib/testsupport/testsupport.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 
     46 struct stat;
     47 
     48 int tor_open_cloexec(const char *path, int flags, unsigned mode);
     49 FILE *tor_fopen_cloexec(const char *path, const char *mode);
     50 int tor_rename(const char *path_old, const char *path_new);
     51 
     52 int replace_file(const char *from, const char *to);
     53 int touch_file(const char *fname);
     54 
     55 MOCK_DECL(int,tor_unlink,(const char *pathname));
     56 
     57 /** Return values from file_status(); see that function's documentation
     58 * for details. */
     59 typedef enum { FN_ERROR, FN_NOENT, FN_FILE, FN_DIR, FN_EMPTY } file_status_t;
     60 
     61 file_status_t file_status(const char *filename);
     62 bool is_file(file_status_t file_type);
     63 bool is_dir(file_status_t file_type);
     64 
     65 int64_t tor_get_avail_disk_space(const char *path);
     66 
     67 ssize_t write_all_to_fd(int fd, const char *buf, size_t count);
     68 ssize_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)
     73 typedef struct open_file_t open_file_t;
     74 int start_writing_to_file(const char *fname, int open_flags, int mode,
     75                          open_file_t **data_out);
     76 FILE *start_writing_to_stdio_file(const char *fname, int open_flags, int mode,
     77                                  open_file_t **data_out);
     78 FILE *fdopen_file(open_file_t *file_data);
     79 int finish_writing_to_file(open_file_t *file_data);
     80 int abort_writing_to_file(open_file_t *file_data);
     81 MOCK_DECL(int, write_str_to_file,(const char *fname, const char *str,
     82                                  int bin));
     83 MOCK_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. */
     88 typedef struct sized_chunk_t {
     89  const char *bytes;
     90  size_t len;
     91 } sized_chunk_t;
     92 struct smartlist_t;
     93 int write_chunks_to_file(const char *fname, const struct smartlist_t *chunks,
     94                         int bin, int no_tempfile);
     95 int append_bytes_to_file(const char *fname, const char *str, size_t len,
     96                         int bin);
     97 int write_bytes_to_new_file(const char *fname, const char *str, size_t len,
     98                            int bin);
     99 
    100 int 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 
    107 MOCK_DECL_ATTR(char *, read_file_to_str,(const char *filename, int flags,
    108                                         struct stat *stat_out),
    109               ATTR_MALLOC);
    110 char *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(). */
    118 ssize_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) */