]>
git.saurik.com Git - apt.git/blob - methods/rred.cc
1 #include <apt-pkg/fileutl.h>
2 #include <apt-pkg/error.h>
3 #include <apt-pkg/acquire-method.h>
4 #include <apt-pkg/strutl.h>
5 #include <apt-pkg/hashes.h>
16 class RredMethod
: public pkgAcqMethod
18 virtual bool Fetch(FetchItem
*Itm
);
22 RredMethod() : pkgAcqMethod("1.1",SingleInstance
| SendConfig
) {};
26 #define BUF_SIZE (1024)
29 #define MODE_CHANGED 0
30 #define MODE_DELETED 1
37 int ed_rec(FILE *ed_cmds
, FILE *in_file
, FILE *out_file
, int line
,
38 char *buffer
, unsigned int bufsize
, Hashes
*hash
) {
46 /* get the current command and parse it*/
47 if (fgets(buffer
, bufsize
, ed_cmds
) == NULL
) {
50 startline
= strtol(buffer
, &idx
, 10);
51 if (startline
< line
) {
56 stopline
= strtol(idx
, &idx
, 10);
64 else if (*idx
== 'a') {
67 else if (*idx
== 'd') {
73 /* get the current position */
75 /* if this is add or change then go to the next full stop */
76 if ((mode
== MODE_CHANGED
) || (mode
== MODE_ADDED
)) {
78 fgets(buffer
, bufsize
, ed_cmds
);
79 while ((strlen(buffer
) == (bufsize
- 1))
80 && (buffer
[bufsize
- 2] != '\n')) {
81 fgets(buffer
, bufsize
, ed_cmds
);
84 } while (strncmp(buffer
, ".", 1) != 0);
86 /* do the recursive call */
87 line
= ed_rec(ed_cmds
, in_file
, out_file
, line
, buffer
, bufsize
,
94 fseek(ed_cmds
, pos
, SEEK_SET
);
95 /* first wind to the current position */
96 if (mode
!= MODE_ADDED
) {
99 while (line
< startline
) {
100 fgets(buffer
, bufsize
, in_file
);
101 written
= fwrite(buffer
, 1, strlen(buffer
), out_file
);
102 hash
->Add((unsigned char*)buffer
, written
);
103 while ((strlen(buffer
) == (bufsize
- 1))
104 && (buffer
[bufsize
- 2] != '\n')) {
105 fgets(buffer
, bufsize
, in_file
);
106 written
= fwrite(buffer
, 1, strlen(buffer
), out_file
);
107 hash
->Add((unsigned char*)buffer
, written
);
111 /* include from ed script */
112 if ((mode
== MODE_ADDED
) || (mode
== MODE_CHANGED
)) {
114 fgets(buffer
, bufsize
, ed_cmds
);
115 if (strncmp(buffer
, ".", 1) != 0) {
116 written
= fwrite(buffer
, 1, strlen(buffer
), out_file
);
117 hash
->Add((unsigned char*)buffer
, written
);
118 while ((strlen(buffer
) == (bufsize
- 1))
119 && (buffer
[bufsize
- 2] != '\n')) {
120 fgets(buffer
, bufsize
, ed_cmds
);
121 written
= fwrite(buffer
, 1, strlen(buffer
), out_file
);
122 hash
->Add((unsigned char*)buffer
, written
);
130 /* ignore the corresponding number of lines from input */
131 if ((mode
== MODE_DELETED
) || (mode
== MODE_CHANGED
)) {
132 while (line
< stopline
) {
133 fgets(buffer
, bufsize
, in_file
);
134 while ((strlen(buffer
) == (bufsize
- 1))
135 && (buffer
[bufsize
- 2] != '\n')) {
136 fgets(buffer
, bufsize
, in_file
);
144 int ed_file(FILE *ed_cmds
, FILE *in_file
, FILE *out_file
, Hashes
*hash
) {
145 char buffer
[BUF_SIZE
];
149 /* we do a tail recursion to read the commands in the right order */
150 result
= ed_rec(ed_cmds
, in_file
, out_file
, 0, buffer
, BUF_SIZE
,
153 /* read the rest from infile */
155 while (fgets(buffer
, BUF_SIZE
, in_file
) != NULL
) {
156 written
= fwrite(buffer
, 1, strlen(buffer
), out_file
);
158 // sha_process_bytes(buffer, written, &sha);
162 // XXX better error handling
163 fprintf(stderr
, "Error: %i\n", result
);
170 // XXX do we need modification times as well?
171 bool RredMethod::Fetch(FetchItem
*Itm
)
174 string Path
= Get
.Host
+ Get
.Path
; // To account for relative paths
175 // Path contains the filename to patch
177 Res
.Filename
= Itm
->DestFile
;
179 // Res.Filename the destination filename
181 // Open the source and destination files (the d'tor of FileFd will do
182 // the cleanup/closing of the fds)
183 FileFd
From(Path
,FileFd::ReadOnly
);
184 FileFd
Patch(Path
+".ed",FileFd::ReadOnly
);
185 FileFd
To(Itm
->DestFile
,FileFd::WriteEmpty
);
187 if (_error
->PendingError() == true)
191 FILE* fFrom
= fdopen(From
.Fd(), "r");
192 FILE* fPatch
= fdopen(Patch
.Fd(), "r");
193 FILE* fTo
= fdopen(To
.Fd(), "w");
194 // now do the actual patching
195 ed_file(fPatch
, fFrom
, fTo
, &Hash
);
197 // XXX need to get the size
198 // Res.Size = Buf.st_size;
199 Res
.TakeHashes(Hash
);
206 int main(int argc
, char *argv
[])
210 Prog
= strrchr(argv
[0],'/');