##// END OF EJS Templates
pure/mpatch: use StringIO instead of mmap (issue1493)...
Martin Geisler -
r7775:5280c397 default
parent child Browse files
Show More
@@ -5,9 +5,11 b''
5 # This software may be used and distributed according to the terms
5 # This software may be used and distributed according to the terms
6 # of the GNU General Public License, incorporated herein by reference.
6 # of the GNU General Public License, incorporated herein by reference.
7
7
8 import struct, mmap
8 import struct
9
9 try:
10 devzero = file("/dev/zero")
10 from cStringIO import StringIO
11 except ImportError:
12 from StringIO import StringIO
11
13
12 # This attempts to apply a series of patches in time proportional to
14 # This attempts to apply a series of patches in time proportional to
13 # the total size of the patches, rather than patches * len(text). This
15 # the total size of the patches, rather than patches * len(text). This
@@ -30,7 +32,16 b' def patches(a, bins):'
30
32
31 if not tl: return a
33 if not tl: return a
32
34
33 m = mmap.mmap(devzero.fileno(), tl, mmap.MAP_PRIVATE)
35 m = StringIO()
36 def move(dest, src, count):
37 """move count bytes from src to dest
38
39 The file pointer is left at the end of dest.
40 """
41 m.seek(src)
42 buf = m.read(count)
43 m.seek(dest)
44 m.write(buf)
34
45
35 # load our original text
46 # load our original text
36 m.write(a)
47 m.write(a)
@@ -54,7 +65,7 b' def patches(a, bins):'
54 def collect(buf, list):
65 def collect(buf, list):
55 start = buf
66 start = buf
56 for l, p in list:
67 for l, p in list:
57 m.move(buf, p, l)
68 move(buf, p, l)
58 buf += l
69 buf += l
59 return (buf - start, start)
70 return (buf - start, start)
60
71
@@ -68,7 +79,8 b' def patches(a, bins):'
68 end = pos + plen
79 end = pos + plen
69 last = 0
80 last = 0
70 while pos < end:
81 while pos < end:
71 p1, p2, l = struct.unpack(">lll", m[pos:pos + 12])
82 m.seek(pos)
83 p1, p2, l = struct.unpack(">lll", m.read(12))
72 pull(new, frags, p1 - last) # what didn't change
84 pull(new, frags, p1 - last) # what didn't change
73 pull([], frags, p2 - p1) # what got deleted
85 pull([], frags, p2 - p1) # what got deleted
74 new.append((l, pos + 12)) # what got added
86 new.append((l, pos + 12)) # what got added
@@ -78,7 +90,8 b' def patches(a, bins):'
78
90
79 t = collect(b2, frags)
91 t = collect(b2, frags)
80
92
81 return m[t[1]:t[1] + t[0]]
93 m.seek(t[1])
94 return m.read(t[0])
82
95
83 def patchedsize(orig, delta):
96 def patchedsize(orig, delta):
84 outlen, last, bin = 0, 0, 0
97 outlen, last, bin = 0, 0, 0
General Comments 0
You need to be logged in to leave comments. Login now