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