Note that the offset is just the caller letting us know where * the current file position is in the file. The use_seek arg tells * us that we should seek over matching data instead of writing it. */
| 76 | * the current file position is in the file. The use_seek arg tells |
| 77 | * us that we should seek over matching data instead of writing it. */ |
| 78 | static int write_sparse(int f, int use_seek, OFF_T offset, const char *buf, int len) |
| 79 | { |
| 80 | int l1 = 0, l2 = 0; |
| 81 | int ret; |
| 82 | |
| 83 | for (l1 = 0; l1 < len && buf[l1] == 0; l1++) {} |
| 84 | for (l2 = 0; l2 < len-l1 && buf[len-(l2+1)] == 0; l2++) {} |
| 85 | |
| 86 | sparse_seek += l1; |
| 87 | |
| 88 | if (l1 == len) |
| 89 | return len; |
| 90 | |
| 91 | if (sparse_seek) { |
| 92 | if (sparse_past_write >= preallocated_len) { |
| 93 | if (do_lseek(f, sparse_seek, SEEK_CUR) < 0) |
| 94 | return -1; |
| 95 | } else if (do_punch_hole(f, sparse_past_write, sparse_seek) < 0) { |
| 96 | sparse_seek = 0; |
| 97 | return -1; |
| 98 | } |
| 99 | } |
| 100 | sparse_seek = l2; |
| 101 | sparse_past_write = offset + len - l2; |
| 102 | |
| 103 | if (use_seek) { |
| 104 | /* The in-place data already matches. */ |
| 105 | if (do_lseek(f, len - (l1+l2), SEEK_CUR) < 0) |
| 106 | return -1; |
| 107 | return len; |
| 108 | } |
| 109 | |
| 110 | while ((ret = write(f, buf + l1, len - (l1+l2))) <= 0) { |
| 111 | if (ret < 0 && errno == EINTR) |
| 112 | continue; |
| 113 | sparse_seek = 0; |
| 114 | return ret; |
| 115 | } |
| 116 | |
| 117 | if (ret != (int)(len - (l1+l2))) { |
| 118 | sparse_seek = 0; |
| 119 | return l1+ret; |
| 120 | } |
| 121 | |
| 122 | return len; |
| 123 | } |
| 124 | |
| 125 | static char *wf_writeBuf; |
| 126 | static size_t wf_writeBufSize; |
no test coverage detected