]>
git.saurik.com Git - apt.git/blob - methods/rred.cc
4 #include <apt-pkg/fileutl.h>
5 #include <apt-pkg/mmap.h>
6 #include <apt-pkg/error.h>
7 #include <apt-pkg/acquire-method.h>
8 #include <apt-pkg/strutl.h>
9 #include <apt-pkg/hashes.h>
10 #include <apt-pkg/configuration.h>
21 /** \brief RredMethod - ed-style incremential patch method {{{
23 * This method implements a patch functionality similar to "patch --ed" that is
24 * used by the "tiffany" incremental packages download stuff. It differs from
25 * "ed" insofar that it is way more restricted (and therefore secure).
26 * The currently supported ed commands are "<em>c</em>hange", "<em>a</em>dd" and
27 * "<em>d</em>elete" (diff doesn't output any other).
28 * Additionally the records must be reverse sorted by line number and
29 * may not overlap (diff *seems* to produce this kind of output).
31 class RredMethod
: public pkgAcqMethod
{
33 // the size of this doesn't really matter (except for performance)
34 const static int BUF_SIZE
= 1024;
35 // the supported ed commands
36 enum Mode
{MODE_CHANGED
='c', MODE_DELETED
='d', MODE_ADDED
='a'};
38 enum State
{ED_OK
, ED_ORDERING
, ED_PARSER
, ED_FAILURE
, MMAP_FAILED
};
40 State
applyFile(FileFd
&ed_cmds
, FILE *in_file
, FILE *out_file
,
41 unsigned long &line
, char *buffer
, Hashes
*hash
) const;
42 void ignoreLineInFile(FILE *fin
, char *buffer
) const;
43 void ignoreLineInFile(FileFd
&fin
, char *buffer
) const;
44 void copyLinesFromFileToFile(FILE *fin
, FILE *fout
, unsigned int lines
,
45 Hashes
*hash
, char *buffer
) const;
46 void copyLinesFromFileToFile(FileFd
&fin
, FILE *fout
, unsigned int lines
,
47 Hashes
*hash
, char *buffer
) const;
49 State
patchFile(FileFd
&Patch
, FileFd
&From
, FileFd
&out_file
, Hashes
*hash
) const;
50 State
patchMMap(FileFd
&Patch
, FileFd
&From
, FileFd
&out_file
, Hashes
*hash
) const;
53 // the methods main method
54 virtual bool Fetch(FetchItem
*Itm
);
57 RredMethod() : pkgAcqMethod("1.1",SingleInstance
| SendConfig
), Debug(false) {};
60 /** \brief applyFile - in reverse order with a tail recursion {{{
62 * As it is expected that the commands are in reversed order in the patch file
63 * we check in the first half if the command is valid, but doesn't execute it
64 * and move a step deeper. After reaching the end of the file we apply the
65 * patches in the correct order: last found command first.
67 * \param ed_cmds patch file to apply
68 * \param in_file base file we want to patch
69 * \param out_file file to write the patched result to
70 * \param line of command operation
71 * \param buffer internal used read/write buffer
72 * \param hash the created file for correctness
73 * \return the success State of the ed command executor
75 RredMethod::State
RredMethod::applyFile(FileFd
&ed_cmds
, FILE *in_file
, FILE *out_file
,
76 unsigned long &line
, char *buffer
, Hashes
*hash
) const {
77 // get the current command and parse it
78 if (ed_cmds
.ReadLine(buffer
, BUF_SIZE
) == NULL
) {
80 std::clog
<< "rred: encounter end of file - we can start patching now." << std::endl
;
85 // parse in the effected linenumbers
88 unsigned long const startline
= strtol(buffer
, &idx
, 10);
89 if (errno
== ERANGE
|| errno
== EINVAL
) {
90 _error
->Errno("rred", "startline is an invalid number");
93 if (startline
> line
) {
94 _error
->Error("rred: The start line (%lu) of the next command is higher than the last line (%lu). This is not allowed.", startline
, line
);
97 unsigned long stopline
;
101 stopline
= strtol(idx
, &idx
, 10);
102 if (errno
== ERANGE
|| errno
== EINVAL
) {
103 _error
->Errno("rred", "stopline is an invalid number");
108 stopline
= startline
;
112 // which command to execute on this line(s)?
116 std::clog
<< "Change from line " << startline
<< " to " << stopline
<< std::endl
;
120 std::clog
<< "Insert after line " << startline
<< std::endl
;
124 std::clog
<< "Delete from line " << startline
<< " to " << stopline
<< std::endl
;
127 _error
->Error("rred: Unknown ed command '%c'. Abort.", *idx
);
130 unsigned char mode
= *idx
;
132 // save the current position
133 unsigned const long long pos
= ed_cmds
.Tell();
135 // if this is add or change then go to the next full stop
136 unsigned int data_length
= 0;
137 if (mode
== MODE_CHANGED
|| mode
== MODE_ADDED
) {
139 ignoreLineInFile(ed_cmds
, buffer
);
142 while (strncmp(buffer
, ".", 1) != 0);
143 data_length
--; // the dot should not be copied
146 // do the recursive call - the last command is the one we need to execute at first
147 const State child
= applyFile(ed_cmds
, in_file
, out_file
, line
, buffer
, hash
);
148 if (child
!= ED_OK
) {
152 // change and delete are working on "line" - add is done after "line"
153 if (mode
!= MODE_ADDED
)
156 // first wind to the current position and copy over all unchanged lines
157 if (line
< startline
) {
158 copyLinesFromFileToFile(in_file
, out_file
, (startline
- line
), hash
, buffer
);
162 if (mode
!= MODE_ADDED
)
165 // include data from ed script
166 if (mode
== MODE_CHANGED
|| mode
== MODE_ADDED
) {
168 copyLinesFromFileToFile(ed_cmds
, out_file
, data_length
, hash
, buffer
);
171 // ignore the corresponding number of lines from input
172 if (mode
== MODE_CHANGED
|| mode
== MODE_DELETED
) {
173 while (line
< stopline
) {
174 ignoreLineInFile(in_file
, buffer
);
181 void RredMethod::copyLinesFromFileToFile(FILE *fin
, FILE *fout
, unsigned int lines
,/*{{{*/
182 Hashes
*hash
, char *buffer
) const {
183 while (0 < lines
--) {
185 fgets(buffer
, BUF_SIZE
, fin
);
186 size_t const written
= fwrite(buffer
, 1, strlen(buffer
), fout
);
187 hash
->Add((unsigned char*)buffer
, written
);
188 } while (strlen(buffer
) == (BUF_SIZE
- 1) &&
189 buffer
[BUF_SIZE
- 2] != '\n');
193 void RredMethod::copyLinesFromFileToFile(FileFd
&fin
, FILE *fout
, unsigned int lines
,/*{{{*/
194 Hashes
*hash
, char *buffer
) const {
195 while (0 < lines
--) {
197 fin
.ReadLine(buffer
, BUF_SIZE
);
198 size_t const written
= fwrite(buffer
, 1, strlen(buffer
), fout
);
199 hash
->Add((unsigned char*)buffer
, written
);
200 } while (strlen(buffer
) == (BUF_SIZE
- 1) &&
201 buffer
[BUF_SIZE
- 2] != '\n');
205 void RredMethod::ignoreLineInFile(FILE *fin
, char *buffer
) const { /*{{{*/
206 fgets(buffer
, BUF_SIZE
, fin
);
207 while (strlen(buffer
) == (BUF_SIZE
- 1) &&
208 buffer
[BUF_SIZE
- 2] != '\n') {
209 fgets(buffer
, BUF_SIZE
, fin
);
214 void RredMethod::ignoreLineInFile(FileFd
&fin
, char *buffer
) const { /*{{{*/
215 fin
.ReadLine(buffer
, BUF_SIZE
);
216 while (strlen(buffer
) == (BUF_SIZE
- 1) &&
217 buffer
[BUF_SIZE
- 2] != '\n') {
218 fin
.ReadLine(buffer
, BUF_SIZE
);
223 RredMethod::State
RredMethod::patchFile(FileFd
&Patch
, FileFd
&From
, /*{{{*/
224 FileFd
&out_file
, Hashes
*hash
) const {
225 char buffer
[BUF_SIZE
];
226 FILE* fFrom
= fdopen(From
.Fd(), "r");
227 FILE* fTo
= fdopen(out_file
.Fd(), "w");
229 /* we do a tail recursion to read the commands in the right order */
230 unsigned long line
= -1; // assign highest possible value
231 State
const result
= applyFile(Patch
, fFrom
, fTo
, line
, buffer
, hash
);
233 /* read the rest from infile */
234 if (result
== ED_OK
) {
235 while (fgets(buffer
, BUF_SIZE
, fFrom
) != NULL
) {
236 size_t const written
= fwrite(buffer
, 1, strlen(buffer
), fTo
);
237 hash
->Add((unsigned char*)buffer
, written
);
244 /* struct EdCommand {{{*/
245 #ifdef _POSIX_MAPPED_FILES
254 #define IOV_COUNT 1024 /* Don't really want IOV_MAX since it can be arbitrarily large */
257 RredMethod::State
RredMethod::patchMMap(FileFd
&Patch
, FileFd
&From
, /*{{{*/
258 FileFd
&out_file
, Hashes
*hash
) const {
259 #ifdef _POSIX_MAPPED_FILES
260 MMap
ed_cmds(Patch
, MMap::ReadOnly
);
261 MMap
in_file(From
, MMap::ReadOnly
);
263 if (ed_cmds
.Size() == 0 || in_file
.Size() == 0)
266 EdCommand
* commands
= 0;
267 size_t command_count
= 0;
268 size_t command_alloc
= 0;
270 const char* begin
= (char*) ed_cmds
.Data();
271 const char* end
= begin
;
272 const char* ed_end
= (char*) ed_cmds
.Data() + ed_cmds
.Size();
274 const char* input
= (char*) in_file
.Data();
275 const char* input_end
= (char*) in_file
.Data() + in_file
.Size();
279 /* 1. Parse entire script. It is executed in reverse order, so we cather it
280 * in the `commands' buffer first
288 while(begin
!= ed_end
&& *begin
== '\n')
290 while(end
!= ed_end
&& *end
!= '\n')
292 if(end
== ed_end
&& begin
== end
)
295 /* Determine command range */
296 const char* tmp
= begin
;
299 /* atoll is safe despite lacking NUL-termination; we know there's an
300 * alphabetic character at end[-1]
303 cmd
.first_line
= atol(begin
);
304 cmd
.last_line
= cmd
.first_line
;
308 cmd
.first_line
= atol(begin
);
309 cmd
.last_line
= atol(tmp
+ 1);
315 // which command to execute on this line(s)?
319 std::clog
<< "Change from line " << cmd
.first_line
<< " to " << cmd
.last_line
<< std::endl
;
323 std::clog
<< "Insert after line " << cmd
.first_line
<< std::endl
;
327 std::clog
<< "Delete from line " << cmd
.first_line
<< " to " << cmd
.last_line
<< std::endl
;
330 _error
->Error("rred: Unknown ed command '%c'. Abort.", end
[-1]);
336 /* Determine the size of the inserted text, so we don't have to scan this
343 if(cmd
.type
== MODE_ADDED
|| cmd
.type
== MODE_CHANGED
) {
344 cmd
.data_start
= begin
- (char*) ed_cmds
.Data();
345 while(end
!= ed_end
) {
347 if(end
[-1] == '.' && end
[-2] == '\n')
353 cmd
.data_end
= end
- (char*) ed_cmds
.Data() - 1;
357 if(command_count
== command_alloc
) {
358 command_alloc
= (command_alloc
+ 64) * 3 / 2;
359 commands
= (EdCommand
*) realloc(commands
, command_alloc
* sizeof(EdCommand
));
361 commands
[command_count
++] = cmd
;
364 struct iovec
* iov
= new struct iovec
[IOV_COUNT
];
367 size_t amount
, remaining
;
371 /* 2. Execute script. We gather writes in a `struct iov' array, and flush
372 * using writev to minimize the number of system calls. Data is read
373 * directly from the memory mappings of the input file and the script.
376 for(i
= command_count
; i
-- > 0; ) {
378 if(cmd
->type
== MODE_ADDED
)
379 amount
= cmd
->first_line
+ 1;
381 amount
= cmd
->first_line
;
385 while(line
!= amount
) {
386 input
= (const char*) memchr(input
, '\n', input_end
- input
);
393 iov
[iov_size
].iov_base
= (void*) begin
;
394 iov
[iov_size
].iov_len
= input
- begin
;
395 hash
->Add((const unsigned char*) begin
, input
- begin
);
397 if(++iov_size
== IOV_COUNT
) {
398 writev(out_file
.Fd(), iov
, IOV_COUNT
);
403 if(cmd
->type
== MODE_DELETED
|| cmd
->type
== MODE_CHANGED
) {
404 remaining
= (cmd
->last_line
- cmd
->first_line
) + 1;
407 input
= (const char*) memchr(input
, '\n', input_end
- input
);
415 if(cmd
->type
== MODE_CHANGED
|| cmd
->type
== MODE_ADDED
) {
416 if(cmd
->data_end
!= cmd
->data_start
) {
417 iov
[iov_size
].iov_base
= (void*) ((char*)ed_cmds
.Data() + cmd
->data_start
);
418 iov
[iov_size
].iov_len
= cmd
->data_end
- cmd
->data_start
;
419 hash
->Add((const unsigned char*) ((char*)ed_cmds
.Data() + cmd
->data_start
),
420 iov
[iov_size
].iov_len
);
422 if(++iov_size
== IOV_COUNT
) {
423 writev(out_file
.Fd(), iov
, IOV_COUNT
);
430 if(input
!= input_end
) {
431 iov
[iov_size
].iov_base
= (void*) input
;
432 iov
[iov_size
].iov_len
= input_end
- input
;
433 hash
->Add((const unsigned char*) input
, input_end
- input
);
438 writev(out_file
.Fd(), iov
, iov_size
);
442 for(i
= 0; i
< iov_size
; i
+= IOV_COUNT
) {
443 if(iov_size
- i
< IOV_COUNT
)
444 writev(out_file
.Fd(), iov
+ i
, iov_size
- i
);
446 writev(out_file
.Fd(), iov
+ i
, IOV_COUNT
);
458 bool RredMethod::Fetch(FetchItem
*Itm
) /*{{{*/
460 Debug
= _config
->FindB("Debug::pkgAcquire::RRed", false);
462 std::string Path
= Get
.Host
+ Get
.Path
; // To account for relative paths
465 Res
.Filename
= Itm
->DestFile
;
466 if (Itm
->Uri
.empty() == true) {
467 Path
= Itm
->DestFile
;
468 Itm
->DestFile
.append(".result");
473 std::clog
<< "Patching " << Path
<< " with " << Path
474 << ".ed and putting result into " << Itm
->DestFile
<< std::endl
;
475 // Open the source and destination files (the d'tor of FileFd will do
476 // the cleanup/closing of the fds)
477 FileFd
From(Path
,FileFd::ReadOnly
);
478 FileFd
Patch(Path
+".ed",FileFd::ReadOnly
, FileFd::Gzip
);
479 FileFd
To(Itm
->DestFile
,FileFd::WriteAtomic
);
481 if (_error
->PendingError() == true)
485 // now do the actual patching
486 State
const result
= patchMMap(Patch
, From
, To
, &Hash
);
487 if (result
== MMAP_FAILED
) {
488 // retry with patchFile
491 To
.Open(Itm
->DestFile
,FileFd::WriteAtomic
);
492 if (_error
->PendingError() == true)
494 if (patchFile(Patch
, From
, To
, &Hash
) != ED_OK
) {
495 return _error
->WarningE("rred", _("Could not patch %s with mmap and with file operation usage - the patch seems to be corrupt."), Path
.c_str());
496 } else if (Debug
== true) {
497 std::clog
<< "rred: finished file patching of " << Path
<< " after mmap failed." << std::endl
;
499 } else if (result
!= ED_OK
) {
500 return _error
->Errno("rred", _("Could not patch %s with mmap (but no mmap specific fail) - the patch seems to be corrupt."), Path
.c_str());
501 } else if (Debug
== true) {
502 std::clog
<< "rred: finished mmap patching of " << Path
<< std::endl
;
505 // write out the result
510 /* Transfer the modification times from the patch file
511 to be able to see in which state the file should be
512 and use the access time from the "old" file */
513 struct stat BufBase
, BufPatch
;
514 if (stat(Path
.c_str(),&BufBase
) != 0 ||
515 stat(std::string(Path
+".ed").c_str(),&BufPatch
) != 0)
516 return _error
->Errno("stat",_("Failed to stat"));
518 struct utimbuf TimeBuf
;
519 TimeBuf
.actime
= BufBase
.st_atime
;
520 TimeBuf
.modtime
= BufPatch
.st_mtime
;
521 if (utime(Itm
->DestFile
.c_str(),&TimeBuf
) != 0)
522 return _error
->Errno("utime",_("Failed to set modification time"));
524 if (stat(Itm
->DestFile
.c_str(),&BufBase
) != 0)
525 return _error
->Errno("stat",_("Failed to stat"));
528 Res
.LastModified
= BufBase
.st_mtime
;
529 Res
.Size
= BufBase
.st_size
;
530 Res
.TakeHashes(Hash
);
536 /** \brief Wrapper class for testing rred */ /*{{{*/
537 class TestRredMethod
: public RredMethod
{
539 /** \brief Run rred in debug test mode
541 * This method can be used to run the rred method outside
542 * of the "normal" acquire environment for easier testing.
544 * \param base basename of all files involved in this rred test
546 bool Run(char const *base
) {
547 _config
->CndSet("Debug::pkgAcquire::RRed", "true");
548 FetchItem
*test
= new FetchItem
;
549 test
->DestFile
= base
;
554 /** \brief Starter for the rred method (or its test method) {{{
556 * Used without parameters is the normal behavior for methods for
557 * the APT acquire system. While this works great for the acquire system
558 * it is very hard to test the method and therefore the method also
559 * accepts one parameter which will switch it directly to debug test mode:
560 * The test mode expects that if "Testfile" is given as parameter
561 * the file "Testfile" should be ed-style patched with "Testfile.ed"
562 * and will write the result to "Testfile.result".
564 int main(int argc
, char *argv
[]) {
570 bool result
= Mth
.Run(argv
[1]);
571 _error
->DumpErrors();