summaryrefslogtreecommitdiff
path: root/methods/rred.cc
blob: ab73398f512d9b66a84d7ca8f2c8440aae65aaab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <apt-pkg/fileutl.h>
#include <apt-pkg/error.h>
#include <apt-pkg/acquire-method.h>
#include <apt-pkg/strutl.h>
#include <apt-pkg/hashes.h>

#include <sys/stat.h>
#include <unistd.h>
#include <utime.h>
#include <stdio.h>
#include <errno.h>
#include <apti18n.h>

const char *Prog;

class RredMethod : public pkgAcqMethod
{
   virtual bool Fetch(FetchItem *Itm);
   
   public:
   
   RredMethod() : pkgAcqMethod("1.1",SingleInstance | SendConfig) {};

};

#define BUF_SIZE	(1024)

// XXX use enums
#define MODE_CHANGED	0
#define MODE_DELETED	1
#define MODE_ADDED		2

#define ED_OK                   0
#define ED_ORDERING		1
#define ED_PARSER		2
#define ED_FAILURE		3

// XXX someone better go out and understand the error reporting/handling here...
int 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;

	/* 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;
	}
	else if (*idx == 'a') {
		mode = MODE_ADDED;
	}
	else if (*idx == 'd') {
		mode = MODE_DELETED;
	}
	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;
}

int ed_file(FILE *ed_cmds, FILE *in_file, FILE *out_file, Hashes *hash) {
	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);
	
	/* read the rest from infile */
	if (result > 0) {
		while (fgets(buffer, BUF_SIZE, in_file) != NULL) {
			written = fwrite(buffer, 1, strlen(buffer), out_file);
			hash->Add((unsigned char*)buffer, written);
		}
	}
	else {
		// XXX better error handling
		fprintf(stderr, "Error: %i\n", result);
		return ED_FAILURE;
	}
	return ED_OK;
}


// XXX do we need modification times as well?
bool RredMethod::Fetch(FetchItem *Itm)
{
   URI Get = Itm->Uri;
   string Path = Get.Host + Get.Path; // To account for relative paths
   // Path contains the filename to patch
   FetchResult Res;
   Res.Filename = Itm->DestFile;
   URIStart(Res);
   // Res.Filename the destination filename

   // Open the source and destination files (the d'tor of FileFd will do 
   // the cleanup/closing of the fds)
   FileFd From(Path,FileFd::ReadOnly);
   FileFd Patch(Path+".ed",FileFd::ReadOnly);
   FileFd To(Itm->DestFile,FileFd::WriteEmpty);   
   To.EraseOnFailure();
   if (_error->PendingError() == true)
      return false;
   
   Hashes Hash;
   FILE* fFrom = fdopen(From.Fd(), "r");
   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) {
      return false;
   }

   // write out the result
   fflush(fFrom);
   fflush(fPatch);
   fflush(fTo);
   From.Close();
   Patch.Close();
   To.Close();

   // Transfer the modification times
   struct stat Buf;
   if (stat(Path.c_str(),&Buf) != 0)
      return _error->Errno("stat",_("Failed to stat"));

   struct utimbuf TimeBuf;
   TimeBuf.actime = Buf.st_atime;
   TimeBuf.modtime = Buf.st_mtime;
   if (utime(Itm->DestFile.c_str(),&TimeBuf) != 0)
      return _error->Errno("utime",_("Failed to set modification time"));

   if (stat(Itm->DestFile.c_str(),&Buf) != 0)
      return _error->Errno("stat",_("Failed to stat"));

   // return done
   Res.LastModified = Buf.st_mtime;
   Res.Size = Buf.st_size;
   Res.TakeHashes(Hash);
   URIDone(Res);

   return true;
}
									/*}}}*/

int main(int argc, char *argv[])
{
   RredMethod Mth;

   Prog = strrchr(argv[0],'/');
   Prog++;
   
   return Mth.Run();
}