]>
Commit | Line | Data |
---|---|---|
578bfd0a AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
7da2b375 | 3 | // $Id: fileutl.cc,v 1.42 2002/09/14 05:29:22 jgg Exp $ |
578bfd0a AL |
4 | /* ###################################################################### |
5 | ||
6 | File Utilities | |
7 | ||
8 | CopyFile - Buffered copy of a single file | |
9 | GetLock - dpkg compatible lock file manipulation (fcntl) | |
10 | ||
614adaa0 MV |
11 | Most of this source is placed in the Public Domain, do with it what |
12 | you will | |
7da2b375 | 13 | It was originally written by Jason Gunthorpe <jgg@debian.org>. |
a3a03f5d | 14 | FileFd gzip support added by Martin Pitt <martin.pitt@canonical.com> |
578bfd0a | 15 | |
614adaa0 MV |
16 | The exception is RunScripts() it is under the GPLv2 |
17 | ||
578bfd0a AL |
18 | ##################################################################### */ |
19 | /*}}}*/ | |
20 | // Include Files /*{{{*/ | |
ea542140 DK |
21 | #include <config.h> |
22 | ||
094a497d | 23 | #include <apt-pkg/fileutl.h> |
1cd1c398 | 24 | #include <apt-pkg/strutl.h> |
094a497d | 25 | #include <apt-pkg/error.h> |
b2e465d6 | 26 | #include <apt-pkg/sptr.h> |
468720c5 | 27 | #include <apt-pkg/aptconfiguration.h> |
75ef8f14 | 28 | #include <apt-pkg/configuration.h> |
b2e465d6 | 29 | |
152ab79e | 30 | #include <cstdlib> |
4f333a8b | 31 | #include <cstring> |
3010fb0e | 32 | #include <cstdio> |
4f333a8b | 33 | |
4d055c05 | 34 | #include <iostream> |
578bfd0a | 35 | #include <unistd.h> |
2c206aa4 | 36 | #include <fcntl.h> |
578bfd0a | 37 | #include <sys/stat.h> |
578bfd0a | 38 | #include <sys/types.h> |
cc2313b7 | 39 | #include <sys/time.h> |
1ae93c94 | 40 | #include <sys/wait.h> |
46e39c8e | 41 | #include <dirent.h> |
54676e1a | 42 | #include <signal.h> |
65a1e968 | 43 | #include <errno.h> |
75ef8f14 | 44 | #include <set> |
46e39c8e | 45 | #include <algorithm> |
2cae0ccb | 46 | |
699b209e DK |
47 | // FIXME: Compressor Fds have some speed disadvantages and are a bit buggy currently, |
48 | // so while the current implementation satisfies the testcases it is not a real option | |
49 | // to disable it for now | |
50 | #define APT_USE_ZLIB 1 | |
51 | #ifdef APT_USE_ZLIB | |
032bd56f | 52 | #include <zlib.h> |
699b209e | 53 | #endif |
032bd56f | 54 | |
2a79d5b5 | 55 | #ifdef WORDS_BIGENDIAN |
2cae0ccb DK |
56 | #include <inttypes.h> |
57 | #endif | |
ea542140 DK |
58 | |
59 | #include <apti18n.h> | |
578bfd0a AL |
60 | /*}}}*/ |
61 | ||
4d055c05 AL |
62 | using namespace std; |
63 | ||
032bd56f DK |
64 | class FileFdPrivate { |
65 | public: | |
699b209e | 66 | #ifdef APT_USE_ZLIB |
032bd56f | 67 | gzFile gz; |
699b209e DK |
68 | #else |
69 | void* gz; | |
70 | #endif | |
71 | pid_t compressor_pid; | |
72 | bool pipe; | |
73 | APT::Configuration::Compressor compressor; | |
74 | FileFd::OpenMode openmode; | |
75 | FileFdPrivate() : gz(NULL), compressor_pid(-1), pipe(false) {}; | |
032bd56f DK |
76 | }; |
77 | ||
614adaa0 MV |
78 | // RunScripts - Run a set of scripts from a configuration subtree /*{{{*/ |
79 | // --------------------------------------------------------------------- | |
80 | /* */ | |
81 | bool RunScripts(const char *Cnf) | |
82 | { | |
83 | Configuration::Item const *Opts = _config->Tree(Cnf); | |
84 | if (Opts == 0 || Opts->Child == 0) | |
85 | return true; | |
86 | Opts = Opts->Child; | |
87 | ||
88 | // Fork for running the system calls | |
89 | pid_t Child = ExecFork(); | |
90 | ||
91 | // This is the child | |
92 | if (Child == 0) | |
93 | { | |
cfba4f69 MV |
94 | if (_config->FindDir("DPkg::Chroot-Directory","/") != "/") |
95 | { | |
96 | std::cerr << "Chrooting into " | |
97 | << _config->FindDir("DPkg::Chroot-Directory") | |
98 | << std::endl; | |
99 | if (chroot(_config->FindDir("DPkg::Chroot-Directory","/").c_str()) != 0) | |
100 | _exit(100); | |
101 | } | |
102 | ||
614adaa0 MV |
103 | if (chdir("/tmp/") != 0) |
104 | _exit(100); | |
105 | ||
106 | unsigned int Count = 1; | |
107 | for (; Opts != 0; Opts = Opts->Next, Count++) | |
108 | { | |
109 | if (Opts->Value.empty() == true) | |
110 | continue; | |
111 | ||
112 | if (system(Opts->Value.c_str()) != 0) | |
113 | _exit(100+Count); | |
114 | } | |
115 | _exit(0); | |
116 | } | |
117 | ||
118 | // Wait for the child | |
119 | int Status = 0; | |
120 | while (waitpid(Child,&Status,0) != Child) | |
121 | { | |
122 | if (errno == EINTR) | |
123 | continue; | |
124 | return _error->Errno("waitpid","Couldn't wait for subprocess"); | |
125 | } | |
126 | ||
127 | // Restore sig int/quit | |
128 | signal(SIGQUIT,SIG_DFL); | |
129 | signal(SIGINT,SIG_DFL); | |
130 | ||
131 | // Check for an error code. | |
132 | if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0) | |
133 | { | |
134 | unsigned int Count = WEXITSTATUS(Status); | |
135 | if (Count > 100) | |
136 | { | |
137 | Count -= 100; | |
138 | for (; Opts != 0 && Count != 1; Opts = Opts->Next, Count--); | |
139 | _error->Error("Problem executing scripts %s '%s'",Cnf,Opts->Value.c_str()); | |
140 | } | |
141 | ||
142 | return _error->Error("Sub-process returned an error code"); | |
143 | } | |
144 | ||
145 | return true; | |
146 | } | |
147 | /*}}}*/ | |
148 | ||
578bfd0a AL |
149 | // CopyFile - Buffered copy of a file /*{{{*/ |
150 | // --------------------------------------------------------------------- | |
151 | /* The caller is expected to set things so that failure causes erasure */ | |
8b89e57f | 152 | bool CopyFile(FileFd &From,FileFd &To) |
578bfd0a AL |
153 | { |
154 | if (From.IsOpen() == false || To.IsOpen() == false) | |
155 | return false; | |
156 | ||
157 | // Buffered copy between fds | |
b2e465d6 | 158 | SPtrArray<unsigned char> Buf = new unsigned char[64000]; |
650faab0 | 159 | unsigned long long Size = From.Size(); |
b0db36b1 | 160 | while (Size != 0) |
578bfd0a | 161 | { |
650faab0 | 162 | unsigned long long ToRead = Size; |
b0db36b1 AL |
163 | if (Size > 64000) |
164 | ToRead = 64000; | |
165 | ||
4a6d5862 | 166 | if (From.Read(Buf,ToRead) == false || |
b0db36b1 | 167 | To.Write(Buf,ToRead) == false) |
578bfd0a | 168 | return false; |
b0db36b1 AL |
169 | |
170 | Size -= ToRead; | |
578bfd0a AL |
171 | } |
172 | ||
578bfd0a AL |
173 | return true; |
174 | } | |
175 | /*}}}*/ | |
176 | // GetLock - Gets a lock file /*{{{*/ | |
177 | // --------------------------------------------------------------------- | |
178 | /* This will create an empty file of the given name and lock it. Once this | |
179 | is done all other calls to GetLock in any other process will fail with | |
180 | -1. The return result is the fd of the file, the call should call | |
181 | close at some time. */ | |
182 | int GetLock(string File,bool Errors) | |
183 | { | |
f659b39a OS |
184 | // GetLock() is used in aptitude on directories with public-write access |
185 | // Use O_NOFOLLOW here to prevent symlink traversal attacks | |
186 | int FD = open(File.c_str(),O_RDWR | O_CREAT | O_NOFOLLOW,0640); | |
578bfd0a AL |
187 | if (FD < 0) |
188 | { | |
b2e465d6 AL |
189 | // Read only .. cant have locking problems there. |
190 | if (errno == EROFS) | |
191 | { | |
192 | _error->Warning(_("Not using locking for read only lock file %s"),File.c_str()); | |
193 | return dup(0); // Need something for the caller to close | |
194 | } | |
195 | ||
578bfd0a | 196 | if (Errors == true) |
b2e465d6 AL |
197 | _error->Errno("open",_("Could not open lock file %s"),File.c_str()); |
198 | ||
199 | // Feh.. We do this to distinguish the lock vs open case.. | |
200 | errno = EPERM; | |
578bfd0a AL |
201 | return -1; |
202 | } | |
b2e465d6 AL |
203 | SetCloseExec(FD,true); |
204 | ||
578bfd0a AL |
205 | // Aquire a write lock |
206 | struct flock fl; | |
c71bc556 AL |
207 | fl.l_type = F_WRLCK; |
208 | fl.l_whence = SEEK_SET; | |
209 | fl.l_start = 0; | |
210 | fl.l_len = 0; | |
578bfd0a AL |
211 | if (fcntl(FD,F_SETLK,&fl) == -1) |
212 | { | |
d89df07a AL |
213 | if (errno == ENOLCK) |
214 | { | |
b2e465d6 AL |
215 | _error->Warning(_("Not using locking for nfs mounted lock file %s"),File.c_str()); |
216 | return dup(0); // Need something for the caller to close | |
d89df07a | 217 | } |
578bfd0a | 218 | if (Errors == true) |
b2e465d6 AL |
219 | _error->Errno("open",_("Could not get lock %s"),File.c_str()); |
220 | ||
221 | int Tmp = errno; | |
578bfd0a | 222 | close(FD); |
b2e465d6 | 223 | errno = Tmp; |
578bfd0a AL |
224 | return -1; |
225 | } | |
226 | ||
227 | return FD; | |
228 | } | |
229 | /*}}}*/ | |
230 | // FileExists - Check if a file exists /*{{{*/ | |
231 | // --------------------------------------------------------------------- | |
36f1098a | 232 | /* Beware: Directories are also files! */ |
578bfd0a AL |
233 | bool FileExists(string File) |
234 | { | |
235 | struct stat Buf; | |
236 | if (stat(File.c_str(),&Buf) != 0) | |
237 | return false; | |
238 | return true; | |
239 | } | |
240 | /*}}}*/ | |
36f1098a DK |
241 | // RealFileExists - Check if a file exists and if it is really a file /*{{{*/ |
242 | // --------------------------------------------------------------------- | |
243 | /* */ | |
244 | bool RealFileExists(string File) | |
245 | { | |
246 | struct stat Buf; | |
247 | if (stat(File.c_str(),&Buf) != 0) | |
248 | return false; | |
249 | return ((Buf.st_mode & S_IFREG) != 0); | |
250 | } | |
251 | /*}}}*/ | |
1cd1c398 DK |
252 | // DirectoryExists - Check if a directory exists and is really one /*{{{*/ |
253 | // --------------------------------------------------------------------- | |
254 | /* */ | |
255 | bool DirectoryExists(string const &Path) | |
256 | { | |
257 | struct stat Buf; | |
258 | if (stat(Path.c_str(),&Buf) != 0) | |
259 | return false; | |
260 | return ((Buf.st_mode & S_IFDIR) != 0); | |
261 | } | |
262 | /*}}}*/ | |
263 | // CreateDirectory - poor man's mkdir -p guarded by a parent directory /*{{{*/ | |
264 | // --------------------------------------------------------------------- | |
265 | /* This method will create all directories needed for path in good old | |
266 | mkdir -p style but refuses to do this if Parent is not a prefix of | |
267 | this Path. Example: /var/cache/ and /var/cache/apt/archives are given, | |
268 | so it will create apt/archives if /var/cache exists - on the other | |
269 | hand if the parent is /var/lib the creation will fail as this path | |
270 | is not a parent of the path to be generated. */ | |
271 | bool CreateDirectory(string const &Parent, string const &Path) | |
272 | { | |
273 | if (Parent.empty() == true || Path.empty() == true) | |
274 | return false; | |
275 | ||
276 | if (DirectoryExists(Path) == true) | |
277 | return true; | |
278 | ||
279 | if (DirectoryExists(Parent) == false) | |
280 | return false; | |
281 | ||
282 | // we are not going to create directories "into the blue" | |
283 | if (Path.find(Parent, 0) != 0) | |
284 | return false; | |
285 | ||
286 | vector<string> const dirs = VectorizeString(Path.substr(Parent.size()), '/'); | |
287 | string progress = Parent; | |
288 | for (vector<string>::const_iterator d = dirs.begin(); d != dirs.end(); ++d) | |
289 | { | |
290 | if (d->empty() == true) | |
291 | continue; | |
292 | ||
293 | progress.append("/").append(*d); | |
294 | if (DirectoryExists(progress) == true) | |
295 | continue; | |
296 | ||
297 | if (mkdir(progress.c_str(), 0755) != 0) | |
298 | return false; | |
299 | } | |
300 | return true; | |
301 | } | |
302 | /*}}}*/ | |
7753e468 | 303 | // CreateAPTDirectoryIfNeeded - ensure that the given directory exists /*{{{*/ |
b29c3712 DK |
304 | // --------------------------------------------------------------------- |
305 | /* a small wrapper around CreateDirectory to check if it exists and to | |
306 | remove the trailing "/apt/" from the parent directory if needed */ | |
7753e468 | 307 | bool CreateAPTDirectoryIfNeeded(string const &Parent, string const &Path) |
b29c3712 DK |
308 | { |
309 | if (DirectoryExists(Path) == true) | |
310 | return true; | |
311 | ||
312 | size_t const len = Parent.size(); | |
313 | if (len > 5 && Parent.find("/apt/", len - 6, 5) == len - 5) | |
314 | { | |
315 | if (CreateDirectory(Parent.substr(0,len-5), Path) == true) | |
316 | return true; | |
317 | } | |
318 | else if (CreateDirectory(Parent, Path) == true) | |
319 | return true; | |
320 | ||
321 | return false; | |
322 | } | |
323 | /*}}}*/ | |
46e39c8e MV |
324 | // GetListOfFilesInDir - returns a vector of files in the given dir /*{{{*/ |
325 | // --------------------------------------------------------------------- | |
326 | /* If an extension is given only files with this extension are included | |
327 | in the returned vector, otherwise every "normal" file is included. */ | |
b39c1859 MV |
328 | std::vector<string> GetListOfFilesInDir(string const &Dir, string const &Ext, |
329 | bool const &SortList, bool const &AllowNoExt) | |
330 | { | |
331 | std::vector<string> ext; | |
332 | ext.reserve(2); | |
333 | if (Ext.empty() == false) | |
334 | ext.push_back(Ext); | |
335 | if (AllowNoExt == true && ext.empty() == false) | |
336 | ext.push_back(""); | |
337 | return GetListOfFilesInDir(Dir, ext, SortList); | |
338 | } | |
339 | std::vector<string> GetListOfFilesInDir(string const &Dir, std::vector<string> const &Ext, | |
340 | bool const &SortList) | |
341 | { | |
342 | // Attention debuggers: need to be set with the environment config file! | |
343 | bool const Debug = _config->FindB("Debug::GetListOfFilesInDir", false); | |
344 | if (Debug == true) | |
345 | { | |
346 | std::clog << "Accept in " << Dir << " only files with the following " << Ext.size() << " extensions:" << std::endl; | |
347 | if (Ext.empty() == true) | |
348 | std::clog << "\tNO extension" << std::endl; | |
349 | else | |
350 | for (std::vector<string>::const_iterator e = Ext.begin(); | |
351 | e != Ext.end(); ++e) | |
352 | std::clog << '\t' << (e->empty() == true ? "NO" : *e) << " extension" << std::endl; | |
353 | } | |
354 | ||
46e39c8e | 355 | std::vector<string> List; |
36f1098a DK |
356 | |
357 | if (DirectoryExists(Dir.c_str()) == false) | |
358 | { | |
359 | _error->Error(_("List of files can't be created as '%s' is not a directory"), Dir.c_str()); | |
360 | return List; | |
361 | } | |
362 | ||
1408e219 | 363 | Configuration::MatchAgainstConfig SilentIgnore("Dir::Ignore-Files-Silently"); |
46e39c8e MV |
364 | DIR *D = opendir(Dir.c_str()); |
365 | if (D == 0) | |
366 | { | |
367 | _error->Errno("opendir",_("Unable to read %s"),Dir.c_str()); | |
368 | return List; | |
369 | } | |
370 | ||
371 | for (struct dirent *Ent = readdir(D); Ent != 0; Ent = readdir(D)) | |
372 | { | |
b39c1859 | 373 | // skip "hidden" files |
46e39c8e MV |
374 | if (Ent->d_name[0] == '.') |
375 | continue; | |
376 | ||
491058e3 DK |
377 | // Make sure it is a file and not something else |
378 | string const File = flCombine(Dir,Ent->d_name); | |
379 | #ifdef _DIRENT_HAVE_D_TYPE | |
380 | if (Ent->d_type != DT_REG) | |
381 | #endif | |
382 | { | |
383 | if (RealFileExists(File.c_str()) == false) | |
384 | { | |
385 | if (SilentIgnore.Match(Ent->d_name) == false) | |
386 | _error->Notice(_("Ignoring '%s' in directory '%s' as it is not a regular file"), Ent->d_name, Dir.c_str()); | |
387 | continue; | |
388 | } | |
389 | } | |
390 | ||
b39c1859 MV |
391 | // check for accepted extension: |
392 | // no extension given -> periods are bad as hell! | |
393 | // extensions given -> "" extension allows no extension | |
394 | if (Ext.empty() == false) | |
395 | { | |
396 | string d_ext = flExtension(Ent->d_name); | |
397 | if (d_ext == Ent->d_name) // no extension | |
398 | { | |
399 | if (std::find(Ext.begin(), Ext.end(), "") == Ext.end()) | |
400 | { | |
401 | if (Debug == true) | |
402 | std::clog << "Bad file: " << Ent->d_name << " → no extension" << std::endl; | |
5edc3966 | 403 | if (SilentIgnore.Match(Ent->d_name) == false) |
491058e3 | 404 | _error->Notice(_("Ignoring file '%s' in directory '%s' as it has no filename extension"), Ent->d_name, Dir.c_str()); |
b39c1859 MV |
405 | continue; |
406 | } | |
407 | } | |
408 | else if (std::find(Ext.begin(), Ext.end(), d_ext) == Ext.end()) | |
409 | { | |
410 | if (Debug == true) | |
411 | std::clog << "Bad file: " << Ent->d_name << " → bad extension »" << flExtension(Ent->d_name) << "«" << std::endl; | |
1408e219 | 412 | if (SilentIgnore.Match(Ent->d_name) == false) |
491058e3 | 413 | _error->Notice(_("Ignoring file '%s' in directory '%s' as it has an invalid filename extension"), Ent->d_name, Dir.c_str()); |
b39c1859 MV |
414 | continue; |
415 | } | |
416 | } | |
46e39c8e | 417 | |
b39c1859 | 418 | // Skip bad filenames ala run-parts |
46e39c8e MV |
419 | const char *C = Ent->d_name; |
420 | for (; *C != 0; ++C) | |
421 | if (isalpha(*C) == 0 && isdigit(*C) == 0 | |
b39c1859 MV |
422 | && *C != '_' && *C != '-') { |
423 | // no required extension -> dot is a bad character | |
424 | if (*C == '.' && Ext.empty() == false) | |
425 | continue; | |
46e39c8e | 426 | break; |
b39c1859 | 427 | } |
46e39c8e | 428 | |
b39c1859 | 429 | // we don't reach the end of the name -> bad character included |
46e39c8e | 430 | if (*C != 0) |
b39c1859 MV |
431 | { |
432 | if (Debug == true) | |
433 | std::clog << "Bad file: " << Ent->d_name << " → bad character »" | |
434 | << *C << "« in filename (period allowed: " << (Ext.empty() ? "no" : "yes") << ")" << std::endl; | |
435 | continue; | |
436 | } | |
437 | ||
438 | // skip filenames which end with a period. These are never valid | |
439 | if (*(C - 1) == '.') | |
440 | { | |
441 | if (Debug == true) | |
442 | std::clog << "Bad file: " << Ent->d_name << " → Period as last character" << std::endl; | |
46e39c8e | 443 | continue; |
b39c1859 | 444 | } |
46e39c8e | 445 | |
b39c1859 MV |
446 | if (Debug == true) |
447 | std::clog << "Accept file: " << Ent->d_name << " in " << Dir << std::endl; | |
46e39c8e MV |
448 | List.push_back(File); |
449 | } | |
450 | closedir(D); | |
451 | ||
452 | if (SortList == true) | |
453 | std::sort(List.begin(),List.end()); | |
454 | return List; | |
455 | } | |
456 | /*}}}*/ | |
578bfd0a AL |
457 | // SafeGetCWD - This is a safer getcwd that returns a dynamic string /*{{{*/ |
458 | // --------------------------------------------------------------------- | |
459 | /* We return / on failure. */ | |
460 | string SafeGetCWD() | |
461 | { | |
462 | // Stash the current dir. | |
463 | char S[300]; | |
464 | S[0] = 0; | |
7f25bdff | 465 | if (getcwd(S,sizeof(S)-2) == 0) |
578bfd0a | 466 | return "/"; |
7f25bdff AL |
467 | unsigned int Len = strlen(S); |
468 | S[Len] = '/'; | |
469 | S[Len+1] = 0; | |
578bfd0a AL |
470 | return S; |
471 | } | |
472 | /*}}}*/ | |
2ec858bc MV |
473 | // GetModificationTime - Get the mtime of the given file or -1 on error /*{{{*/ |
474 | // --------------------------------------------------------------------- | |
475 | /* We return / on failure. */ | |
476 | time_t GetModificationTime(string const &Path) | |
477 | { | |
478 | struct stat St; | |
479 | if (stat(Path.c_str(), &St) < 0) | |
480 | return -1; | |
481 | return St.st_mtime; | |
482 | } | |
483 | /*}}}*/ | |
8ce4327b AL |
484 | // flNotDir - Strip the directory from the filename /*{{{*/ |
485 | // --------------------------------------------------------------------- | |
486 | /* */ | |
487 | string flNotDir(string File) | |
488 | { | |
489 | string::size_type Res = File.rfind('/'); | |
490 | if (Res == string::npos) | |
491 | return File; | |
492 | Res++; | |
493 | return string(File,Res,Res - File.length()); | |
494 | } | |
495 | /*}}}*/ | |
d38b7b3d AL |
496 | // flNotFile - Strip the file from the directory name /*{{{*/ |
497 | // --------------------------------------------------------------------- | |
171c45bc | 498 | /* Result ends in a / */ |
d38b7b3d AL |
499 | string flNotFile(string File) |
500 | { | |
501 | string::size_type Res = File.rfind('/'); | |
502 | if (Res == string::npos) | |
171c45bc | 503 | return "./"; |
d38b7b3d AL |
504 | Res++; |
505 | return string(File,0,Res); | |
506 | } | |
507 | /*}}}*/ | |
b2e465d6 AL |
508 | // flExtension - Return the extension for the file /*{{{*/ |
509 | // --------------------------------------------------------------------- | |
510 | /* */ | |
511 | string flExtension(string File) | |
512 | { | |
513 | string::size_type Res = File.rfind('.'); | |
514 | if (Res == string::npos) | |
515 | return File; | |
516 | Res++; | |
517 | return string(File,Res,Res - File.length()); | |
518 | } | |
519 | /*}}}*/ | |
421c8d10 AL |
520 | // flNoLink - If file is a symlink then deref it /*{{{*/ |
521 | // --------------------------------------------------------------------- | |
522 | /* If the name is not a link then the returned path is the input. */ | |
523 | string flNoLink(string File) | |
524 | { | |
525 | struct stat St; | |
526 | if (lstat(File.c_str(),&St) != 0 || S_ISLNK(St.st_mode) == 0) | |
527 | return File; | |
528 | if (stat(File.c_str(),&St) != 0) | |
529 | return File; | |
530 | ||
531 | /* Loop resolving the link. There is no need to limit the number of | |
532 | loops because the stat call above ensures that the symlink is not | |
533 | circular */ | |
534 | char Buffer[1024]; | |
535 | string NFile = File; | |
536 | while (1) | |
537 | { | |
538 | // Read the link | |
539 | int Res; | |
540 | if ((Res = readlink(NFile.c_str(),Buffer,sizeof(Buffer))) <= 0 || | |
541 | (unsigned)Res >= sizeof(Buffer)) | |
542 | return File; | |
543 | ||
544 | // Append or replace the previous path | |
545 | Buffer[Res] = 0; | |
546 | if (Buffer[0] == '/') | |
547 | NFile = Buffer; | |
548 | else | |
549 | NFile = flNotFile(NFile) + Buffer; | |
550 | ||
551 | // See if we are done | |
552 | if (lstat(NFile.c_str(),&St) != 0) | |
553 | return File; | |
554 | if (S_ISLNK(St.st_mode) == 0) | |
555 | return NFile; | |
556 | } | |
557 | } | |
558 | /*}}}*/ | |
b2e465d6 AL |
559 | // flCombine - Combine a file and a directory /*{{{*/ |
560 | // --------------------------------------------------------------------- | |
561 | /* If the file is an absolute path then it is just returned, otherwise | |
562 | the directory is pre-pended to it. */ | |
563 | string flCombine(string Dir,string File) | |
564 | { | |
565 | if (File.empty() == true) | |
566 | return string(); | |
567 | ||
568 | if (File[0] == '/' || Dir.empty() == true) | |
569 | return File; | |
570 | if (File.length() >= 2 && File[0] == '.' && File[1] == '/') | |
571 | return File; | |
572 | if (Dir[Dir.length()-1] == '/') | |
573 | return Dir + File; | |
574 | return Dir + '/' + File; | |
575 | } | |
576 | /*}}}*/ | |
3b5421b4 AL |
577 | // SetCloseExec - Set the close on exec flag /*{{{*/ |
578 | // --------------------------------------------------------------------- | |
579 | /* */ | |
580 | void SetCloseExec(int Fd,bool Close) | |
581 | { | |
582 | if (fcntl(Fd,F_SETFD,(Close == false)?0:FD_CLOEXEC) != 0) | |
583 | { | |
584 | cerr << "FATAL -> Could not set close on exec " << strerror(errno) << endl; | |
585 | exit(100); | |
586 | } | |
587 | } | |
588 | /*}}}*/ | |
589 | // SetNonBlock - Set the nonblocking flag /*{{{*/ | |
590 | // --------------------------------------------------------------------- | |
591 | /* */ | |
592 | void SetNonBlock(int Fd,bool Block) | |
593 | { | |
0a8a80e5 AL |
594 | int Flags = fcntl(Fd,F_GETFL) & (~O_NONBLOCK); |
595 | if (fcntl(Fd,F_SETFL,Flags | ((Block == false)?0:O_NONBLOCK)) != 0) | |
3b5421b4 AL |
596 | { |
597 | cerr << "FATAL -> Could not set non-blocking flag " << strerror(errno) << endl; | |
598 | exit(100); | |
599 | } | |
600 | } | |
601 | /*}}}*/ | |
602 | // WaitFd - Wait for a FD to become readable /*{{{*/ | |
603 | // --------------------------------------------------------------------- | |
b2e465d6 | 604 | /* This waits for a FD to become readable using select. It is useful for |
6d5dd02a AL |
605 | applications making use of non-blocking sockets. The timeout is |
606 | in seconds. */ | |
1084d58a | 607 | bool WaitFd(int Fd,bool write,unsigned long timeout) |
3b5421b4 AL |
608 | { |
609 | fd_set Set; | |
cc2313b7 | 610 | struct timeval tv; |
3b5421b4 AL |
611 | FD_ZERO(&Set); |
612 | FD_SET(Fd,&Set); | |
6d5dd02a AL |
613 | tv.tv_sec = timeout; |
614 | tv.tv_usec = 0; | |
1084d58a | 615 | if (write == true) |
b0db36b1 AL |
616 | { |
617 | int Res; | |
618 | do | |
619 | { | |
620 | Res = select(Fd+1,0,&Set,0,(timeout != 0?&tv:0)); | |
621 | } | |
622 | while (Res < 0 && errno == EINTR); | |
623 | ||
624 | if (Res <= 0) | |
625 | return false; | |
1084d58a AL |
626 | } |
627 | else | |
628 | { | |
b0db36b1 AL |
629 | int Res; |
630 | do | |
631 | { | |
632 | Res = select(Fd+1,&Set,0,0,(timeout != 0?&tv:0)); | |
633 | } | |
634 | while (Res < 0 && errno == EINTR); | |
635 | ||
636 | if (Res <= 0) | |
637 | return false; | |
cc2313b7 | 638 | } |
1084d58a | 639 | |
3b5421b4 AL |
640 | return true; |
641 | } | |
642 | /*}}}*/ | |
54676e1a AL |
643 | // ExecFork - Magical fork that sanitizes the context before execing /*{{{*/ |
644 | // --------------------------------------------------------------------- | |
645 | /* This is used if you want to cleanse the environment for the forked | |
646 | child, it fixes up the important signals and nukes all of the fds, | |
647 | otherwise acts like normal fork. */ | |
75ef8f14 | 648 | pid_t ExecFork() |
54676e1a AL |
649 | { |
650 | // Fork off the process | |
651 | pid_t Process = fork(); | |
652 | if (Process < 0) | |
653 | { | |
654 | cerr << "FATAL -> Failed to fork." << endl; | |
655 | exit(100); | |
656 | } | |
657 | ||
658 | // Spawn the subprocess | |
659 | if (Process == 0) | |
660 | { | |
661 | // Setup the signals | |
662 | signal(SIGPIPE,SIG_DFL); | |
663 | signal(SIGQUIT,SIG_DFL); | |
664 | signal(SIGINT,SIG_DFL); | |
665 | signal(SIGWINCH,SIG_DFL); | |
666 | signal(SIGCONT,SIG_DFL); | |
667 | signal(SIGTSTP,SIG_DFL); | |
75ef8f14 MV |
668 | |
669 | set<int> KeepFDs; | |
670 | Configuration::Item const *Opts = _config->Tree("APT::Keep-Fds"); | |
671 | if (Opts != 0 && Opts->Child != 0) | |
672 | { | |
673 | Opts = Opts->Child; | |
674 | for (; Opts != 0; Opts = Opts->Next) | |
675 | { | |
676 | if (Opts->Value.empty() == true) | |
677 | continue; | |
678 | int fd = atoi(Opts->Value.c_str()); | |
679 | KeepFDs.insert(fd); | |
680 | } | |
681 | } | |
682 | ||
54676e1a AL |
683 | // Close all of our FDs - just in case |
684 | for (int K = 3; K != 40; K++) | |
75ef8f14 MV |
685 | { |
686 | if(KeepFDs.find(K) == KeepFDs.end()) | |
007dc9e0 | 687 | fcntl(K,F_SETFD,FD_CLOEXEC); |
75ef8f14 | 688 | } |
54676e1a AL |
689 | } |
690 | ||
691 | return Process; | |
692 | } | |
693 | /*}}}*/ | |
ddc1d8d0 AL |
694 | // ExecWait - Fancy waitpid /*{{{*/ |
695 | // --------------------------------------------------------------------- | |
2c9a72d1 | 696 | /* Waits for the given sub process. If Reap is set then no errors are |
ddc1d8d0 AL |
697 | generated. Otherwise a failed subprocess will generate a proper descriptive |
698 | message */ | |
3826564e | 699 | bool ExecWait(pid_t Pid,const char *Name,bool Reap) |
ddc1d8d0 AL |
700 | { |
701 | if (Pid <= 1) | |
702 | return true; | |
703 | ||
704 | // Wait and collect the error code | |
705 | int Status; | |
706 | while (waitpid(Pid,&Status,0) != Pid) | |
707 | { | |
708 | if (errno == EINTR) | |
709 | continue; | |
710 | ||
711 | if (Reap == true) | |
712 | return false; | |
713 | ||
db0db9fe | 714 | return _error->Error(_("Waited for %s but it wasn't there"),Name); |
ddc1d8d0 AL |
715 | } |
716 | ||
717 | ||
718 | // Check for an error code. | |
719 | if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0) | |
720 | { | |
721 | if (Reap == true) | |
722 | return false; | |
ab7f4d7c | 723 | if (WIFSIGNALED(Status) != 0) |
40e7fe0e | 724 | { |
ab7f4d7c MV |
725 | if( WTERMSIG(Status) == SIGSEGV) |
726 | return _error->Error(_("Sub-process %s received a segmentation fault."),Name); | |
727 | else | |
728 | return _error->Error(_("Sub-process %s received signal %u."),Name, WTERMSIG(Status)); | |
40e7fe0e | 729 | } |
ddc1d8d0 AL |
730 | |
731 | if (WIFEXITED(Status) != 0) | |
b2e465d6 | 732 | return _error->Error(_("Sub-process %s returned an error code (%u)"),Name,WEXITSTATUS(Status)); |
ddc1d8d0 | 733 | |
b2e465d6 | 734 | return _error->Error(_("Sub-process %s exited unexpectedly"),Name); |
ddc1d8d0 AL |
735 | } |
736 | ||
737 | return true; | |
738 | } | |
739 | /*}}}*/ | |
578bfd0a | 740 | |
699b209e DK |
741 | // ExecCompressor - Open a de/compressor pipe /*{{{*/ |
742 | // --------------------------------------------------------------------- | |
743 | /* This opens the compressor, either in compress mode or decompress | |
744 | mode. FileFd is always the compressor input/output file, | |
745 | OutFd is the created pipe, Input for Compress, Output for Decompress. */ | |
746 | bool ExecCompressor(APT::Configuration::Compressor const &Prog, | |
747 | pid_t *Pid, int const FileFd, int &OutFd, bool const Comp) | |
748 | { | |
749 | if (Pid != NULL) | |
750 | *Pid = -1; | |
751 | ||
752 | // No compression | |
753 | if (Prog.Binary.empty() == true) | |
754 | { | |
755 | OutFd = dup(FileFd); | |
756 | return true; | |
757 | } | |
758 | ||
759 | // Handle 'decompression' of empty files | |
760 | if (Comp == false) | |
761 | { | |
762 | struct stat Buf; | |
763 | fstat(FileFd, &Buf); | |
764 | if (Buf.st_size == 0 && S_ISFIFO(Buf.st_mode) == false) | |
765 | { | |
766 | OutFd = FileFd; | |
767 | return true; | |
768 | } | |
769 | } | |
770 | ||
771 | // Create a data pipe | |
772 | int Pipe[2] = {-1,-1}; | |
773 | if (pipe(Pipe) != 0) | |
774 | return _error->Errno("pipe",_("Failed to create subprocess IPC")); | |
775 | for (int J = 0; J != 2; J++) | |
776 | SetCloseExec(Pipe[J],true); | |
777 | ||
778 | if (Comp == true) | |
779 | OutFd = Pipe[1]; | |
780 | else | |
781 | OutFd = Pipe[0]; | |
782 | ||
783 | // The child.. | |
784 | pid_t child = ExecFork(); | |
785 | if (Pid != NULL) | |
786 | *Pid = child; | |
787 | if (child == 0) | |
788 | { | |
789 | if (Comp == true) | |
790 | { | |
791 | dup2(FileFd,STDOUT_FILENO); | |
792 | dup2(Pipe[0],STDIN_FILENO); | |
793 | } | |
794 | else | |
795 | { | |
796 | dup2(FileFd,STDIN_FILENO); | |
797 | dup2(Pipe[1],STDOUT_FILENO); | |
798 | } | |
799 | ||
800 | SetCloseExec(STDOUT_FILENO,false); | |
801 | SetCloseExec(STDIN_FILENO,false); | |
802 | ||
803 | std::vector<char const*> Args; | |
804 | Args.push_back(Prog.Binary.c_str()); | |
805 | std::vector<std::string> const * const addArgs = | |
806 | (Comp == true) ? &(Prog.CompressArgs) : &(Prog.UncompressArgs); | |
807 | for (std::vector<std::string>::const_iterator a = addArgs->begin(); | |
808 | a != addArgs->end(); ++a) | |
809 | Args.push_back(a->c_str()); | |
810 | Args.push_back(NULL); | |
811 | ||
812 | execvp(Args[0],(char **)&Args[0]); | |
813 | cerr << _("Failed to exec compressor ") << Args[0] << endl; | |
814 | _exit(100); | |
815 | } | |
816 | if (Comp == true) | |
817 | close(Pipe[0]); | |
818 | else | |
819 | close(Pipe[1]); | |
820 | ||
821 | if (Pid == NULL) | |
822 | ExecWait(child, Prog.Binary.c_str(), true); | |
823 | ||
824 | return true; | |
825 | } | |
826 | bool ExecCompressor(APT::Configuration::Compressor const &Prog, | |
827 | pid_t *Pid, std::string const &FileName, int &OutFd, bool const Comp) | |
828 | { | |
829 | if (Pid != NULL) | |
830 | *Pid = -1; | |
831 | ||
832 | // No compression | |
833 | if (Prog.Binary.empty() == true) | |
834 | { | |
835 | if (Comp == true) | |
836 | OutFd = open(FileName.c_str(), O_WRONLY, 0666); | |
837 | else | |
838 | OutFd = open(FileName.c_str(), O_RDONLY); | |
839 | return true; | |
840 | } | |
841 | ||
842 | // Handle 'decompression' of empty files | |
843 | if (Comp == false) | |
844 | { | |
845 | struct stat Buf; | |
846 | stat(FileName.c_str(), &Buf); | |
847 | if (Buf.st_size == 0) | |
848 | { | |
849 | OutFd = open(FileName.c_str(), O_RDONLY); | |
850 | return true; | |
851 | } | |
852 | } | |
853 | ||
854 | // Create a data pipe | |
855 | int Pipe[2] = {-1,-1}; | |
856 | if (pipe(Pipe) != 0) | |
857 | return _error->Errno("pipe",_("Failed to create subprocess IPC")); | |
858 | for (int J = 0; J != 2; J++) | |
859 | SetCloseExec(Pipe[J],true); | |
860 | ||
861 | if (Comp == true) | |
862 | OutFd = Pipe[1]; | |
863 | else | |
864 | OutFd = Pipe[0]; | |
865 | ||
866 | // The child.. | |
867 | pid_t child = ExecFork(); | |
868 | if (Pid != NULL) | |
869 | *Pid = child; | |
870 | if (child == 0) | |
871 | { | |
872 | if (Comp == true) | |
873 | { | |
874 | dup2(Pipe[0],STDIN_FILENO); | |
875 | SetCloseExec(STDIN_FILENO,false); | |
876 | } | |
877 | else | |
878 | { | |
879 | dup2(Pipe[1],STDOUT_FILENO); | |
880 | SetCloseExec(STDOUT_FILENO,false); | |
881 | } | |
882 | ||
883 | std::vector<char const*> Args; | |
884 | Args.push_back(Prog.Binary.c_str()); | |
885 | std::vector<std::string> const * const addArgs = | |
886 | (Comp == true) ? &(Prog.CompressArgs) : &(Prog.UncompressArgs); | |
887 | for (std::vector<std::string>::const_iterator a = addArgs->begin(); | |
888 | a != addArgs->end(); ++a) | |
889 | Args.push_back(a->c_str()); | |
890 | Args.push_back("--stdout"); | |
891 | Args.push_back(FileName.c_str()); | |
892 | Args.push_back(NULL); | |
893 | ||
894 | execvp(Args[0],(char **)&Args[0]); | |
895 | cerr << _("Failed to exec compressor ") << Args[0] << endl; | |
896 | _exit(100); | |
897 | } | |
898 | if (Comp == true) | |
899 | close(Pipe[0]); | |
900 | else | |
901 | close(Pipe[1]); | |
902 | ||
903 | if (Pid == NULL) | |
904 | ExecWait(child, Prog.Binary.c_str(), false); | |
905 | ||
906 | return true; | |
907 | } | |
908 | /*}}}*/ | |
909 | ||
13d87e2e | 910 | // FileFd::Open - Open a file /*{{{*/ |
578bfd0a AL |
911 | // --------------------------------------------------------------------- |
912 | /* The most commonly used open mode combinations are given with Mode */ | |
032bd56f | 913 | bool FileFd::Open(string FileName,OpenMode Mode,CompressMode Compress, unsigned long const Perms) |
578bfd0a | 914 | { |
257e8d66 DK |
915 | if (Mode == ReadOnlyGzip) |
916 | return Open(FileName, ReadOnly, Gzip, Perms); | |
13d87e2e | 917 | Close(); |
032bd56f | 918 | d = new FileFdPrivate; |
699b209e | 919 | d->openmode = Mode; |
1164783d | 920 | Flags = AutoClose; |
257e8d66 | 921 | |
468720c5 DK |
922 | if (Compress == Auto && (Mode & WriteOnly) == WriteOnly) |
923 | return _error->Error("Autodetection on %s only works in ReadOnly openmode!", FileName.c_str()); | |
924 | if ((Mode & WriteOnly) != WriteOnly && (Mode & (Atomic | Create | Empty | Exclusive)) != 0) | |
925 | return _error->Error("ReadOnly mode for %s doesn't accept additional flags!", FileName.c_str()); | |
926 | ||
257e8d66 DK |
927 | int fileflags = 0; |
928 | #define if_FLAGGED_SET(FLAG, MODE) if ((Mode & FLAG) == FLAG) fileflags |= MODE | |
929 | if_FLAGGED_SET(ReadWrite, O_RDWR); | |
930 | else if_FLAGGED_SET(ReadOnly, O_RDONLY); | |
931 | else if_FLAGGED_SET(WriteOnly, O_WRONLY); | |
932 | else return _error->Error("No openmode provided in FileFd::Open for %s", FileName.c_str()); | |
933 | ||
934 | if_FLAGGED_SET(Create, O_CREAT); | |
935 | if_FLAGGED_SET(Exclusive, O_EXCL); | |
936 | else if_FLAGGED_SET(Atomic, O_EXCL); | |
937 | if_FLAGGED_SET(Empty, O_TRUNC); | |
938 | #undef if_FLAGGED_SET | |
939 | ||
468720c5 DK |
940 | // FIXME: Denote inbuilt compressors somehow - as we don't need to have the binaries for them |
941 | std::vector<APT::Configuration::Compressor> const compressors = APT::Configuration::getCompressors(); | |
942 | std::vector<APT::Configuration::Compressor>::const_iterator compressor = compressors.begin(); | |
943 | if (Compress == Auto) | |
944 | { | |
945 | Compress = None; | |
946 | for (; compressor != compressors.end(); ++compressor) | |
947 | { | |
948 | std::string file = std::string(FileName).append(compressor->Extension); | |
949 | if (FileExists(file) == false) | |
950 | continue; | |
951 | FileName = file; | |
952 | if (compressor->Binary == ".") | |
953 | Compress = None; | |
954 | else | |
955 | Compress = Extension; | |
956 | break; | |
957 | } | |
958 | } | |
959 | else if (Compress == Extension) | |
960 | { | |
961 | Compress = None; | |
962 | std::string ext = flExtension(FileName); | |
963 | if (ext != FileName) | |
964 | { | |
965 | ext = "." + ext; | |
966 | for (; compressor != compressors.end(); ++compressor) | |
967 | if (ext == compressor->Extension) | |
968 | break; | |
969 | } | |
970 | } | |
971 | else if (Compress != None) | |
972 | { | |
973 | std::string name; | |
974 | switch (Compress) | |
975 | { | |
976 | case Gzip: name = "gzip"; break; | |
977 | case Bzip2: name = "bzip2"; break; | |
978 | case Lzma: name = "lzma"; break; | |
979 | case Xz: name = "xz"; break; | |
980 | default: return _error->Error("Can't find a match for specified compressor mode for file %s", FileName.c_str()); | |
981 | } | |
982 | for (; compressor != compressors.end(); ++compressor) | |
983 | if (compressor->Name == name) | |
984 | break; | |
985 | if (compressor == compressors.end() && name != "gzip") | |
986 | return _error->Error("Can't find a configured compressor %s for file %s", name.c_str(), FileName.c_str()); | |
987 | } | |
988 | ||
989 | // if we have them, use inbuilt compressors instead of forking | |
990 | if (compressor != compressors.end()) | |
991 | { | |
699b209e | 992 | #ifdef APT_USE_ZLIB |
468720c5 DK |
993 | if (compressor->Name == "gzip") |
994 | { | |
995 | Compress = Gzip; | |
996 | compressor = compressors.end(); | |
997 | } | |
699b209e DK |
998 | else |
999 | #endif | |
1000 | if (compressor->Name == ".") | |
468720c5 DK |
1001 | { |
1002 | Compress = None; | |
1003 | compressor = compressors.end(); | |
1004 | } | |
1005 | } | |
1006 | ||
257e8d66 DK |
1007 | if ((Mode & Atomic) == Atomic) |
1008 | { | |
1009 | Flags |= Replace; | |
1010 | char *name = strdup((FileName + ".XXXXXX").c_str()); | |
1011 | TemporaryFileName = string(mktemp(name)); | |
1012 | free(name); | |
1013 | } | |
1014 | else if ((Mode & (Exclusive | Create)) == (Exclusive | Create)) | |
1015 | { | |
1016 | // for atomic, this will be done by rename in Close() | |
1017 | unlink(FileName.c_str()); | |
1018 | } | |
1019 | if ((Mode & Empty) == Empty) | |
578bfd0a | 1020 | { |
257e8d66 DK |
1021 | struct stat Buf; |
1022 | if (lstat(FileName.c_str(),&Buf) == 0 && S_ISLNK(Buf.st_mode)) | |
1023 | unlink(FileName.c_str()); | |
1024 | } | |
c4fc2fd7 | 1025 | |
468720c5 DK |
1026 | if (compressor != compressors.end()) |
1027 | { | |
1028 | if ((Mode & ReadWrite) == ReadWrite) | |
699b209e | 1029 | return _error->Error("External compressors like %s do not support readwrite mode for file %s", compressor->Name.c_str(), FileName.c_str()); |
4a9db827 | 1030 | |
699b209e DK |
1031 | if (ExecCompressor(*compressor, NULL /*d->compressor_pid*/, FileName, iFd, ((Mode & ReadOnly) != ReadOnly)) == false) |
1032 | return _error->Error("Forking external compressor %s is not implemented for %s", compressor->Name.c_str(), FileName.c_str()); | |
1033 | d->pipe = true; | |
1034 | d->compressor = *compressor; | |
468720c5 DK |
1035 | } |
1036 | else | |
257e8d66 | 1037 | { |
468720c5 DK |
1038 | if (TemporaryFileName.empty() == false) |
1039 | iFd = open(TemporaryFileName.c_str(), fileflags, Perms); | |
1040 | else | |
1041 | iFd = open(FileName.c_str(), fileflags, Perms); | |
1042 | ||
1043 | if (iFd != -1) | |
fc81e8f2 | 1044 | { |
468720c5 DK |
1045 | if (OpenInternDescriptor(Mode, Compress) == false) |
1046 | { | |
1047 | close (iFd); | |
1048 | iFd = -1; | |
1049 | } | |
fc81e8f2 | 1050 | } |
257e8d66 | 1051 | } |
578bfd0a | 1052 | |
257e8d66 | 1053 | if (iFd == -1) |
b2e465d6 | 1054 | return _error->Errno("open",_("Could not open file %s"),FileName.c_str()); |
257e8d66 | 1055 | |
13d87e2e AL |
1056 | this->FileName = FileName; |
1057 | SetCloseExec(iFd,true); | |
1058 | return true; | |
578bfd0a | 1059 | } |
257e8d66 DK |
1060 | /*}}}*/ |
1061 | // FileFd::OpenDescriptor - Open a filedescriptor /*{{{*/ | |
1062 | // --------------------------------------------------------------------- | |
1063 | /* */ | |
1064 | bool FileFd::OpenDescriptor(int Fd, OpenMode Mode, CompressMode Compress, bool AutoClose) | |
144c0969 JAK |
1065 | { |
1066 | Close(); | |
032bd56f | 1067 | d = new FileFdPrivate; |
699b209e | 1068 | d->openmode = Mode; |
144c0969 JAK |
1069 | Flags = (AutoClose) ? FileFd::AutoClose : 0; |
1070 | iFd = Fd; | |
468720c5 DK |
1071 | if (OpenInternDescriptor(Mode, Compress) == false) |
1072 | { | |
1073 | if (AutoClose) | |
1074 | close (iFd); | |
1075 | return _error->Errno("gzdopen",_("Could not open file descriptor %d"), Fd); | |
144c0969 JAK |
1076 | } |
1077 | this->FileName = ""; | |
1078 | return true; | |
468720c5 DK |
1079 | } |
1080 | bool FileFd::OpenInternDescriptor(OpenMode Mode, CompressMode Compress) | |
1081 | { | |
1082 | if (Compress == None) | |
1083 | return true; | |
699b209e | 1084 | #ifdef APT_USE_ZLIB |
468720c5 DK |
1085 | else if (Compress == Gzip) |
1086 | { | |
1087 | if ((Mode & ReadWrite) == ReadWrite) | |
032bd56f | 1088 | d->gz = gzdopen(iFd, "r+"); |
468720c5 | 1089 | else if ((Mode & WriteOnly) == WriteOnly) |
032bd56f | 1090 | d->gz = gzdopen(iFd, "w"); |
468720c5 | 1091 | else |
032bd56f DK |
1092 | d->gz = gzdopen (iFd, "r"); |
1093 | if (d->gz == NULL) | |
468720c5 | 1094 | return false; |
032bd56f | 1095 | Flags |= Compressed; |
468720c5 | 1096 | } |
699b209e | 1097 | #endif |
468720c5 | 1098 | else |
699b209e DK |
1099 | { |
1100 | std::string name; | |
1101 | switch (Compress) | |
1102 | { | |
1103 | case Gzip: name = "gzip"; break; | |
1104 | case Bzip2: name = "bzip2"; break; | |
1105 | case Lzma: name = "lzma"; break; | |
1106 | case Xz: name = "xz"; break; | |
1107 | default: return _error->Error("Can't find a match for specified compressor mode for file %s", FileName.c_str()); | |
1108 | } | |
1109 | std::vector<APT::Configuration::Compressor> const compressors = APT::Configuration::getCompressors(); | |
1110 | std::vector<APT::Configuration::Compressor>::const_iterator compressor = compressors.begin(); | |
1111 | for (; compressor != compressors.end(); ++compressor) | |
1112 | if (compressor->Name == name) | |
1113 | break; | |
1114 | if (compressor == compressors.end() || | |
1115 | ExecCompressor(*compressor, NULL /*&(d->compressor_pid)*/, | |
1116 | FileName, iFd, ((Mode & ReadOnly) != ReadOnly)) == false) | |
1117 | return _error->Error("Forking external compressor %s is not implemented for %s", name.c_str(), FileName.c_str()); | |
1118 | d->pipe = true; | |
1119 | } | |
468720c5 | 1120 | return true; |
144c0969 | 1121 | } |
578bfd0a | 1122 | /*}}}*/ |
8e06abb2 | 1123 | // FileFd::~File - Closes the file /*{{{*/ |
578bfd0a AL |
1124 | // --------------------------------------------------------------------- |
1125 | /* If the proper modes are selected then we close the Fd and possibly | |
1126 | unlink the file on error. */ | |
8e06abb2 | 1127 | FileFd::~FileFd() |
578bfd0a AL |
1128 | { |
1129 | Close(); | |
1130 | } | |
1131 | /*}}}*/ | |
8e06abb2 | 1132 | // FileFd::Read - Read a bit of the file /*{{{*/ |
578bfd0a | 1133 | // --------------------------------------------------------------------- |
b0db36b1 AL |
1134 | /* We are carefull to handle interruption by a signal while reading |
1135 | gracefully. */ | |
650faab0 | 1136 | bool FileFd::Read(void *To,unsigned long long Size,unsigned long long *Actual) |
578bfd0a | 1137 | { |
b0db36b1 AL |
1138 | int Res; |
1139 | errno = 0; | |
f604cf55 AL |
1140 | if (Actual != 0) |
1141 | *Actual = 0; | |
699b209e | 1142 | *((char *)To) = '\0'; |
b0db36b1 | 1143 | do |
578bfd0a | 1144 | { |
699b209e | 1145 | #ifdef APT_USE_ZLIB |
032bd56f DK |
1146 | if (d->gz != NULL) |
1147 | Res = gzread(d->gz,To,Size); | |
a3a03f5d | 1148 | else |
699b209e | 1149 | #endif |
a3a03f5d | 1150 | Res = read(iFd,To,Size); |
b0db36b1 AL |
1151 | if (Res < 0 && errno == EINTR) |
1152 | continue; | |
1153 | if (Res < 0) | |
1154 | { | |
1155 | Flags |= Fail; | |
b2e465d6 | 1156 | return _error->Errno("read",_("Read error")); |
b0db36b1 | 1157 | } |
578bfd0a | 1158 | |
b0db36b1 AL |
1159 | To = (char *)To + Res; |
1160 | Size -= Res; | |
f604cf55 AL |
1161 | if (Actual != 0) |
1162 | *Actual += Res; | |
b0db36b1 AL |
1163 | } |
1164 | while (Res > 0 && Size > 0); | |
1165 | ||
1166 | if (Size == 0) | |
1167 | return true; | |
1168 | ||
ddc1d8d0 | 1169 | // Eof handling |
f604cf55 | 1170 | if (Actual != 0) |
ddc1d8d0 AL |
1171 | { |
1172 | Flags |= HitEof; | |
1173 | return true; | |
1174 | } | |
1175 | ||
b0db36b1 | 1176 | Flags |= Fail; |
650faab0 | 1177 | return _error->Error(_("read, still have %llu to read but none left"), Size); |
578bfd0a AL |
1178 | } |
1179 | /*}}}*/ | |
032bd56f DK |
1180 | // FileFd::ReadLine - Read a complete line from the file /*{{{*/ |
1181 | // --------------------------------------------------------------------- | |
1182 | /* Beware: This method can be quiet slow for big buffers on UNcompressed | |
1183 | files because of the naive implementation! */ | |
1184 | char* FileFd::ReadLine(char *To, unsigned long long const Size) | |
1185 | { | |
699b209e DK |
1186 | *To = '\0'; |
1187 | #ifdef APT_USE_ZLIB | |
032bd56f DK |
1188 | if (d->gz != NULL) |
1189 | return gzgets(d->gz, To, Size); | |
699b209e | 1190 | #endif |
032bd56f DK |
1191 | |
1192 | unsigned long long read = 0; | |
1193 | if (Read(To, Size, &read) == false) | |
1194 | return NULL; | |
1195 | char* c = To; | |
1196 | for (; *c != '\n' && *c != '\0' && read != 0; --read, ++c) | |
1197 | ; // find the end of the line | |
1198 | if (*c != '\0') | |
1199 | *c = '\0'; | |
1200 | if (read != 0) | |
1201 | Seek(Tell() - read); | |
1202 | return To; | |
1203 | } | |
1204 | /*}}}*/ | |
8e06abb2 | 1205 | // FileFd::Write - Write to the file /*{{{*/ |
578bfd0a AL |
1206 | // --------------------------------------------------------------------- |
1207 | /* */ | |
650faab0 | 1208 | bool FileFd::Write(const void *From,unsigned long long Size) |
578bfd0a | 1209 | { |
b0db36b1 AL |
1210 | int Res; |
1211 | errno = 0; | |
1212 | do | |
578bfd0a | 1213 | { |
699b209e | 1214 | #ifdef APT_USE_ZLIB |
032bd56f DK |
1215 | if (d->gz != NULL) |
1216 | Res = gzwrite(d->gz,From,Size); | |
a3a03f5d | 1217 | else |
699b209e | 1218 | #endif |
a3a03f5d | 1219 | Res = write(iFd,From,Size); |
b0db36b1 AL |
1220 | if (Res < 0 && errno == EINTR) |
1221 | continue; | |
1222 | if (Res < 0) | |
1223 | { | |
1224 | Flags |= Fail; | |
b2e465d6 | 1225 | return _error->Errno("write",_("Write error")); |
b0db36b1 AL |
1226 | } |
1227 | ||
1228 | From = (char *)From + Res; | |
1229 | Size -= Res; | |
578bfd0a | 1230 | } |
b0db36b1 | 1231 | while (Res > 0 && Size > 0); |
578bfd0a | 1232 | |
b0db36b1 AL |
1233 | if (Size == 0) |
1234 | return true; | |
1235 | ||
1236 | Flags |= Fail; | |
650faab0 | 1237 | return _error->Error(_("write, still have %llu to write but couldn't"), Size); |
578bfd0a AL |
1238 | } |
1239 | /*}}}*/ | |
8e06abb2 | 1240 | // FileFd::Seek - Seek in the file /*{{{*/ |
578bfd0a AL |
1241 | // --------------------------------------------------------------------- |
1242 | /* */ | |
650faab0 | 1243 | bool FileFd::Seek(unsigned long long To) |
578bfd0a | 1244 | { |
699b209e DK |
1245 | if (d->pipe == true) |
1246 | { | |
1247 | // FIXME: What about OpenDescriptor() stuff here? | |
1248 | close(iFd); | |
1249 | bool result = ExecCompressor(d->compressor, NULL, FileName, iFd, (d->openmode & ReadOnly) != ReadOnly); | |
1250 | if (result == true && To != 0) | |
1251 | result &= Skip(To); | |
1252 | return result; | |
1253 | } | |
a3a03f5d | 1254 | int res; |
699b209e | 1255 | #ifdef USE_ZLIB |
032bd56f DK |
1256 | if (d->gz) |
1257 | res = gzseek(d->gz,To,SEEK_SET); | |
a3a03f5d | 1258 | else |
699b209e | 1259 | #endif |
a3a03f5d | 1260 | res = lseek(iFd,To,SEEK_SET); |
1261 | if (res != (signed)To) | |
578bfd0a AL |
1262 | { |
1263 | Flags |= Fail; | |
650faab0 | 1264 | return _error->Error("Unable to seek to %llu", To); |
578bfd0a AL |
1265 | } |
1266 | ||
727f18af AL |
1267 | return true; |
1268 | } | |
1269 | /*}}}*/ | |
1270 | // FileFd::Skip - Seek in the file /*{{{*/ | |
1271 | // --------------------------------------------------------------------- | |
1272 | /* */ | |
650faab0 | 1273 | bool FileFd::Skip(unsigned long long Over) |
727f18af | 1274 | { |
a3a03f5d | 1275 | int res; |
699b209e | 1276 | #ifdef USE_ZLIB |
032bd56f DK |
1277 | if (d->gz != NULL) |
1278 | res = gzseek(d->gz,Over,SEEK_CUR); | |
a3a03f5d | 1279 | else |
699b209e | 1280 | #endif |
a3a03f5d | 1281 | res = lseek(iFd,Over,SEEK_CUR); |
1282 | if (res < 0) | |
727f18af AL |
1283 | { |
1284 | Flags |= Fail; | |
650faab0 | 1285 | return _error->Error("Unable to seek ahead %llu",Over); |
727f18af AL |
1286 | } |
1287 | ||
6d5dd02a AL |
1288 | return true; |
1289 | } | |
1290 | /*}}}*/ | |
1291 | // FileFd::Truncate - Truncate the file /*{{{*/ | |
1292 | // --------------------------------------------------------------------- | |
1293 | /* */ | |
650faab0 | 1294 | bool FileFd::Truncate(unsigned long long To) |
6d5dd02a | 1295 | { |
032bd56f | 1296 | if (d->gz != NULL) |
a3a03f5d | 1297 | { |
1298 | Flags |= Fail; | |
1299 | return _error->Error("Truncating gzipped files is not implemented (%s)", FileName.c_str()); | |
1300 | } | |
6d5dd02a AL |
1301 | if (ftruncate(iFd,To) != 0) |
1302 | { | |
1303 | Flags |= Fail; | |
650faab0 | 1304 | return _error->Error("Unable to truncate to %llu",To); |
6d5dd02a AL |
1305 | } |
1306 | ||
578bfd0a AL |
1307 | return true; |
1308 | } | |
1309 | /*}}}*/ | |
7f25bdff AL |
1310 | // FileFd::Tell - Current seek position /*{{{*/ |
1311 | // --------------------------------------------------------------------- | |
1312 | /* */ | |
650faab0 | 1313 | unsigned long long FileFd::Tell() |
7f25bdff | 1314 | { |
a3a03f5d | 1315 | off_t Res; |
699b209e | 1316 | #ifdef USE_ZLIB |
032bd56f DK |
1317 | if (d->gz != NULL) |
1318 | Res = gztell(d->gz); | |
a3a03f5d | 1319 | else |
699b209e | 1320 | #endif |
a3a03f5d | 1321 | Res = lseek(iFd,0,SEEK_CUR); |
7f25bdff AL |
1322 | if (Res == (off_t)-1) |
1323 | _error->Errno("lseek","Failed to determine the current file position"); | |
1324 | return Res; | |
1325 | } | |
1326 | /*}}}*/ | |
4260fd39 | 1327 | // FileFd::FileSize - Return the size of the file /*{{{*/ |
578bfd0a AL |
1328 | // --------------------------------------------------------------------- |
1329 | /* */ | |
650faab0 | 1330 | unsigned long long FileFd::FileSize() |
578bfd0a AL |
1331 | { |
1332 | struct stat Buf; | |
699b209e | 1333 | if (d->pipe == false && fstat(iFd,&Buf) != 0) |
44dc669e | 1334 | return _error->Errno("fstat","Unable to determine the file size"); |
699b209e DK |
1335 | |
1336 | // for compressor pipes st_size is undefined and at 'best' zero | |
1337 | if (d->pipe == true || S_ISFIFO(Buf.st_mode)) | |
1338 | { | |
1339 | // we set it here, too, as we get the info here for free | |
1340 | // in theory the Open-methods should take care of it already | |
1341 | d->pipe = true; | |
1342 | if (stat(FileName.c_str(), &Buf) != 0) | |
1343 | return _error->Errno("stat","Unable to determine the file size"); | |
1344 | } | |
1345 | ||
4260fd39 DK |
1346 | return Buf.st_size; |
1347 | } | |
1348 | /*}}}*/ | |
1349 | // FileFd::Size - Return the size of the content in the file /*{{{*/ | |
1350 | // --------------------------------------------------------------------- | |
1351 | /* */ | |
650faab0 | 1352 | unsigned long long FileFd::Size() |
4260fd39 | 1353 | { |
650faab0 | 1354 | unsigned long long size = FileSize(); |
44dc669e | 1355 | |
699b209e DK |
1356 | // for compressor pipes st_size is undefined and at 'best' zero, |
1357 | // so we 'read' the content and 'seek' back - see there | |
1358 | if (d->pipe == true) | |
1359 | { | |
1360 | // FIXME: If we have read first and then FileSize() the report is wrong | |
1361 | size = 0; | |
1362 | char ignore[1000]; | |
1363 | unsigned long long read = 0; | |
1364 | do { | |
1365 | Read(ignore, sizeof(ignore), &read); | |
1366 | size += read; | |
1367 | } while(read != 0); | |
1368 | Seek(0); | |
1369 | } | |
1370 | #ifdef USE_ZLIB | |
44dc669e | 1371 | // only check gzsize if we are actually a gzip file, just checking for |
032bd56f | 1372 | // "gz" is not sufficient as uncompressed files could be opened with |
44dc669e | 1373 | // gzopen in "direct" mode as well |
699b209e | 1374 | else if (d->gz && !gzdirect(d->gz) && size > 0) |
9c182afa MP |
1375 | { |
1376 | /* unfortunately zlib.h doesn't provide a gzsize(), so we have to do | |
1377 | * this ourselves; the original (uncompressed) file size is the last 32 | |
1378 | * bits of the file */ | |
650faab0 | 1379 | // FIXME: Size for gz-files is limited by 32bit… no largefile support |
4260fd39 | 1380 | off_t orig_pos = lseek(iFd, 0, SEEK_CUR); |
9c182afa MP |
1381 | if (lseek(iFd, -4, SEEK_END) < 0) |
1382 | return _error->Errno("lseek","Unable to seek to end of gzipped file"); | |
f330c0f3 | 1383 | size = 0L; |
9c182afa MP |
1384 | if (read(iFd, &size, 4) != 4) |
1385 | return _error->Errno("read","Unable to read original size of gzipped file"); | |
f330c0f3 DK |
1386 | |
1387 | #ifdef WORDS_BIGENDIAN | |
2cae0ccb DK |
1388 | uint32_t tmp_size = size; |
1389 | uint8_t const * const p = (uint8_t const * const) &tmp_size; | |
1390 | tmp_size = (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]; | |
1391 | size = tmp_size; | |
f330c0f3 | 1392 | #endif |
9c182afa MP |
1393 | |
1394 | if (lseek(iFd, orig_pos, SEEK_SET) < 0) | |
1395 | return _error->Errno("lseek","Unable to seek in gzipped file"); | |
1396 | return size; | |
1397 | } | |
699b209e | 1398 | #endif |
9c182afa | 1399 | |
44dc669e | 1400 | return size; |
578bfd0a AL |
1401 | } |
1402 | /*}}}*/ | |
76a763e1 DK |
1403 | // FileFd::ModificationTime - Return the time of last touch /*{{{*/ |
1404 | // --------------------------------------------------------------------- | |
1405 | /* */ | |
1406 | time_t FileFd::ModificationTime() | |
1407 | { | |
1408 | struct stat Buf; | |
699b209e | 1409 | if (d->pipe == false && fstat(iFd,&Buf) != 0) |
76a763e1 DK |
1410 | { |
1411 | _error->Errno("fstat","Unable to determine the modification time of file %s", FileName.c_str()); | |
1412 | return 0; | |
1413 | } | |
699b209e DK |
1414 | |
1415 | // for compressor pipes st_size is undefined and at 'best' zero | |
1416 | if (d->pipe == true || S_ISFIFO(Buf.st_mode)) | |
1417 | { | |
1418 | // we set it here, too, as we get the info here for free | |
1419 | // in theory the Open-methods should take care of it already | |
1420 | d->pipe = true; | |
1421 | if (stat(FileName.c_str(), &Buf) != 0) | |
1422 | { | |
1423 | _error->Errno("fstat","Unable to determine the modification time of file %s", FileName.c_str()); | |
1424 | return 0; | |
1425 | } | |
1426 | } | |
1427 | ||
76a763e1 DK |
1428 | return Buf.st_mtime; |
1429 | } | |
1430 | /*}}}*/ | |
8e06abb2 | 1431 | // FileFd::Close - Close the file if the close flag is set /*{{{*/ |
578bfd0a AL |
1432 | // --------------------------------------------------------------------- |
1433 | /* */ | |
8e06abb2 | 1434 | bool FileFd::Close() |
578bfd0a | 1435 | { |
032bd56f DK |
1436 | if (iFd == -1) |
1437 | return true; | |
1438 | ||
578bfd0a AL |
1439 | bool Res = true; |
1440 | if ((Flags & AutoClose) == AutoClose) | |
d13c2d3f | 1441 | { |
699b209e | 1442 | #ifdef USE_ZLIB |
032bd56f DK |
1443 | if (d != NULL && d->gz != NULL) { |
1444 | int const e = gzclose(d->gz); | |
d13c2d3f | 1445 | // gzdopen() on empty files always fails with "buffer error" here, ignore that |
1446 | if (e != 0 && e != Z_BUF_ERROR) | |
3184b4cf | 1447 | Res &= _error->Errno("close",_("Problem closing the gzip file %s"), FileName.c_str()); |
d13c2d3f | 1448 | } else |
699b209e | 1449 | #endif |
d13c2d3f | 1450 | if (iFd > 0 && close(iFd) != 0) |
3184b4cf | 1451 | Res &= _error->Errno("close",_("Problem closing the file %s"), FileName.c_str()); |
d13c2d3f | 1452 | } |
3010fb0e | 1453 | |
62d073d9 | 1454 | if ((Flags & Replace) == Replace && iFd >= 0) { |
3010fb0e | 1455 | if (rename(TemporaryFileName.c_str(), FileName.c_str()) != 0) |
62d073d9 DK |
1456 | Res &= _error->Errno("rename",_("Problem renaming the file %s to %s"), TemporaryFileName.c_str(), FileName.c_str()); |
1457 | ||
fd3b761e | 1458 | FileName = TemporaryFileName; // for the unlink() below. |
257e8d66 | 1459 | TemporaryFileName.clear(); |
3010fb0e | 1460 | } |
62d073d9 DK |
1461 | |
1462 | iFd = -1; | |
1463 | ||
578bfd0a AL |
1464 | if ((Flags & Fail) == Fail && (Flags & DelOnFail) == DelOnFail && |
1465 | FileName.empty() == false) | |
1466 | if (unlink(FileName.c_str()) != 0) | |
62d073d9 | 1467 | Res &= _error->WarningE("unlnk",_("Problem unlinking the file %s"), FileName.c_str()); |
3010fb0e | 1468 | |
032bd56f DK |
1469 | if (d != NULL) |
1470 | { | |
699b209e DK |
1471 | // if (d->compressor_pid != -1) |
1472 | // ExecWait(d->compressor_pid, "FileFdCompressor", true); | |
032bd56f DK |
1473 | delete d; |
1474 | d = NULL; | |
1475 | } | |
3010fb0e | 1476 | |
578bfd0a AL |
1477 | return Res; |
1478 | } | |
1479 | /*}}}*/ | |
b2e465d6 AL |
1480 | // FileFd::Sync - Sync the file /*{{{*/ |
1481 | // --------------------------------------------------------------------- | |
1482 | /* */ | |
1483 | bool FileFd::Sync() | |
1484 | { | |
1485 | #ifdef _POSIX_SYNCHRONIZED_IO | |
1486 | if (fsync(iFd) != 0) | |
1487 | return _error->Errno("sync",_("Problem syncing the file")); | |
1488 | #endif | |
1489 | return true; | |
1490 | } | |
1491 | /*}}}*/ | |
699b209e DK |
1492 | |
1493 | gzFile FileFd::gzFd() { return (gzFile) d->gz; } |