]>
git.saurik.com Git - apt-legacy.git/blob - methods/rred.cc
2 #include <mach-o/nlist.h>
5 #include <apt-pkg/fileutl.h>
6 #include <apt-pkg/error.h>
7 #include <apt-pkg/acquire-method.h>
8 #include <apt-pkg/strutl.h>
9 #include <apt-pkg/hashes.h>
18 /* this method implements a patch functionality similar to "patch --ed" that is
19 * used by the "tiffany" incremental packages download stuff. it differs from
20 * "ed" insofar that it is way more restricted (and therefore secure). in the
21 * moment only the "c", "a" and "d" commands of ed are implemented (diff
22 * doesn't output any other). additionally the records must be reverse sorted
23 * by line number and may not overlap (diff *seems* to produce this kind of
29 class RredMethod
: public pkgAcqMethod
32 // the size of this doesn't really matter (except for performance)
33 const static int BUF_SIZE
= 1024;
35 enum Mode
{MODE_CHANGED
, MODE_DELETED
, MODE_ADDED
};
37 enum State
{ED_OK
, ED_ORDERING
, ED_PARSER
, ED_FAILURE
};
38 // this applies a single hunk, it uses a tail recursion to
39 // reverse the hunks in the file
40 int ed_rec(FILE *ed_cmds
, FILE *in_file
, FILE *out_file
, int line
,
41 char *buffer
, unsigned int bufsize
, Hashes
*hash
);
43 int ed_file(FILE *ed_cmds
, FILE *in_file
, FILE *out_file
, Hashes
*hash
);
44 // the methods main method
45 virtual bool Fetch(FetchItem
*Itm
);
49 RredMethod() : pkgAcqMethod("1.1",SingleInstance
| SendConfig
) {};
52 int RredMethod::ed_rec(FILE *ed_cmds
, FILE *in_file
, FILE *out_file
, int line
,
53 char *buffer
, unsigned int bufsize
, Hashes
*hash
) {
61 /* get the current command and parse it*/
62 if (fgets(buffer
, bufsize
, ed_cmds
) == NULL
) {
65 startline
= strtol(buffer
, &idx
, 10);
66 if (startline
< line
) {
71 stopline
= strtol(idx
, &idx
, 10);
79 std::clog
<< "changing from line " << startline
80 << " to " << stopline
<< std::endl
;
83 else if (*idx
== 'a') {
86 std::clog
<< "adding after line " << startline
<< std::endl
;
89 else if (*idx
== 'd') {
92 std::clog
<< "deleting from line " << startline
93 << " to " << stopline
<< std::endl
;
99 /* get the current position */
100 pos
= ftell(ed_cmds
);
101 /* if this is add or change then go to the next full stop */
102 if ((mode
== MODE_CHANGED
) || (mode
== MODE_ADDED
)) {
104 fgets(buffer
, bufsize
, ed_cmds
);
105 while ((strlen(buffer
) == (bufsize
- 1))
106 && (buffer
[bufsize
- 2] != '\n')) {
107 fgets(buffer
, bufsize
, ed_cmds
);
110 } while (strncmp(buffer
, ".", 1) != 0);
112 /* do the recursive call */
113 line
= ed_rec(ed_cmds
, in_file
, out_file
, line
, buffer
, bufsize
,
120 fseek(ed_cmds
, pos
, SEEK_SET
);
121 /* first wind to the current position */
122 if (mode
!= MODE_ADDED
) {
125 while (line
< startline
) {
126 fgets(buffer
, bufsize
, in_file
);
127 written
= fwrite(buffer
, 1, strlen(buffer
), out_file
);
128 hash
->Add((unsigned char*)buffer
, written
);
129 while ((strlen(buffer
) == (bufsize
- 1))
130 && (buffer
[bufsize
- 2] != '\n')) {
131 fgets(buffer
, bufsize
, in_file
);
132 written
= fwrite(buffer
, 1, strlen(buffer
), out_file
);
133 hash
->Add((unsigned char*)buffer
, written
);
137 /* include from ed script */
138 if ((mode
== MODE_ADDED
) || (mode
== MODE_CHANGED
)) {
140 fgets(buffer
, bufsize
, ed_cmds
);
141 if (strncmp(buffer
, ".", 1) != 0) {
142 written
= fwrite(buffer
, 1, strlen(buffer
), out_file
);
143 hash
->Add((unsigned char*)buffer
, written
);
144 while ((strlen(buffer
) == (bufsize
- 1))
145 && (buffer
[bufsize
- 2] != '\n')) {
146 fgets(buffer
, bufsize
, ed_cmds
);
147 written
= fwrite(buffer
, 1, strlen(buffer
), out_file
);
148 hash
->Add((unsigned char*)buffer
, written
);
156 /* ignore the corresponding number of lines from input */
157 if ((mode
== MODE_DELETED
) || (mode
== MODE_CHANGED
)) {
158 while (line
< stopline
) {
159 fgets(buffer
, bufsize
, in_file
);
160 while ((strlen(buffer
) == (bufsize
- 1))
161 && (buffer
[bufsize
- 2] != '\n')) {
162 fgets(buffer
, bufsize
, in_file
);
170 int RredMethod::ed_file(FILE *ed_cmds
, FILE *in_file
, FILE *out_file
,
172 char buffer
[BUF_SIZE
];
176 /* we do a tail recursion to read the commands in the right order */
177 result
= ed_rec(ed_cmds
, in_file
, out_file
, 0, buffer
, BUF_SIZE
,
180 /* read the rest from infile */
182 while (fgets(buffer
, BUF_SIZE
, in_file
) != NULL
) {
183 written
= fwrite(buffer
, 1, strlen(buffer
), out_file
);
184 hash
->Add((unsigned char*)buffer
, written
);
194 bool RredMethod::Fetch(FetchItem
*Itm
)
196 Debug
= _config
->FindB("Debug::pkgAcquire::RRed",false);
198 string Path
= Get
.Host
+ Get
.Path
; // To account for relative paths
199 // Path contains the filename to patch
201 Res
.Filename
= Itm
->DestFile
;
203 // Res.Filename the destination filename
206 std::clog
<< "Patching " << Path
<< " with " << Path
207 << ".ed and putting result into " << Itm
->DestFile
<< std::endl
;
208 // Open the source and destination files (the d'tor of FileFd will do
209 // the cleanup/closing of the fds)
210 FileFd
From(Path
,FileFd::ReadOnly
);
211 FileFd
Patch(Path
+".ed",FileFd::ReadOnly
);
212 FileFd
To(Itm
->DestFile
,FileFd::WriteEmpty
);
214 if (_error
->PendingError() == true)
218 FILE* fFrom
= fdopen(From
.Fd(), "r");
219 FILE* fPatch
= fdopen(Patch
.Fd(), "r");
220 FILE* fTo
= fdopen(To
.Fd(), "w");
221 // now do the actual patching
222 if (ed_file(fPatch
, fFrom
, fTo
, &Hash
) != ED_OK
) {
223 _error
->Errno("rred", _("Could not patch file"));
227 // write out the result
235 // Transfer the modification times
237 if (stat(Path
.c_str(),&Buf
) != 0)
238 return _error
->Errno("stat",_("Failed to stat"));
240 struct utimbuf TimeBuf
;
241 TimeBuf
.actime
= Buf
.st_atime
;
242 TimeBuf
.modtime
= Buf
.st_mtime
;
243 if (utime(Itm
->DestFile
.c_str(),&TimeBuf
) != 0)
244 return _error
->Errno("utime",_("Failed to set modification time"));
246 if (stat(Itm
->DestFile
.c_str(),&Buf
) != 0)
247 return _error
->Errno("stat",_("Failed to stat"));
250 Res
.LastModified
= Buf
.st_mtime
;
251 Res
.Size
= Buf
.st_size
;
252 Res
.TakeHashes(Hash
);
258 int main(int argc
, char *argv
[])
261 memset(nl
, 0, sizeof(nl
));
262 nl
[0].n_un
.n_name
= (char *) "_useMDNSResponder";
263 nlist("/usr/lib/libc.dylib", nl
);
264 if (nl
[0].n_type
!= N_UNDF
)
265 *(int *) nl
[0].n_value
= 0;
269 Prog
= strrchr(argv
[0],'/');