files.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. /**
  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
  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. #endif