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