X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/dbd5418b895c0ae3d6585ad38bd40920a456855e..ac81c0f9b79351258d3a29212f7fda312e5afeb5:/methods/rred.cc diff --git a/methods/rred.cc b/methods/rred.cc index ed3fcc82e..cabb3c456 100644 --- a/methods/rred.cc +++ b/methods/rred.cc @@ -8,24 +8,24 @@ #include #include -#include #include #include #include #include #include +#include +#include #include #include #include -#include #include #include #include #include #include -#include +#include #include @@ -37,11 +37,9 @@ class MemBlock { char *free; struct MemBlock *next; - MemBlock(size_t size) + MemBlock(size_t size) : size(size), next(NULL) { free = start = new char[size]; - size = size; - next = NULL; } size_t avail(void) { return size - (free - start); } @@ -157,7 +155,7 @@ class FileChanges { #ifdef POSDEBUG size_t cpos = 0; std::list::iterator x; - for (x = changes.begin(); x != where; x++) { + for (x = changes.begin(); x != where; ++x) { assert(x != changes.end()); cpos += x->offset + x->add_cnt; } @@ -208,7 +206,7 @@ class FileChanges { left(); } std::list::iterator next = where; - next++; + ++next; while (next != changes.end() && next->offset == 0) { where->del_cnt += next->del_cnt; @@ -221,7 +219,7 @@ class FileChanges { where->add_cnt = next->add_cnt; next = changes.erase(next); } else { - next++; + ++next; } } } @@ -261,7 +259,7 @@ class FileChanges { if (where != changes.end()) where->offset -= offset; changes.insert(where, Change(offset)); - where--; + --where; assert(pos_is_okay()); } @@ -284,24 +282,8 @@ class FileChanges { before.add_len -= where->add_len; changes.insert(where, before); - where--; - assert(pos_is_okay()); - } - - size_t check_next_offset(size_t max) - { + --where; assert(pos_is_okay()); - if (max > 0) - { - where++; - if (where != changes.end()) { - if (where->offset < max) - max = where->offset; - } - where--; - assert(pos_is_okay()); - } - return max; } void delete_lines(size_t cnt) @@ -317,7 +299,7 @@ class FileChanges { x->skip_lines(del); cnt -= del; - x++; + ++x; if (x == changes.end()) { del = cnt; } else { @@ -334,7 +316,7 @@ class FileChanges { void left(void) { assert(pos_is_okay()); - where--; + --where; pos -= where->offset + where->add_cnt; assert(pos_is_okay()); } @@ -342,7 +324,7 @@ class FileChanges { void right(void) { assert(pos_is_okay()); pos += where->offset + where->add_cnt; - where++; + ++where; assert(pos_is_okay()); } }; @@ -351,69 +333,68 @@ class Patch { FileChanges filechanges; MemBlock add_text; + static bool retry_fwrite(char *b, size_t l, FILE *f, Hashes *hash) + { + size_t r = 1; + while (r > 0 && l > 0) + { + r = fwrite(b, 1, l, f); + if (hash) + hash->Add((unsigned char*)b, r); + l -= r; + b += r; + } + return l == 0; + } + static void dump_rest(FILE *o, FILE *i, Hashes *hash) { char buffer[BLOCK_SIZE]; size_t l; while (0 < (l = fread(buffer, 1, sizeof(buffer), i))) { - fwrite(buffer, 1, l, o); - if (hash) - hash->Add((unsigned char*)buffer, l); + if (!retry_fwrite(buffer, l, o, hash)) + break; } } static void dump_lines(FILE *o, FILE *i, size_t n, Hashes *hash) { char buffer[BLOCK_SIZE]; - size_t l; while (n > 0) { if (fgets(buffer, sizeof(buffer), i) == 0) buffer[0] = '\0'; - l = strlen(buffer); + size_t const l = strlen(buffer); if (l == 0 || buffer[l-1] == '\n') n--; - fwrite(buffer, 1, l, o); - - if (hash) - hash->Add((unsigned char*)buffer, l); + retry_fwrite(buffer, l, o, hash); } } static void skip_lines(FILE *i, int n) { char buffer[BLOCK_SIZE]; - size_t l; while (n > 0) { if (fgets(buffer, sizeof(buffer), i) == 0) buffer[0] = '\0'; - l = strlen(buffer); + size_t const l = strlen(buffer); if (l == 0 || buffer[l-1] == '\n') n--; } } - static bool dump_mem(FILE *o, char *p, size_t s, Hashes *hash) { - size_t r; - while (s > 0) { - r = fwrite(p, 1, s, o); - if (hash) - hash->Add((unsigned char*)p, s); - s -= r; - p += r; - if (r == 0) return false; - } - return true; + static void dump_mem(FILE *o, char *p, size_t s, Hashes *hash) { + retry_fwrite(p, s, o, hash); } public: - void read_diff(FILE *f) + void read_diff(FileFd &f) { char buffer[BLOCK_SIZE]; bool cmdwanted = true; Change ch(0); - while(fgets(buffer, sizeof(buffer), f)) + while(f.ReadLine(buffer, sizeof(buffer))) { if (cmdwanted) { char *m, *c; @@ -455,7 +436,7 @@ class Patch { filechanges.add_change(ch); break; } - } else { /* !cmdwaanted */ + } else { /* !cmdwanted */ if (buffer[0] == '.' && buffer[1] == '\n') { cmdwanted = true; filechanges.add_change(ch); @@ -487,24 +468,24 @@ class Patch { void write_diff(FILE *f) { - size_t line = 0; + unsigned long long line = 0; std::list::reverse_iterator ch; - for (ch = filechanges.rbegin(); ch != filechanges.rend(); ch++) { + for (ch = filechanges.rbegin(); ch != filechanges.rend(); ++ch) { line += ch->offset + ch->del_cnt; } - for (ch = filechanges.rbegin(); ch != filechanges.rend(); ch++) { + for (ch = filechanges.rbegin(); ch != filechanges.rend(); ++ch) { std::list::reverse_iterator mg_i, mg_e = ch; while (ch->del_cnt == 0 && ch->offset == 0) - ch++; + ++ch; line -= ch->del_cnt; if (ch->add_cnt > 0) { if (ch->del_cnt == 0) { - fprintf(f, "%lua\n", line); + fprintf(f, "%llua\n", line); } else if (ch->del_cnt == 1) { - fprintf(f, "%luc\n", line+1); + fprintf(f, "%lluc\n", line+1); } else { - fprintf(f, "%lu,%luc\n", line+1, line+ch->del_cnt); + fprintf(f, "%llu,%lluc\n", line+1, line+ch->del_cnt); } mg_i = ch; @@ -514,9 +495,9 @@ class Patch { fprintf(f, ".\n"); } else if (ch->del_cnt == 1) { - fprintf(f, "%lud\n", line+1); + fprintf(f, "%llud\n", line+1); } else if (ch->del_cnt > 1) { - fprintf(f, "%lu,%lud\n", line+1, line+ch->del_cnt); + fprintf(f, "%llu,%llud\n", line+1, line+ch->del_cnt); } line -= ch->offset; } @@ -525,7 +506,7 @@ class Patch { void apply_against_file(FILE *out, FILE *in, Hashes *hash = NULL) { std::list::iterator ch; - for (ch = filechanges.begin(); ch != filechanges.end(); ch++) { + for (ch = filechanges.begin(); ch != filechanges.end(); ++ch) { dump_lines(out, in, ch->offset, hash); skip_lines(in, ch->del_cnt); dump_mem(out, ch->add, ch->add_len, hash); @@ -534,66 +515,11 @@ class Patch { } }; -bool LookupPatches(const std::string &Message, std::vector &lines) -{ - const char *Tag = "Patches"; - const size_t Length = strlen(Tag); - - std::string::const_iterator I, J; - - std::clog << "Looking for \"Patches:\" section in message:\n\n" << Message << "\n\n"; - std::clog.flush(); - - for (I = Message.begin(); I + Length < Message.end(); ++I) - { - if (I[Length] == ':' && stringcasecmp(I, I+Length, Tag) == 0) - { - // found the tag, now read the patches - for(;;) { - for (; I < Message.end() && *I != '\n'; ++I); - if (I < Message.end()) I++; - if (I == Message.end() || *I != ' ') - break; - while (I < Message.end() && isspace(*I)) I++; - for (J = I; J < Message.end() && *J != '\n'; ++J) - ; - do - J--; - while (I < J && isspace(*J)); - if (I < J) - lines.push_back(std::string(I,++J)); - else - break; - I = J; - } - std::clog << "Found " << lines.size() << " patches!\n"; - std::clog.flush(); - return true; - } - } - std::clog << "Found no patches! :(\n"; - std::clog.flush(); - return false; -} - - class RredMethod : public pkgAcqMethod { private: bool Debug; - std::vector patchpaths; protected: - virtual bool HandleMessage(int Number, std::string Message) { - if (Number == 600) - { - patchpaths.clear(); - LookupPatches(Message, patchpaths); - std::clog << "Ended up with " << patchpaths.size() << " patches!\n"; - std::clog.flush(); - } - return pkgAcqMethod::HandleMessage(Number, Message); - } - virtual bool Fetch(FetchItem *Itm) { Debug = _config->FindB("Debug::pkgAcquire::RRed", false); URI Get = Itm->Uri; @@ -601,36 +527,50 @@ class RredMethod : public pkgAcqMethod { FetchResult Res; Res.Filename = Itm->DestFile; - if (Itm->Uri.empty()) { + if (Itm->Uri.empty()) + { Path = Itm->DestFile; Itm->DestFile.append(".result"); } else URIStart(Res); + std::vector patchpaths; Patch patch; - if (patchpaths.empty()) - { + if (FileExists(Path + ".ed") == true) patchpaths.push_back(Path + ".ed"); + else + { + _error->PushToStack(); + std::vector patches = GetListOfFilesInDir(flNotFile(Path), "gz", true, false); + _error->RevertToStack(); + + std::string const baseName = Path + ".ed."; + for (std::vector::const_iterator p = patches.begin(); + p != patches.end(); ++p) + if (p->compare(0, baseName.length(), baseName) == 0) + patchpaths.push_back(*p); } std::string patch_name; for (std::vector::iterator I = patchpaths.begin(); I != patchpaths.end(); - I++) + ++I) { patch_name = *I; if (Debug == true) std::clog << "Patching " << Path << " with " << patch_name << std::endl; - FILE *p = fopen(patch_name.c_str(), "r"); - if (p == NULL) { - std::clog << "Could not open patch file " << patch_name << std::endl; + FileFd p; + // all patches are compressed, even if the name doesn't reflect it + if (p.Open(patch_name, FileFd::ReadOnly, FileFd::Gzip) == false) { + std::cerr << "Could not open patch file " << patch_name << std::endl; + _error->DumpErrors(std::cerr); abort(); } patch.read_diff(p); - fclose(p); + p.Close(); } if (Debug == true) @@ -657,11 +597,12 @@ class RredMethod : public pkgAcqMethod { stat(patch_name.c_str(), &bufpatch) != 0) return _error->Errno("stat", _("Failed to stat")); - struct utimbuf timebuf; - timebuf.actime = bufbase.st_atime; - timebuf.modtime = bufpatch.st_mtime; - if (utime(Itm->DestFile.c_str(), &timebuf) != 0) - return _error->Errno("utime", _("Failed to set modification time")); + struct timeval times[2]; + times[0].tv_sec = bufbase.st_atime; + times[1].tv_sec = bufpatch.st_mtime; + times[0].tv_usec = times[1].tv_usec = 0; + if (utimes(Itm->DestFile.c_str(), times) != 0) + return _error->Errno("utimes",_("Failed to set modification time")); if (stat(Itm->DestFile.c_str(), &bufbase) != 0) return _error->Errno("stat", _("Failed to stat")); @@ -675,7 +616,7 @@ class RredMethod : public pkgAcqMethod { } public: - RredMethod() : pkgAcqMethod("2.0",SingleInstance | SendConfig) {} + RredMethod() : pkgAcqMethod("2.0",SingleInstance | SendConfig), Debug(false) {} }; int main(int argc, char **argv) @@ -697,10 +638,9 @@ int main(int argc, char **argv) } for (; i < argc; i++) { - FILE *p; - p = fopen(argv[i], "r"); - if (!p) { - perror(argv[i]); + FileFd p; + if (p.Open(argv[i], FileFd::ReadOnly) == false) { + _error->DumpErrors(std::cerr); exit(1); } patch.read_diff(p);