]>
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
, FileFd
&in_file
, FileFd
&out_file
,
41 unsigned long &line
, char *buffer
, Hashes
*hash
) const;
42 void ignoreLineInFile(FileFd
&fin
, char *buffer
) const;
43 void copyLinesFromFileToFile(FileFd
&fin
, FileFd
&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(FileFd
&ed_cmds
, FileFd
&in_file
, FileFd
&out_file
,
73 unsigned long &line
, char *buffer
, Hashes
*hash
) const {
74 // get the current command and parse it
75 if (ed_cmds
.ReadLine(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 long pos
= ed_cmds
.Tell();
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
) {
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(FileFd
&fin
, FileFd
&fout
, unsigned int lines
,/*{{{*/
179 Hashes
*hash
, char *buffer
) const {
180 while (0 < lines
--) {
182 fin
.ReadLine(buffer
, BUF_SIZE
);
183 unsigned long long const towrite
= strlen(buffer
);
184 fout
.Write(buffer
, towrite
);
185 hash
->Add((unsigned char*)buffer
, towrite
);
186 } while (strlen(buffer
) == (BUF_SIZE
- 1) &&
187 buffer
[BUF_SIZE
- 2] != '\n');
191 void RredMethod::ignoreLineInFile(FileFd
&fin
, char *buffer
) const { /*{{{*/
192 fin
.ReadLine(buffer
, BUF_SIZE
);
193 while (strlen(buffer
) == (BUF_SIZE
- 1) &&
194 buffer
[BUF_SIZE
- 2] != '\n') {
195 fin
.ReadLine(buffer
, BUF_SIZE
);
200 RredMethod::State
RredMethod::patchFile(FileFd
&Patch
, FileFd
&From
, /*{{{*/
201 FileFd
&out_file
, Hashes
*hash
) const {
202 char buffer
[BUF_SIZE
];
204 /* we do a tail recursion to read the commands in the right order */
205 unsigned long line
= -1; // assign highest possible value
206 State
const result
= applyFile(Patch
, From
, out_file
, line
, buffer
, hash
);
208 /* read the rest from infile */
209 if (result
== ED_OK
) {
210 while (From
.ReadLine(buffer
, BUF_SIZE
) != NULL
) {
211 unsigned long long const towrite
= strlen(buffer
);
212 out_file
.Write(buffer
, towrite
);
213 hash
->Add((unsigned char*)buffer
, towrite
);
219 /* struct EdCommand {{{*/
220 #ifdef _POSIX_MAPPED_FILES
229 #define IOV_COUNT 1024 /* Don't really want IOV_MAX since it can be arbitrarily large */
232 RredMethod::State
RredMethod::patchMMap(FileFd
&Patch
, FileFd
&From
, /*{{{*/
233 FileFd
&out_file
, Hashes
*hash
) const {
234 #ifdef _POSIX_MAPPED_FILES
235 MMap
ed_cmds(Patch
, MMap::ReadOnly
);
236 MMap
in_file(From
, MMap::ReadOnly
);
238 unsigned long long const ed_size
= ed_cmds
.Size();
239 unsigned long long const in_size
= in_file
.Size();
240 if (ed_size
== 0 || in_size
== 0)
243 EdCommand
* commands
= 0;
244 size_t command_count
= 0;
245 size_t command_alloc
= 0;
247 const char* begin
= (char*) ed_cmds
.Data();
248 const char* end
= begin
;
249 const char* ed_end
= (char*) ed_cmds
.Data() + ed_size
;
251 const char* input
= (char*) in_file
.Data();
252 const char* input_end
= (char*) in_file
.Data() + in_size
;
256 /* 1. Parse entire script. It is executed in reverse order, so we cather it
257 * in the `commands' buffer first
265 while(begin
!= ed_end
&& *begin
== '\n')
267 while(end
!= ed_end
&& *end
!= '\n')
269 if(end
== ed_end
&& begin
== end
)
272 /* Determine command range */
273 const char* tmp
= begin
;
276 /* atoll is safe despite lacking NUL-termination; we know there's an
277 * alphabetic character at end[-1]
280 cmd
.first_line
= atol(begin
);
281 cmd
.last_line
= cmd
.first_line
;
285 cmd
.first_line
= atol(begin
);
286 cmd
.last_line
= atol(tmp
+ 1);
292 // which command to execute on this line(s)?
296 std::clog
<< "Change from line " << cmd
.first_line
<< " to " << cmd
.last_line
<< std::endl
;
300 std::clog
<< "Insert after line " << cmd
.first_line
<< std::endl
;
304 std::clog
<< "Delete from line " << cmd
.first_line
<< " to " << cmd
.last_line
<< std::endl
;
307 _error
->Error("rred: Unknown ed command '%c'. Abort.", end
[-1]);
313 /* Determine the size of the inserted text, so we don't have to scan this
320 if(cmd
.type
== MODE_ADDED
|| cmd
.type
== MODE_CHANGED
) {
321 cmd
.data_start
= begin
- (char*) ed_cmds
.Data();
322 while(end
!= ed_end
) {
324 if(end
[-1] == '.' && end
[-2] == '\n')
330 cmd
.data_end
= end
- (char*) ed_cmds
.Data() - 1;
334 if(command_count
== command_alloc
) {
335 command_alloc
= (command_alloc
+ 64) * 3 / 2;
336 EdCommand
* newCommands
= (EdCommand
*) realloc(commands
, command_alloc
* sizeof(EdCommand
));
337 if (newCommands
== NULL
) {
341 commands
= newCommands
;
343 commands
[command_count
++] = cmd
;
346 struct iovec
* iov
= new struct iovec
[IOV_COUNT
];
349 size_t amount
, remaining
;
353 /* 2. Execute script. We gather writes in a `struct iov' array, and flush
354 * using writev to minimize the number of system calls. Data is read
355 * directly from the memory mappings of the input file and the script.
358 for(i
= command_count
; i
-- > 0; ) {
360 if(cmd
->type
== MODE_ADDED
)
361 amount
= cmd
->first_line
+ 1;
363 amount
= cmd
->first_line
;
367 while(line
!= amount
) {
368 input
= (const char*) memchr(input
, '\n', input_end
- input
);
375 iov
[iov_size
].iov_base
= (void*) begin
;
376 iov
[iov_size
].iov_len
= input
- begin
;
377 hash
->Add((const unsigned char*) begin
, input
- begin
);
379 if(++iov_size
== IOV_COUNT
) {
380 writev(out_file
.Fd(), iov
, IOV_COUNT
);
385 if(cmd
->type
== MODE_DELETED
|| cmd
->type
== MODE_CHANGED
) {
386 remaining
= (cmd
->last_line
- cmd
->first_line
) + 1;
389 input
= (const char*) memchr(input
, '\n', input_end
- input
);
397 if(cmd
->type
== MODE_CHANGED
|| cmd
->type
== MODE_ADDED
) {
398 if(cmd
->data_end
!= cmd
->data_start
) {
399 iov
[iov_size
].iov_base
= (void*) ((char*)ed_cmds
.Data() + cmd
->data_start
);
400 iov
[iov_size
].iov_len
= cmd
->data_end
- cmd
->data_start
;
401 hash
->Add((const unsigned char*) ((char*)ed_cmds
.Data() + cmd
->data_start
),
402 iov
[iov_size
].iov_len
);
404 if(++iov_size
== IOV_COUNT
) {
405 writev(out_file
.Fd(), iov
, IOV_COUNT
);
412 if(input
!= input_end
) {
413 iov
[iov_size
].iov_base
= (void*) input
;
414 iov
[iov_size
].iov_len
= input_end
- input
;
415 hash
->Add((const unsigned char*) input
, input_end
- input
);
420 writev(out_file
.Fd(), iov
, iov_size
);
424 for(i
= 0; i
< iov_size
; i
+= IOV_COUNT
) {
425 if(iov_size
- i
< IOV_COUNT
)
426 writev(out_file
.Fd(), iov
+ i
, iov_size
- i
);
428 writev(out_file
.Fd(), iov
+ i
, IOV_COUNT
);
440 bool RredMethod::Fetch(FetchItem
*Itm
) /*{{{*/
442 Debug
= _config
->FindB("Debug::pkgAcquire::RRed", false);
444 std::string Path
= Get
.Host
+ Get
.Path
; // To account for relative paths
447 Res
.Filename
= Itm
->DestFile
;
448 if (Itm
->Uri
.empty() == true) {
449 Path
= Itm
->DestFile
;
450 Itm
->DestFile
.append(".result");
455 std::clog
<< "Patching " << Path
<< " with " << Path
456 << ".ed and putting result into " << Itm
->DestFile
<< std::endl
;
457 // Open the source and destination files (the d'tor of FileFd will do
458 // the cleanup/closing of the fds)
459 FileFd
From(Path
,FileFd::ReadOnly
);
460 FileFd
Patch(Path
+".ed",FileFd::ReadOnly
, FileFd::Gzip
);
461 FileFd
To(Itm
->DestFile
,FileFd::WriteAtomic
);
463 if (_error
->PendingError() == true)
467 // now do the actual patching
468 State
const result
= patchMMap(Patch
, From
, To
, &Hash
);
469 if (result
== MMAP_FAILED
) {
470 // retry with patchFile
473 To
.Open(Itm
->DestFile
,FileFd::WriteAtomic
);
474 if (_error
->PendingError() == true)
476 if (patchFile(Patch
, From
, To
, &Hash
) != ED_OK
) {
477 return _error
->WarningE("rred", _("Could not patch %s with mmap and with file operation usage - the patch seems to be corrupt."), Path
.c_str());
478 } else if (Debug
== true) {
479 std::clog
<< "rred: finished file patching of " << Path
<< " after mmap failed." << std::endl
;
481 } else if (result
!= ED_OK
) {
482 return _error
->Errno("rred", _("Could not patch %s with mmap (but no mmap specific fail) - the patch seems to be corrupt."), Path
.c_str());
483 } else if (Debug
== true) {
484 std::clog
<< "rred: finished mmap patching of " << Path
<< std::endl
;
487 // write out the result
492 /* Transfer the modification times from the patch file
493 to be able to see in which state the file should be
494 and use the access time from the "old" file */
495 struct stat BufBase
, BufPatch
;
496 if (stat(Path
.c_str(),&BufBase
) != 0 ||
497 stat(std::string(Path
+".ed").c_str(),&BufPatch
) != 0)
498 return _error
->Errno("stat",_("Failed to stat"));
500 struct utimbuf TimeBuf
;
501 TimeBuf
.actime
= BufBase
.st_atime
;
502 TimeBuf
.modtime
= BufPatch
.st_mtime
;
503 if (utime(Itm
->DestFile
.c_str(),&TimeBuf
) != 0)
504 return _error
->Errno("utime",_("Failed to set modification time"));
506 if (stat(Itm
->DestFile
.c_str(),&BufBase
) != 0)
507 return _error
->Errno("stat",_("Failed to stat"));
510 Res
.LastModified
= BufBase
.st_mtime
;
511 Res
.Size
= BufBase
.st_size
;
512 Res
.TakeHashes(Hash
);
518 /** \brief Wrapper class for testing rred */ /*{{{*/
519 class TestRredMethod
: public RredMethod
{
521 /** \brief Run rred in debug test mode
523 * This method can be used to run the rred method outside
524 * of the "normal" acquire environment for easier testing.
526 * \param base basename of all files involved in this rred test
528 bool Run(char const *base
) {
529 _config
->CndSet("Debug::pkgAcquire::RRed", "true");
530 FetchItem
*test
= new FetchItem
;
531 test
->DestFile
= base
;
536 /** \brief Starter for the rred method (or its test method) {{{
538 * Used without parameters is the normal behavior for methods for
539 * the APT acquire system. While this works great for the acquire system
540 * it is very hard to test the method and therefore the method also
541 * accepts one parameter which will switch it directly to debug test mode:
542 * The test mode expects that if "Testfile" is given as parameter
543 * the file "Testfile" should be ed-style patched with "Testfile.ed"
544 * and will write the result to "Testfile.result".
546 int main(int argc
, char *argv
[]) {
552 bool result
= Mth
.Run(argv
[1]);
553 _error
->DumpErrors();