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