]>
git.saurik.com Git - apt.git/blob - methods/rred.cc
1 // Copyright (c) 2014 Anthony Towns
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 2 of the License, or
6 // (at your option) any later version.
10 #include <apt-pkg/fileutl.h>
11 #include <apt-pkg/mmap.h>
12 #include <apt-pkg/error.h>
13 #include <apt-pkg/acquire-method.h>
14 #include <apt-pkg/strutl.h>
15 #include <apt-pkg/hashes.h>
16 #include <apt-pkg/configuration.h>
33 #define BLOCK_SIZE (512*1024)
39 struct MemBlock
*next
;
41 MemBlock(size_t size
) : size(size
), next(NULL
)
43 free
= start
= new char[size
];
46 size_t avail(void) { return size
- (free
- start
); }
51 free
= start
= new char[BLOCK_SIZE
];
67 char *add_easy(char *src
, size_t len
, char *last
)
70 for (MemBlock
*k
= this; k
; k
= k
->next
) {
71 if (k
->free
== last
) {
72 if (len
<= k
->avail()) {
73 char *n
= k
->add(src
, len
);
81 } else if (last
>= start
&& last
< free
) {
89 char *add(char *src
, size_t len
) {
92 if (len
> BLOCK_SIZE
) {
93 next
= new MemBlock(len
);
98 return next
->add(src
, len
);
102 memcpy(dst
, src
, len
);
110 * 1. write out <offset> lines unchanged
111 * 2. skip <del_cnt> lines from source
112 * 3. write out <add_cnt> lines (<add>/<add_len>)
116 size_t add_cnt
; /* lines */
117 size_t add_len
; /* bytes */
123 del_cnt
= add_cnt
= add_len
= 0;
127 /* actually, don't write <lines> lines from <add> */
128 void skip_lines(size_t lines
)
131 char *s
= (char*) memchr(add
, '\n', add_len
);
134 add_len
-= (s
- add
);
139 assert(add_cnt
== 0);
150 std::list
<struct Change
> changes
;
151 std::list
<struct Change
>::iterator where
;
152 size_t pos
; // line number is as far left of iterator as possible
154 bool pos_is_okay(void)
158 std::list
<struct Change
>::iterator x
;
159 for (x
= changes
.begin(); x
!= where
; ++x
) {
160 assert(x
!= changes
.end());
161 cpos
+= x
->offset
+ x
->add_cnt
;
171 where
= changes
.end();
175 std::list
<struct Change
>::iterator
begin(void) { return changes
.begin(); }
176 std::list
<struct Change
>::iterator
end(void) { return changes
.end(); }
178 std::list
<struct Change
>::reverse_iterator
rbegin(void) { return changes
.rbegin(); }
179 std::list
<struct Change
>::reverse_iterator
rend(void) { return changes
.rend(); }
181 void add_change(Change c
) {
182 assert(pos_is_okay());
183 go_to_change_for(c
.offset
);
184 assert(pos
+ where
->offset
== c
.offset
);
186 delete_lines(c
.del_cnt
);
187 assert(pos
+ where
->offset
== c
.offset
);
189 assert(pos_is_okay());
190 if (where
->add_len
> 0)
192 assert(where
->add_len
== 0 && where
->add_cnt
== 0);
194 where
->add_len
= c
.add_len
;
195 where
->add_cnt
= c
.add_cnt
;
198 assert(pos_is_okay());
200 assert(pos_is_okay());
206 while (where
->offset
== 0 && where
!= changes
.begin()) {
209 std::list
<struct Change
>::iterator next
= where
;
212 while (next
!= changes
.end() && next
->offset
== 0) {
213 where
->del_cnt
+= next
->del_cnt
;
215 if (next
->add
== NULL
) {
216 next
= changes
.erase(next
);
217 } else if (where
->add
== NULL
) {
218 where
->add
= next
->add
;
219 where
->add_len
= next
->add_len
;
220 where
->add_cnt
= next
->add_cnt
;
221 next
= changes
.erase(next
);
228 void go_to_change_for(size_t line
)
230 while(where
!= changes
.end()) {
235 if (pos
+ where
->offset
+ where
->add_cnt
<= line
) {
239 // line is somewhere in this slot
240 if (line
< pos
+ where
->offset
) {
242 } else if (line
== pos
+ where
->offset
) {
250 /* it goes before this patch */
254 void new_change(void) { insert(where
->offset
); }
256 void insert(size_t offset
)
258 assert(pos_is_okay());
259 assert(where
== changes
.end() || offset
<= where
->offset
);
260 if (where
!= changes
.end())
261 where
->offset
-= offset
;
262 changes
.insert(where
, Change(offset
));
264 assert(pos_is_okay());
267 void split(size_t offset
)
269 assert(pos_is_okay());
271 assert(where
->offset
< offset
);
272 assert(offset
< where
->offset
+ where
->add_cnt
);
274 size_t keep_lines
= offset
- where
->offset
;
276 Change
before(*where
);
280 where
->skip_lines(keep_lines
);
282 before
.add_cnt
= keep_lines
;
283 before
.add_len
-= where
->add_len
;
285 changes
.insert(where
, before
);
287 assert(pos_is_okay());
290 void delete_lines(size_t cnt
)
292 std::list
<struct Change
>::iterator x
= where
;
293 assert(pos_is_okay());
304 if (x
== changes
.end()) {
312 where
->del_cnt
+= del
;
315 assert(pos_is_okay());
319 assert(pos_is_okay());
321 pos
-= where
->offset
+ where
->add_cnt
;
322 assert(pos_is_okay());
326 assert(pos_is_okay());
327 pos
+= where
->offset
+ where
->add_cnt
;
329 assert(pos_is_okay());
334 FileChanges filechanges
;
337 static bool retry_fwrite(char *b
, size_t l
, FILE *f
, Hashes
*hash
)
340 while (r
> 0 && l
> 0)
342 r
= fwrite(b
, 1, l
, f
);
344 hash
->Add((unsigned char*)b
, r
);
351 static void dump_rest(FILE *o
, FILE *i
, Hashes
*hash
)
353 char buffer
[BLOCK_SIZE
];
355 while (0 < (l
= fread(buffer
, 1, sizeof(buffer
), i
))) {
356 if (!retry_fwrite(buffer
, l
, o
, hash
))
361 static void dump_lines(FILE *o
, FILE *i
, size_t n
, Hashes
*hash
)
363 char buffer
[BLOCK_SIZE
];
365 if (fgets(buffer
, sizeof(buffer
), i
) == 0)
367 size_t const l
= strlen(buffer
);
368 if (l
== 0 || buffer
[l
-1] == '\n')
370 retry_fwrite(buffer
, l
, o
, hash
);
374 static void skip_lines(FILE *i
, int n
)
376 char buffer
[BLOCK_SIZE
];
378 if (fgets(buffer
, sizeof(buffer
), i
) == 0)
380 size_t const l
= strlen(buffer
);
381 if (l
== 0 || buffer
[l
-1] == '\n')
386 static void dump_mem(FILE *o
, char *p
, size_t s
, Hashes
*hash
) {
387 retry_fwrite(p
, s
, o
, hash
);
392 void read_diff(FileFd
&f
)
394 char buffer
[BLOCK_SIZE
];
395 bool cmdwanted
= true;
398 while(f
.ReadLine(buffer
, sizeof(buffer
)))
403 s
= strtol(buffer
, &m
, 10);
405 s
= e
= ch
.offset
+ ch
.add_cnt
;
407 } else if (*m
== ',') {
409 e
= strtol(m
, &c
, 10);
429 ch
.del_cnt
= e
- s
+ 1;
433 ch
.del_cnt
= e
- s
+ 1;
437 filechanges
.add_change(ch
);
440 } else { /* !cmdwanted */
441 if (buffer
[0] == '.' && buffer
[1] == '\n') {
443 filechanges
.add_change(ch
);
449 last
= ch
.add
+ ch
.add_len
;
451 add
= add_text
.add_easy(buffer
, l
, last
);
457 filechanges
.add_change(ch
);
460 ch
.offset
+= ch
.add_cnt
;
470 void write_diff(FILE *f
)
473 std::list
<struct Change
>::reverse_iterator ch
;
474 for (ch
= filechanges
.rbegin(); ch
!= filechanges
.rend(); ++ch
) {
475 line
+= ch
->offset
+ ch
->del_cnt
;
478 for (ch
= filechanges
.rbegin(); ch
!= filechanges
.rend(); ++ch
) {
479 std::list
<struct Change
>::reverse_iterator mg_i
, mg_e
= ch
;
480 while (ch
->del_cnt
== 0 && ch
->offset
== 0)
483 if (ch
->add_cnt
> 0) {
484 if (ch
->del_cnt
== 0) {
485 fprintf(f
, "%lua\n", line
);
486 } else if (ch
->del_cnt
== 1) {
487 fprintf(f
, "%luc\n", line
+1);
489 fprintf(f
, "%lu,%luc\n", line
+1, line
+ch
->del_cnt
);
494 dump_mem(f
, mg_i
->add
, mg_i
->add_len
, NULL
);
495 } while (mg_i
-- != mg_e
);
498 } else if (ch
->del_cnt
== 1) {
499 fprintf(f
, "%lud\n", line
+1);
500 } else if (ch
->del_cnt
> 1) {
501 fprintf(f
, "%lu,%lud\n", line
+1, line
+ch
->del_cnt
);
507 void apply_against_file(FILE *out
, FILE *in
, Hashes
*hash
= NULL
)
509 std::list
<struct Change
>::iterator ch
;
510 for (ch
= filechanges
.begin(); ch
!= filechanges
.end(); ++ch
) {
511 dump_lines(out
, in
, ch
->offset
, hash
);
512 skip_lines(in
, ch
->del_cnt
);
513 dump_mem(out
, ch
->add
, ch
->add_len
, hash
);
515 dump_rest(out
, in
, hash
);
519 class RredMethod
: public pkgAcqMethod
{
524 virtual bool Fetch(FetchItem
*Itm
) {
525 Debug
= _config
->FindB("Debug::pkgAcquire::RRed", false);
527 std::string Path
= Get
.Host
+ Get
.Path
; // rred:/path - no host
530 Res
.Filename
= Itm
->DestFile
;
531 if (Itm
->Uri
.empty())
533 Path
= Itm
->DestFile
;
534 Itm
->DestFile
.append(".result");
538 std::vector
<std::string
> patchpaths
;
541 if (FileExists(Path
+ ".ed") == true)
542 patchpaths
.push_back(Path
+ ".ed");
545 _error
->PushToStack();
546 std::vector
<std::string
> patches
= GetListOfFilesInDir(flNotFile(Path
), "gz", true, false);
547 _error
->RevertToStack();
549 std::string
const baseName
= Path
+ ".ed.";
550 for (std::vector
<std::string
>::const_iterator p
= patches
.begin();
551 p
!= patches
.end(); ++p
)
552 if (p
->compare(0, baseName
.length(), baseName
) == 0)
553 patchpaths
.push_back(*p
);
556 std::string patch_name
;
557 for (std::vector
<std::string
>::iterator I
= patchpaths
.begin();
558 I
!= patchpaths
.end();
563 std::clog
<< "Patching " << Path
<< " with " << patch_name
567 // all patches are compressed, even if the name doesn't reflect it
568 if (p
.Open(patch_name
, FileFd::ReadOnly
, FileFd::Gzip
) == false) {
569 std::cerr
<< "Could not open patch file " << patch_name
<< std::endl
;
570 _error
->DumpErrors(std::cerr
);
578 std::clog
<< "Applying patches against " << Path
579 << " and writing results to " << Itm
->DestFile
582 FILE *inp
= fopen(Path
.c_str(), "r");
583 FILE *out
= fopen(Itm
->DestFile
.c_str(), "w");
587 patch
.apply_against_file(out
, inp
, &hash
);
593 std::clog
<< "rred: finished file patching of " << Path
<< "." << std::endl
;
596 struct stat bufbase
, bufpatch
;
597 if (stat(Path
.c_str(), &bufbase
) != 0 ||
598 stat(patch_name
.c_str(), &bufpatch
) != 0)
599 return _error
->Errno("stat", _("Failed to stat"));
601 struct timeval times
[2];
602 times
[0].tv_sec
= bufbase
.st_atime
;
603 times
[1].tv_sec
= bufpatch
.st_mtime
;
604 times
[0].tv_usec
= times
[1].tv_usec
= 0;
605 if (utimes(Itm
->DestFile
.c_str(), times
) != 0)
606 return _error
->Errno("utimes",_("Failed to set modification time"));
608 if (stat(Itm
->DestFile
.c_str(), &bufbase
) != 0)
609 return _error
->Errno("stat", _("Failed to stat"));
611 Res
.LastModified
= bufbase
.st_mtime
;
612 Res
.Size
= bufbase
.st_size
;
613 Res
.TakeHashes(hash
);
620 RredMethod() : pkgAcqMethod("2.0",SingleInstance
| SendConfig
), Debug(false) {}
623 int main(int argc
, char **argv
)
626 bool just_diff
= true;
634 if (argc
> 1 && strcmp(argv
[1], "-f") == 0) {
641 for (; i
< argc
; i
++) {
643 if (p
.Open(argv
[i
], FileFd::ReadOnly
) == false) {
644 _error
->DumpErrors(std::cerr
);
651 patch
.write_diff(stdout
);
657 patch
.apply_against_file(out
, inp
);