archive_read_open_filename.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. /*-
  2. * Copyright (c) 2003-2010 Tim Kientzle
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "archive_platform.h"
  26. #ifdef HAVE_SYS_IOCTL_H
  27. #include <sys/ioctl.h>
  28. #endif
  29. #ifdef HAVE_SYS_STAT_H
  30. #include <sys/stat.h>
  31. #endif
  32. #ifdef HAVE_ERRNO_H
  33. #include <errno.h>
  34. #endif
  35. #ifdef HAVE_FCNTL_H
  36. #include <fcntl.h>
  37. #endif
  38. #ifdef HAVE_IO_H
  39. #include <io.h>
  40. #endif
  41. #ifdef HAVE_STDLIB_H
  42. #include <stdlib.h>
  43. #endif
  44. #ifdef HAVE_STRING_H
  45. #include <string.h>
  46. #endif
  47. #ifdef HAVE_UNISTD_H
  48. #include <unistd.h>
  49. #endif
  50. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  51. #include <sys/disk.h>
  52. #elif defined(__NetBSD__) || defined(__OpenBSD__)
  53. #include <sys/disklabel.h>
  54. #include <sys/dkio.h>
  55. #elif defined(__DragonFly__)
  56. #include <sys/diskslice.h>
  57. #endif
  58. #include "archive.h"
  59. #include "archive_private.h"
  60. #include "archive_string.h"
  61. #ifndef O_BINARY
  62. #define O_BINARY 0
  63. #endif
  64. #ifndef O_CLOEXEC
  65. #define O_CLOEXEC 0
  66. #endif
  67. struct read_file_data {
  68. int fd;
  69. size_t block_size;
  70. void *buffer;
  71. mode_t st_mode; /* Mode bits for opened file. */
  72. char use_lseek;
  73. enum fnt_e { FNT_STDIN, FNT_MBS, FNT_WCS } filename_type;
  74. union {
  75. char m[1];/* MBS filename. */
  76. wchar_t w[1];/* WCS filename. */
  77. } filename; /* Must be last! */
  78. };
  79. static int file_open(struct archive *, void *);
  80. static int file_close(struct archive *, void *);
  81. static int file_close2(struct archive *, void *);
  82. static int file_switch(struct archive *, void *, void *);
  83. static ssize_t file_read(struct archive *, void *, const void **buff);
  84. static int64_t file_seek(struct archive *, void *, int64_t request, int);
  85. static int64_t file_skip(struct archive *, void *, int64_t request);
  86. static int64_t file_skip_lseek(struct archive *, void *, int64_t request);
  87. int
  88. archive_read_open_file(struct archive *a, const char *filename,
  89. size_t block_size)
  90. {
  91. return (archive_read_open_filename(a, filename, block_size));
  92. }
  93. int
  94. archive_read_open_filename(struct archive *a, const char *filename,
  95. size_t block_size)
  96. {
  97. const char *filenames[2];
  98. filenames[0] = filename;
  99. filenames[1] = NULL;
  100. return archive_read_open_filenames(a, filenames, block_size);
  101. }
  102. int
  103. archive_read_open_filenames(struct archive *a, const char **filenames,
  104. size_t block_size)
  105. {
  106. struct read_file_data *mine;
  107. const char *filename = NULL;
  108. if (filenames)
  109. filename = *(filenames++);
  110. archive_clear_error(a);
  111. do
  112. {
  113. if (filename == NULL)
  114. filename = "";
  115. mine = calloc(1,
  116. sizeof(*mine) + strlen(filename));
  117. if (mine == NULL)
  118. goto no_memory;
  119. strcpy(mine->filename.m, filename);
  120. mine->block_size = block_size;
  121. mine->fd = -1;
  122. mine->buffer = NULL;
  123. mine->st_mode = mine->use_lseek = 0;
  124. if (filename == NULL || filename[0] == '\0') {
  125. mine->filename_type = FNT_STDIN;
  126. } else
  127. mine->filename_type = FNT_MBS;
  128. if (archive_read_append_callback_data(a, mine) != (ARCHIVE_OK))
  129. return (ARCHIVE_FATAL);
  130. if (filenames == NULL)
  131. break;
  132. filename = *(filenames++);
  133. } while (filename != NULL && filename[0] != '\0');
  134. archive_read_set_open_callback(a, file_open);
  135. archive_read_set_read_callback(a, file_read);
  136. archive_read_set_skip_callback(a, file_skip);
  137. archive_read_set_close_callback(a, file_close);
  138. archive_read_set_switch_callback(a, file_switch);
  139. archive_read_set_seek_callback(a, file_seek);
  140. return (archive_read_open1(a));
  141. no_memory:
  142. archive_set_error(a, ENOMEM, "No memory");
  143. return (ARCHIVE_FATAL);
  144. }
  145. /*
  146. * This function is an implementation detail of archive_read_open_filename_w,
  147. * which is exposed as a separate API on Windows.
  148. */
  149. #if !defined(_WIN32) || defined(__CYGWIN__)
  150. static
  151. #endif
  152. int
  153. archive_read_open_filenames_w(struct archive *a, const wchar_t **wfilenames,
  154. size_t block_size)
  155. {
  156. struct read_file_data *mine;
  157. const wchar_t *wfilename = NULL;
  158. if (wfilenames)
  159. wfilename = *(wfilenames++);
  160. archive_clear_error(a);
  161. do
  162. {
  163. if (wfilename == NULL)
  164. wfilename = L"";
  165. mine = calloc(1,
  166. sizeof(*mine) + wcslen(wfilename) * sizeof(wchar_t));
  167. if (mine == NULL)
  168. goto no_memory;
  169. mine->block_size = block_size;
  170. mine->fd = -1;
  171. if (wfilename == NULL || wfilename[0] == L'\0') {
  172. mine->filename_type = FNT_STDIN;
  173. } else {
  174. #if defined(_WIN32) && !defined(__CYGWIN__)
  175. mine->filename_type = FNT_WCS;
  176. wcscpy(mine->filename.w, wfilename);
  177. #else
  178. /*
  179. * POSIX system does not support a wchar_t interface for
  180. * open() system call, so we have to translate a wchar_t
  181. * filename to multi-byte one and use it.
  182. */
  183. struct archive_string fn;
  184. archive_string_init(&fn);
  185. if (archive_string_append_from_wcs(&fn, wfilename,
  186. wcslen(wfilename)) != 0) {
  187. if (errno == ENOMEM)
  188. archive_set_error(a, errno,
  189. "Can't allocate memory");
  190. else
  191. archive_set_error(a, EINVAL,
  192. "Failed to convert a wide-character"
  193. " filename to a multi-byte filename");
  194. archive_string_free(&fn);
  195. free(mine);
  196. return (ARCHIVE_FATAL);
  197. }
  198. mine->filename_type = FNT_MBS;
  199. strcpy(mine->filename.m, fn.s);
  200. archive_string_free(&fn);
  201. #endif
  202. }
  203. if (archive_read_append_callback_data(a, mine) != (ARCHIVE_OK))
  204. return (ARCHIVE_FATAL);
  205. if (wfilenames == NULL)
  206. break;
  207. wfilename = *(wfilenames++);
  208. } while (wfilename != NULL && wfilename[0] != '\0');
  209. archive_read_set_open_callback(a, file_open);
  210. archive_read_set_read_callback(a, file_read);
  211. archive_read_set_skip_callback(a, file_skip);
  212. archive_read_set_close_callback(a, file_close);
  213. archive_read_set_switch_callback(a, file_switch);
  214. archive_read_set_seek_callback(a, file_seek);
  215. return (archive_read_open1(a));
  216. no_memory:
  217. archive_set_error(a, ENOMEM, "No memory");
  218. return (ARCHIVE_FATAL);
  219. }
  220. int
  221. archive_read_open_filename_w(struct archive *a, const wchar_t *wfilename,
  222. size_t block_size)
  223. {
  224. const wchar_t *wfilenames[2];
  225. wfilenames[0] = wfilename;
  226. wfilenames[1] = NULL;
  227. return archive_read_open_filenames_w(a, wfilenames, block_size);
  228. }
  229. static int
  230. file_open(struct archive *a, void *client_data)
  231. {
  232. struct stat st;
  233. struct read_file_data *mine = (struct read_file_data *)client_data;
  234. void *buffer;
  235. const char *filename = NULL;
  236. #if defined(_WIN32) && !defined(__CYGWIN__)
  237. const wchar_t *wfilename = NULL;
  238. #endif
  239. int fd = -1;
  240. int is_disk_like = 0;
  241. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  242. off_t mediasize = 0; /* FreeBSD-specific, so off_t okay here. */
  243. #elif defined(__NetBSD__) || defined(__OpenBSD__)
  244. struct disklabel dl;
  245. #elif defined(__DragonFly__)
  246. struct partinfo pi;
  247. #endif
  248. archive_clear_error(a);
  249. if (mine->filename_type == FNT_STDIN) {
  250. /* We used to delegate stdin support by
  251. * directly calling archive_read_open_fd(a,0,block_size)
  252. * here, but that doesn't (and shouldn't) handle the
  253. * end-of-file flush when reading stdout from a pipe.
  254. * Basically, read_open_fd() is intended for folks who
  255. * are willing to handle such details themselves. This
  256. * API is intended to be a little smarter for folks who
  257. * want easy handling of the common case.
  258. */
  259. fd = 0;
  260. #if defined(__CYGWIN__) || defined(_WIN32)
  261. setmode(0, O_BINARY);
  262. #endif
  263. filename = "";
  264. } else if (mine->filename_type == FNT_MBS) {
  265. filename = mine->filename.m;
  266. fd = open(filename, O_RDONLY | O_BINARY | O_CLOEXEC);
  267. __archive_ensure_cloexec_flag(fd);
  268. if (fd < 0) {
  269. archive_set_error(a, errno,
  270. "Failed to open '%s'", filename);
  271. return (ARCHIVE_FATAL);
  272. }
  273. } else {
  274. #if defined(_WIN32) && !defined(__CYGWIN__)
  275. wfilename = mine->filename.w;
  276. fd = _wopen(wfilename, O_RDONLY | O_BINARY);
  277. if (fd < 0 && errno == ENOENT) {
  278. wchar_t *fullpath;
  279. fullpath = __la_win_permissive_name_w(wfilename);
  280. if (fullpath != NULL) {
  281. fd = _wopen(fullpath, O_RDONLY | O_BINARY);
  282. free(fullpath);
  283. }
  284. }
  285. if (fd < 0) {
  286. archive_set_error(a, errno,
  287. "Failed to open '%ls'", wfilename);
  288. return (ARCHIVE_FATAL);
  289. }
  290. #else
  291. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  292. "Unexpedted operation in archive_read_open_filename");
  293. goto fail;
  294. #endif
  295. }
  296. if (fstat(fd, &st) != 0) {
  297. #if defined(_WIN32) && !defined(__CYGWIN__)
  298. if (mine->filename_type == FNT_WCS)
  299. archive_set_error(a, errno, "Can't stat '%ls'",
  300. wfilename);
  301. else
  302. #endif
  303. archive_set_error(a, errno, "Can't stat '%s'",
  304. filename);
  305. goto fail;
  306. }
  307. /*
  308. * Determine whether the input looks like a disk device or a
  309. * tape device. The results are used below to select an I/O
  310. * strategy:
  311. * = "disk-like" devices support arbitrary lseek() and will
  312. * support I/O requests of any size. So we get easy skipping
  313. * and can cheat on block sizes to get better performance.
  314. * = "tape-like" devices require strict blocking and use
  315. * specialized ioctls for seeking.
  316. * = "socket-like" devices cannot seek at all but can improve
  317. * performance by using nonblocking I/O to read "whatever is
  318. * available right now".
  319. *
  320. * Right now, we only specially recognize disk-like devices,
  321. * but it should be straightforward to add probes and strategy
  322. * here for tape-like and socket-like devices.
  323. */
  324. if (S_ISREG(st.st_mode)) {
  325. /* Safety: Tell the extractor not to overwrite the input. */
  326. archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino);
  327. /* Regular files act like disks. */
  328. is_disk_like = 1;
  329. }
  330. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  331. /* FreeBSD: if it supports DIOCGMEDIASIZE ioctl, it's disk-like. */
  332. else if (S_ISCHR(st.st_mode) &&
  333. ioctl(fd, DIOCGMEDIASIZE, &mediasize) == 0 &&
  334. mediasize > 0) {
  335. is_disk_like = 1;
  336. }
  337. #elif defined(__NetBSD__) || defined(__OpenBSD__)
  338. /* Net/OpenBSD: if it supports DIOCGDINFO ioctl, it's disk-like. */
  339. else if ((S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) &&
  340. ioctl(fd, DIOCGDINFO, &dl) == 0 &&
  341. dl.d_partitions[DISKPART(st.st_rdev)].p_size > 0) {
  342. is_disk_like = 1;
  343. }
  344. #elif defined(__DragonFly__)
  345. /* DragonFly BSD: if it supports DIOCGPART ioctl, it's disk-like. */
  346. else if (S_ISCHR(st.st_mode) &&
  347. ioctl(fd, DIOCGPART, &pi) == 0 &&
  348. pi.media_size > 0) {
  349. is_disk_like = 1;
  350. }
  351. #elif defined(__linux__)
  352. /* Linux: All block devices are disk-like. */
  353. else if (S_ISBLK(st.st_mode) &&
  354. lseek(fd, 0, SEEK_CUR) == 0 &&
  355. lseek(fd, 0, SEEK_SET) == 0 &&
  356. lseek(fd, 0, SEEK_END) > 0 &&
  357. lseek(fd, 0, SEEK_SET) == 0) {
  358. is_disk_like = 1;
  359. }
  360. #endif
  361. /* TODO: Add an "is_tape_like" variable and appropriate tests. */
  362. /* Disk-like devices prefer power-of-two block sizes. */
  363. /* Use provided block_size as a guide so users have some control. */
  364. if (is_disk_like) {
  365. size_t new_block_size = 64 * 1024;
  366. while (new_block_size < mine->block_size
  367. && new_block_size < 64 * 1024 * 1024)
  368. new_block_size *= 2;
  369. mine->block_size = new_block_size;
  370. }
  371. buffer = malloc(mine->block_size);
  372. if (buffer == NULL) {
  373. archive_set_error(a, ENOMEM, "No memory");
  374. goto fail;
  375. }
  376. mine->buffer = buffer;
  377. mine->fd = fd;
  378. /* Remember mode so close can decide whether to flush. */
  379. mine->st_mode = st.st_mode;
  380. /* Disk-like inputs can use lseek(). */
  381. if (is_disk_like)
  382. mine->use_lseek = 1;
  383. return (ARCHIVE_OK);
  384. fail:
  385. /*
  386. * Don't close file descriptors not opened or ones pointing referring
  387. * to `FNT_STDIN`.
  388. */
  389. if (fd != -1 && fd != 0)
  390. close(fd);
  391. return (ARCHIVE_FATAL);
  392. }
  393. static ssize_t
  394. file_read(struct archive *a, void *client_data, const void **buff)
  395. {
  396. struct read_file_data *mine = (struct read_file_data *)client_data;
  397. ssize_t bytes_read;
  398. /* TODO: If a recent lseek() operation has left us
  399. * mis-aligned, read and return a short block to try to get
  400. * us back in alignment. */
  401. /* TODO: Someday, try mmap() here; if that succeeds, give
  402. * the entire file to libarchive as a single block. That
  403. * could be a lot faster than block-by-block manual I/O. */
  404. /* TODO: We might be able to improve performance on pipes and
  405. * sockets by setting non-blocking I/O and just accepting
  406. * whatever we get here instead of waiting for a full block
  407. * worth of data. */
  408. *buff = mine->buffer;
  409. for (;;) {
  410. bytes_read = read(mine->fd, mine->buffer, mine->block_size);
  411. if (bytes_read < 0) {
  412. if (errno == EINTR)
  413. continue;
  414. else if (mine->filename_type == FNT_STDIN)
  415. archive_set_error(a, errno,
  416. "Error reading stdin");
  417. else if (mine->filename_type == FNT_MBS)
  418. archive_set_error(a, errno,
  419. "Error reading '%s'", mine->filename.m);
  420. else
  421. archive_set_error(a, errno,
  422. "Error reading '%ls'", mine->filename.w);
  423. }
  424. return (bytes_read);
  425. }
  426. }
  427. /*
  428. * Regular files and disk-like block devices can use simple lseek
  429. * without needing to round the request to the block size.
  430. *
  431. * TODO: This can leave future reads mis-aligned. Since we know the
  432. * offset here, we should store it and use it in file_read() above
  433. * to determine whether we should perform a short read to get back
  434. * into alignment. Long series of mis-aligned reads can negatively
  435. * impact disk throughput. (Of course, the performance impact should
  436. * be carefully tested; extra code complexity is only worthwhile if
  437. * it does provide measurable improvement.)
  438. *
  439. * TODO: Be lazy about the actual seek. There are a few pathological
  440. * cases where libarchive makes a bunch of seek requests in a row
  441. * without any intervening reads. This isn't a huge performance
  442. * problem, since the kernel handles seeks lazily already, but
  443. * it would be very slightly faster if we simply remembered the
  444. * seek request here and then actually performed the seek at the
  445. * top of the read callback above.
  446. */
  447. static int64_t
  448. file_skip_lseek(struct archive *a, void *client_data, int64_t request)
  449. {
  450. struct read_file_data *mine = (struct read_file_data *)client_data;
  451. #if defined(_WIN32) && !defined(__CYGWIN__)
  452. /* We use _lseeki64() on Windows. */
  453. int64_t old_offset, new_offset;
  454. #else
  455. off_t old_offset, new_offset;
  456. #endif
  457. /* We use off_t here because lseek() is declared that way. */
  458. /* TODO: Deal with case where off_t isn't 64 bits.
  459. * This shouldn't be a problem on Linux or other POSIX
  460. * systems, since the configuration logic for libarchive
  461. * tries to obtain a 64-bit off_t.
  462. */
  463. if ((old_offset = lseek(mine->fd, 0, SEEK_CUR)) >= 0 &&
  464. (new_offset = lseek(mine->fd, request, SEEK_CUR)) >= 0)
  465. return (new_offset - old_offset);
  466. /* If lseek() fails, don't bother trying again. */
  467. mine->use_lseek = 0;
  468. /* Let libarchive recover with read+discard */
  469. if (errno == ESPIPE)
  470. return (0);
  471. /* If the input is corrupted or truncated, fail. */
  472. if (mine->filename_type == FNT_STDIN)
  473. archive_set_error(a, errno, "Error seeking in stdin");
  474. else if (mine->filename_type == FNT_MBS)
  475. archive_set_error(a, errno, "Error seeking in '%s'",
  476. mine->filename.m);
  477. else
  478. archive_set_error(a, errno, "Error seeking in '%ls'",
  479. mine->filename.w);
  480. return (-1);
  481. }
  482. /*
  483. * TODO: Implement another file_skip_XXXX that uses MTIO ioctls to
  484. * accelerate operation on tape drives.
  485. */
  486. static int64_t
  487. file_skip(struct archive *a, void *client_data, int64_t request)
  488. {
  489. struct read_file_data *mine = (struct read_file_data *)client_data;
  490. /* Delegate skip requests. */
  491. if (mine->use_lseek)
  492. return (file_skip_lseek(a, client_data, request));
  493. /* If we can't skip, return 0; libarchive will read+discard instead. */
  494. return (0);
  495. }
  496. /*
  497. * TODO: Store the offset and use it in the read callback.
  498. */
  499. static int64_t
  500. file_seek(struct archive *a, void *client_data, int64_t request, int whence)
  501. {
  502. struct read_file_data *mine = (struct read_file_data *)client_data;
  503. int64_t r;
  504. /* We use off_t here because lseek() is declared that way. */
  505. /* See above for notes about when off_t is less than 64 bits. */
  506. r = lseek(mine->fd, request, whence);
  507. if (r >= 0)
  508. return r;
  509. /* If the input is corrupted or truncated, fail. */
  510. if (mine->filename_type == FNT_STDIN)
  511. archive_set_error(a, errno, "Error seeking in stdin");
  512. else if (mine->filename_type == FNT_MBS)
  513. archive_set_error(a, errno, "Error seeking in '%s'",
  514. mine->filename.m);
  515. else
  516. archive_set_error(a, errno, "Error seeking in '%ls'",
  517. mine->filename.w);
  518. return (ARCHIVE_FATAL);
  519. }
  520. static int
  521. file_close2(struct archive *a, void *client_data)
  522. {
  523. struct read_file_data *mine = (struct read_file_data *)client_data;
  524. (void)a; /* UNUSED */
  525. /* Only flush and close if open succeeded. */
  526. if (mine->fd >= 0) {
  527. /*
  528. * Sometimes, we should flush the input before closing.
  529. * Regular files: faster to just close without flush.
  530. * Disk-like devices: Ditto.
  531. * Tapes: must not flush (user might need to
  532. * read the "next" item on a non-rewind device).
  533. * Pipes and sockets: must flush (otherwise, the
  534. * program feeding the pipe or socket may complain).
  535. * Here, I flush everything except for regular files and
  536. * device nodes.
  537. */
  538. if (!S_ISREG(mine->st_mode)
  539. && !S_ISCHR(mine->st_mode)
  540. && !S_ISBLK(mine->st_mode)) {
  541. ssize_t bytesRead;
  542. do {
  543. bytesRead = read(mine->fd, mine->buffer,
  544. mine->block_size);
  545. } while (bytesRead > 0);
  546. }
  547. /* If a named file was opened, then it needs to be closed. */
  548. if (mine->filename_type != FNT_STDIN)
  549. close(mine->fd);
  550. }
  551. free(mine->buffer);
  552. mine->buffer = NULL;
  553. mine->fd = -1;
  554. return (ARCHIVE_OK);
  555. }
  556. static int
  557. file_close(struct archive *a, void *client_data)
  558. {
  559. struct read_file_data *mine = (struct read_file_data *)client_data;
  560. file_close2(a, client_data);
  561. free(mine);
  562. return (ARCHIVE_OK);
  563. }
  564. static int
  565. file_switch(struct archive *a, void *client_data1, void *client_data2)
  566. {
  567. file_close2(a, client_data1);
  568. return file_open(a, client_data2);
  569. }