@matheusbsbs / matheus-bagliano / commits / 9d2f5ddfe5
Fork 0

vcs-svn: learn to maintain a sliding view of a file

Each section of a Subversion-format delta only requires examining (and keeping in random-access memory) a small portion of the preimage. At any moment, this portion starts at a certain file offset and has a well-defined length, and as the delta is applied, the portion advances from the beginning to the end of the preimage. Add a move_window function to keep track of this view into the preimage. You can use it like this: buffer_init(f, NULL); struct sliding_view window = SLIDING_VIEW_INIT(f); move_window(&window, 3, 7); /* (1) */ move_window(&window, 5, 5); /* (2) */ move_window(&window, 12, 2); /* (3) */ strbuf_release(&window.buf); buffer_deinit(f); The data structure is called sliding_view instead of _window to prevent confusion with svndiff0 Windows. In this example, (1) reads 10 bytes and discards the first 3; (2) discards the first 2, which are not needed any more; and (3) skips 2 bytes and reads 2 new bytes to work with. When move_window returns, the file position indicator is at position window->off + window->width and the data from positions window->off to the current file position are stored in window->buf. This function performs only sequential access from the input file and never seeks, so it can be safely used on pipes and sockets. On end-of-file, move_window silently reads less than the caller requested. On other errors, it prints a message and returns -1. Helped-by: David Barr <david.barr@cordelta.com> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>

Jonathan Nieder committed Jan 2, 2011 at 21:54 UTC 9d2f5ddfe56fcc228a36dd079f0897e0f474eb4e
4 files changed +98
Makefile
+2
@@ -1851,6 +1851,7 @@ XDIFF_OBJS += xdiff/xpatience.o
1851
1852 VCSSVN_OBJS += vcs-svn/string_pool.o
1853 VCSSVN_OBJS += vcs-svn/line_buffer.o
1854 +VCSSVN_OBJS += vcs-svn/sliding_window.o
1855 VCSSVN_OBJS += vcs-svn/repo_tree.o
1856 VCSSVN_OBJS += vcs-svn/fast_export.o
1857 VCSSVN_OBJS += vcs-svn/svndump.o
@@ -1993,6 +1994,7 @@ VCSSVN_H += vcs-svn/obj_pool.h
1994 VCSSVN_H += vcs-svn/trp.h
1995 VCSSVN_H += vcs-svn/string_pool.h
1996 VCSSVN_H += vcs-svn/line_buffer.h
1997 +VCSSVN_H += vcs-svn/sliding_window.h
1998 VCSSVN_H += vcs-svn/repo_tree.h
1999 VCSSVN_H += vcs-svn/fast_export.h
2000 VCSSVN_H += vcs-svn/svndump.h
vcs-svn/LICENSE
+2
@@ -1,6 +1,8 @@
1 Copyright (C) 2010 David Barr <david.barr@cordelta.com>.
2 All rights reserved.
3
4 +Copyright (C) 2010 Jonathan Nieder <jrnieder@gmail.com>.
5 +
6 Copyright (C) 2008 Jason Evans <jasone@canonware.com>.
7 All rights reserved.
8
vcs-svn/sliding_window.c new
+77
@@ -0,0 +1,77 @@
1 +/*
2 + * Licensed under a two-clause BSD-style license.
3 + * See LICENSE for details.
4 + */
5 +
6 +#include "git-compat-util.h"
7 +#include "sliding_window.h"
8 +#include "line_buffer.h"
9 +#include "strbuf.h"
10 +
11 +static int input_error(struct line_buffer *file)
12 +{
13 + if (!buffer_ferror(file))
14 + return error("delta preimage ends early");
15 + return error("cannot read delta preimage: %s", strerror(errno));
16 +}
17 +
18 +static int skip_or_whine(struct line_buffer *file, off_t gap)
19 +{
20 + if (buffer_skip_bytes(file, gap) != gap)
21 + return input_error(file);
22 + return 0;
23 +}
24 +
25 +static int read_to_fill_or_whine(struct line_buffer *file,
26 + struct strbuf *buf, size_t width)
27 +{
28 + buffer_read_binary(file, buf, width - buf->len);
29 + if (buf->len != width)
30 + return input_error(file);
31 + return 0;
32 +}
33 +
34 +static int check_overflow(off_t a, size_t b)
35 +{
36 + if (b > maximum_signed_value_of_type(off_t))
37 + return error("unrepresentable length in delta: "
38 + "%"PRIuMAX" > OFF_MAX", (uintmax_t) b);
39 + if (signed_add_overflows(a, (off_t) b))
40 + return error("unrepresentable offset in delta: "
41 + "%"PRIuMAX" + %"PRIuMAX" > OFF_MAX",
42 + (uintmax_t) a, (uintmax_t) b);
43 + return 0;
44 +}
45 +
46 +int move_window(struct sliding_view *view, off_t off, size_t width)
47 +{
48 + off_t file_offset;
49 + assert(view);
50 + assert(view->width <= view->buf.len);
51 + assert(!check_overflow(view->off, view->buf.len));
52 +
53 + if (check_overflow(off, width))
54 + return -1;
55 + if (off < view->off || off + width < view->off + view->width)
56 + return error("invalid delta: window slides left");
57 +
58 + file_offset = view->off + view->buf.len;
59 + if (off < file_offset) {
60 + /* Move the overlapping region into place. */
61 + strbuf_remove(&view->buf, 0, off - view->off);
62 + } else {
63 + /* Seek ahead to skip the gap. */
64 + if (skip_or_whine(view->file, off - file_offset))
65 + return -1;
66 + strbuf_setlen(&view->buf, 0);
67 + }
68 +
69 + if (view->buf.len > width)
70 + ; /* Already read. */
71 + else if (read_to_fill_or_whine(view->file, &view->buf, width))
72 + return -1;
73 +
74 + view->off = off;
75 + view->width = width;
76 + return 0;
77 +}
vcs-svn/sliding_window.h new
+17
@@ -0,0 +1,17 @@
1 +#ifndef SLIDING_WINDOW_H_
2 +#define SLIDING_WINDOW_H_
3 +
4 +#include "strbuf.h"
5 +
6 +struct sliding_view {
7 + struct line_buffer *file;
8 + off_t off;
9 + size_t width;
10 + struct strbuf buf;
11 +};
12 +
13 +#define SLIDING_VIEW_INIT(input) { (input), 0, 0, STRBUF_INIT }
14 +
15 +extern int move_window(struct sliding_view *view, off_t off, size_t width);
16 +
17 +#endif