fileio.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * File IO utilities used in rsync.
  3. *
  4. * Copyright (C) 1998 Andrew Tridgell
  5. * Copyright (C) 2002 Martin Pool
  6. * Copyright (C) 2004-2009 Wayne Davison
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, visit the http://fsf.org website.
  20. */
  21. #include "rsync.h"
  22. #ifndef ENODATA
  23. #define ENODATA EAGAIN
  24. #endif
  25. extern int sparse_files;
  26. static OFF_T sparse_seek = 0;
  27. int sparse_end(int f, OFF_T size)
  28. {
  29. int ret;
  30. if (!sparse_seek)
  31. return 0;
  32. #ifdef HAVE_FTRUNCATE
  33. ret = do_ftruncate(f, size);
  34. #else
  35. if (do_lseek(f, sparse_seek-1, SEEK_CUR) != size-1)
  36. ret = -1;
  37. else {
  38. do {
  39. ret = write(f, "", 1);
  40. } while (ret < 0 && errno == EINTR);
  41. ret = ret <= 0 ? -1 : 0;
  42. }
  43. #endif
  44. sparse_seek = 0;
  45. return ret;
  46. }
  47. static int write_sparse(int f, char *buf, int len)
  48. {
  49. int l1 = 0, l2 = 0;
  50. int ret;
  51. for (l1 = 0; l1 < len && buf[l1] == 0; l1++) {}
  52. for (l2 = 0; l2 < len-l1 && buf[len-(l2+1)] == 0; l2++) {}
  53. sparse_seek += l1;
  54. if (l1 == len)
  55. return len;
  56. if (sparse_seek)
  57. do_lseek(f, sparse_seek, SEEK_CUR);
  58. sparse_seek = l2;
  59. while ((ret = write(f, buf + l1, len - (l1+l2))) <= 0) {
  60. if (ret < 0 && errno == EINTR)
  61. continue;
  62. return ret;
  63. }
  64. if (ret != (int)(len - (l1+l2)))
  65. return l1+ret;
  66. return len;
  67. }
  68. static char *wf_writeBuf;
  69. static size_t wf_writeBufSize;
  70. static size_t wf_writeBufCnt;
  71. int flush_write_file(int f)
  72. {
  73. int ret = 0;
  74. char *bp = wf_writeBuf;
  75. while (wf_writeBufCnt > 0) {
  76. if ((ret = write(f, bp, wf_writeBufCnt)) < 0) {
  77. if (errno == EINTR)
  78. continue;
  79. return ret;
  80. }
  81. wf_writeBufCnt -= ret;
  82. bp += ret;
  83. }
  84. return ret;
  85. }
  86. /*
  87. * write_file does not allow incomplete writes. It loops internally
  88. * until len bytes are written or errno is set.
  89. */
  90. int write_file(int f, char *buf, int len)
  91. {
  92. int ret = 0;
  93. while (len > 0) {
  94. int r1;
  95. if (sparse_files > 0) {
  96. int len1 = MIN(len, SPARSE_WRITE_SIZE);
  97. r1 = write_sparse(f, buf, len1);
  98. } else {
  99. if (!wf_writeBuf) {
  100. wf_writeBufSize = WRITE_SIZE * 8;
  101. wf_writeBufCnt = 0;
  102. wf_writeBuf = new_array(char, wf_writeBufSize);
  103. if (!wf_writeBuf)
  104. out_of_memory("write_file");
  105. }
  106. r1 = (int)MIN((size_t)len, wf_writeBufSize - wf_writeBufCnt);
  107. if (r1) {
  108. memcpy(wf_writeBuf + wf_writeBufCnt, buf, r1);
  109. wf_writeBufCnt += r1;
  110. }
  111. if (wf_writeBufCnt == wf_writeBufSize) {
  112. if (flush_write_file(f) < 0)
  113. return -1;
  114. if (!r1 && len)
  115. continue;
  116. }
  117. }
  118. if (r1 <= 0) {
  119. if (ret > 0)
  120. return ret;
  121. return r1;
  122. }
  123. len -= r1;
  124. buf += r1;
  125. ret += r1;
  126. }
  127. return ret;
  128. }
  129. /* This provides functionality somewhat similar to mmap() but using read().
  130. * It gives sliding window access to a file. mmap() is not used because of
  131. * the possibility of another program (such as a mailer) truncating the
  132. * file thus giving us a SIGBUS. */
  133. struct map_struct *map_file(int fd, OFF_T len, int32 read_size,
  134. int32 blk_size)
  135. {
  136. struct map_struct *map;
  137. if (!(map = new0(struct map_struct)))
  138. out_of_memory("map_file");
  139. if (blk_size && (read_size % blk_size))
  140. read_size += blk_size - (read_size % blk_size);
  141. map->fd = fd;
  142. map->file_size = len;
  143. map->def_window_size = read_size;
  144. return map;
  145. }
  146. /* slide the read window in the file */
  147. char *map_ptr(struct map_struct *map, OFF_T offset, int32 len)
  148. {
  149. int32 nread;
  150. OFF_T window_start, read_start;
  151. int32 window_size, read_size, read_offset;
  152. if (len == 0)
  153. return NULL;
  154. if (len < 0) {
  155. rprintf(FERROR, "invalid len passed to map_ptr: %ld\n",
  156. (long)len);
  157. exit_cleanup(RERR_FILEIO);
  158. }
  159. /* in most cases the region will already be available */
  160. if (offset >= map->p_offset && offset+len <= map->p_offset+map->p_len)
  161. return map->p + (offset - map->p_offset);
  162. /* nope, we are going to have to do a read. Work out our desired window */
  163. window_start = offset;
  164. window_size = map->def_window_size;
  165. if (window_start + window_size > map->file_size)
  166. window_size = (int32)(map->file_size - window_start);
  167. if (len > window_size)
  168. window_size = len;
  169. /* make sure we have allocated enough memory for the window */
  170. if (window_size > map->p_size) {
  171. map->p = realloc_array(map->p, char, window_size);
  172. if (!map->p)
  173. out_of_memory("map_ptr");
  174. map->p_size = window_size;
  175. }
  176. /* Now try to avoid re-reading any bytes by reusing any bytes
  177. * from the previous buffer. */
  178. if (window_start >= map->p_offset &&
  179. window_start < map->p_offset + map->p_len &&
  180. window_start + window_size >= map->p_offset + map->p_len) {
  181. read_start = map->p_offset + map->p_len;
  182. read_offset = (int32)(read_start - window_start);
  183. read_size = window_size - read_offset;
  184. memmove(map->p, map->p + (map->p_len - read_offset), read_offset);
  185. } else {
  186. read_start = window_start;
  187. read_size = window_size;
  188. read_offset = 0;
  189. }
  190. if (read_size <= 0) {
  191. rprintf(FERROR, "invalid read_size of %ld in map_ptr\n",
  192. (long)read_size);
  193. exit_cleanup(RERR_FILEIO);
  194. }
  195. if (map->p_fd_offset != read_start) {
  196. OFF_T ret = do_lseek(map->fd, read_start, SEEK_SET);
  197. if (ret != read_start) {
  198. rsyserr(FERROR, errno, "lseek returned %.0f, not %.0f",
  199. (double)ret, (double)read_start);
  200. exit_cleanup(RERR_FILEIO);
  201. }
  202. map->p_fd_offset = read_start;
  203. }
  204. map->p_offset = window_start;
  205. map->p_len = window_size;
  206. while (read_size > 0) {
  207. nread = read(map->fd, map->p + read_offset, read_size);
  208. if (nread <= 0) {
  209. if (!map->status)
  210. map->status = nread ? errno : ENODATA;
  211. /* The best we can do is zero the buffer -- the file
  212. * has changed mid transfer! */
  213. memset(map->p + read_offset, 0, read_size);
  214. break;
  215. }
  216. map->p_fd_offset += nread;
  217. read_offset += nread;
  218. read_size -= nread;
  219. }
  220. return map->p;
  221. }
  222. int unmap_file(struct map_struct *map)
  223. {
  224. int ret;
  225. if (map->p) {
  226. free(map->p);
  227. map->p = NULL;
  228. }
  229. ret = map->status;
  230. memset(map, 0, sizeof map[0]);
  231. free(map);
  232. return ret;
  233. }