fileio.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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-2008 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 char last_byte;
  27. static size_t sparse_seek = 0;
  28. int sparse_end(int f)
  29. {
  30. int ret;
  31. if (!sparse_seek)
  32. return 0;
  33. do_lseek(f, sparse_seek-1, SEEK_CUR);
  34. sparse_seek = 0;
  35. do {
  36. ret = write(f, "", 1);
  37. } while (ret < 0 && errno == EINTR);
  38. return ret <= 0 ? -1 : 0;
  39. }
  40. static int write_sparse(int f, char *buf, size_t len)
  41. {
  42. size_t l1 = 0, l2 = 0;
  43. int ret;
  44. for (l1 = 0; l1 < len && buf[l1] == 0; l1++) {}
  45. for (l2 = 0; l2 < len-l1 && buf[len-(l2+1)] == 0; l2++) {}
  46. /* XXX Riddle me this: why does this function SLOW DOWN when I
  47. * remove the following (unneeded) line?? Core Duo weirdness? */
  48. last_byte = buf[len-1];
  49. sparse_seek += l1;
  50. if (l1 == len)
  51. return len;
  52. if (sparse_seek)
  53. do_lseek(f, sparse_seek, SEEK_CUR);
  54. sparse_seek = l2;
  55. while ((ret = write(f, buf + l1, len - (l1+l2))) <= 0) {
  56. if (ret < 0 && errno == EINTR)
  57. continue;
  58. return ret;
  59. }
  60. if (ret != (int)(len - (l1+l2)))
  61. return l1+ret;
  62. return len;
  63. }
  64. static char *wf_writeBuf;
  65. static size_t wf_writeBufSize;
  66. static size_t wf_writeBufCnt;
  67. int flush_write_file(int f)
  68. {
  69. int ret = 0;
  70. char *bp = wf_writeBuf;
  71. while (wf_writeBufCnt > 0) {
  72. if ((ret = write(f, bp, wf_writeBufCnt)) < 0) {
  73. if (errno == EINTR)
  74. continue;
  75. return ret;
  76. }
  77. wf_writeBufCnt -= ret;
  78. bp += ret;
  79. }
  80. return ret;
  81. }
  82. /*
  83. * write_file does not allow incomplete writes. It loops internally
  84. * until len bytes are written or errno is set.
  85. */
  86. int write_file(int f,char *buf,size_t len)
  87. {
  88. int ret = 0;
  89. while (len > 0) {
  90. int r1;
  91. if (sparse_files > 0) {
  92. int len1 = MIN(len, SPARSE_WRITE_SIZE);
  93. r1 = write_sparse(f, buf, len1);
  94. } else {
  95. if (!wf_writeBuf) {
  96. wf_writeBufSize = WRITE_SIZE * 8;
  97. wf_writeBufCnt = 0;
  98. wf_writeBuf = new_array(char, wf_writeBufSize);
  99. if (!wf_writeBuf)
  100. out_of_memory("write_file");
  101. }
  102. r1 = MIN(len, wf_writeBufSize - wf_writeBufCnt);
  103. if (r1) {
  104. memcpy(wf_writeBuf + wf_writeBufCnt, buf, r1);
  105. wf_writeBufCnt += r1;
  106. }
  107. if (wf_writeBufCnt == wf_writeBufSize) {
  108. if (flush_write_file(f) < 0)
  109. return -1;
  110. if (!r1 && len)
  111. continue;
  112. }
  113. }
  114. if (r1 <= 0) {
  115. if (ret > 0)
  116. return ret;
  117. return r1;
  118. }
  119. len -= r1;
  120. buf += r1;
  121. ret += r1;
  122. }
  123. return ret;
  124. }
  125. /* This provides functionality somewhat similar to mmap() but using read().
  126. * It gives sliding window access to a file. mmap() is not used because of
  127. * the possibility of another program (such as a mailer) truncating the
  128. * file thus giving us a SIGBUS. */
  129. struct map_struct *map_file(int fd, OFF_T len, int32 read_size,
  130. int32 blk_size)
  131. {
  132. struct map_struct *map;
  133. if (!(map = new0(struct map_struct)))
  134. out_of_memory("map_file");
  135. if (blk_size && (read_size % blk_size))
  136. read_size += blk_size - (read_size % blk_size);
  137. map->fd = fd;
  138. map->file_size = len;
  139. map->def_window_size = read_size;
  140. return map;
  141. }
  142. /* slide the read window in the file */
  143. char *map_ptr(struct map_struct *map, OFF_T offset, int32 len)
  144. {
  145. int32 nread;
  146. OFF_T window_start, read_start;
  147. int32 window_size, read_size, read_offset;
  148. if (len == 0)
  149. return NULL;
  150. if (len < 0) {
  151. rprintf(FERROR, "invalid len passed to map_ptr: %ld\n",
  152. (long)len);
  153. exit_cleanup(RERR_FILEIO);
  154. }
  155. /* in most cases the region will already be available */
  156. if (offset >= map->p_offset && offset+len <= map->p_offset+map->p_len)
  157. return map->p + (offset - map->p_offset);
  158. /* nope, we are going to have to do a read. Work out our desired window */
  159. window_start = offset;
  160. window_size = map->def_window_size;
  161. if (window_start + window_size > map->file_size)
  162. window_size = (int32)(map->file_size - window_start);
  163. if (len > window_size)
  164. window_size = len;
  165. /* make sure we have allocated enough memory for the window */
  166. if (window_size > map->p_size) {
  167. map->p = realloc_array(map->p, char, window_size);
  168. if (!map->p)
  169. out_of_memory("map_ptr");
  170. map->p_size = window_size;
  171. }
  172. /* Now try to avoid re-reading any bytes by reusing any bytes
  173. * from the previous buffer. */
  174. if (window_start >= map->p_offset &&
  175. window_start < map->p_offset + map->p_len &&
  176. window_start + window_size >= map->p_offset + map->p_len) {
  177. read_start = map->p_offset + map->p_len;
  178. read_offset = (int32)(read_start - window_start);
  179. read_size = window_size - read_offset;
  180. memmove(map->p, map->p + (map->p_len - read_offset), read_offset);
  181. } else {
  182. read_start = window_start;
  183. read_size = window_size;
  184. read_offset = 0;
  185. }
  186. if (read_size <= 0) {
  187. rprintf(FERROR, "invalid read_size of %ld in map_ptr\n",
  188. (long)read_size);
  189. exit_cleanup(RERR_FILEIO);
  190. }
  191. if (map->p_fd_offset != read_start) {
  192. OFF_T ret = do_lseek(map->fd, read_start, SEEK_SET);
  193. if (ret != read_start) {
  194. rsyserr(FERROR, errno, "lseek returned %.0f, not %.0f",
  195. (double)ret, (double)read_start);
  196. exit_cleanup(RERR_FILEIO);
  197. }
  198. map->p_fd_offset = read_start;
  199. }
  200. map->p_offset = window_start;
  201. map->p_len = window_size;
  202. while (read_size > 0) {
  203. nread = read(map->fd, map->p + read_offset, read_size);
  204. if (nread <= 0) {
  205. if (!map->status)
  206. map->status = nread ? errno : ENODATA;
  207. /* The best we can do is zero the buffer -- the file
  208. * has changed mid transfer! */
  209. memset(map->p + read_offset, 0, read_size);
  210. break;
  211. }
  212. map->p_fd_offset += nread;
  213. read_offset += nread;
  214. read_size -= nread;
  215. }
  216. return map->p;
  217. }
  218. int unmap_file(struct map_struct *map)
  219. {
  220. int ret;
  221. if (map->p) {
  222. free(map->p);
  223. map->p = NULL;
  224. }
  225. ret = map->status;
  226. memset(map, 0, sizeof map[0]);
  227. free(map);
  228. return ret;
  229. }