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