files.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #ifndef TOR_FS_H
  6. #define TOR_FS_H
  7. #include "lib/cc/compat_compiler.h"
  8. #include "lib/cc/torint.h"
  9. #include "lib/testsupport/testsupport.h"
  10. #include <stddef.h>
  11. #include <stdio.h>
  12. #ifdef _WIN32
  13. /* We need these for struct stat to work */
  14. #ifdef HAVE_SYS_TYPES_H
  15. #include <sys/types.h>
  16. #endif
  17. #ifdef HAVE_SYS_STAT_H
  18. #include <sys/stat.h>
  19. #endif
  20. #endif
  21. #ifndef O_BINARY
  22. #define O_BINARY 0
  23. #endif
  24. #ifndef O_TEXT
  25. #define O_TEXT 0
  26. #endif
  27. #ifndef O_NOFOLLOW
  28. #define O_NOFOLLOW 0
  29. #endif
  30. struct stat;
  31. int tor_open_cloexec(const char *path, int flags, unsigned mode);
  32. FILE *tor_fopen_cloexec(const char *path, const char *mode);
  33. int tor_rename(const char *path_old, const char *path_new);
  34. int replace_file(const char *from, const char *to);
  35. int touch_file(const char *fname);
  36. MOCK_DECL(int,tor_unlink,(const char *pathname));
  37. /** Return values from file_status(); see that function's documentation
  38. * for details. */
  39. typedef enum { FN_ERROR, FN_NOENT, FN_FILE, FN_DIR, FN_EMPTY } file_status_t;
  40. file_status_t file_status(const char *filename);
  41. int64_t tor_get_avail_disk_space(const char *path);
  42. ssize_t write_all_to_fd(int fd, const char *buf, size_t count);
  43. ssize_t read_all_from_fd(int fd, char *buf, size_t count);
  44. #define OPEN_FLAGS_REPLACE (O_WRONLY|O_CREAT|O_TRUNC)
  45. #define OPEN_FLAGS_APPEND (O_WRONLY|O_CREAT|O_APPEND)
  46. #define OPEN_FLAGS_DONT_REPLACE (O_CREAT|O_EXCL|O_APPEND|O_WRONLY)
  47. typedef struct open_file_t open_file_t;
  48. int start_writing_to_file(const char *fname, int open_flags, int mode,
  49. open_file_t **data_out);
  50. FILE *start_writing_to_stdio_file(const char *fname, int open_flags, int mode,
  51. open_file_t **data_out);
  52. FILE *fdopen_file(open_file_t *file_data);
  53. int finish_writing_to_file(open_file_t *file_data);
  54. int abort_writing_to_file(open_file_t *file_data);
  55. MOCK_DECL(int, write_str_to_file,(const char *fname, const char *str,
  56. int bin));
  57. MOCK_DECL(int, write_bytes_to_file,(const char *fname, const char *str,
  58. size_t len,int bin));
  59. /** An ad-hoc type to hold a string of characters and a count; used by
  60. * write_chunks_to_file. */
  61. typedef struct sized_chunk_t {
  62. const char *bytes;
  63. size_t len;
  64. } sized_chunk_t;
  65. struct smartlist_t;
  66. int write_chunks_to_file(const char *fname, const struct smartlist_t *chunks,
  67. int bin, int no_tempfile);
  68. int append_bytes_to_file(const char *fname, const char *str, size_t len,
  69. int bin);
  70. int write_bytes_to_new_file(const char *fname, const char *str, size_t len,
  71. int bin);
  72. /** Flag for read_file_to_str: open the file in binary mode. */
  73. #define RFTS_BIN 1
  74. /** Flag for read_file_to_str: it's okay if the file doesn't exist. */
  75. #define RFTS_IGNORE_MISSING 2
  76. MOCK_DECL_ATTR(char *, read_file_to_str,(const char *filename, int flags,
  77. struct stat *stat_out),
  78. ATTR_MALLOC);
  79. char *read_file_to_str_until_eof(int fd, size_t max_bytes_to_read,
  80. size_t *sz_out)
  81. ATTR_MALLOC;
  82. #endif