static void handle_command(const char *command, const char *arg, struct line_buffer *buf) { switch (*command) { case 'b': if (starts_with(command, "binary ")) { struct strbuf sb = STRBUF_INIT; strbuf_addch(&sb, '>'); buffer_read_binary(buf, &sb, strtouint32(arg)); fwrite(sb.buf, 1, sb.len, stdout); strbuf_release(&sb); return; } case 'c': if (starts_with(command, "copy ")) { buffer_copy_bytes(buf, strtouint32(arg)); return; } case 's': if (starts_with(command, "skip ")) { buffer_skip_bytes(buf, strtouint32(arg)); return; } default: die("unrecognized command: %s", command); } }
void fast_export_blob(uint32_t mode, uint32_t mark, uint32_t len, struct line_buffer *input) { if (mode == REPO_MODE_LNK) { /* svn symlink blobs start with "link " */ buffer_skip_bytes(input, 5); len -= 5; } printf("blob\nmark :%"PRIu32"\ndata %"PRIu32"\n", mark, len); buffer_copy_bytes(input, len); fputc('\n', stdout); }
void buffer_copy_bytes(uint32_t len) { uint32_t in; while (len > 0 && !feof(infile) && !ferror(infile)) { in = len < COPY_BUFFER_LEN ? len : COPY_BUFFER_LEN; in = fread(byte_buffer, 1, in, infile); len -= in; fwrite(byte_buffer, 1, in, stdout); if (ferror(stdout)) { buffer_skip_bytes(len); return; } } }
off_t buffer_copy_bytes(struct line_buffer *buf, off_t nbytes) { char byte_buffer[COPY_BUFFER_LEN]; off_t done = 0; while (done < nbytes && !feof(buf->infile) && !ferror(buf->infile)) { off_t len = nbytes - done; size_t in = len < COPY_BUFFER_LEN ? len : COPY_BUFFER_LEN; in = fread(byte_buffer, 1, in, buf->infile); done += in; fwrite(byte_buffer, 1, in, stdout); if (ferror(stdout)) return done + buffer_skip_bytes(buf, nbytes - done); } return done; }
void fast_export_blob_delta(uint32_t mode, uint32_t old_mode, const char *old_data, off_t len, struct line_buffer *input) { long postimage_len; assert(len >= 0); postimage_len = apply_delta(len, input, old_data, old_mode); if (mode == REPO_MODE_LNK) { buffer_skip_bytes(&postimage, strlen("link ")); postimage_len -= strlen("link "); } printf("data %ld\n", postimage_len); buffer_copy_bytes(&postimage, postimage_len); fputc('\n', stdout); }
void fast_export_data(uint32_t mode, off_t len, struct line_buffer *input) { assert(len >= 0); if (mode == REPO_MODE_LNK) { /* svn symlink blobs start with "link " */ if (len < 5) die("invalid dump: symlink too short for \"link\" prefix"); len -= 5; if (buffer_skip_bytes(input, 5) != 5) die_short_read(input); } printf("data %"PRIuMAX"\n", (uintmax_t) len); if (buffer_copy_bytes(input, len) != len) die_short_read(input); fputc('\n', stdout); }
static void handle_node(void) { if (node_ctx.propLength != LENGTH_UNKNOWN && node_ctx.propLength) read_props(); if (node_ctx.srcRev) node_ctx.srcMode = repo_copy(node_ctx.srcRev, node_ctx.src, node_ctx.dst); if (node_ctx.textLength != LENGTH_UNKNOWN && node_ctx.type != REPO_MODE_DIR) node_ctx.mark = next_blob_mark(); if (node_ctx.action == NODEACT_DELETE) { repo_delete(node_ctx.dst); } else if (node_ctx.action == NODEACT_CHANGE || node_ctx.action == NODEACT_REPLACE) { if (node_ctx.action == NODEACT_REPLACE && node_ctx.type == REPO_MODE_DIR) repo_replace(node_ctx.dst, node_ctx.mark); else if (node_ctx.propLength != LENGTH_UNKNOWN) repo_modify(node_ctx.dst, node_ctx.type, node_ctx.mark); else if (node_ctx.textLength != LENGTH_UNKNOWN) node_ctx.srcMode = repo_replace(node_ctx.dst, node_ctx.mark); } else if (node_ctx.action == NODEACT_ADD) { if (node_ctx.srcRev && node_ctx.propLength != LENGTH_UNKNOWN) repo_modify(node_ctx.dst, node_ctx.type, node_ctx.mark); else if (node_ctx.srcRev && node_ctx.textLength != LENGTH_UNKNOWN) node_ctx.srcMode = repo_replace(node_ctx.dst, node_ctx.mark); else if ((node_ctx.type == REPO_MODE_DIR && !node_ctx.srcRev) || node_ctx.textLength != LENGTH_UNKNOWN) repo_add(node_ctx.dst, node_ctx.type, node_ctx.mark); } if (node_ctx.propLength == LENGTH_UNKNOWN && node_ctx.srcMode) node_ctx.type = node_ctx.srcMode; if (node_ctx.mark) fast_export_blob(node_ctx.type, node_ctx.mark, node_ctx.textLength); else if (node_ctx.textLength != LENGTH_UNKNOWN) buffer_skip_bytes(node_ctx.textLength); }
int main(int argc, char *argv[]) { char *s; if (argc != 1) usage("test-line-buffer < input.txt"); if (buffer_init(NULL)) die_errno("open error"); while ((s = buffer_read_line())) { s = buffer_read_string(strtouint32(s)); fputs(s, stdout); fputc('\n', stdout); buffer_skip_bytes(1); if (!(s = buffer_read_line())) break; buffer_copy_bytes(strtouint32(s) + 1); } if (buffer_deinit()) die("input error"); if (ferror(stdout)) die("output error"); buffer_reset(); return 0; }
static int skip_or_whine(struct line_buffer *file, off_t gap) { if (buffer_skip_bytes(file, gap) != gap) return input_error(file); return 0; }
void svndump_read(const char *url) { char *val; char *t; uint32_t active_ctx = DUMP_CTX; uint32_t len; uint32_t key; reset_dump_ctx(pool_intern(url)); while ((t = buffer_read_line())) { val = strstr(t, ": "); if (!val) continue; *val++ = '\0'; *val++ = '\0'; key = pool_intern(t); if (key == keys.uuid) { dump_ctx.uuid = pool_intern(val); } else if (key == keys.revision_number) { if (active_ctx == NODE_CTX) handle_node(); if (active_ctx != DUMP_CTX) handle_revision(); active_ctx = REV_CTX; reset_rev_ctx(atoi(val)); } else if (key == keys.node_path) { if (active_ctx == NODE_CTX) handle_node(); active_ctx = NODE_CTX; reset_node_ctx(val); } else if (key == keys.node_kind) { if (!strcmp(val, "dir")) node_ctx.type = REPO_MODE_DIR; else if (!strcmp(val, "file")) node_ctx.type = REPO_MODE_BLB; else fprintf(stderr, "Unknown node-kind: %s\n", val); } else if (key == keys.node_action) { if (!strcmp(val, "delete")) { node_ctx.action = NODEACT_DELETE; } else if (!strcmp(val, "add")) { node_ctx.action = NODEACT_ADD; } else if (!strcmp(val, "change")) { node_ctx.action = NODEACT_CHANGE; } else if (!strcmp(val, "replace")) { node_ctx.action = NODEACT_REPLACE; } else { fprintf(stderr, "Unknown node-action: %s\n", val); node_ctx.action = NODEACT_UNKNOWN; } } else if (key == keys.node_copyfrom_path) { pool_tok_seq(REPO_MAX_PATH_DEPTH, node_ctx.src, "/", val); } else if (key == keys.node_copyfrom_rev) { node_ctx.srcRev = atoi(val); } else if (key == keys.text_content_length) { node_ctx.textLength = atoi(val); } else if (key == keys.prop_content_length) { node_ctx.propLength = atoi(val); } else if (key == keys.content_length) { len = atoi(val); buffer_read_line(); if (active_ctx == REV_CTX) { read_props(); } else if (active_ctx == NODE_CTX) { handle_node(); active_ctx = REV_CTX; } else { fprintf(stderr, "Unexpected content length header: %d\n", len); buffer_skip_bytes(len); } } } if (active_ctx == NODE_CTX) handle_node(); if (active_ctx != DUMP_CTX) handle_revision(); }
void svndump_read(const char *url) { char *val; char *t; uint32_t active_ctx = DUMP_CTX; uint32_t len; reset_dump_ctx(url); while ((t = buffer_read_line(&input))) { val = strchr(t, ':'); if (!val) continue; val++; if (*val != ' ') continue; val++; /* strlen(key) + 1 */ switch (val - t - 1) { case sizeof("SVN-fs-dump-format-version"): if (constcmp(t, "SVN-fs-dump-format-version")) continue; dump_ctx.version = atoi(val); if (dump_ctx.version > 3) die("expected svn dump format version <= 3, found %"PRIu32, dump_ctx.version); break; case sizeof("UUID"): if (constcmp(t, "UUID")) continue; strbuf_reset(&dump_ctx.uuid); strbuf_addstr(&dump_ctx.uuid, val); break; case sizeof("Revision-number"): if (constcmp(t, "Revision-number")) continue; if (active_ctx == NODE_CTX) handle_node(); if (active_ctx == REV_CTX) begin_revision(); if (active_ctx != DUMP_CTX) end_revision(); active_ctx = REV_CTX; reset_rev_ctx(atoi(val)); break; case sizeof("Node-path"): if (prefixcmp(t, "Node-")) continue; if (!constcmp(t + strlen("Node-"), "path")) { if (active_ctx == NODE_CTX) handle_node(); if (active_ctx == REV_CTX) begin_revision(); active_ctx = NODE_CTX; reset_node_ctx(val); break; } if (constcmp(t + strlen("Node-"), "kind")) continue; if (!strcmp(val, "dir")) node_ctx.type = REPO_MODE_DIR; else if (!strcmp(val, "file")) node_ctx.type = REPO_MODE_BLB; else fprintf(stderr, "Unknown node-kind: %s\n", val); break; case sizeof("Node-action"): if (constcmp(t, "Node-action")) continue; if (!strcmp(val, "delete")) { node_ctx.action = NODEACT_DELETE; } else if (!strcmp(val, "add")) { node_ctx.action = NODEACT_ADD; } else if (!strcmp(val, "change")) { node_ctx.action = NODEACT_CHANGE; } else if (!strcmp(val, "replace")) { node_ctx.action = NODEACT_REPLACE; } else { fprintf(stderr, "Unknown node-action: %s\n", val); node_ctx.action = NODEACT_UNKNOWN; } break; case sizeof("Node-copyfrom-path"): if (constcmp(t, "Node-copyfrom-path")) continue; strbuf_reset(&node_ctx.src); strbuf_addstr(&node_ctx.src, val); break; case sizeof("Node-copyfrom-rev"): if (constcmp(t, "Node-copyfrom-rev")) continue; node_ctx.srcRev = atoi(val); break; case sizeof("Text-content-length"): if (!constcmp(t, "Text-content-length")) { char *end; uintmax_t textlen; textlen = strtoumax(val, &end, 10); if (!isdigit(*val) || *end) die("invalid dump: non-numeric length %s", val); if (textlen > maximum_signed_value_of_type(off_t)) die("unrepresentable length in dump: %s", val); node_ctx.text_length = (off_t) textlen; break; } if (constcmp(t, "Prop-content-length")) continue; node_ctx.propLength = atoi(val); break; case sizeof("Text-delta"): if (!constcmp(t, "Text-delta")) { node_ctx.text_delta = !strcmp(val, "true"); break; } if (constcmp(t, "Prop-delta")) continue; node_ctx.prop_delta = !strcmp(val, "true"); break; case sizeof("Content-length"): if (constcmp(t, "Content-length")) continue; len = atoi(val); t = buffer_read_line(&input); if (!t) die_short_read(); if (*t) die("invalid dump: expected blank line after content length header"); if (active_ctx == REV_CTX) { read_props(); } else if (active_ctx == NODE_CTX) { handle_node(); active_ctx = INTERNODE_CTX; } else { fprintf(stderr, "Unexpected content length header: %"PRIu32"\n", len); if (buffer_skip_bytes(&input, len) != len) die_short_read(); } } } if (buffer_ferror(&input)) die_short_read(); if (active_ctx == NODE_CTX) handle_node(); if (active_ctx == REV_CTX) begin_revision(); if (active_ctx != DUMP_CTX) end_revision(); }