- 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);
- }
+ fgets(buffer, BUF_SIZE, fin);
+ size_t const written = fwrite(buffer, 1, strlen(buffer), fout);
+ hash->Add((unsigned char*)buffer, written);
+ } while (strlen(buffer) == (BUF_SIZE - 1) &&
+ buffer[BUF_SIZE - 2] != '\n');
+ }
+}
+ /*}}}*/
+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(FileFd &Patch, FileFd &From, /*{{{*/
+ FileFd &out_file, Hashes *hash) const {
+ char buffer[BUF_SIZE];
+ FILE* fFrom = fdopen(From.Fd(), "r");
+ FILE* fPatch = fdopen(Patch.Fd(), "r");
+ FILE* fTo = fdopen(out_file.Fd(), "w");
+
+ /* we do a tail recursion to read the commands in the right order */
+ unsigned long line = -1; // assign highest possible value
+ State const result = applyFile(fPatch, fFrom, fTo, line, buffer, hash);
+
+ /* read the rest from infile */
+ if (result == ED_OK) {
+ while (fgets(buffer, BUF_SIZE, fFrom) != NULL) {
+ size_t const written = fwrite(buffer, 1, strlen(buffer), fTo);
+ hash->Add((unsigned char*)buffer, written);
+ }
+ fflush(fTo);
+ }
+ return result;
+}
+ /*}}}*/
+struct EdCommand { /*{{{*/
+ size_t data_start;
+ size_t data_end;
+ size_t data_lines;
+ size_t first_line;
+ size_t last_line;
+ char type;
+};
+#define IOV_COUNT 1024 /* Don't really want IOV_MAX since it can be arbitrarily large */
+ /*}}}*/
+RredMethod::State RredMethod::patchMMap(FileFd &Patch, FileFd &From, /*{{{*/
+ FileFd &out_file, Hashes *hash) const {
+#ifdef _POSIX_MAPPED_FILES
+ MMap ed_cmds(Patch, MMap::ReadOnly);
+ MMap in_file(From, MMap::ReadOnly);
+
+ if (ed_cmds.Size() == 0 || in_file.Size() == 0)
+ return MMAP_FAILED;
+
+ EdCommand* commands = 0;
+ size_t command_count = 0;
+ size_t command_alloc = 0;
+
+ const char* begin = (char*) ed_cmds.Data();
+ const char* end = begin;
+ const char* ed_end = (char*) ed_cmds.Data() + ed_cmds.Size();
+
+ const char* input = (char*) in_file.Data();
+ const char* input_end = (char*) in_file.Data() + in_file.Size();
+
+ size_t i;
+
+ /* 1. Parse entire script. It is executed in reverse order, so we cather it
+ * in the `commands' buffer first
+ */
+
+ for(;;) {
+ EdCommand cmd;
+ cmd.data_start = 0;
+ cmd.data_end = 0;
+
+ while(begin != ed_end && *begin == '\n')
+ ++begin;
+ while(end != ed_end && *end != '\n')
+ ++end;
+ if(end == ed_end && begin == end)
+ break;
+
+ /* Determine command range */
+ const char* tmp = begin;
+
+ for(;;) {
+ /* atoll is safe despite lacking NUL-termination; we know there's an
+ * alphabetic character at end[-1]
+ */
+ if(tmp == end) {
+ cmd.first_line = atol(begin);
+ cmd.last_line = cmd.first_line;
+ break;