files.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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 Wrappers for reading and writing data to files on disk.
  9. **/
  10. #ifdef _WIN32
  11. #include <windows.h>
  12. #endif
  13. #include "lib/fs/files.h"
  14. #include "lib/fs/path.h"
  15. #include "lib/container/smartlist.h"
  16. #include "lib/log/torlog.h"
  17. #include "lib/log/util_bug.h"
  18. #include "lib/log/escape.h"
  19. #include "lib/err/torerr.h"
  20. #include "lib/malloc/util_malloc.h"
  21. #include "lib/sandbox/sandbox.h"
  22. #include "lib/string/printf.h"
  23. #include "lib/string/util_string.h"
  24. #include "lib/fdio/fdio.h"
  25. #ifdef HAVE_SYS_TYPES_H
  26. #include <sys/types.h>
  27. #endif
  28. #ifdef HAVE_SYS_STAT_H
  29. #include <sys/stat.h>
  30. #endif
  31. #ifdef HAVE_UTIME_H
  32. #include <utime.h>
  33. #endif
  34. #ifdef HAVE_SYS_TIME_H
  35. #include <sys/time.h>
  36. #endif
  37. #ifdef HAVE_FCNTL_H
  38. #include <fcntl.h>
  39. #endif
  40. #ifdef HAVE_UNISTD_H
  41. #include <unistd.h>
  42. #endif
  43. #include <errno.h>
  44. #include <stdio.h>
  45. #include <string.h>
  46. /** As open(path, flags, mode), but return an fd with the close-on-exec mode
  47. * set. */
  48. int
  49. tor_open_cloexec(const char *path, int flags, unsigned mode)
  50. {
  51. int fd;
  52. const char *p = sandbox_intern_string(path);
  53. #ifdef O_CLOEXEC
  54. fd = open(p, flags|O_CLOEXEC, mode);
  55. if (fd >= 0)
  56. return fd;
  57. /* If we got an error, see if it is EINVAL. EINVAL might indicate that,
  58. * even though we were built on a system with O_CLOEXEC support, we
  59. * are running on one without. */
  60. if (errno != EINVAL)
  61. return -1;
  62. #endif /* defined(O_CLOEXEC) */
  63. log_debug(LD_FS, "Opening %s with flags %x", p, flags);
  64. fd = open(p, flags, mode);
  65. #ifdef FD_CLOEXEC
  66. if (fd >= 0) {
  67. if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
  68. log_warn(LD_FS,"Couldn't set FD_CLOEXEC: %s", strerror(errno));
  69. close(fd);
  70. return -1;
  71. }
  72. }
  73. #endif /* defined(FD_CLOEXEC) */
  74. return fd;
  75. }
  76. /** As fopen(path,mode), but ensures that the O_CLOEXEC bit is set on the
  77. * underlying file handle. */
  78. FILE *
  79. tor_fopen_cloexec(const char *path, const char *mode)
  80. {
  81. FILE *result = fopen(path, mode);
  82. #ifdef FD_CLOEXEC
  83. if (result != NULL) {
  84. if (fcntl(fileno(result), F_SETFD, FD_CLOEXEC) == -1) {
  85. log_warn(LD_FS,"Couldn't set FD_CLOEXEC: %s", strerror(errno));
  86. fclose(result);
  87. return NULL;
  88. }
  89. }
  90. #endif /* defined(FD_CLOEXEC) */
  91. return result;
  92. }
  93. /** As rename(), but work correctly with the sandbox. */
  94. int
  95. tor_rename(const char *path_old, const char *path_new)
  96. {
  97. log_debug(LD_FS, "Renaming %s to %s", path_old, path_new);
  98. return rename(sandbox_intern_string(path_old),
  99. sandbox_intern_string(path_new));
  100. }
  101. /**
  102. * Rename the file <b>from</b> to the file <b>to</b>. On Unix, this is
  103. * the same as rename(2). On windows, this removes <b>to</b> first if
  104. * it already exists.
  105. * Returns 0 on success. Returns -1 and sets errno on failure.
  106. */
  107. int
  108. replace_file(const char *from, const char *to)
  109. {
  110. #ifndef _WIN32
  111. return tor_rename(from, to);
  112. #else
  113. switch (file_status(to))
  114. {
  115. case FN_NOENT:
  116. break;
  117. case FN_FILE:
  118. case FN_EMPTY:
  119. if (unlink(to)) return -1;
  120. break;
  121. case FN_ERROR:
  122. return -1;
  123. case FN_DIR:
  124. errno = EISDIR;
  125. return -1;
  126. }
  127. return tor_rename(from,to);
  128. #endif /* !defined(_WIN32) */
  129. }
  130. /** Change <b>fname</b>'s modification time to now. */
  131. int
  132. touch_file(const char *fname)
  133. {
  134. if (utime(fname, NULL)!=0)
  135. return -1;
  136. return 0;
  137. }
  138. /** Wrapper for unlink() to make it mockable for the test suite; returns 0
  139. * if unlinking the file succeeded, -1 and sets errno if unlinking fails.
  140. */
  141. MOCK_IMPL(int,
  142. tor_unlink,(const char *pathname))
  143. {
  144. return unlink(pathname);
  145. }
  146. /** Write <b>count</b> bytes from <b>buf</b> to <b>fd</b>. Return the number
  147. * of bytes written, or -1 on error. Only use if fd is a blocking fd. */
  148. ssize_t
  149. write_all_to_fd(int fd, const char *buf, size_t count)
  150. {
  151. size_t written = 0;
  152. ssize_t result;
  153. raw_assert(count < SSIZE_MAX);
  154. while (written != count) {
  155. result = write(fd, buf+written, count-written);
  156. if (result<0)
  157. return -1;
  158. written += result;
  159. }
  160. return (ssize_t)count;
  161. }
  162. /** Read from <b>fd</b> to <b>buf</b>, until we get <b>count</b> bytes or
  163. * reach the end of the file. Return the number of bytes read, or -1 on
  164. * error. Only use if fd is a blocking fd. */
  165. ssize_t
  166. read_all_from_fd(int fd, char *buf, size_t count)
  167. {
  168. size_t numread = 0;
  169. ssize_t result;
  170. if (count > SIZE_T_CEILING || count > SSIZE_MAX) {
  171. errno = EINVAL;
  172. return -1;
  173. }
  174. while (numread < count) {
  175. result = read(fd, buf+numread, count-numread);
  176. if (result<0)
  177. return -1;
  178. else if (result == 0)
  179. break;
  180. numread += result;
  181. }
  182. return (ssize_t)numread;
  183. }
  184. /** Return:
  185. * FN_ERROR if filename can't be read, is NULL, or is zero-length,
  186. * FN_NOENT if it doesn't exist,
  187. * FN_FILE if it is a non-empty regular file, or a FIFO on unix-like systems,
  188. * FN_EMPTY for zero-byte regular files,
  189. * FN_DIR if it's a directory, and
  190. * FN_ERROR for any other file type.
  191. * On FN_ERROR and FN_NOENT, sets errno. (errno is not set when FN_ERROR
  192. * is returned due to an unhandled file type.) */
  193. file_status_t
  194. file_status(const char *fname)
  195. {
  196. struct stat st;
  197. char *f;
  198. int r;
  199. if (!fname || strlen(fname) == 0) {
  200. return FN_ERROR;
  201. }
  202. f = tor_strdup(fname);
  203. clean_fname_for_stat(f);
  204. log_debug(LD_FS, "stat()ing %s", f);
  205. r = stat(sandbox_intern_string(f), &st);
  206. tor_free(f);
  207. if (r) {
  208. if (errno == ENOENT) {
  209. return FN_NOENT;
  210. }
  211. return FN_ERROR;
  212. }
  213. if (st.st_mode & S_IFDIR) {
  214. return FN_DIR;
  215. } else if (st.st_mode & S_IFREG) {
  216. if (st.st_size > 0) {
  217. return FN_FILE;
  218. } else if (st.st_size == 0) {
  219. return FN_EMPTY;
  220. } else {
  221. return FN_ERROR;
  222. }
  223. #ifndef _WIN32
  224. } else if (st.st_mode & S_IFIFO) {
  225. return FN_FILE;
  226. #endif
  227. } else {
  228. return FN_ERROR;
  229. }
  230. }
  231. /** Create a file named <b>fname</b> with the contents <b>str</b>. Overwrite
  232. * the previous <b>fname</b> if possible. Return 0 on success, -1 on failure.
  233. *
  234. * This function replaces the old file atomically, if possible. This
  235. * function, and all other functions in util.c that create files, create them
  236. * with mode 0600.
  237. */
  238. MOCK_IMPL(int,
  239. write_str_to_file,(const char *fname, const char *str, int bin))
  240. {
  241. #ifdef _WIN32
  242. if (!bin && strchr(str, '\r')) {
  243. log_warn(LD_BUG,
  244. "We're writing a text string that already contains a CR to %s",
  245. escaped(fname));
  246. }
  247. #endif /* defined(_WIN32) */
  248. return write_bytes_to_file(fname, str, strlen(str), bin);
  249. }
  250. /** Represents a file that we're writing to, with support for atomic commit:
  251. * we can write into a temporary file, and either remove the file on
  252. * failure, or replace the original file on success. */
  253. struct open_file_t {
  254. char *tempname; /**< Name of the temporary file. */
  255. char *filename; /**< Name of the original file. */
  256. unsigned rename_on_close:1; /**< Are we using the temporary file or not? */
  257. unsigned binary:1; /**< Did we open in binary mode? */
  258. int fd; /**< fd for the open file. */
  259. FILE *stdio_file; /**< stdio wrapper for <b>fd</b>. */
  260. };
  261. /** Try to start writing to the file in <b>fname</b>, passing the flags
  262. * <b>open_flags</b> to the open() syscall, creating the file (if needed) with
  263. * access value <b>mode</b>. If the O_APPEND flag is set, we append to the
  264. * original file. Otherwise, we open a new temporary file in the same
  265. * directory, and either replace the original or remove the temporary file
  266. * when we're done.
  267. *
  268. * Return the fd for the newly opened file, and store working data in
  269. * *<b>data_out</b>. The caller should not close the fd manually:
  270. * instead, call finish_writing_to_file() or abort_writing_to_file().
  271. * Returns -1 on failure.
  272. *
  273. * NOTE: When not appending, the flags O_CREAT and O_TRUNC are treated
  274. * as true and the flag O_EXCL is treated as false.
  275. *
  276. * NOTE: Ordinarily, O_APPEND means "seek to the end of the file before each
  277. * write()". We don't do that.
  278. */
  279. int
  280. start_writing_to_file(const char *fname, int open_flags, int mode,
  281. open_file_t **data_out)
  282. {
  283. open_file_t *new_file = tor_malloc_zero(sizeof(open_file_t));
  284. const char *open_name;
  285. int append = 0;
  286. tor_assert(fname);
  287. tor_assert(data_out);
  288. #if (O_BINARY != 0 && O_TEXT != 0)
  289. tor_assert((open_flags & (O_BINARY|O_TEXT)) != 0);
  290. #endif
  291. new_file->fd = -1;
  292. new_file->filename = tor_strdup(fname);
  293. if (open_flags & O_APPEND) {
  294. open_name = fname;
  295. new_file->rename_on_close = 0;
  296. append = 1;
  297. open_flags &= ~O_APPEND;
  298. } else {
  299. tor_asprintf(&new_file->tempname, "%s.tmp", fname);
  300. open_name = new_file->tempname;
  301. /* We always replace an existing temporary file if there is one. */
  302. open_flags |= O_CREAT|O_TRUNC;
  303. open_flags &= ~O_EXCL;
  304. new_file->rename_on_close = 1;
  305. }
  306. #if O_BINARY != 0
  307. if (open_flags & O_BINARY)
  308. new_file->binary = 1;
  309. #endif
  310. new_file->fd = tor_open_cloexec(open_name, open_flags, mode);
  311. if (new_file->fd < 0) {
  312. log_warn(LD_FS, "Couldn't open \"%s\" (%s) for writing: %s",
  313. open_name, fname, strerror(errno));
  314. goto err;
  315. }
  316. if (append) {
  317. if (tor_fd_seekend(new_file->fd) < 0) {
  318. log_warn(LD_FS, "Couldn't seek to end of file \"%s\": %s", open_name,
  319. strerror(errno));
  320. goto err;
  321. }
  322. }
  323. *data_out = new_file;
  324. return new_file->fd;
  325. err:
  326. if (new_file->fd >= 0)
  327. close(new_file->fd);
  328. *data_out = NULL;
  329. tor_free(new_file->filename);
  330. tor_free(new_file->tempname);
  331. tor_free(new_file);
  332. return -1;
  333. }
  334. /** Given <b>file_data</b> from start_writing_to_file(), return a stdio FILE*
  335. * that can be used to write to the same file. The caller should not mix
  336. * stdio calls with non-stdio calls. */
  337. FILE *
  338. fdopen_file(open_file_t *file_data)
  339. {
  340. tor_assert(file_data);
  341. if (file_data->stdio_file)
  342. return file_data->stdio_file;
  343. tor_assert(file_data->fd >= 0);
  344. if (!(file_data->stdio_file = fdopen(file_data->fd,
  345. file_data->binary?"ab":"a"))) {
  346. log_warn(LD_FS, "Couldn't fdopen \"%s\" [%d]: %s", file_data->filename,
  347. file_data->fd, strerror(errno));
  348. }
  349. return file_data->stdio_file;
  350. }
  351. /** Combines start_writing_to_file with fdopen_file(): arguments are as
  352. * for start_writing_to_file, but */
  353. FILE *
  354. start_writing_to_stdio_file(const char *fname, int open_flags, int mode,
  355. open_file_t **data_out)
  356. {
  357. FILE *res;
  358. if (start_writing_to_file(fname, open_flags, mode, data_out)<0)
  359. return NULL;
  360. if (!(res = fdopen_file(*data_out))) {
  361. abort_writing_to_file(*data_out);
  362. *data_out = NULL;
  363. }
  364. return res;
  365. }
  366. /** Helper function: close and free the underlying file and memory in
  367. * <b>file_data</b>. If we were writing into a temporary file, then delete
  368. * that file (if abort_write is true) or replaces the target file with
  369. * the temporary file (if abort_write is false). */
  370. static int
  371. finish_writing_to_file_impl(open_file_t *file_data, int abort_write)
  372. {
  373. int r = 0;
  374. tor_assert(file_data && file_data->filename);
  375. if (file_data->stdio_file) {
  376. if (fclose(file_data->stdio_file)) {
  377. log_warn(LD_FS, "Error closing \"%s\": %s", file_data->filename,
  378. strerror(errno));
  379. abort_write = r = -1;
  380. }
  381. } else if (file_data->fd >= 0 && close(file_data->fd) < 0) {
  382. log_warn(LD_FS, "Error flushing \"%s\": %s", file_data->filename,
  383. strerror(errno));
  384. abort_write = r = -1;
  385. }
  386. if (file_data->rename_on_close) {
  387. tor_assert(file_data->tempname && file_data->filename);
  388. if (!abort_write) {
  389. tor_assert(strcmp(file_data->filename, file_data->tempname));
  390. if (replace_file(file_data->tempname, file_data->filename)) {
  391. log_warn(LD_FS, "Error replacing \"%s\": %s", file_data->filename,
  392. strerror(errno));
  393. abort_write = r = -1;
  394. }
  395. }
  396. if (abort_write) {
  397. int res = unlink(file_data->tempname);
  398. if (res != 0) {
  399. /* We couldn't unlink and we'll leave a mess behind */
  400. log_warn(LD_FS, "Failed to unlink %s: %s",
  401. file_data->tempname, strerror(errno));
  402. r = -1;
  403. }
  404. }
  405. }
  406. tor_free(file_data->filename);
  407. tor_free(file_data->tempname);
  408. tor_free(file_data);
  409. return r;
  410. }
  411. /** Finish writing to <b>file_data</b>: close the file handle, free memory as
  412. * needed, and if using a temporary file, replace the original file with
  413. * the temporary file. */
  414. int
  415. finish_writing_to_file(open_file_t *file_data)
  416. {
  417. return finish_writing_to_file_impl(file_data, 0);
  418. }
  419. /** Finish writing to <b>file_data</b>: close the file handle, free memory as
  420. * needed, and if using a temporary file, delete it. */
  421. int
  422. abort_writing_to_file(open_file_t *file_data)
  423. {
  424. return finish_writing_to_file_impl(file_data, 1);
  425. }
  426. /** Helper: given a set of flags as passed to open(2), open the file
  427. * <b>fname</b> and write all the sized_chunk_t structs in <b>chunks</b> to
  428. * the file. Do so as atomically as possible e.g. by opening temp files and
  429. * renaming. */
  430. static int
  431. write_chunks_to_file_impl(const char *fname, const smartlist_t *chunks,
  432. int open_flags)
  433. {
  434. open_file_t *file = NULL;
  435. int fd;
  436. ssize_t result;
  437. fd = start_writing_to_file(fname, open_flags, 0600, &file);
  438. if (fd<0)
  439. return -1;
  440. SMARTLIST_FOREACH(chunks, sized_chunk_t *, chunk,
  441. {
  442. result = write_all_to_fd(fd, chunk->bytes, chunk->len);
  443. if (result < 0) {
  444. log_warn(LD_FS, "Error writing to \"%s\": %s", fname,
  445. strerror(errno));
  446. goto err;
  447. }
  448. tor_assert((size_t)result == chunk->len);
  449. });
  450. return finish_writing_to_file(file);
  451. err:
  452. abort_writing_to_file(file);
  453. return -1;
  454. }
  455. /** Given a smartlist of sized_chunk_t, write them to a file
  456. * <b>fname</b>, overwriting or creating the file as necessary.
  457. * If <b>no_tempfile</b> is 0 then the file will be written
  458. * atomically. */
  459. int
  460. write_chunks_to_file(const char *fname, const smartlist_t *chunks, int bin,
  461. int no_tempfile)
  462. {
  463. int flags = OPEN_FLAGS_REPLACE|(bin?O_BINARY:O_TEXT);
  464. if (no_tempfile) {
  465. /* O_APPEND stops write_chunks_to_file from using tempfiles */
  466. flags |= O_APPEND;
  467. }
  468. return write_chunks_to_file_impl(fname, chunks, flags);
  469. }
  470. /** Write <b>len</b> bytes, starting at <b>str</b>, to <b>fname</b>
  471. using the open() flags passed in <b>flags</b>. */
  472. static int
  473. write_bytes_to_file_impl(const char *fname, const char *str, size_t len,
  474. int flags)
  475. {
  476. int r;
  477. sized_chunk_t c = { str, len };
  478. smartlist_t *chunks = smartlist_new();
  479. smartlist_add(chunks, &c);
  480. r = write_chunks_to_file_impl(fname, chunks, flags);
  481. smartlist_free(chunks);
  482. return r;
  483. }
  484. /** As write_str_to_file, but does not assume a NUL-terminated
  485. * string. Instead, we write <b>len</b> bytes, starting at <b>str</b>. */
  486. MOCK_IMPL(int,
  487. write_bytes_to_file,(const char *fname, const char *str, size_t len,
  488. int bin))
  489. {
  490. return write_bytes_to_file_impl(fname, str, len,
  491. OPEN_FLAGS_REPLACE|(bin?O_BINARY:O_TEXT));
  492. }
  493. /** As write_bytes_to_file, but if the file already exists, append the bytes
  494. * to the end of the file instead of overwriting it. */
  495. int
  496. append_bytes_to_file(const char *fname, const char *str, size_t len,
  497. int bin)
  498. {
  499. return write_bytes_to_file_impl(fname, str, len,
  500. OPEN_FLAGS_APPEND|(bin?O_BINARY:O_TEXT));
  501. }
  502. /** Like write_str_to_file(), but also return -1 if there was a file
  503. already residing in <b>fname</b>. */
  504. int
  505. write_bytes_to_new_file(const char *fname, const char *str, size_t len,
  506. int bin)
  507. {
  508. return write_bytes_to_file_impl(fname, str, len,
  509. OPEN_FLAGS_DONT_REPLACE|
  510. (bin?O_BINARY:O_TEXT));
  511. }
  512. /**
  513. * Read the contents of the open file <b>fd</b> presuming it is a FIFO
  514. * (or similar) file descriptor for which the size of the file isn't
  515. * known ahead of time. Return NULL on failure, and a NUL-terminated
  516. * string on success. On success, set <b>sz_out</b> to the number of
  517. * bytes read.
  518. */
  519. char *
  520. read_file_to_str_until_eof(int fd, size_t max_bytes_to_read, size_t *sz_out)
  521. {
  522. ssize_t r;
  523. size_t pos = 0;
  524. char *string = NULL;
  525. size_t string_max = 0;
  526. if (max_bytes_to_read+1 >= SIZE_T_CEILING) {
  527. errno = EINVAL;
  528. return NULL;
  529. }
  530. do {
  531. /* XXXX This "add 1K" approach is a little goofy; if we care about
  532. * performance here, we should be doubling. But in practice we shouldn't
  533. * be using this function on big files anyway. */
  534. string_max = pos + 1024;
  535. if (string_max > max_bytes_to_read)
  536. string_max = max_bytes_to_read + 1;
  537. string = tor_realloc(string, string_max);
  538. r = read(fd, string + pos, string_max - pos - 1);
  539. if (r < 0) {
  540. int save_errno = errno;
  541. tor_free(string);
  542. errno = save_errno;
  543. return NULL;
  544. }
  545. pos += r;
  546. } while (r > 0 && pos < max_bytes_to_read);
  547. tor_assert(pos < string_max);
  548. *sz_out = pos;
  549. string[pos] = '\0';
  550. return string;
  551. }
  552. /** Read the contents of <b>filename</b> into a newly allocated
  553. * string; return the string on success or NULL on failure.
  554. *
  555. * If <b>stat_out</b> is provided, store the result of stat()ing the
  556. * file into <b>stat_out</b>.
  557. *
  558. * If <b>flags</b> &amp; RFTS_BIN, open the file in binary mode.
  559. * If <b>flags</b> &amp; RFTS_IGNORE_MISSING, don't warn if the file
  560. * doesn't exist.
  561. */
  562. /*
  563. * This function <em>may</em> return an erroneous result if the file
  564. * is modified while it is running, but must not crash or overflow.
  565. * Right now, the error case occurs when the file length grows between
  566. * the call to stat and the call to read_all: the resulting string will
  567. * be truncated.
  568. */
  569. MOCK_IMPL(char *,
  570. read_file_to_str, (const char *filename, int flags, struct stat *stat_out))
  571. {
  572. int fd; /* router file */
  573. struct stat statbuf;
  574. char *string;
  575. ssize_t r;
  576. int bin = flags & RFTS_BIN;
  577. tor_assert(filename);
  578. fd = tor_open_cloexec(filename,O_RDONLY|(bin?O_BINARY:O_TEXT),0);
  579. if (fd<0) {
  580. int severity = LOG_WARN;
  581. int save_errno = errno;
  582. if (errno == ENOENT && (flags & RFTS_IGNORE_MISSING))
  583. severity = LOG_INFO;
  584. log_fn(severity, LD_FS,"Could not open \"%s\": %s",filename,
  585. strerror(errno));
  586. errno = save_errno;
  587. return NULL;
  588. }
  589. if (fstat(fd, &statbuf)<0) {
  590. int save_errno = errno;
  591. close(fd);
  592. log_warn(LD_FS,"Could not fstat \"%s\".",filename);
  593. errno = save_errno;
  594. return NULL;
  595. }
  596. #ifndef _WIN32
  597. /** When we detect that we're reading from a FIFO, don't read more than
  598. * this many bytes. It's insane overkill for most uses. */
  599. #define FIFO_READ_MAX (1024*1024)
  600. if (S_ISFIFO(statbuf.st_mode)) {
  601. size_t sz = 0;
  602. string = read_file_to_str_until_eof(fd, FIFO_READ_MAX, &sz);
  603. int save_errno = errno;
  604. if (string && stat_out) {
  605. statbuf.st_size = sz;
  606. memcpy(stat_out, &statbuf, sizeof(struct stat));
  607. }
  608. close(fd);
  609. if (!string)
  610. errno = save_errno;
  611. return string;
  612. }
  613. #endif /* !defined(_WIN32) */
  614. if ((uint64_t)(statbuf.st_size)+1 >= SIZE_T_CEILING) {
  615. close(fd);
  616. errno = EINVAL;
  617. return NULL;
  618. }
  619. string = tor_malloc((size_t)(statbuf.st_size+1));
  620. r = read_all_from_fd(fd,string,(size_t)statbuf.st_size);
  621. if (r<0) {
  622. int save_errno = errno;
  623. log_warn(LD_FS,"Error reading from file \"%s\": %s", filename,
  624. strerror(errno));
  625. tor_free(string);
  626. close(fd);
  627. errno = save_errno;
  628. return NULL;
  629. }
  630. string[r] = '\0'; /* NUL-terminate the result. */
  631. #if defined(_WIN32) || defined(__CYGWIN__)
  632. if (!bin && strchr(string, '\r')) {
  633. log_debug(LD_FS, "We didn't convert CRLF to LF as well as we hoped "
  634. "when reading %s. Coping.",
  635. filename);
  636. tor_strstrip(string, "\r");
  637. r = strlen(string);
  638. }
  639. if (!bin) {
  640. statbuf.st_size = (size_t) r;
  641. } else
  642. #endif /* defined(_WIN32) || defined(__CYGWIN__) */
  643. if (r != statbuf.st_size) {
  644. /* Unless we're using text mode on win32, we'd better have an exact
  645. * match for size. */
  646. int save_errno = errno;
  647. log_warn(LD_FS,"Could read only %d of %ld bytes of file \"%s\".",
  648. (int)r, (long)statbuf.st_size,filename);
  649. tor_free(string);
  650. close(fd);
  651. errno = save_errno;
  652. return NULL;
  653. }
  654. close(fd);
  655. if (stat_out) {
  656. memcpy(stat_out, &statbuf, sizeof(struct stat));
  657. }
  658. return string;
  659. }