summaryrefslogtreecommitdiff
path: root/methods/rred.cc
diff options
context:
space:
mode:
authorDavid Kalnischkies <kalnischkies@gmail.com>2009-11-05 02:05:21 +0100
committerDavid Kalnischkies <kalnischkies@gmail.com>2009-11-05 02:05:21 +0100
commitc7139d8c8c04e69150ab975c0e2698d05ed6a298 (patch)
tree86339e31a1b24b21f1f7de35d2e436a6e55b6aea /methods/rred.cc
parentc262653cc21b4302c3fc5f724d254f68bfca84b7 (diff)
rewrite and refactor rred method to be able to handle even big (>30 MB)
patches (Closes: #554349) and hardening the method itself by using more constants and a return value which can't be misinterpreted as linenumber
Diffstat (limited to 'methods/rred.cc')
-rw-r--r--methods/rred.cc358
1 files changed, 187 insertions, 171 deletions
diff --git a/methods/rred.cc b/methods/rred.cc
index 3d4b37e83..2d4dd768b 100644
--- a/methods/rred.cc
+++ b/methods/rred.cc
@@ -1,3 +1,4 @@
+// Includes /*{{{*/
#include <apt-pkg/fileutl.h>
#include <apt-pkg/error.h>
#include <apt-pkg/acquire-method.h>
@@ -10,185 +11,205 @@
#include <stdio.h>
#include <errno.h>
#include <apti18n.h>
-
-/* this method implements a patch functionality similar to "patch --ed" that is
- * used by the "tiffany" incremental packages download stuff. it differs from
- * "ed" insofar that it is way more restricted (and therefore secure). in the
- * moment only the "c", "a" and "d" commands of ed are implemented (diff
- * doesn't output any other). additionally the records must be reverse sorted
- * by line number and may not overlap (diff *seems* to produce this kind of
- * output).
+ /*}}}*/
+/** \brief RredMethod - ed-style incremential patch method {{{
+ *
+ * This method implements a patch functionality similar to "patch --ed" that is
+ * used by the "tiffany" incremental packages download stuff. It differs from
+ * "ed" insofar that it is way more restricted (and therefore secure).
+ * The currently supported ed commands are "<em>c</em>hange", "<em>a</em>dd" and
+ * "<em>d</em>elete" (diff doesn't output any other).
+ * Additionally the records must be reverse sorted by line number and
+ * may not overlap (diff *seems* to produce this kind of output).
* */
+class RredMethod : public pkgAcqMethod {
+ bool Debug;
+ // the size of this doesn't really matter (except for performance)
+ const static int BUF_SIZE = 1024;
+ // the supported ed commands
+ enum Mode {MODE_CHANGED='c', MODE_DELETED='d', MODE_ADDED='a'};
+ // return values
+ enum State {ED_OK=0, ED_ORDERING=1, ED_PARSER=2, ED_FAILURE=3};
-const char *Prog;
+ State applyFile(FILE *ed_cmds, FILE *in_file, FILE *out_file,
+ unsigned long &line, char *buffer, Hashes *hash) const;
+ void ignoreLineInFile(FILE *fin, char *buffer) const;
+ void copyLineFromFileToFile(FILE *fin, FILE *fout,
+ Hashes *hash, char *buffer) const;
-class RredMethod : public pkgAcqMethod
-{
- bool Debug;
- // the size of this doesn't really matter (except for performance)
- const static int BUF_SIZE = 1024;
- // the ed commands
- enum Mode {MODE_CHANGED, MODE_DELETED, MODE_ADDED};
- // return values
- enum State {ED_OK, ED_ORDERING, ED_PARSER, ED_FAILURE};
- // this applies a single hunk, it uses a tail recursion to
- // reverse the hunks in the file
- int ed_rec(FILE *ed_cmds, FILE *in_file, FILE *out_file, int line,
- char *buffer, unsigned int bufsize, Hashes *hash);
- // apply a patch file
- int ed_file(FILE *ed_cmds, FILE *in_file, FILE *out_file, Hashes *hash);
+ State patchFile(FILE *ed_cmds, FILE *in_file, FILE *out_file, Hashes *hash) const;
protected:
- // the methods main method
- virtual bool Fetch(FetchItem *Itm);
-
- public:
-
- RredMethod() : pkgAcqMethod("1.1",SingleInstance | SendConfig) {};
+ // the methods main method
+ virtual bool Fetch(FetchItem *Itm);
+
+public:
+ RredMethod() : pkgAcqMethod("1.1",SingleInstance | SendConfig) {};
};
+ /*}}}*/
+/** \brief applyFile - in reverse order with a tail recursion {{{
+ *
+ * As it is expected that the commands are in reversed order in the patch file
+ * we check in the first half if the command is valid, but doesn't execute it
+ * and move a step deeper. After reaching the end of the file we apply the
+ * patches in the correct order: last found command first.
+ *
+ * \param ed_cmds patch file to apply
+ * \param in_file base file we want to patch
+ * \param out_file file to write the patched result to
+ * \param line of command operation
+ * \param buffer internal used read/write buffer
+ * \param hash the created file for correctness
+ * \return the success State of the ed command executor
+ */
+RredMethod::State RredMethod::applyFile(FILE *ed_cmds, FILE *in_file, FILE *out_file,
+ unsigned long &line, char *buffer, Hashes *hash) const {
+ // get the current command and parse it
+ if (fgets(buffer, BUF_SIZE, ed_cmds) == NULL) {
+ if (Debug == true)
+ std::clog << "rred: encounter end of file - we can start patching now.";
+ line = 0;
+ return ED_OK;
+ }
-int RredMethod::ed_rec(FILE *ed_cmds, FILE *in_file, FILE *out_file, int line,
- char *buffer, unsigned int bufsize, Hashes *hash) {
- int pos;
- int startline;
- int stopline;
- int mode;
- int written;
- char *idx;
+ // parse in the effected linenumbers
+ char* idx;
+ errno=0;
+ unsigned long const startline = strtol(buffer, &idx, 10);
+ if (errno == ERANGE || errno == EINVAL) {
+ _error->Errno("rred", "startline is an invalid number");
+ return ED_PARSER;
+ }
+ if (startline > line) {
+ _error->Error("rred: The start line (%lu) of the next command is higher than the last line (%lu). This is not allowed.", startline, line);
+ return ED_ORDERING;
+ }
+ unsigned long stopline;
+ if (*idx == ',') {
+ idx++;
+ errno=0;
+ stopline = strtol(idx, &idx, 10);
+ if (errno == ERANGE || errno == EINVAL) {
+ _error->Errno("rred", "stopline is an invalid number");
+ return ED_PARSER;
+ }
+ }
+ else {
+ stopline = startline;
+ }
+ line = startline;
- /* get the current command and parse it*/
- if (fgets(buffer, bufsize, ed_cmds) == NULL) {
- return line;
- }
- startline = strtol(buffer, &idx, 10);
- if (startline < line) {
- return ED_ORDERING;
- }
- if (*idx == ',') {
- idx++;
- stopline = strtol(idx, &idx, 10);
- }
- else {
- stopline = startline;
- }
- if (*idx == 'c') {
- mode = MODE_CHANGED;
- if (Debug == true) {
- std::clog << "changing from line " << startline
- << " to " << stopline << std::endl;
- }
- }
- else if (*idx == 'a') {
- mode = MODE_ADDED;
- if (Debug == true) {
- std::clog << "adding after line " << startline << std::endl;
- }
- }
- else if (*idx == 'd') {
- mode = MODE_DELETED;
- if (Debug == true) {
- std::clog << "deleting from line " << startline
- << " to " << stopline << std::endl;
- }
- }
- else {
- return ED_PARSER;
- }
- /* get the current position */
- pos = ftell(ed_cmds);
- /* if this is add or change then go to the next full stop */
- if ((mode == MODE_CHANGED) || (mode == MODE_ADDED)) {
- do {
- fgets(buffer, bufsize, ed_cmds);
- while ((strlen(buffer) == (bufsize - 1))
- && (buffer[bufsize - 2] != '\n')) {
- fgets(buffer, bufsize, ed_cmds);
- buffer[0] = ' ';
- }
- } while (strncmp(buffer, ".", 1) != 0);
- }
- /* do the recursive call */
- line = ed_rec(ed_cmds, in_file, out_file, line, buffer, bufsize,
- hash);
- /* pass on errors */
- if (line < 0) {
- return line;
- }
- /* apply our hunk */
- fseek(ed_cmds, pos, SEEK_SET);
- /* first wind to the current position */
- if (mode != MODE_ADDED) {
- startline -= 1;
- }
- while (line < startline) {
- fgets(buffer, bufsize, in_file);
- written = fwrite(buffer, 1, strlen(buffer), out_file);
- hash->Add((unsigned char*)buffer, written);
- while ((strlen(buffer) == (bufsize - 1))
- && (buffer[bufsize - 2] != '\n')) {
- fgets(buffer, bufsize, in_file);
- written = fwrite(buffer, 1, strlen(buffer), out_file);
- hash->Add((unsigned char*)buffer, written);
- }
- line++;
- }
- /* include from ed script */
- if ((mode == MODE_ADDED) || (mode == MODE_CHANGED)) {
- do {
- fgets(buffer, bufsize, ed_cmds);
- if (strncmp(buffer, ".", 1) != 0) {
- written = fwrite(buffer, 1, strlen(buffer), out_file);
- hash->Add((unsigned char*)buffer, written);
- while ((strlen(buffer) == (bufsize - 1))
- && (buffer[bufsize - 2] != '\n')) {
- fgets(buffer, bufsize, ed_cmds);
- written = fwrite(buffer, 1, strlen(buffer), out_file);
- hash->Add((unsigned char*)buffer, written);
- }
- }
- else {
- break;
- }
- } while (1);
- }
- /* ignore the corresponding number of lines from input */
- if ((mode == MODE_DELETED) || (mode == MODE_CHANGED)) {
- while (line < stopline) {
- fgets(buffer, bufsize, in_file);
- while ((strlen(buffer) == (bufsize - 1))
- && (buffer[bufsize - 2] != '\n')) {
- fgets(buffer, bufsize, in_file);
- }
- line++;
- }
- }
- return line;
-}
+ // which command to execute on this line(s)?
+ switch (*idx) {
+ case MODE_CHANGED:
+ if (Debug == true)
+ std::clog << "Change from line " << startline << " to " << stopline << std::endl;
+ break;
+ case MODE_ADDED:
+ if (Debug == true)
+ std::clog << "Insert after line " << startline << std::endl;
+ break;
+ case MODE_DELETED:
+ if (Debug == true)
+ std::clog << "Delete from line " << startline << " to " << stopline << std::endl;
+ break;
+ default:
+ _error->Error("rred: Unknown ed command '%c'. Abort.", *idx);
+ return ED_PARSER;
+ }
+ unsigned char mode = *idx;
+
+ // save the current position
+ unsigned const long pos = ftell(ed_cmds);
+
+ // if this is add or change then go to the next full stop
+ if (mode == MODE_CHANGED || mode == MODE_ADDED) {
+ do
+ ignoreLineInFile(ed_cmds, buffer);
+ while (strncmp(buffer, ".", 1) != 0);
+ }
+
+ // do the recursive call - the last command is the one we need to execute at first
+ const State child = applyFile(ed_cmds, in_file, out_file, line, buffer, hash);
+ if (child != ED_OK) {
+ return child;
+ }
+
+ // change and delete are working on "line" - add is done after "line"
+ if (mode != MODE_ADDED)
+ line++;
+
+ // first wind to the current position and copy over all unchanged lines
+ while (line < startline) {
+ fgets(buffer, BUF_SIZE, in_file);
+ copyLineFromFileToFile(in_file, out_file, hash, buffer);
+ line++;
+ }
+
+ if (mode != MODE_ADDED)
+ line--;
-int RredMethod::ed_file(FILE *ed_cmds, FILE *in_file, FILE *out_file,
- Hashes *hash) {
+ // include data from ed script
+ if (mode == MODE_CHANGED || mode == MODE_ADDED) {
+ fseek(ed_cmds, pos, SEEK_SET);
+ while(fgets(buffer, BUF_SIZE, ed_cmds) != NULL) {
+ if (strncmp(buffer, ".", 1) != 0)
+ copyLineFromFileToFile(ed_cmds, out_file, hash, buffer);
+ else
+ break;
+ }
+ }
+
+ // ignore the corresponding number of lines from input
+ if (mode == MODE_CHANGED || mode == MODE_DELETED) {
+ while (line < stopline) {
+ ignoreLineInFile(in_file, buffer);
+ line++;
+ }
+ }
+ return ED_OK;
+}
+ /*}}}*/
+void RredMethod::copyLineFromFileToFile(FILE *fin, FILE *fout, /*{{{*/
+ Hashes *hash, char *buffer) const {
+ size_t written = fwrite(buffer, 1, strlen(buffer), fout);
+ hash->Add((unsigned char*)buffer, written);
+ while (strlen(buffer) == (BUF_SIZE - 1) &&
+ buffer[BUF_SIZE - 2] != '\n') {
+ fgets(buffer, BUF_SIZE, fin);
+ written = fwrite(buffer, 1, strlen(buffer), fout);
+ hash->Add((unsigned char*)buffer, written);
+ }
+}
+ /*}}}*/
+void RredMethod::ignoreLineInFile(FILE *fin, char *buffer) const { /*{{{*/
+ fgets(buffer, BUF_SIZE, fin);
+ while (strlen(buffer) == (BUF_SIZE - 1) &&
+ buffer[BUF_SIZE - 2] != '\n') {
+ fgets(buffer, BUF_SIZE, fin);
+ buffer[0] = ' ';
+ }
+}
+ /*}}}*/
+RredMethod::State RredMethod::patchFile(FILE *ed_cmds, FILE *in_file, FILE *out_file, /*{{{*/
+ Hashes *hash) const {
char buffer[BUF_SIZE];
- int result;
- int written;
/* we do a tail recursion to read the commands in the right order */
- result = ed_rec(ed_cmds, in_file, out_file, 0, buffer, BUF_SIZE,
- hash);
+ unsigned long line = -1; // assign highest possible value
+ State result = applyFile(ed_cmds, in_file, out_file, line, buffer, hash);
/* read the rest from infile */
- if (result >= 0) {
+ if (result == ED_OK) {
while (fgets(buffer, BUF_SIZE, in_file) != NULL) {
- written = fwrite(buffer, 1, strlen(buffer), out_file);
+ size_t const written = fwrite(buffer, 1, strlen(buffer), out_file);
hash->Add((unsigned char*)buffer, written);
}
}
- else {
- return ED_FAILURE;
- }
- return ED_OK;
+ return result;
}
-
-bool RredMethod::Fetch(FetchItem *Itm)
+ /*}}}*/
+bool RredMethod::Fetch(FetchItem *Itm) /*{{{*/
{
Debug = _config->FindB("Debug::pkgAcquire::RRed", false);
URI Get = Itm->Uri;
@@ -219,7 +240,7 @@ bool RredMethod::Fetch(FetchItem *Itm)
FILE* fPatch = fdopen(Patch.Fd(), "r");
FILE* fTo = fdopen(To.Fd(), "w");
// now do the actual patching
- if (ed_file(fPatch, fFrom, fTo, &Hash) != ED_OK) {
+ if (patchFile(fPatch, fFrom, fTo, &Hash) != ED_OK) {
_error->Errno("rred", _("Could not patch file"));
return false;
}
@@ -256,10 +277,8 @@ bool RredMethod::Fetch(FetchItem *Itm)
return true;
}
-
-/**
- * \brief Wrapper class for testing rred
- */
+ /*}}}*/
+/** \brief Wrapper class for testing rred */ /*{{{*/
class TestRredMethod : public RredMethod {
public:
/** \brief Run rred in debug test mode
@@ -276,9 +295,8 @@ public:
return Fetch(test);
}
};
-
-/**
- * \brief Starter for the rred method (or its test method)
+ /*}}}*/
+/** \brief Starter for the rred method (or its test method) {{{
*
* Used without parameters is the normal behavior for methods for
* the APT acquire system. While this works great for the acquire system
@@ -289,9 +307,6 @@ public:
* and will write the result to "Testfile.result".
*/
int main(int argc, char *argv[]) {
- Prog = strrchr(argv[0],'/');
- Prog++;
-
if (argc == 0) {
RredMethod Mth;
return Mth.Run();
@@ -302,3 +317,4 @@ int main(int argc, char *argv[]) {
return result;
}
}
+ /*}}}*/