Show More
@@ -1,389 +1,414 b'' | |||||
1 | # censor code related to censoring revision |
|
1 | # censor code related to censoring revision | |
2 | # coding: utf8 |
|
2 | # coding: utf8 | |
3 | # |
|
3 | # | |
4 | # Copyright 2021 Pierre-Yves David <pierre-yves.david@octobus.net> |
|
4 | # Copyright 2021 Pierre-Yves David <pierre-yves.david@octobus.net> | |
5 | # Copyright 2015 Google, Inc <martinvonz@google.com> |
|
5 | # Copyright 2015 Google, Inc <martinvonz@google.com> | |
6 | # |
|
6 | # | |
7 | # This software may be used and distributed according to the terms of the |
|
7 | # This software may be used and distributed according to the terms of the | |
8 | # GNU General Public License version 2 or any later version. |
|
8 | # GNU General Public License version 2 or any later version. | |
9 |
|
9 | |||
10 | import contextlib |
|
10 | import contextlib | |
11 | import os |
|
11 | import os | |
12 |
|
12 | |||
13 | from ..node import ( |
|
13 | from ..node import ( | |
14 | nullrev, |
|
14 | nullrev, | |
15 | ) |
|
15 | ) | |
16 | from .constants import ( |
|
16 | from .constants import ( | |
17 | COMP_MODE_PLAIN, |
|
17 | COMP_MODE_PLAIN, | |
18 | ENTRY_DATA_COMPRESSED_LENGTH, |
|
18 | ENTRY_DATA_COMPRESSED_LENGTH, | |
19 | ENTRY_DATA_COMPRESSION_MODE, |
|
19 | ENTRY_DATA_COMPRESSION_MODE, | |
20 | ENTRY_DATA_OFFSET, |
|
20 | ENTRY_DATA_OFFSET, | |
21 | ENTRY_DATA_UNCOMPRESSED_LENGTH, |
|
21 | ENTRY_DATA_UNCOMPRESSED_LENGTH, | |
22 | ENTRY_DELTA_BASE, |
|
22 | ENTRY_DELTA_BASE, | |
23 | ENTRY_LINK_REV, |
|
23 | ENTRY_LINK_REV, | |
24 | ENTRY_NODE_ID, |
|
24 | ENTRY_NODE_ID, | |
25 | ENTRY_PARENT_1, |
|
25 | ENTRY_PARENT_1, | |
26 | ENTRY_PARENT_2, |
|
26 | ENTRY_PARENT_2, | |
27 | ENTRY_SIDEDATA_COMPRESSED_LENGTH, |
|
27 | ENTRY_SIDEDATA_COMPRESSED_LENGTH, | |
28 | ENTRY_SIDEDATA_COMPRESSION_MODE, |
|
28 | ENTRY_SIDEDATA_COMPRESSION_MODE, | |
29 | ENTRY_SIDEDATA_OFFSET, |
|
29 | ENTRY_SIDEDATA_OFFSET, | |
30 | REVLOGV0, |
|
30 | REVLOGV0, | |
31 | REVLOGV1, |
|
31 | REVLOGV1, | |
32 | ) |
|
32 | ) | |
33 | from ..i18n import _ |
|
33 | from ..i18n import _ | |
34 |
|
34 | |||
35 | from .. import ( |
|
35 | from .. import ( | |
36 | error, |
|
36 | error, | |
37 | pycompat, |
|
37 | pycompat, | |
38 | revlogutils, |
|
38 | revlogutils, | |
39 | util, |
|
39 | util, | |
40 | ) |
|
40 | ) | |
41 | from ..utils import ( |
|
41 | from ..utils import ( | |
42 | storageutil, |
|
42 | storageutil, | |
43 | ) |
|
43 | ) | |
44 | from . import ( |
|
44 | from . import ( | |
45 | constants, |
|
45 | constants, | |
46 | deltas, |
|
46 | deltas, | |
47 | ) |
|
47 | ) | |
48 |
|
48 | |||
49 |
|
49 | |||
50 | def v1_censor(rl, tr, censornode, tombstone=b''): |
|
50 | def v1_censor(rl, tr, censornode, tombstone=b''): | |
51 | """censors a revision in a "version 1" revlog""" |
|
51 | """censors a revision in a "version 1" revlog""" | |
52 | assert rl._format_version == constants.REVLOGV1, rl._format_version |
|
52 | assert rl._format_version == constants.REVLOGV1, rl._format_version | |
53 |
|
53 | |||
54 | # avoid cycle |
|
54 | # avoid cycle | |
55 | from .. import revlog |
|
55 | from .. import revlog | |
56 |
|
56 | |||
57 | censorrev = rl.rev(censornode) |
|
57 | censorrev = rl.rev(censornode) | |
58 | tombstone = storageutil.packmeta({b'censored': tombstone}, b'') |
|
58 | tombstone = storageutil.packmeta({b'censored': tombstone}, b'') | |
59 |
|
59 | |||
60 | # Rewriting the revlog in place is hard. Our strategy for censoring is |
|
60 | # Rewriting the revlog in place is hard. Our strategy for censoring is | |
61 | # to create a new revlog, copy all revisions to it, then replace the |
|
61 | # to create a new revlog, copy all revisions to it, then replace the | |
62 | # revlogs on transaction close. |
|
62 | # revlogs on transaction close. | |
63 | # |
|
63 | # | |
64 | # This is a bit dangerous. We could easily have a mismatch of state. |
|
64 | # This is a bit dangerous. We could easily have a mismatch of state. | |
65 | newrl = revlog.revlog( |
|
65 | newrl = revlog.revlog( | |
66 | rl.opener, |
|
66 | rl.opener, | |
67 | target=rl.target, |
|
67 | target=rl.target, | |
68 | radix=rl.radix, |
|
68 | radix=rl.radix, | |
69 | postfix=b'tmpcensored', |
|
69 | postfix=b'tmpcensored', | |
70 | censorable=True, |
|
70 | censorable=True, | |
71 | ) |
|
71 | ) | |
72 | newrl._format_version = rl._format_version |
|
72 | newrl._format_version = rl._format_version | |
73 | newrl._format_flags = rl._format_flags |
|
73 | newrl._format_flags = rl._format_flags | |
74 | newrl._generaldelta = rl._generaldelta |
|
74 | newrl._generaldelta = rl._generaldelta | |
75 | newrl._parse_index = rl._parse_index |
|
75 | newrl._parse_index = rl._parse_index | |
76 |
|
76 | |||
77 | for rev in rl.revs(): |
|
77 | for rev in rl.revs(): | |
78 | node = rl.node(rev) |
|
78 | node = rl.node(rev) | |
79 | p1, p2 = rl.parents(node) |
|
79 | p1, p2 = rl.parents(node) | |
80 |
|
80 | |||
81 | if rev == censorrev: |
|
81 | if rev == censorrev: | |
82 | newrl.addrawrevision( |
|
82 | newrl.addrawrevision( | |
83 | tombstone, |
|
83 | tombstone, | |
84 | tr, |
|
84 | tr, | |
85 | rl.linkrev(censorrev), |
|
85 | rl.linkrev(censorrev), | |
86 | p1, |
|
86 | p1, | |
87 | p2, |
|
87 | p2, | |
88 | censornode, |
|
88 | censornode, | |
89 | constants.REVIDX_ISCENSORED, |
|
89 | constants.REVIDX_ISCENSORED, | |
90 | ) |
|
90 | ) | |
91 |
|
91 | |||
92 | if newrl.deltaparent(rev) != nullrev: |
|
92 | if newrl.deltaparent(rev) != nullrev: | |
93 | m = _(b'censored revision stored as delta; cannot censor') |
|
93 | m = _(b'censored revision stored as delta; cannot censor') | |
94 | h = _( |
|
94 | h = _( | |
95 | b'censoring of revlogs is not fully implemented;' |
|
95 | b'censoring of revlogs is not fully implemented;' | |
96 | b' please report this bug' |
|
96 | b' please report this bug' | |
97 | ) |
|
97 | ) | |
98 | raise error.Abort(m, hint=h) |
|
98 | raise error.Abort(m, hint=h) | |
99 | continue |
|
99 | continue | |
100 |
|
100 | |||
101 | if rl.iscensored(rev): |
|
101 | if rl.iscensored(rev): | |
102 | if rl.deltaparent(rev) != nullrev: |
|
102 | if rl.deltaparent(rev) != nullrev: | |
103 | m = _( |
|
103 | m = _( | |
104 | b'cannot censor due to censored ' |
|
104 | b'cannot censor due to censored ' | |
105 | b'revision having delta stored' |
|
105 | b'revision having delta stored' | |
106 | ) |
|
106 | ) | |
107 | raise error.Abort(m) |
|
107 | raise error.Abort(m) | |
108 | rawtext = rl._chunk(rev) |
|
108 | rawtext = rl._chunk(rev) | |
109 | else: |
|
109 | else: | |
110 | rawtext = rl.rawdata(rev) |
|
110 | rawtext = rl.rawdata(rev) | |
111 |
|
111 | |||
112 | newrl.addrawrevision( |
|
112 | newrl.addrawrevision( | |
113 | rawtext, tr, rl.linkrev(rev), p1, p2, node, rl.flags(rev) |
|
113 | rawtext, tr, rl.linkrev(rev), p1, p2, node, rl.flags(rev) | |
114 | ) |
|
114 | ) | |
115 |
|
115 | |||
116 | tr.addbackup(rl._indexfile, location=b'store') |
|
116 | tr.addbackup(rl._indexfile, location=b'store') | |
117 | if not rl._inline: |
|
117 | if not rl._inline: | |
118 | tr.addbackup(rl._datafile, location=b'store') |
|
118 | tr.addbackup(rl._datafile, location=b'store') | |
119 |
|
119 | |||
120 | rl.opener.rename(newrl._indexfile, rl._indexfile) |
|
120 | rl.opener.rename(newrl._indexfile, rl._indexfile) | |
121 | if not rl._inline: |
|
121 | if not rl._inline: | |
122 | rl.opener.rename(newrl._datafile, rl._datafile) |
|
122 | rl.opener.rename(newrl._datafile, rl._datafile) | |
123 |
|
123 | |||
124 | rl.clearcaches() |
|
124 | rl.clearcaches() | |
125 | rl._loadindex() |
|
125 | rl._loadindex() | |
126 |
|
126 | |||
127 |
|
127 | |||
128 | def v2_censor(rl, tr, censornode, tombstone=b''): |
|
128 | def v2_censor(rl, tr, censornode, tombstone=b''): | |
129 | """censors a revision in a "version 2" revlog""" |
|
129 | """censors a revision in a "version 2" revlog""" | |
130 | # General principle |
|
130 | # General principle | |
131 | # |
|
131 | # | |
132 | # We create new revlog files (index/data/sidedata) to copy the content of |
|
132 | # We create new revlog files (index/data/sidedata) to copy the content of | |
133 | # the existing data without the censored data. |
|
133 | # the existing data without the censored data. | |
134 | # |
|
134 | # | |
135 | # We need to recompute new delta for any revision that used the censored |
|
135 | # We need to recompute new delta for any revision that used the censored | |
136 | # revision as delta base. As the cumulative size of the new delta may be |
|
136 | # revision as delta base. As the cumulative size of the new delta may be | |
137 | # large, we store them in a temporary file until they are stored in their |
|
137 | # large, we store them in a temporary file until they are stored in their | |
138 | # final destination. |
|
138 | # final destination. | |
139 | # |
|
139 | # | |
140 | # All data before the censored data can be blindly copied. The rest needs |
|
140 | # All data before the censored data can be blindly copied. The rest needs | |
141 | # to be copied as we go and the associated index entry needs adjustement. |
|
141 | # to be copied as we go and the associated index entry needs adjustement. | |
142 |
|
142 | |||
143 | assert rl._format_version != REVLOGV0, rl._format_version |
|
143 | assert rl._format_version != REVLOGV0, rl._format_version | |
144 | assert rl._format_version != REVLOGV1, rl._format_version |
|
144 | assert rl._format_version != REVLOGV1, rl._format_version | |
145 |
|
145 | |||
146 | old_index = rl.index |
|
146 | old_index = rl.index | |
147 | docket = rl._docket |
|
147 | docket = rl._docket | |
148 |
|
148 | |||
149 | censor_rev = rl.rev(censornode) |
|
149 | censor_rev = rl.rev(censornode) | |
150 | tombstone = storageutil.packmeta({b'censored': tombstone}, b'') |
|
150 | tombstone = storageutil.packmeta({b'censored': tombstone}, b'') | |
151 |
|
151 | |||
152 | censored_entry = rl.index[censor_rev] |
|
152 | censored_entry = rl.index[censor_rev] | |
153 | index_cutoff = rl.index.entry_size * censor_rev |
|
153 | index_cutoff = rl.index.entry_size * censor_rev | |
154 | data_cutoff = censored_entry[ENTRY_DATA_OFFSET] >> 16 |
|
154 | data_cutoff = censored_entry[ENTRY_DATA_OFFSET] >> 16 | |
155 | sidedata_cutoff = rl.sidedata_cut_off(censor_rev) |
|
155 | sidedata_cutoff = rl.sidedata_cut_off(censor_rev) | |
156 |
|
156 | |||
157 | # rev β (new_base, data_start, data_end) |
|
157 | # rev β (new_base, data_start, data_end) | |
158 | rewritten_entries = {} |
|
158 | rewritten_entries = {} | |
159 |
|
159 | |||
160 | dc = deltas.deltacomputer(rl) |
|
160 | dc = deltas.deltacomputer(rl) | |
161 | excl = [censor_rev] |
|
161 | excl = [censor_rev] | |
162 |
|
162 | |||
163 | with pycompat.unnamedtempfile(mode=b"w+b") as tmp_storage: |
|
163 | with pycompat.unnamedtempfile(mode=b"w+b") as tmp_storage: | |
164 | with rl._segmentfile._open_read() as dfh: |
|
164 | with rl._segmentfile._open_read() as dfh: | |
165 | for rev in range(censor_rev + 1, len(old_index)): |
|
165 | for rev in range(censor_rev + 1, len(old_index)): | |
166 | entry = old_index[rev] |
|
166 | entry = old_index[rev] | |
167 | if censor_rev != entry[ENTRY_DELTA_BASE]: |
|
167 | if censor_rev != entry[ENTRY_DELTA_BASE]: | |
168 | continue |
|
168 | continue | |
169 | # This is a revision that use the censored revision as the base |
|
169 | # This is a revision that use the censored revision as the base | |
170 | # for its delta. We need a need new deltas |
|
170 | # for its delta. We need a need new deltas | |
171 | if entry[ENTRY_DATA_UNCOMPRESSED_LENGTH] == 0: |
|
171 | if entry[ENTRY_DATA_UNCOMPRESSED_LENGTH] == 0: | |
172 | # this revision is empty, we can delta against nullrev |
|
172 | # this revision is empty, we can delta against nullrev | |
173 | rewritten_entries[rev] = (nullrev, 0, 0) |
|
173 | rewritten_entries[rev] = (nullrev, 0, 0) | |
174 | else: |
|
174 | else: | |
175 |
|
175 | |||
176 | text = rl.rawdata(rev, _df=dfh) |
|
176 | text = rl.rawdata(rev, _df=dfh) | |
177 | info = revlogutils.revisioninfo( |
|
177 | info = revlogutils.revisioninfo( | |
178 | node=entry[ENTRY_NODE_ID], |
|
178 | node=entry[ENTRY_NODE_ID], | |
179 | p1=rl.node(entry[ENTRY_PARENT_1]), |
|
179 | p1=rl.node(entry[ENTRY_PARENT_1]), | |
180 | p2=rl.node(entry[ENTRY_PARENT_2]), |
|
180 | p2=rl.node(entry[ENTRY_PARENT_2]), | |
181 | btext=[text], |
|
181 | btext=[text], | |
182 | textlen=len(text), |
|
182 | textlen=len(text), | |
183 | cachedelta=None, |
|
183 | cachedelta=None, | |
184 | flags=entry[ENTRY_DATA_OFFSET] & 0xFFFF, |
|
184 | flags=entry[ENTRY_DATA_OFFSET] & 0xFFFF, | |
185 | ) |
|
185 | ) | |
186 | d = dc.finddeltainfo( |
|
186 | d = dc.finddeltainfo( | |
187 | info, dfh, excluded_bases=excl, target_rev=rev |
|
187 | info, dfh, excluded_bases=excl, target_rev=rev | |
188 | ) |
|
188 | ) | |
189 | default_comp = rl._docket.default_compression_header |
|
189 | default_comp = rl._docket.default_compression_header | |
190 | comp_mode, d = deltas.delta_compression(default_comp, d) |
|
190 | comp_mode, d = deltas.delta_compression(default_comp, d) | |
191 | # using `tell` is a bit lazy, but we are not here for speed |
|
191 | # using `tell` is a bit lazy, but we are not here for speed | |
192 | start = tmp_storage.tell() |
|
192 | start = tmp_storage.tell() | |
193 | tmp_storage.write(d.data[1]) |
|
193 | tmp_storage.write(d.data[1]) | |
194 | end = tmp_storage.tell() |
|
194 | end = tmp_storage.tell() | |
195 | rewritten_entries[rev] = (d.base, start, end, comp_mode) |
|
195 | rewritten_entries[rev] = (d.base, start, end, comp_mode) | |
196 |
|
196 | |||
197 | old_index_filepath = rl.opener.join(docket.index_filepath()) |
|
197 | old_index_filepath = rl.opener.join(docket.index_filepath()) | |
198 | old_data_filepath = rl.opener.join(docket.data_filepath()) |
|
198 | old_data_filepath = rl.opener.join(docket.data_filepath()) | |
199 | old_sidedata_filepath = rl.opener.join(docket.sidedata_filepath()) |
|
199 | old_sidedata_filepath = rl.opener.join(docket.sidedata_filepath()) | |
200 |
|
200 | |||
201 | new_index_filepath = rl.opener.join(docket.new_index_file()) |
|
201 | new_index_filepath = rl.opener.join(docket.new_index_file()) | |
202 | new_data_filepath = rl.opener.join(docket.new_data_file()) |
|
202 | new_data_filepath = rl.opener.join(docket.new_data_file()) | |
203 | new_sidedata_filepath = rl.opener.join(docket.new_sidedata_file()) |
|
203 | new_sidedata_filepath = rl.opener.join(docket.new_sidedata_file()) | |
204 |
|
204 | |||
205 | util.copyfile( |
|
205 | util.copyfile( | |
206 | old_index_filepath, new_index_filepath, nb_bytes=index_cutoff |
|
206 | old_index_filepath, new_index_filepath, nb_bytes=index_cutoff | |
207 | ) |
|
207 | ) | |
208 | util.copyfile( |
|
208 | util.copyfile( | |
209 | old_data_filepath, new_data_filepath, nb_bytes=data_cutoff |
|
209 | old_data_filepath, new_data_filepath, nb_bytes=data_cutoff | |
210 | ) |
|
210 | ) | |
211 | util.copyfile( |
|
211 | util.copyfile( | |
212 | old_sidedata_filepath, |
|
212 | old_sidedata_filepath, | |
213 | new_sidedata_filepath, |
|
213 | new_sidedata_filepath, | |
214 | nb_bytes=sidedata_cutoff, |
|
214 | nb_bytes=sidedata_cutoff, | |
215 | ) |
|
215 | ) | |
216 | rl.opener.register_file(docket.index_filepath()) |
|
216 | rl.opener.register_file(docket.index_filepath()) | |
217 | rl.opener.register_file(docket.data_filepath()) |
|
217 | rl.opener.register_file(docket.data_filepath()) | |
218 | rl.opener.register_file(docket.sidedata_filepath()) |
|
218 | rl.opener.register_file(docket.sidedata_filepath()) | |
219 |
|
219 | |||
220 | docket.index_end = index_cutoff |
|
220 | docket.index_end = index_cutoff | |
221 | docket.data_end = data_cutoff |
|
221 | docket.data_end = data_cutoff | |
222 | docket.sidedata_end = sidedata_cutoff |
|
222 | docket.sidedata_end = sidedata_cutoff | |
223 |
|
223 | |||
224 | # reload the revlog internal information |
|
224 | # reload the revlog internal information | |
225 | rl.clearcaches() |
|
225 | rl.clearcaches() | |
226 | rl._loadindex(docket=docket) |
|
226 | rl._loadindex(docket=docket) | |
227 |
|
227 | |||
228 | @contextlib.contextmanager |
|
228 | @contextlib.contextmanager | |
229 | def all_files(): |
|
229 | def all_files(): | |
230 | # hide opening in an helper function to please check-code, black |
|
230 | # hide opening in an helper function to please check-code, black | |
231 | # and various python ersion at the same time |
|
231 | # and various python ersion at the same time | |
232 | with open(old_data_filepath, 'rb') as old_data_file: |
|
232 | with open(old_data_filepath, 'rb') as old_data_file: | |
233 | with open(old_sidedata_filepath, 'rb') as old_sidedata_file: |
|
233 | with open(old_sidedata_filepath, 'rb') as old_sidedata_file: | |
234 | with open(new_index_filepath, 'r+b') as new_index_file: |
|
234 | with open(new_index_filepath, 'r+b') as new_index_file: | |
235 | with open(new_data_filepath, 'r+b') as new_data_file: |
|
235 | with open(new_data_filepath, 'r+b') as new_data_file: | |
236 | with open( |
|
236 | with open( | |
237 | new_sidedata_filepath, 'r+b' |
|
237 | new_sidedata_filepath, 'r+b' | |
238 | ) as new_sidedata_file: |
|
238 | ) as new_sidedata_file: | |
239 | yield ( |
|
239 | yield ( | |
240 | old_data_file, |
|
240 | old_data_file, | |
241 | old_sidedata_file, |
|
241 | old_sidedata_file, | |
242 | new_index_file, |
|
242 | new_index_file, | |
243 | new_data_file, |
|
243 | new_data_file, | |
244 | new_sidedata_file, |
|
244 | new_sidedata_file, | |
245 | ) |
|
245 | ) | |
246 |
|
246 | |||
247 | # we dont need to open the old index file since its content already |
|
247 | # we dont need to open the old index file since its content already | |
248 | # exist in a usable form in `old_index`. |
|
248 | # exist in a usable form in `old_index`. | |
249 | with all_files() as open_files: |
|
249 | with all_files() as open_files: | |
250 | ( |
|
250 | ( | |
251 | old_data_file, |
|
251 | old_data_file, | |
252 | old_sidedata_file, |
|
252 | old_sidedata_file, | |
253 | new_index_file, |
|
253 | new_index_file, | |
254 | new_data_file, |
|
254 | new_data_file, | |
255 | new_sidedata_file, |
|
255 | new_sidedata_file, | |
256 | ) = open_files |
|
256 | ) = open_files | |
257 | new_index_file.seek(0, os.SEEK_END) |
|
257 | new_index_file.seek(0, os.SEEK_END) | |
258 | assert new_index_file.tell() == index_cutoff |
|
258 | assert new_index_file.tell() == index_cutoff | |
259 | new_data_file.seek(0, os.SEEK_END) |
|
259 | new_data_file.seek(0, os.SEEK_END) | |
260 | assert new_data_file.tell() == data_cutoff |
|
260 | assert new_data_file.tell() == data_cutoff | |
261 | new_sidedata_file.seek(0, os.SEEK_END) |
|
261 | new_sidedata_file.seek(0, os.SEEK_END) | |
262 | assert new_sidedata_file.tell() == sidedata_cutoff |
|
262 | assert new_sidedata_file.tell() == sidedata_cutoff | |
263 |
|
263 | |||
264 | # writing the censored revision |
|
264 | # writing the censored revision | |
265 | _rewrite_censor( |
|
265 | _rewrite_censor( | |
266 | rl, |
|
266 | rl, | |
267 | old_index, |
|
267 | old_index, | |
268 | open_files, |
|
268 | open_files, | |
269 | censor_rev, |
|
269 | censor_rev, | |
270 | tombstone, |
|
270 | tombstone, | |
271 | ) |
|
271 | ) | |
272 |
|
272 | |||
273 |
|
|
273 | # Writing all subsequent revisions | |
274 | for rev in range(censor_rev + 1, len(old_index)): |
|
274 | for rev in range(censor_rev + 1, len(old_index)): | |
275 | entry = old_index[rev] |
|
275 | _rewrite_simple( | |
276 | flags = entry[ENTRY_DATA_OFFSET] & 0xFFFF |
|
276 | rl, | |
277 | old_data_offset = entry[ENTRY_DATA_OFFSET] >> 16 |
|
277 | old_index, | |
|
278 | open_files, | |||
|
279 | rev, | |||
|
280 | rewritten_entries, | |||
|
281 | tmp_storage, | |||
|
282 | ) | |||
|
283 | docket.write(transaction=None, stripping=True) | |||
|
284 | ||||
278 |
|
285 | |||
279 | if rev not in rewritten_entries: |
|
286 | def _rewrite_simple( | |
280 | old_data_file.seek(old_data_offset) |
|
287 | revlog, | |
281 | new_data_size = entry[ENTRY_DATA_COMPRESSED_LENGTH] |
|
288 | old_index, | |
282 | new_data = old_data_file.read(new_data_size) |
|
289 | all_files, | |
283 | data_delta_base = entry[ENTRY_DELTA_BASE] |
|
290 | rev, | |
284 | d_comp_mode = entry[ENTRY_DATA_COMPRESSION_MODE] |
|
291 | rewritten_entries, | |
285 | else: |
|
292 | tmp_storage, | |
286 | ( |
|
293 | ): | |
287 | data_delta_base, |
|
294 | """append a normal revision to the index after the rewritten one(s)""" | |
288 | start, |
|
295 | ( | |
289 | end, |
|
296 | old_data_file, | |
290 | d_comp_mode, |
|
297 | old_sidedata_file, | |
291 | ) = rewritten_entries[rev] |
|
298 | new_index_file, | |
292 | new_data_size = end - start |
|
299 | new_data_file, | |
293 | tmp_storage.seek(start) |
|
300 | new_sidedata_file, | |
294 | new_data = tmp_storage.read(new_data_size) |
|
301 | ) = all_files | |
295 |
|
302 | entry = old_index[rev] | ||
296 | # It might be faster to group continuous read/write operation, |
|
303 | flags = entry[ENTRY_DATA_OFFSET] & 0xFFFF | |
297 | # however, this is censor, an operation that is not focussed |
|
304 | old_data_offset = entry[ENTRY_DATA_OFFSET] >> 16 | |
298 | # around stellar performance. So I have not written this |
|
|||
299 | # optimisation yet. |
|
|||
300 | new_data_offset = new_data_file.tell() |
|
|||
301 | new_data_file.write(new_data) |
|
|||
302 |
|
305 | |||
303 | sidedata_size = entry[ENTRY_SIDEDATA_COMPRESSED_LENGTH] |
|
306 | if rev not in rewritten_entries: | |
304 | new_sidedata_offset = new_sidedata_file.tell() |
|
307 | old_data_file.seek(old_data_offset) | |
305 | if 0 < sidedata_size: |
|
308 | new_data_size = entry[ENTRY_DATA_COMPRESSED_LENGTH] | |
306 | old_sidedata_offset = entry[ENTRY_SIDEDATA_OFFSET] |
|
309 | new_data = old_data_file.read(new_data_size) | |
307 | old_sidedata_file.seek(old_sidedata_offset) |
|
310 | data_delta_base = entry[ENTRY_DELTA_BASE] | |
308 | new_sidedata = old_sidedata_file.read(sidedata_size) |
|
311 | d_comp_mode = entry[ENTRY_DATA_COMPRESSION_MODE] | |
309 | new_sidedata_file.write(new_sidedata) |
|
312 | else: | |
|
313 | ( | |||
|
314 | data_delta_base, | |||
|
315 | start, | |||
|
316 | end, | |||
|
317 | d_comp_mode, | |||
|
318 | ) = rewritten_entries[rev] | |||
|
319 | new_data_size = end - start | |||
|
320 | tmp_storage.seek(start) | |||
|
321 | new_data = tmp_storage.read(new_data_size) | |||
310 |
|
322 | |||
311 | data_uncompressed_length = entry[ENTRY_DATA_UNCOMPRESSED_LENGTH] |
|
323 | # It might be faster to group continuous read/write operation, | |
312 | sd_com_mode = entry[ENTRY_SIDEDATA_COMPRESSION_MODE] |
|
324 | # however, this is censor, an operation that is not focussed | |
313 | assert data_delta_base <= rev, (data_delta_base, rev) |
|
325 | # around stellar performance. So I have not written this | |
|
326 | # optimisation yet. | |||
|
327 | new_data_offset = new_data_file.tell() | |||
|
328 | new_data_file.write(new_data) | |||
314 |
|
329 | |||
315 | new_entry = revlogutils.entry( |
|
330 | sidedata_size = entry[ENTRY_SIDEDATA_COMPRESSED_LENGTH] | |
316 | flags=flags, |
|
331 | new_sidedata_offset = new_sidedata_file.tell() | |
317 | data_offset=new_data_offset, |
|
332 | if 0 < sidedata_size: | |
318 | data_compressed_length=new_data_size, |
|
333 | old_sidedata_offset = entry[ENTRY_SIDEDATA_OFFSET] | |
319 | data_uncompressed_length=data_uncompressed_length, |
|
334 | old_sidedata_file.seek(old_sidedata_offset) | |
320 | data_delta_base=data_delta_base, |
|
335 | new_sidedata = old_sidedata_file.read(sidedata_size) | |
321 | link_rev=entry[ENTRY_LINK_REV], |
|
336 | new_sidedata_file.write(new_sidedata) | |
322 | parent_rev_1=entry[ENTRY_PARENT_1], |
|
337 | ||
323 | parent_rev_2=entry[ENTRY_PARENT_2], |
|
338 | data_uncompressed_length = entry[ENTRY_DATA_UNCOMPRESSED_LENGTH] | |
324 | node_id=entry[ENTRY_NODE_ID], |
|
339 | sd_com_mode = entry[ENTRY_SIDEDATA_COMPRESSION_MODE] | |
325 | sidedata_offset=new_sidedata_offset, |
|
340 | assert data_delta_base <= rev, (data_delta_base, rev) | |
326 | sidedata_compressed_length=sidedata_size, |
|
|||
327 | data_compression_mode=d_comp_mode, |
|
|||
328 | sidedata_compression_mode=sd_com_mode, |
|
|||
329 | ) |
|
|||
330 | rl.index.append(new_entry) |
|
|||
331 | entry_bin = rl.index.entry_binary(rev) |
|
|||
332 | new_index_file.write(entry_bin) |
|
|||
333 |
|
341 | |||
334 | docket.index_end = new_index_file.tell() |
|
342 | new_entry = revlogutils.entry( | |
335 | docket.data_end = new_data_file.tell() |
|
343 | flags=flags, | |
336 | docket.sidedata_end = new_sidedata_file.tell() |
|
344 | data_offset=new_data_offset, | |
|
345 | data_compressed_length=new_data_size, | |||
|
346 | data_uncompressed_length=data_uncompressed_length, | |||
|
347 | data_delta_base=data_delta_base, | |||
|
348 | link_rev=entry[ENTRY_LINK_REV], | |||
|
349 | parent_rev_1=entry[ENTRY_PARENT_1], | |||
|
350 | parent_rev_2=entry[ENTRY_PARENT_2], | |||
|
351 | node_id=entry[ENTRY_NODE_ID], | |||
|
352 | sidedata_offset=new_sidedata_offset, | |||
|
353 | sidedata_compressed_length=sidedata_size, | |||
|
354 | data_compression_mode=d_comp_mode, | |||
|
355 | sidedata_compression_mode=sd_com_mode, | |||
|
356 | ) | |||
|
357 | revlog.index.append(new_entry) | |||
|
358 | entry_bin = revlog.index.entry_binary(rev) | |||
|
359 | new_index_file.write(entry_bin) | |||
337 |
|
360 | |||
338 | docket.write(transaction=None, stripping=True) |
|
361 | revlog._docket.index_end = new_index_file.tell() | |
|
362 | revlog._docket.data_end = new_data_file.tell() | |||
|
363 | revlog._docket.sidedata_end = new_sidedata_file.tell() | |||
339 |
|
364 | |||
340 |
|
365 | |||
341 | def _rewrite_censor( |
|
366 | def _rewrite_censor( | |
342 | revlog, |
|
367 | revlog, | |
343 | old_index, |
|
368 | old_index, | |
344 | all_files, |
|
369 | all_files, | |
345 | rev, |
|
370 | rev, | |
346 | tombstone, |
|
371 | tombstone, | |
347 | ): |
|
372 | ): | |
348 | """rewrite and append a censored revision""" |
|
373 | """rewrite and append a censored revision""" | |
349 | ( |
|
374 | ( | |
350 | old_data_file, |
|
375 | old_data_file, | |
351 | old_sidedata_file, |
|
376 | old_sidedata_file, | |
352 | new_index_file, |
|
377 | new_index_file, | |
353 | new_data_file, |
|
378 | new_data_file, | |
354 | new_sidedata_file, |
|
379 | new_sidedata_file, | |
355 | ) = all_files |
|
380 | ) = all_files | |
356 | entry = old_index[rev] |
|
381 | entry = old_index[rev] | |
357 |
|
382 | |||
358 | # XXX consider trying the default compression too |
|
383 | # XXX consider trying the default compression too | |
359 | new_data_size = len(tombstone) |
|
384 | new_data_size = len(tombstone) | |
360 | new_data_offset = new_data_file.tell() |
|
385 | new_data_offset = new_data_file.tell() | |
361 | new_data_file.write(tombstone) |
|
386 | new_data_file.write(tombstone) | |
362 |
|
387 | |||
363 | # we are not adding any sidedata as they might leak info about the censored version |
|
388 | # we are not adding any sidedata as they might leak info about the censored version | |
364 |
|
389 | |||
365 | link_rev = entry[ENTRY_LINK_REV] |
|
390 | link_rev = entry[ENTRY_LINK_REV] | |
366 |
|
391 | |||
367 | p1 = entry[ENTRY_PARENT_1] |
|
392 | p1 = entry[ENTRY_PARENT_1] | |
368 | p2 = entry[ENTRY_PARENT_2] |
|
393 | p2 = entry[ENTRY_PARENT_2] | |
369 |
|
394 | |||
370 | new_entry = revlogutils.entry( |
|
395 | new_entry = revlogutils.entry( | |
371 | flags=constants.REVIDX_ISCENSORED, |
|
396 | flags=constants.REVIDX_ISCENSORED, | |
372 | data_offset=new_data_offset, |
|
397 | data_offset=new_data_offset, | |
373 | data_compressed_length=new_data_size, |
|
398 | data_compressed_length=new_data_size, | |
374 | data_uncompressed_length=new_data_size, |
|
399 | data_uncompressed_length=new_data_size, | |
375 | data_delta_base=rev, |
|
400 | data_delta_base=rev, | |
376 | link_rev=link_rev, |
|
401 | link_rev=link_rev, | |
377 | parent_rev_1=p1, |
|
402 | parent_rev_1=p1, | |
378 | parent_rev_2=p2, |
|
403 | parent_rev_2=p2, | |
379 | node_id=entry[ENTRY_NODE_ID], |
|
404 | node_id=entry[ENTRY_NODE_ID], | |
380 | sidedata_offset=0, |
|
405 | sidedata_offset=0, | |
381 | sidedata_compressed_length=0, |
|
406 | sidedata_compressed_length=0, | |
382 | data_compression_mode=COMP_MODE_PLAIN, |
|
407 | data_compression_mode=COMP_MODE_PLAIN, | |
383 | sidedata_compression_mode=COMP_MODE_PLAIN, |
|
408 | sidedata_compression_mode=COMP_MODE_PLAIN, | |
384 | ) |
|
409 | ) | |
385 | revlog.index.append(new_entry) |
|
410 | revlog.index.append(new_entry) | |
386 | entry_bin = revlog.index.entry_binary(rev) |
|
411 | entry_bin = revlog.index.entry_binary(rev) | |
387 | new_index_file.write(entry_bin) |
|
412 | new_index_file.write(entry_bin) | |
388 | revlog._docket.index_end = new_index_file.tell() |
|
413 | revlog._docket.index_end = new_index_file.tell() | |
389 | revlog._docket.data_end = new_data_file.tell() |
|
414 | revlog._docket.data_end = new_data_file.tell() |
General Comments 0
You need to be logged in to leave comments.
Login now