Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/odb/streaming.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / odb / streaming.c` 331 lines ยท 7807 bytes Raw Blame History 1 /* 2 * Copyright (c) 2011, Google Inc. 3 */ 4 5 #include "git-compat-util.h" 6 #include "convert.h" 7 #include "environment.h" 8 #include "repository.h" 9 #include "odb.h" 10 #include "odb/source.h" 11 #include "odb/streaming.h" 12 #include "replace-object.h" 13 14 #define FILTER_BUFFER (1024*16) 15 16 /***************************************************************** 17 * 18 * Filtered stream 19 * 20 *****************************************************************/ 21 22 struct odb_filtered_read_stream { 23 struct odb_stream base; 24 struct odb_stream *upstream; 25 struct stream_filter *filter; 26 char ibuf[FILTER_BUFFER]; 27 char obuf[FILTER_BUFFER]; 28 int i_end, i_ptr; 29 int o_end, o_ptr; 30 int input_finished; 31 }; 32 33 static int close_istream_filtered(struct odb_stream *_fs) 34 { 35 struct odb_filtered_read_stream *fs = (struct odb_filtered_read_stream *)_fs; 36 free_stream_filter(fs->filter); 37 return odb_stream_close(fs->upstream); 38 } 39 40 static ssize_t read_istream_filtered(struct odb_stream *_fs, char *buf, 41 size_t sz) 42 { 43 struct odb_filtered_read_stream *fs = (struct odb_filtered_read_stream *)_fs; 44 size_t filled = 0; 45 46 while (sz) { 47 /* do we already have filtered output? */ 48 if (fs->o_ptr < fs->o_end) { 49 size_t to_move = fs->o_end - fs->o_ptr; 50 if (sz < to_move) 51 to_move = sz; 52 memcpy(buf + filled, fs->obuf + fs->o_ptr, to_move); 53 fs->o_ptr += to_move; 54 sz -= to_move; 55 filled += to_move; 56 continue; 57 } 58 fs->o_end = fs->o_ptr = 0; 59 60 /* do we have anything to feed the filter with? */ 61 if (fs->i_ptr < fs->i_end) { 62 size_t to_feed = fs->i_end - fs->i_ptr; 63 size_t to_receive = FILTER_BUFFER; 64 if (stream_filter(fs->filter, 65 fs->ibuf + fs->i_ptr, &to_feed, 66 fs->obuf, &to_receive)) 67 return -1; 68 fs->i_ptr = fs->i_end - to_feed; 69 fs->o_end = FILTER_BUFFER - to_receive; 70 continue; 71 } 72 73 /* tell the filter to drain upon no more input */ 74 if (fs->input_finished) { 75 size_t to_receive = FILTER_BUFFER; 76 if (stream_filter(fs->filter, 77 NULL, NULL, 78 fs->obuf, &to_receive)) 79 return -1; 80 fs->o_end = FILTER_BUFFER - to_receive; 81 if (!fs->o_end) 82 break; 83 continue; 84 } 85 fs->i_end = fs->i_ptr = 0; 86 87 /* refill the input from the upstream */ 88 if (!fs->input_finished) { 89 fs->i_end = odb_stream_read(fs->upstream, fs->ibuf, FILTER_BUFFER); 90 if (fs->i_end < 0) 91 return -1; 92 if (fs->i_end) 93 continue; 94 } 95 fs->input_finished = 1; 96 } 97 return filled; 98 } 99 100 static struct odb_stream *attach_stream_filter(struct odb_stream *st, 101 struct stream_filter *filter) 102 { 103 struct odb_filtered_read_stream *fs; 104 105 CALLOC_ARRAY(fs, 1); 106 fs->base.close = close_istream_filtered; 107 fs->base.read = read_istream_filtered; 108 fs->upstream = st; 109 fs->filter = filter; 110 fs->base.size = -1; /* unknown */ 111 fs->base.type = st->type; 112 113 return &fs->base; 114 } 115 116 /***************************************************************** 117 * 118 * In-core stream 119 * 120 *****************************************************************/ 121 122 struct odb_incore_read_stream { 123 struct odb_stream base; 124 char *buf; /* from odb_read_object_info_extended() */ 125 unsigned long read_ptr; 126 }; 127 128 static int close_istream_incore(struct odb_stream *_st) 129 { 130 struct odb_incore_read_stream *st = (struct odb_incore_read_stream *)_st; 131 free(st->buf); 132 return 0; 133 } 134 135 static ssize_t read_istream_incore(struct odb_stream *_st, char *buf, size_t sz) 136 { 137 struct odb_incore_read_stream *st = (struct odb_incore_read_stream *)_st; 138 size_t read_size = sz; 139 size_t remainder = st->base.size - st->read_ptr; 140 141 if (remainder <= read_size) 142 read_size = remainder; 143 if (read_size) { 144 memcpy(buf, st->buf + st->read_ptr, read_size); 145 st->read_ptr += read_size; 146 } 147 return read_size; 148 } 149 150 static int open_istream_incore(struct odb_stream **out, 151 struct object_database *odb, 152 const struct object_id *oid) 153 { 154 struct object_info oi = OBJECT_INFO_INIT; 155 struct odb_incore_read_stream stream = { 156 .base.close = close_istream_incore, 157 .base.read = read_istream_incore, 158 }; 159 struct odb_incore_read_stream *st; 160 int ret; 161 162 oi.typep = &stream.base.type; 163 oi.sizep = &stream.base.size; 164 oi.contentp = (void **)&stream.buf; 165 ret = odb_read_object_info_extended(odb, oid, &oi, 166 OBJECT_INFO_DIE_IF_CORRUPT); 167 if (ret) 168 return ret; 169 170 CALLOC_ARRAY(st, 1); 171 *st = stream; 172 *out = &st->base; 173 174 return 0; 175 } 176 177 /***************************************************************************** 178 * static helpers variables and functions for users of streaming interface 179 *****************************************************************************/ 180 181 static int istream_source(struct odb_stream **out, 182 struct object_database *odb, 183 const struct object_id *oid) 184 { 185 if (!odb_source_read_object_stream(out, odb->source, oid)) 186 return 0; 187 return open_istream_incore(out, odb, oid); 188 } 189 190 /**************************************************************** 191 * Users of streaming interface 192 ****************************************************************/ 193 194 int odb_stream_close(struct odb_stream *st) 195 { 196 int r = st->close(st); 197 free(st); 198 return r; 199 } 200 201 ssize_t odb_stream_read(struct odb_stream *st, void *buf, size_t sz) 202 { 203 return st->read(st, buf, sz); 204 } 205 206 struct odb_stream *odb_stream_from_object(struct object_database *odb, 207 const struct object_id *oid, 208 struct stream_filter *filter) 209 { 210 struct odb_stream *st; 211 const struct object_id *real = lookup_replace_object(odb->repo, oid); 212 int ret = istream_source(&st, odb, real); 213 214 if (ret) 215 return NULL; 216 217 if (filter) { 218 /* Add "&& !is_null_stream_filter(filter)" for performance */ 219 struct odb_stream *nst = attach_stream_filter(st, filter); 220 if (!nst) { 221 odb_stream_close(st); 222 return NULL; 223 } 224 st = nst; 225 } 226 227 return st; 228 } 229 230 int odb_stream_blob_to_fd(struct object_database *odb, 231 int fd, 232 const struct object_id *oid, 233 struct stream_filter *filter, 234 int can_seek) 235 { 236 struct odb_stream *st; 237 ssize_t kept = 0; 238 int result = -1; 239 240 st = odb_stream_from_object(odb, oid, filter); 241 if (!st) { 242 if (filter) 243 free_stream_filter(filter); 244 return result; 245 } 246 if (st->type != OBJ_BLOB) 247 goto close_and_exit; 248 for (;;) { 249 char buf[1024 * 16]; 250 ssize_t wrote, holeto; 251 ssize_t readlen = odb_stream_read(st, buf, sizeof(buf)); 252 253 if (readlen < 0) 254 goto close_and_exit; 255 if (!readlen) 256 break; 257 if (can_seek && sizeof(buf) == readlen) { 258 for (holeto = 0; holeto < readlen; holeto++) 259 if (buf[holeto]) 260 break; 261 if (readlen == holeto) { 262 kept += holeto; 263 continue; 264 } 265 } 266 267 if (kept && lseek(fd, kept, SEEK_CUR) == (off_t) -1) 268 goto close_and_exit; 269 else 270 kept = 0; 271 wrote = write_in_full(fd, buf, readlen); 272 273 if (wrote < 0) 274 goto close_and_exit; 275 } 276 if (kept && (lseek(fd, kept - 1, SEEK_CUR) == (off_t) -1 || 277 xwrite(fd, "", 1) != 1)) 278 goto close_and_exit; 279 result = 0; 280 281 close_and_exit: 282 odb_stream_close(st); 283 return result; 284 } 285 286 struct fd_stream { 287 struct odb_stream base; 288 int fd; 289 size_t remaining; 290 }; 291 292 static ssize_t fd_stream_read(struct odb_stream *stream, 293 char *buf, size_t len) 294 { 295 struct fd_stream *fds = container_of(stream, struct fd_stream, base); 296 ssize_t read_result; 297 size_t count; 298 299 if (!fds->remaining) 300 return 0; 301 302 count = fds->remaining < len ? fds->remaining : len; 303 read_result = read_in_full(fds->fd, buf, count); 304 if (read_result < 0 || (size_t)read_result != count) 305 return -1; 306 307 fds->remaining -= count; 308 309 return read_result; 310 } 311 312 static int fd_stream_close(struct odb_stream *stream UNUSED) 313 { 314 /* The file descriptor is owned by the caller for now. */ 315 return 0; 316 } 317 318 struct odb_stream *odb_stream_from_fd(int fd, size_t size, enum object_type type) 319 { 320 struct fd_stream *fds; 321 322 CALLOC_ARRAY(fds, 1); 323 fds->base.read = fd_stream_read; 324 fds->base.close = fd_stream_close; 325 fds->base.size = size; 326 fds->base.type = type; 327 fds->fd = fd; 328 fds->remaining = size; 329 330 return &fds->base; 331 }