]>
git.saurik.com Git - apt.git/blob - methods/rred.cc
2 #include <apt-pkg/fileutl.h>
3 #include <apt-pkg/mmap.h>
4 #include <apt-pkg/error.h>
5 #include <apt-pkg/acquire-method.h>
6 #include <apt-pkg/strutl.h>
7 #include <apt-pkg/hashes.h>
18 /** \brief RredMethod - ed-style incremential patch method {{{
20 * This method implements a patch functionality similar to "patch --ed" that is
21 * used by the "tiffany" incremental packages download stuff. It differs from
22 * "ed" insofar that it is way more restricted (and therefore secure).
23 * The currently supported ed commands are "<em>c</em>hange", "<em>a</em>dd" and
24 * "<em>d</em>elete" (diff doesn't output any other).
25 * Additionally the records must be reverse sorted by line number and
26 * may not overlap (diff *seems* to produce this kind of output).
28 class RredMethod
: public pkgAcqMethod
{
30 // the size of this doesn't really matter (except for performance)
31 const static int BUF_SIZE
= 1024;
32 // the supported ed commands
33 enum Mode
{MODE_CHANGED
='c', MODE_DELETED
='d', MODE_ADDED
='a'};
35 enum State
{ED_OK
, ED_ORDERING
, ED_PARSER
, ED_FAILURE
, MMAP_FAILED
};
37 State
applyFile(gzFile
&ed_cmds
, FILE *in_file
, FILE *out_file
,
38 unsigned long &line
, char *buffer
, Hashes
*hash
) const;
39 void ignoreLineInFile(FILE *fin
, char *buffer
) const;
40 void ignoreLineInFile(gzFile
&fin
, char *buffer
) const;
41 void copyLinesFromFileToFile(FILE *fin
, FILE *fout
, unsigned int lines
,
42 Hashes
*hash
, char *buffer
) const;
43 void copyLinesFromFileToFile(gzFile
&fin
, FILE *fout
, unsigned int lines
,
44 Hashes
*hash
, char *buffer
) const;
46 State
patchFile(FileFd
&Patch
, FileFd
&From
, FileFd
&out_file
, Hashes
*hash
) const;
47 State
patchMMap(FileFd
&Patch
, FileFd
&From
, FileFd
&out_file
, Hashes
*hash
) const;
50 // the methods main method
51 virtual bool Fetch(FetchItem
*Itm
);
54 RredMethod() : pkgAcqMethod("1.1",SingleInstance
| SendConfig
), Debug(false) {};
57 /** \brief applyFile - in reverse order with a tail recursion {{{
59 * As it is expected that the commands are in reversed order in the patch file
60 * we check in the first half if the command is valid, but doesn't execute it
61 * and move a step deeper. After reaching the end of the file we apply the
62 * patches in the correct order: last found command first.
64 * \param ed_cmds patch file to apply
65 * \param in_file base file we want to patch
66 * \param out_file file to write the patched result to
67 * \param line of command operation
68 * \param buffer internal used read/write buffer
69 * \param hash the created file for correctness
70 * \return the success State of the ed command executor
72 RredMethod::State
RredMethod::applyFile(gzFile
&ed_cmds
, FILE *in_file
, FILE *out_file
,
73 unsigned long &line
, char *buffer
, Hashes
*hash
) const {
74 // get the current command and parse it
75 if (gzgets(ed_cmds
, buffer
, BUF_SIZE
) == NULL
) {
77 std::clog
<< "rred: encounter end of file - we can start patching now." << std::endl
;
82 // parse in the effected linenumbers
85 unsigned long const startline
= strtol(buffer
, &idx
, 10);
86 if (errno
== ERANGE
|| errno
== EINVAL
) {
87 _error
->Errno("rred", "startline is an invalid number");
90 if (startline
> line
) {
91 _error
->Error("rred: The start line (%lu) of the next command is higher than the last line (%lu). This is not allowed.", startline
, line
);
94 unsigned long stopline
;
98 stopline
= strtol(idx
, &idx
, 10);
99 if (errno
== ERANGE
|| errno
== EINVAL
) {
100 _error
->Errno("rred", "stopline is an invalid number");
105 stopline
= startline
;
109 // which command to execute on this line(s)?
113 std::clog
<< "Change from line " << startline
<< " to " << stopline
<< std::endl
;
117 std::clog
<< "Insert after line " << startline
<< std::endl
;
121 std::clog
<< "Delete from line " << startline
<< " to " << stopline
<< std::endl
;
124 _error
->Error("rred: Unknown ed command '%c'. Abort.", *idx
);
127 unsigned char mode
= *idx
;
129 // save the current position
130 unsigned const long pos
= gztell(ed_cmds
);
132 // if this is add or change then go to the next full stop
133 unsigned int data_length
= 0;
134 if (mode
== MODE_CHANGED
|| mode
== MODE_ADDED
) {
136 ignoreLineInFile(ed_cmds
, buffer
);
139 while (strncmp(buffer
, ".", 1) != 0);
140 data_length
--; // the dot should not be copied
143 // do the recursive call - the last command is the one we need to execute at first
144 const State child
= applyFile(ed_cmds
, in_file
, out_file
, line
, buffer
, hash
);
145 if (child
!= ED_OK
) {
149 // change and delete are working on "line" - add is done after "line"
150 if (mode
!= MODE_ADDED
)
153 // first wind to the current position and copy over all unchanged lines
154 if (line
< startline
) {
155 copyLinesFromFileToFile(in_file
, out_file
, (startline
- line
), hash
, buffer
);
159 if (mode
!= MODE_ADDED
)
162 // include data from ed script
163 if (mode
== MODE_CHANGED
|| mode
== MODE_ADDED
) {
164 gzseek(ed_cmds
, pos
, SEEK_SET
);
165 copyLinesFromFileToFile(ed_cmds
, out_file
, data_length
, hash
, buffer
);
168 // ignore the corresponding number of lines from input
169 if (mode
== MODE_CHANGED
|| mode
== MODE_DELETED
) {
170 while (line
< stopline
) {
171 ignoreLineInFile(in_file
, buffer
);
178 void RredMethod::copyLinesFromFileToFile(FILE *fin
, FILE *fout
, unsigned int lines
,/*{{{*/
179 Hashes
*hash
, char *buffer
) const {
180 while (0 < lines
--) {
182 fgets(buffer
, BUF_SIZE
, fin
);
183 size_t const written
= fwrite(buffer
, 1, strlen(buffer
), fout
);
184 hash
->Add((unsigned char*)buffer
, written
);
185 } while (strlen(buffer
) == (BUF_SIZE
- 1) &&
186 buffer
[BUF_SIZE
- 2] != '\n');
190 void RredMethod::copyLinesFromFileToFile(gzFile
&fin
, FILE *fout
, unsigned int lines
,/*{{{*/
191 Hashes
*hash
, char *buffer
) const {
192 while (0 < lines
--) {
194 gzgets(fin
, buffer
, BUF_SIZE
);
195 size_t const written
= fwrite(buffer
, 1, strlen(buffer
), fout
);
196 hash
->Add((unsigned char*)buffer
, written
);
197 } while (strlen(buffer
) == (BUF_SIZE
- 1) &&
198 buffer
[BUF_SIZE
- 2] != '\n');
202 void RredMethod::ignoreLineInFile(FILE *fin
, char *buffer
) const { /*{{{*/
203 fgets(buffer
, BUF_SIZE
, fin
);
204 while (strlen(buffer
) == (BUF_SIZE
- 1) &&
205 buffer
[BUF_SIZE
- 2] != '\n') {
206 fgets(buffer
, BUF_SIZE
, fin
);
211 void RredMethod::ignoreLineInFile(gzFile
&fin
, char *buffer
) const { /*{{{*/
212 gzgets(fin
, buffer
, BUF_SIZE
);
213 while (strlen(buffer
) == (BUF_SIZE
- 1) &&
214 buffer
[BUF_SIZE
- 2] != '\n') {
215 gzgets(fin
, buffer
, BUF_SIZE
);
220 RredMethod::State
RredMethod::patchFile(FileFd
&Patch
, FileFd
&From
, /*{{{*/
221 FileFd
&out_file
, Hashes
*hash
) const {
222 char buffer
[BUF_SIZE
];
223 FILE* fFrom
= fdopen(From
.Fd(), "r");
224 gzFile fPatch
= Patch
.gzFd();
225 FILE* fTo
= fdopen(out_file
.Fd(), "w");
227 /* we do a tail recursion to read the commands in the right order */
228 unsigned long line
= -1; // assign highest possible value
229 State
const result
= applyFile(fPatch
, fFrom
, fTo
, line
, buffer
, hash
);
231 /* read the rest from infile */
232 if (result
== ED_OK
) {
233 while (fgets(buffer
, BUF_SIZE
, fFrom
) != NULL
) {
234 size_t const written
= fwrite(buffer
, 1, strlen(buffer
), fTo
);
235 hash
->Add((unsigned char*)buffer
, written
);
242 /* struct EdCommand {{{*/
243 #ifdef _POSIX_MAPPED_FILES
252 #define IOV_COUNT 1024 /* Don't really want IOV_MAX since it can be arbitrarily large */
255 RredMethod::State
RredMethod::patchMMap(FileFd
&Patch
, FileFd
&From
, /*{{{*/
256 FileFd
&out_file
, Hashes
*hash
) const {
257 #ifdef _POSIX_MAPPED_FILES
258 MMap
ed_cmds(MMap::ReadOnly
);
259 if (Patch
.gzFd() != NULL
) {
260 unsigned long mapSize
= Patch
.Size();
261 DynamicMMap
* dyn
= new DynamicMMap(0, mapSize
, 0);
262 if (dyn
->validData() == false) {
266 dyn
->AddSize(mapSize
);
267 gzread(Patch
.gzFd(), dyn
->Data(), mapSize
);
270 ed_cmds
= MMap(Patch
, MMap::ReadOnly
);
272 MMap
in_file(From
, MMap::ReadOnly
);
274 if (ed_cmds
.Size() == 0 || in_file
.Size() == 0)
277 EdCommand
* commands
= 0;
278 size_t command_count
= 0;
279 size_t command_alloc
= 0;
281 const char* begin
= (char*) ed_cmds
.Data();
282 const char* end
= begin
;
283 const char* ed_end
= (char*) ed_cmds
.Data() + ed_cmds
.Size();
285 const char* input
= (char*) in_file
.Data();
286 const char* input_end
= (char*) in_file
.Data() + in_file
.Size();
290 /* 1. Parse entire script. It is executed in reverse order, so we cather it
291 * in the `commands' buffer first
299 while(begin
!= ed_end
&& *begin
== '\n')
301 while(end
!= ed_end
&& *end
!= '\n')
303 if(end
== ed_end
&& begin
== end
)
306 /* Determine command range */
307 const char* tmp
= begin
;
310 /* atoll is safe despite lacking NUL-termination; we know there's an
311 * alphabetic character at end[-1]
314 cmd
.first_line
= atol(begin
);
315 cmd
.last_line
= cmd
.first_line
;
319 cmd
.first_line
= atol(begin
);
320 cmd
.last_line
= atol(tmp
+ 1);
326 // which command to execute on this line(s)?
330 std::clog
<< "Change from line " << cmd
.first_line
<< " to " << cmd
.last_line
<< std::endl
;
334 std::clog
<< "Insert after line " << cmd
.first_line
<< std::endl
;
338 std::clog
<< "Delete from line " << cmd
.first_line
<< " to " << cmd
.last_line
<< std::endl
;
341 _error
->Error("rred: Unknown ed command '%c'. Abort.", end
[-1]);
347 /* Determine the size of the inserted text, so we don't have to scan this
354 if(cmd
.type
== MODE_ADDED
|| cmd
.type
== MODE_CHANGED
) {
355 cmd
.data_start
= begin
- (char*) ed_cmds
.Data();
356 while(end
!= ed_end
) {
358 if(end
[-1] == '.' && end
[-2] == '\n')
364 cmd
.data_end
= end
- (char*) ed_cmds
.Data() - 1;
368 if(command_count
== command_alloc
) {
369 command_alloc
= (command_alloc
+ 64) * 3 / 2;
370 commands
= (EdCommand
*) realloc(commands
, command_alloc
* sizeof(EdCommand
));
372 commands
[command_count
++] = cmd
;
375 struct iovec
* iov
= new struct iovec
[IOV_COUNT
];
378 size_t amount
, remaining
;
382 /* 2. Execute script. We gather writes in a `struct iov' array, and flush
383 * using writev to minimize the number of system calls. Data is read
384 * directly from the memory mappings of the input file and the script.
387 for(i
= command_count
; i
-- > 0; ) {
389 if(cmd
->type
== MODE_ADDED
)
390 amount
= cmd
->first_line
+ 1;
392 amount
= cmd
->first_line
;
396 while(line
!= amount
) {
397 input
= (const char*) memchr(input
, '\n', input_end
- input
);
404 iov
[iov_size
].iov_base
= (void*) begin
;
405 iov
[iov_size
].iov_len
= input
- begin
;
406 hash
->Add((const unsigned char*) begin
, input
- begin
);
408 if(++iov_size
== IOV_COUNT
) {
409 writev(out_file
.Fd(), iov
, IOV_COUNT
);
414 if(cmd
->type
== MODE_DELETED
|| cmd
->type
== MODE_CHANGED
) {
415 remaining
= (cmd
->last_line
- cmd
->first_line
) + 1;
418 input
= (const char*) memchr(input
, '\n', input_end
- input
);
426 if(cmd
->type
== MODE_CHANGED
|| cmd
->type
== MODE_ADDED
) {
427 if(cmd
->data_end
!= cmd
->data_start
) {
428 iov
[iov_size
].iov_base
= (void*) ((char*)ed_cmds
.Data() + cmd
->data_start
);
429 iov
[iov_size
].iov_len
= cmd
->data_end
- cmd
->data_start
;
430 hash
->Add((const unsigned char*) ((char*)ed_cmds
.Data() + cmd
->data_start
),
431 iov
[iov_size
].iov_len
);
433 if(++iov_size
== IOV_COUNT
) {
434 writev(out_file
.Fd(), iov
, IOV_COUNT
);
441 if(input
!= input_end
) {
442 iov
[iov_size
].iov_base
= (void*) input
;
443 iov
[iov_size
].iov_len
= input_end
- input
;
444 hash
->Add((const unsigned char*) input
, input_end
- input
);
449 writev(out_file
.Fd(), iov
, iov_size
);
453 for(i
= 0; i
< iov_size
; i
+= IOV_COUNT
) {
454 if(iov_size
- i
< IOV_COUNT
)
455 writev(out_file
.Fd(), iov
+ i
, iov_size
- i
);
457 writev(out_file
.Fd(), iov
+ i
, IOV_COUNT
);
469 bool RredMethod::Fetch(FetchItem
*Itm
) /*{{{*/
471 Debug
= _config
->FindB("Debug::pkgAcquire::RRed", false);
473 string Path
= Get
.Host
+ Get
.Path
; // To account for relative paths
476 Res
.Filename
= Itm
->DestFile
;
477 if (Itm
->Uri
.empty() == true) {
478 Path
= Itm
->DestFile
;
479 Itm
->DestFile
.append(".result");
484 std::clog
<< "Patching " << Path
<< " with " << Path
485 << ".ed and putting result into " << Itm
->DestFile
<< std::endl
;
486 // Open the source and destination files (the d'tor of FileFd will do
487 // the cleanup/closing of the fds)
488 FileFd
From(Path
,FileFd::ReadOnly
);
489 FileFd
Patch(Path
+".ed",FileFd::ReadOnlyGzip
);
490 FileFd
To(Itm
->DestFile
,FileFd::WriteAtomic
);
492 if (_error
->PendingError() == true)
496 // now do the actual patching
497 State
const result
= patchMMap(Patch
, From
, To
, &Hash
);
498 if (result
== MMAP_FAILED
) {
499 // retry with patchFile
502 To
.Open(Itm
->DestFile
,FileFd::WriteAtomic
);
503 if (_error
->PendingError() == true)
505 if (patchFile(Patch
, From
, To
, &Hash
) != ED_OK
) {
506 return _error
->WarningE("rred", _("Could not patch %s with mmap and with file operation usage - the patch seems to be corrupt."), Path
.c_str());
507 } else if (Debug
== true) {
508 std::clog
<< "rred: finished file patching of " << Path
<< " after mmap failed." << std::endl
;
510 } else if (result
!= ED_OK
) {
511 return _error
->Errno("rred", _("Could not patch %s with mmap (but no mmap specific fail) - the patch seems to be corrupt."), Path
.c_str());
512 } else if (Debug
== true) {
513 std::clog
<< "rred: finished mmap patching of " << Path
<< std::endl
;
516 // write out the result
521 /* Transfer the modification times from the patch file
522 to be able to see in which state the file should be
523 and use the access time from the "old" file */
524 struct stat BufBase
, BufPatch
;
525 if (stat(Path
.c_str(),&BufBase
) != 0 ||
526 stat(string(Path
+".ed").c_str(),&BufPatch
) != 0)
527 return _error
->Errno("stat",_("Failed to stat"));
529 struct utimbuf TimeBuf
;
530 TimeBuf
.actime
= BufBase
.st_atime
;
531 TimeBuf
.modtime
= BufPatch
.st_mtime
;
532 if (utime(Itm
->DestFile
.c_str(),&TimeBuf
) != 0)
533 return _error
->Errno("utime",_("Failed to set modification time"));
535 if (stat(Itm
->DestFile
.c_str(),&BufBase
) != 0)
536 return _error
->Errno("stat",_("Failed to stat"));
539 Res
.LastModified
= BufBase
.st_mtime
;
540 Res
.Size
= BufBase
.st_size
;
541 Res
.TakeHashes(Hash
);
547 /** \brief Wrapper class for testing rred */ /*{{{*/
548 class TestRredMethod
: public RredMethod
{
550 /** \brief Run rred in debug test mode
552 * This method can be used to run the rred method outside
553 * of the "normal" acquire environment for easier testing.
555 * \param base basename of all files involved in this rred test
557 bool Run(char const *base
) {
558 _config
->CndSet("Debug::pkgAcquire::RRed", "true");
559 FetchItem
*test
= new FetchItem
;
560 test
->DestFile
= base
;
565 /** \brief Starter for the rred method (or its test method) {{{
567 * Used without parameters is the normal behavior for methods for
568 * the APT acquire system. While this works great for the acquire system
569 * it is very hard to test the method and therefore the method also
570 * accepts one parameter which will switch it directly to debug test mode:
571 * The test mode expects that if "Testfile" is given as parameter
572 * the file "Testfile" should be ed-style patched with "Testfile.ed"
573 * and will write the result to "Testfile.result".
575 int main(int argc
, char *argv
[]) {
581 bool result
= Mth
.Run(argv
[1]);
582 _error
->DumpErrors();