files.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. /**
  6. * \file files.h
  7. *
  8. * \brief Header for files.c
  9. **/
  10. #ifndef TOR_FS_H
  11. #define TOR_FS_H
  12. #include "lib/cc/compat_compiler.h"
  13. #include "lib/cc/torint.h"
  14. #include "lib/testsupport/testsupport.h"
  15. #include <stddef.h>
  16. #include <stdio.h>
  17. #ifdef _WIN32
  18. /* We need these for struct stat to work */
  19. #ifdef HAVE_SYS_TYPES_H
  20. #include <sys/types.h>
  21. #endif
  22. #ifdef HAVE_SYS_STAT_H
  23. #include <sys/stat.h>
  24. #endif
  25. #endif /* defined(_WIN32) */
  26. #ifndef O_BINARY
  27. #define O_BINARY 0
  28. #endif
  29. #ifndef O_TEXT
  30. #define O_TEXT 0
  31. #endif
  32. #ifndef O_NOFOLLOW
  33. #define O_NOFOLLOW 0
  34. #endif
  35. struct stat;
  36. int tor_open_cloexec(const char *path, int flags, unsigned mode);
  37. FILE *tor_fopen_cloexec(const char *path, const char *mode);
  38. int tor_rename(const char *path_old, const char *path_new);
  39. int replace_file(const char *from, const char *to);
  40. int touch_file(const char *fname);
  41. MOCK_DECL(int,tor_unlink,(const char *pathname));
  42. /** Return values from file_status(); see that function's documentation
  43. * for details. */
  44. typedef enum { FN_ERROR, FN_NOENT, FN_FILE, FN_DIR, FN_EMPTY } file_status_t;
  45. file_status_t file_status(const char *filename);
  46. int64_t tor_get_avail_disk_space(const char *path);
  47. ssize_t write_all_to_fd(int fd, const char *buf, size_t count);
  48. ssize_t read_all_from_fd(int fd, char *buf, size_t count);
  49. #define OPEN_FLAGS_REPLACE (O_WRONLY|O_CREAT|O_TRUNC)
  50. #define OPEN_FLAGS_APPEND (O_WRONLY|O_CREAT|O_APPEND)
  51. #define OPEN_FLAGS_DONT_REPLACE (O_CREAT|O_EXCL|O_APPEND|O_WRONLY)
  52. typedef struct open_file_t open_file_t;
  53. int start_writing_to_file(const char *fname, int open_flags, int mode,
  54. open_file_t **data_out);
  55. FILE *start_writing_to_stdio_file(const char *fname, int open_flags, int mode,
  56. open_file_t **data_out);
  57. FILE *fdopen_file(open_file_t *file_data);
  58. int finish_writing_to_file(open_file_t *file_data);
  59. int abort_writing_to_file(open_file_t *file_data);
  60. MOCK_DECL(int, write_str_to_file,(const char *fname, const char *str,
  61. int bin));
  62. MOCK_DECL(int, write_bytes_to_file,(const char *fname, const char *str,
  63. size_t len,int bin));
  64. /** An ad-hoc type to hold a string of characters and a count; used by
  65. * write_chunks_to_file. */
  66. typedef struct sized_chunk_t {
  67. const char *bytes;
  68. size_t len;
  69. } sized_chunk_t;
  70. struct smartlist_t;
  71. int write_chunks_to_file(const char *fname, const struct smartlist_t *chunks,
  72. int bin, int no_tempfile);
  73. int append_bytes_to_file(const char *fname, const char *str, size_t len,
  74. int bin);
  75. int write_bytes_to_new_file(const char *fname, const char *str, size_t len,
  76. int bin);
  77. /** Flag for read_file_to_str: open the file in binary mode. */
  78. #define RFTS_BIN 1
  79. /** Flag for read_file_to_str: it's okay if the file doesn't exist. */
  80. #define RFTS_IGNORE_MISSING 2
  81. MOCK_DECL_ATTR(char *, read_file_to_str,(const char *filename, int flags,
  82. struct stat *stat_out),
  83. ATTR_MALLOC);
  84. char *read_file_to_str_until_eof(int fd, size_t max_bytes_to_read,
  85. size_t *sz_out)
  86. ATTR_MALLOC;
  87. #if !defined(HAVE_GETDELIM) || defined(TOR_UNIT_TESTS)
  88. /** Internal back-end function to implement getdelim(): only exists when
  89. * Tor is built for unit tests, or when Tor is built on an operating system
  90. * without its own getdelim(). */
  91. ssize_t compat_getdelim_(char **lineptr, size_t *n, int delim, FILE *stream);
  92. #endif /* !defined(HAVE_GETDELIM) || defined(TOR_UNIT_TESTS) */
  93. #ifdef HAVE_GETDELIM
  94. /**
  95. * Cross-platform wrapper for getdelim(): behaves as the POSIX-standard
  96. * getdelim() function.
  97. *
  98. * See `getdelim(3)` for more information.
  99. *
  100. * Note that this function will use the libc memory allocator -- so any memory
  101. * passed to this function must come from raw_malloc(), and must be freed by
  102. * raw_free() -- don't use tor_malloc() and tor_free() with this.
  103. */
  104. #define tor_getdelim(lineptr, n, delim, stream) \
  105. getdelim((lineptr), (n), (delim), (stream))
  106. #else /* !(defined(HAVE_GETDELIM)) */
  107. #define tor_getdelim(lineptr, n, delim, stream) \
  108. compat_getdelim_((lineptr), (n), (delim), (stream))
  109. #endif /* defined(HAVE_GETDELIM) */
  110. #ifdef HAVE_GETLINE
  111. /**
  112. * Cross-platform wrapper for getline(): behaves as the POSIX-standard
  113. * getline() function.
  114. *
  115. * See tor_getdelim() for usage notes.
  116. */
  117. #define tor_getline(lineptr, n, stream) \
  118. getline((lineptr), (n), (stream))
  119. #else /* !(defined(HAVE_GETLINE)) */
  120. #define tor_getline(lineptr, n, stream) \
  121. tor_getdelim((lineptr), (n), '\n', (stream))
  122. #endif /* defined(HAVE_GETLINE) */
  123. #endif /* !defined(TOR_FS_H) */