]> git.saurik.com Git - apt.git/blame - apt-pkg/contrib/fileutl.cc
- provide a {Package,Version}List similar to {Package,Version}Set
[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>
75ef8f14 27#include <apt-pkg/configuration.h>
b2e465d6 28
152ab79e 29#include <cstdlib>
4f333a8b 30#include <cstring>
3010fb0e 31#include <cstdio>
4f333a8b 32
4d055c05 33#include <iostream>
578bfd0a 34#include <unistd.h>
2c206aa4 35#include <fcntl.h>
578bfd0a 36#include <sys/stat.h>
578bfd0a 37#include <sys/types.h>
cc2313b7 38#include <sys/time.h>
1ae93c94 39#include <sys/wait.h>
46e39c8e 40#include <dirent.h>
54676e1a 41#include <signal.h>
65a1e968 42#include <errno.h>
75ef8f14 43#include <set>
46e39c8e 44#include <algorithm>
2cae0ccb 45
2a79d5b5 46#ifdef WORDS_BIGENDIAN
2cae0ccb
DK
47#include <inttypes.h>
48#endif
ea542140
DK
49
50#include <apti18n.h>
578bfd0a
AL
51 /*}}}*/
52
4d055c05
AL
53using namespace std;
54
614adaa0
MV
55// RunScripts - Run a set of scripts from a configuration subtree /*{{{*/
56// ---------------------------------------------------------------------
57/* */
58bool RunScripts(const char *Cnf)
59{
60 Configuration::Item const *Opts = _config->Tree(Cnf);
61 if (Opts == 0 || Opts->Child == 0)
62 return true;
63 Opts = Opts->Child;
64
65 // Fork for running the system calls
66 pid_t Child = ExecFork();
67
68 // This is the child
69 if (Child == 0)
70 {
cfba4f69
MV
71 if (_config->FindDir("DPkg::Chroot-Directory","/") != "/")
72 {
73 std::cerr << "Chrooting into "
74 << _config->FindDir("DPkg::Chroot-Directory")
75 << std::endl;
76 if (chroot(_config->FindDir("DPkg::Chroot-Directory","/").c_str()) != 0)
77 _exit(100);
78 }
79
614adaa0
MV
80 if (chdir("/tmp/") != 0)
81 _exit(100);
82
83 unsigned int Count = 1;
84 for (; Opts != 0; Opts = Opts->Next, Count++)
85 {
86 if (Opts->Value.empty() == true)
87 continue;
88
89 if (system(Opts->Value.c_str()) != 0)
90 _exit(100+Count);
91 }
92 _exit(0);
93 }
94
95 // Wait for the child
96 int Status = 0;
97 while (waitpid(Child,&Status,0) != Child)
98 {
99 if (errno == EINTR)
100 continue;
101 return _error->Errno("waitpid","Couldn't wait for subprocess");
102 }
103
104 // Restore sig int/quit
105 signal(SIGQUIT,SIG_DFL);
106 signal(SIGINT,SIG_DFL);
107
108 // Check for an error code.
109 if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
110 {
111 unsigned int Count = WEXITSTATUS(Status);
112 if (Count > 100)
113 {
114 Count -= 100;
115 for (; Opts != 0 && Count != 1; Opts = Opts->Next, Count--);
116 _error->Error("Problem executing scripts %s '%s'",Cnf,Opts->Value.c_str());
117 }
118
119 return _error->Error("Sub-process returned an error code");
120 }
121
122 return true;
123}
124 /*}}}*/
125
578bfd0a
AL
126// CopyFile - Buffered copy of a file /*{{{*/
127// ---------------------------------------------------------------------
128/* The caller is expected to set things so that failure causes erasure */
8b89e57f 129bool CopyFile(FileFd &From,FileFd &To)
578bfd0a
AL
130{
131 if (From.IsOpen() == false || To.IsOpen() == false)
132 return false;
133
134 // Buffered copy between fds
b2e465d6 135 SPtrArray<unsigned char> Buf = new unsigned char[64000];
650faab0 136 unsigned long long Size = From.Size();
b0db36b1 137 while (Size != 0)
578bfd0a 138 {
650faab0 139 unsigned long long ToRead = Size;
b0db36b1
AL
140 if (Size > 64000)
141 ToRead = 64000;
142
4a6d5862 143 if (From.Read(Buf,ToRead) == false ||
b0db36b1 144 To.Write(Buf,ToRead) == false)
578bfd0a 145 return false;
b0db36b1
AL
146
147 Size -= ToRead;
578bfd0a
AL
148 }
149
578bfd0a
AL
150 return true;
151}
152 /*}}}*/
153// GetLock - Gets a lock file /*{{{*/
154// ---------------------------------------------------------------------
155/* This will create an empty file of the given name and lock it. Once this
156 is done all other calls to GetLock in any other process will fail with
157 -1. The return result is the fd of the file, the call should call
158 close at some time. */
159int GetLock(string File,bool Errors)
160{
f659b39a
OS
161 // GetLock() is used in aptitude on directories with public-write access
162 // Use O_NOFOLLOW here to prevent symlink traversal attacks
163 int FD = open(File.c_str(),O_RDWR | O_CREAT | O_NOFOLLOW,0640);
578bfd0a
AL
164 if (FD < 0)
165 {
b2e465d6
AL
166 // Read only .. cant have locking problems there.
167 if (errno == EROFS)
168 {
169 _error->Warning(_("Not using locking for read only lock file %s"),File.c_str());
170 return dup(0); // Need something for the caller to close
171 }
172
578bfd0a 173 if (Errors == true)
b2e465d6
AL
174 _error->Errno("open",_("Could not open lock file %s"),File.c_str());
175
176 // Feh.. We do this to distinguish the lock vs open case..
177 errno = EPERM;
578bfd0a
AL
178 return -1;
179 }
b2e465d6
AL
180 SetCloseExec(FD,true);
181
578bfd0a
AL
182 // Aquire a write lock
183 struct flock fl;
c71bc556
AL
184 fl.l_type = F_WRLCK;
185 fl.l_whence = SEEK_SET;
186 fl.l_start = 0;
187 fl.l_len = 0;
578bfd0a
AL
188 if (fcntl(FD,F_SETLK,&fl) == -1)
189 {
d89df07a
AL
190 if (errno == ENOLCK)
191 {
b2e465d6
AL
192 _error->Warning(_("Not using locking for nfs mounted lock file %s"),File.c_str());
193 return dup(0); // Need something for the caller to close
d89df07a 194 }
578bfd0a 195 if (Errors == true)
b2e465d6
AL
196 _error->Errno("open",_("Could not get lock %s"),File.c_str());
197
198 int Tmp = errno;
578bfd0a 199 close(FD);
b2e465d6 200 errno = Tmp;
578bfd0a
AL
201 return -1;
202 }
203
204 return FD;
205}
206 /*}}}*/
207// FileExists - Check if a file exists /*{{{*/
208// ---------------------------------------------------------------------
36f1098a 209/* Beware: Directories are also files! */
578bfd0a
AL
210bool FileExists(string File)
211{
212 struct stat Buf;
213 if (stat(File.c_str(),&Buf) != 0)
214 return false;
215 return true;
216}
217 /*}}}*/
36f1098a
DK
218// RealFileExists - Check if a file exists and if it is really a file /*{{{*/
219// ---------------------------------------------------------------------
220/* */
221bool RealFileExists(string File)
222{
223 struct stat Buf;
224 if (stat(File.c_str(),&Buf) != 0)
225 return false;
226 return ((Buf.st_mode & S_IFREG) != 0);
227}
228 /*}}}*/
1cd1c398
DK
229// DirectoryExists - Check if a directory exists and is really one /*{{{*/
230// ---------------------------------------------------------------------
231/* */
232bool DirectoryExists(string const &Path)
233{
234 struct stat Buf;
235 if (stat(Path.c_str(),&Buf) != 0)
236 return false;
237 return ((Buf.st_mode & S_IFDIR) != 0);
238}
239 /*}}}*/
240// CreateDirectory - poor man's mkdir -p guarded by a parent directory /*{{{*/
241// ---------------------------------------------------------------------
242/* This method will create all directories needed for path in good old
243 mkdir -p style but refuses to do this if Parent is not a prefix of
244 this Path. Example: /var/cache/ and /var/cache/apt/archives are given,
245 so it will create apt/archives if /var/cache exists - on the other
246 hand if the parent is /var/lib the creation will fail as this path
247 is not a parent of the path to be generated. */
248bool CreateDirectory(string const &Parent, string const &Path)
249{
250 if (Parent.empty() == true || Path.empty() == true)
251 return false;
252
253 if (DirectoryExists(Path) == true)
254 return true;
255
256 if (DirectoryExists(Parent) == false)
257 return false;
258
259 // we are not going to create directories "into the blue"
260 if (Path.find(Parent, 0) != 0)
261 return false;
262
263 vector<string> const dirs = VectorizeString(Path.substr(Parent.size()), '/');
264 string progress = Parent;
265 for (vector<string>::const_iterator d = dirs.begin(); d != dirs.end(); ++d)
266 {
267 if (d->empty() == true)
268 continue;
269
270 progress.append("/").append(*d);
271 if (DirectoryExists(progress) == true)
272 continue;
273
274 if (mkdir(progress.c_str(), 0755) != 0)
275 return false;
276 }
277 return true;
278}
279 /*}}}*/
7753e468 280// CreateAPTDirectoryIfNeeded - ensure that the given directory exists /*{{{*/
b29c3712
DK
281// ---------------------------------------------------------------------
282/* a small wrapper around CreateDirectory to check if it exists and to
283 remove the trailing "/apt/" from the parent directory if needed */
7753e468 284bool CreateAPTDirectoryIfNeeded(string const &Parent, string const &Path)
b29c3712
DK
285{
286 if (DirectoryExists(Path) == true)
287 return true;
288
289 size_t const len = Parent.size();
290 if (len > 5 && Parent.find("/apt/", len - 6, 5) == len - 5)
291 {
292 if (CreateDirectory(Parent.substr(0,len-5), Path) == true)
293 return true;
294 }
295 else if (CreateDirectory(Parent, Path) == true)
296 return true;
297
298 return false;
299}
300 /*}}}*/
46e39c8e
MV
301// GetListOfFilesInDir - returns a vector of files in the given dir /*{{{*/
302// ---------------------------------------------------------------------
303/* If an extension is given only files with this extension are included
304 in the returned vector, otherwise every "normal" file is included. */
b39c1859
MV
305std::vector<string> GetListOfFilesInDir(string const &Dir, string const &Ext,
306 bool const &SortList, bool const &AllowNoExt)
307{
308 std::vector<string> ext;
309 ext.reserve(2);
310 if (Ext.empty() == false)
311 ext.push_back(Ext);
312 if (AllowNoExt == true && ext.empty() == false)
313 ext.push_back("");
314 return GetListOfFilesInDir(Dir, ext, SortList);
315}
316std::vector<string> GetListOfFilesInDir(string const &Dir, std::vector<string> const &Ext,
317 bool const &SortList)
318{
319 // Attention debuggers: need to be set with the environment config file!
320 bool const Debug = _config->FindB("Debug::GetListOfFilesInDir", false);
321 if (Debug == true)
322 {
323 std::clog << "Accept in " << Dir << " only files with the following " << Ext.size() << " extensions:" << std::endl;
324 if (Ext.empty() == true)
325 std::clog << "\tNO extension" << std::endl;
326 else
327 for (std::vector<string>::const_iterator e = Ext.begin();
328 e != Ext.end(); ++e)
329 std::clog << '\t' << (e->empty() == true ? "NO" : *e) << " extension" << std::endl;
330 }
331
46e39c8e 332 std::vector<string> List;
36f1098a
DK
333
334 if (DirectoryExists(Dir.c_str()) == false)
335 {
336 _error->Error(_("List of files can't be created as '%s' is not a directory"), Dir.c_str());
337 return List;
338 }
339
1408e219 340 Configuration::MatchAgainstConfig SilentIgnore("Dir::Ignore-Files-Silently");
46e39c8e
MV
341 DIR *D = opendir(Dir.c_str());
342 if (D == 0)
343 {
344 _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
345 return List;
346 }
347
348 for (struct dirent *Ent = readdir(D); Ent != 0; Ent = readdir(D))
349 {
b39c1859 350 // skip "hidden" files
46e39c8e
MV
351 if (Ent->d_name[0] == '.')
352 continue;
353
491058e3
DK
354 // Make sure it is a file and not something else
355 string const File = flCombine(Dir,Ent->d_name);
356#ifdef _DIRENT_HAVE_D_TYPE
357 if (Ent->d_type != DT_REG)
358#endif
359 {
360 if (RealFileExists(File.c_str()) == false)
361 {
362 if (SilentIgnore.Match(Ent->d_name) == false)
363 _error->Notice(_("Ignoring '%s' in directory '%s' as it is not a regular file"), Ent->d_name, Dir.c_str());
364 continue;
365 }
366 }
367
b39c1859
MV
368 // check for accepted extension:
369 // no extension given -> periods are bad as hell!
370 // extensions given -> "" extension allows no extension
371 if (Ext.empty() == false)
372 {
373 string d_ext = flExtension(Ent->d_name);
374 if (d_ext == Ent->d_name) // no extension
375 {
376 if (std::find(Ext.begin(), Ext.end(), "") == Ext.end())
377 {
378 if (Debug == true)
379 std::clog << "Bad file: " << Ent->d_name << " → no extension" << std::endl;
5edc3966 380 if (SilentIgnore.Match(Ent->d_name) == false)
491058e3 381 _error->Notice(_("Ignoring file '%s' in directory '%s' as it has no filename extension"), Ent->d_name, Dir.c_str());
b39c1859
MV
382 continue;
383 }
384 }
385 else if (std::find(Ext.begin(), Ext.end(), d_ext) == Ext.end())
386 {
387 if (Debug == true)
388 std::clog << "Bad file: " << Ent->d_name << " → bad extension »" << flExtension(Ent->d_name) << "«" << std::endl;
1408e219 389 if (SilentIgnore.Match(Ent->d_name) == false)
491058e3 390 _error->Notice(_("Ignoring file '%s' in directory '%s' as it has an invalid filename extension"), Ent->d_name, Dir.c_str());
b39c1859
MV
391 continue;
392 }
393 }
46e39c8e 394
b39c1859 395 // Skip bad filenames ala run-parts
46e39c8e
MV
396 const char *C = Ent->d_name;
397 for (; *C != 0; ++C)
398 if (isalpha(*C) == 0 && isdigit(*C) == 0
b39c1859
MV
399 && *C != '_' && *C != '-') {
400 // no required extension -> dot is a bad character
401 if (*C == '.' && Ext.empty() == false)
402 continue;
46e39c8e 403 break;
b39c1859 404 }
46e39c8e 405
b39c1859 406 // we don't reach the end of the name -> bad character included
46e39c8e 407 if (*C != 0)
b39c1859
MV
408 {
409 if (Debug == true)
410 std::clog << "Bad file: " << Ent->d_name << " → bad character »"
411 << *C << "« in filename (period allowed: " << (Ext.empty() ? "no" : "yes") << ")" << std::endl;
412 continue;
413 }
414
415 // skip filenames which end with a period. These are never valid
416 if (*(C - 1) == '.')
417 {
418 if (Debug == true)
419 std::clog << "Bad file: " << Ent->d_name << " → Period as last character" << std::endl;
46e39c8e 420 continue;
b39c1859 421 }
46e39c8e 422
b39c1859
MV
423 if (Debug == true)
424 std::clog << "Accept file: " << Ent->d_name << " in " << Dir << std::endl;
46e39c8e
MV
425 List.push_back(File);
426 }
427 closedir(D);
428
429 if (SortList == true)
430 std::sort(List.begin(),List.end());
431 return List;
432}
433 /*}}}*/
578bfd0a
AL
434// SafeGetCWD - This is a safer getcwd that returns a dynamic string /*{{{*/
435// ---------------------------------------------------------------------
436/* We return / on failure. */
437string SafeGetCWD()
438{
439 // Stash the current dir.
440 char S[300];
441 S[0] = 0;
7f25bdff 442 if (getcwd(S,sizeof(S)-2) == 0)
578bfd0a 443 return "/";
7f25bdff
AL
444 unsigned int Len = strlen(S);
445 S[Len] = '/';
446 S[Len+1] = 0;
578bfd0a
AL
447 return S;
448}
449 /*}}}*/
2ec858bc
MV
450// GetModificationTime - Get the mtime of the given file or -1 on error /*{{{*/
451// ---------------------------------------------------------------------
452/* We return / on failure. */
453time_t GetModificationTime(string const &Path)
454{
455 struct stat St;
456 if (stat(Path.c_str(), &St) < 0)
457 return -1;
458 return St.st_mtime;
459}
460 /*}}}*/
8ce4327b
AL
461// flNotDir - Strip the directory from the filename /*{{{*/
462// ---------------------------------------------------------------------
463/* */
464string flNotDir(string File)
465{
466 string::size_type Res = File.rfind('/');
467 if (Res == string::npos)
468 return File;
469 Res++;
470 return string(File,Res,Res - File.length());
471}
472 /*}}}*/
d38b7b3d
AL
473// flNotFile - Strip the file from the directory name /*{{{*/
474// ---------------------------------------------------------------------
171c45bc 475/* Result ends in a / */
d38b7b3d
AL
476string flNotFile(string File)
477{
478 string::size_type Res = File.rfind('/');
479 if (Res == string::npos)
171c45bc 480 return "./";
d38b7b3d
AL
481 Res++;
482 return string(File,0,Res);
483}
484 /*}}}*/
b2e465d6
AL
485// flExtension - Return the extension for the file /*{{{*/
486// ---------------------------------------------------------------------
487/* */
488string flExtension(string File)
489{
490 string::size_type Res = File.rfind('.');
491 if (Res == string::npos)
492 return File;
493 Res++;
494 return string(File,Res,Res - File.length());
495}
496 /*}}}*/
421c8d10
AL
497// flNoLink - If file is a symlink then deref it /*{{{*/
498// ---------------------------------------------------------------------
499/* If the name is not a link then the returned path is the input. */
500string flNoLink(string File)
501{
502 struct stat St;
503 if (lstat(File.c_str(),&St) != 0 || S_ISLNK(St.st_mode) == 0)
504 return File;
505 if (stat(File.c_str(),&St) != 0)
506 return File;
507
508 /* Loop resolving the link. There is no need to limit the number of
509 loops because the stat call above ensures that the symlink is not
510 circular */
511 char Buffer[1024];
512 string NFile = File;
513 while (1)
514 {
515 // Read the link
516 int Res;
517 if ((Res = readlink(NFile.c_str(),Buffer,sizeof(Buffer))) <= 0 ||
518 (unsigned)Res >= sizeof(Buffer))
519 return File;
520
521 // Append or replace the previous path
522 Buffer[Res] = 0;
523 if (Buffer[0] == '/')
524 NFile = Buffer;
525 else
526 NFile = flNotFile(NFile) + Buffer;
527
528 // See if we are done
529 if (lstat(NFile.c_str(),&St) != 0)
530 return File;
531 if (S_ISLNK(St.st_mode) == 0)
532 return NFile;
533 }
534}
535 /*}}}*/
b2e465d6
AL
536// flCombine - Combine a file and a directory /*{{{*/
537// ---------------------------------------------------------------------
538/* If the file is an absolute path then it is just returned, otherwise
539 the directory is pre-pended to it. */
540string flCombine(string Dir,string File)
541{
542 if (File.empty() == true)
543 return string();
544
545 if (File[0] == '/' || Dir.empty() == true)
546 return File;
547 if (File.length() >= 2 && File[0] == '.' && File[1] == '/')
548 return File;
549 if (Dir[Dir.length()-1] == '/')
550 return Dir + File;
551 return Dir + '/' + File;
552}
553 /*}}}*/
3b5421b4
AL
554// SetCloseExec - Set the close on exec flag /*{{{*/
555// ---------------------------------------------------------------------
556/* */
557void SetCloseExec(int Fd,bool Close)
558{
559 if (fcntl(Fd,F_SETFD,(Close == false)?0:FD_CLOEXEC) != 0)
560 {
561 cerr << "FATAL -> Could not set close on exec " << strerror(errno) << endl;
562 exit(100);
563 }
564}
565 /*}}}*/
566// SetNonBlock - Set the nonblocking flag /*{{{*/
567// ---------------------------------------------------------------------
568/* */
569void SetNonBlock(int Fd,bool Block)
570{
0a8a80e5
AL
571 int Flags = fcntl(Fd,F_GETFL) & (~O_NONBLOCK);
572 if (fcntl(Fd,F_SETFL,Flags | ((Block == false)?0:O_NONBLOCK)) != 0)
3b5421b4
AL
573 {
574 cerr << "FATAL -> Could not set non-blocking flag " << strerror(errno) << endl;
575 exit(100);
576 }
577}
578 /*}}}*/
579// WaitFd - Wait for a FD to become readable /*{{{*/
580// ---------------------------------------------------------------------
b2e465d6 581/* This waits for a FD to become readable using select. It is useful for
6d5dd02a
AL
582 applications making use of non-blocking sockets. The timeout is
583 in seconds. */
1084d58a 584bool WaitFd(int Fd,bool write,unsigned long timeout)
3b5421b4
AL
585{
586 fd_set Set;
cc2313b7 587 struct timeval tv;
3b5421b4
AL
588 FD_ZERO(&Set);
589 FD_SET(Fd,&Set);
6d5dd02a
AL
590 tv.tv_sec = timeout;
591 tv.tv_usec = 0;
1084d58a 592 if (write == true)
b0db36b1
AL
593 {
594 int Res;
595 do
596 {
597 Res = select(Fd+1,0,&Set,0,(timeout != 0?&tv:0));
598 }
599 while (Res < 0 && errno == EINTR);
600
601 if (Res <= 0)
602 return false;
1084d58a
AL
603 }
604 else
605 {
b0db36b1
AL
606 int Res;
607 do
608 {
609 Res = select(Fd+1,&Set,0,0,(timeout != 0?&tv:0));
610 }
611 while (Res < 0 && errno == EINTR);
612
613 if (Res <= 0)
614 return false;
cc2313b7 615 }
1084d58a 616
3b5421b4
AL
617 return true;
618}
619 /*}}}*/
54676e1a
AL
620// ExecFork - Magical fork that sanitizes the context before execing /*{{{*/
621// ---------------------------------------------------------------------
622/* This is used if you want to cleanse the environment for the forked
623 child, it fixes up the important signals and nukes all of the fds,
624 otherwise acts like normal fork. */
75ef8f14 625pid_t ExecFork()
54676e1a
AL
626{
627 // Fork off the process
628 pid_t Process = fork();
629 if (Process < 0)
630 {
631 cerr << "FATAL -> Failed to fork." << endl;
632 exit(100);
633 }
634
635 // Spawn the subprocess
636 if (Process == 0)
637 {
638 // Setup the signals
639 signal(SIGPIPE,SIG_DFL);
640 signal(SIGQUIT,SIG_DFL);
641 signal(SIGINT,SIG_DFL);
642 signal(SIGWINCH,SIG_DFL);
643 signal(SIGCONT,SIG_DFL);
644 signal(SIGTSTP,SIG_DFL);
75ef8f14
MV
645
646 set<int> KeepFDs;
647 Configuration::Item const *Opts = _config->Tree("APT::Keep-Fds");
648 if (Opts != 0 && Opts->Child != 0)
649 {
650 Opts = Opts->Child;
651 for (; Opts != 0; Opts = Opts->Next)
652 {
653 if (Opts->Value.empty() == true)
654 continue;
655 int fd = atoi(Opts->Value.c_str());
656 KeepFDs.insert(fd);
657 }
658 }
659
54676e1a
AL
660 // Close all of our FDs - just in case
661 for (int K = 3; K != 40; K++)
75ef8f14
MV
662 {
663 if(KeepFDs.find(K) == KeepFDs.end())
007dc9e0 664 fcntl(K,F_SETFD,FD_CLOEXEC);
75ef8f14 665 }
54676e1a
AL
666 }
667
668 return Process;
669}
670 /*}}}*/
ddc1d8d0
AL
671// ExecWait - Fancy waitpid /*{{{*/
672// ---------------------------------------------------------------------
2c9a72d1 673/* Waits for the given sub process. If Reap is set then no errors are
ddc1d8d0
AL
674 generated. Otherwise a failed subprocess will generate a proper descriptive
675 message */
3826564e 676bool ExecWait(pid_t Pid,const char *Name,bool Reap)
ddc1d8d0
AL
677{
678 if (Pid <= 1)
679 return true;
680
681 // Wait and collect the error code
682 int Status;
683 while (waitpid(Pid,&Status,0) != Pid)
684 {
685 if (errno == EINTR)
686 continue;
687
688 if (Reap == true)
689 return false;
690
db0db9fe 691 return _error->Error(_("Waited for %s but it wasn't there"),Name);
ddc1d8d0
AL
692 }
693
694
695 // Check for an error code.
696 if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
697 {
698 if (Reap == true)
699 return false;
ab7f4d7c 700 if (WIFSIGNALED(Status) != 0)
40e7fe0e 701 {
ab7f4d7c
MV
702 if( WTERMSIG(Status) == SIGSEGV)
703 return _error->Error(_("Sub-process %s received a segmentation fault."),Name);
704 else
705 return _error->Error(_("Sub-process %s received signal %u."),Name, WTERMSIG(Status));
40e7fe0e 706 }
ddc1d8d0
AL
707
708 if (WIFEXITED(Status) != 0)
b2e465d6 709 return _error->Error(_("Sub-process %s returned an error code (%u)"),Name,WEXITSTATUS(Status));
ddc1d8d0 710
b2e465d6 711 return _error->Error(_("Sub-process %s exited unexpectedly"),Name);
ddc1d8d0
AL
712 }
713
714 return true;
715}
716 /*}}}*/
578bfd0a 717
13d87e2e 718// FileFd::Open - Open a file /*{{{*/
578bfd0a
AL
719// ---------------------------------------------------------------------
720/* The most commonly used open mode combinations are given with Mode */
13d87e2e 721bool FileFd::Open(string FileName,OpenMode Mode, unsigned long Perms)
578bfd0a 722{
13d87e2e 723 Close();
1164783d 724 Flags = AutoClose;
578bfd0a
AL
725 switch (Mode)
726 {
727 case ReadOnly:
728 iFd = open(FileName.c_str(),O_RDONLY);
729 break;
c4fc2fd7 730
731 case ReadOnlyGzip:
732 iFd = open(FileName.c_str(),O_RDONLY);
d13c2d3f 733 if (iFd > 0) {
c4fc2fd7 734 gz = gzdopen (iFd, "r");
735 if (gz == NULL) {
736 close (iFd);
737 iFd = -1;
738 }
a3a03f5d 739 }
578bfd0a
AL
740 break;
741
4a9db827 742 case WriteAtomic:
50b513a1 743 {
3010fb0e
JAK
744 Flags |= Replace;
745 char *name = strdup((FileName + ".XXXXXX").c_str());
746 TemporaryFileName = string(mktemp(name));
747 iFd = open(TemporaryFileName.c_str(),O_RDWR | O_CREAT | O_EXCL,Perms);
748 free(name);
50b513a1
AL
749 break;
750 }
4a9db827 751
fc81e8f2
JAK
752 case WriteEmpty:
753 {
754 struct stat Buf;
755 if (lstat(FileName.c_str(),&Buf) == 0 && S_ISLNK(Buf.st_mode))
756 unlink(FileName.c_str());
757 iFd = open(FileName.c_str(),O_RDWR | O_CREAT | O_TRUNC,Perms);
758 break;
759 }
578bfd0a
AL
760
761 case WriteExists:
762 iFd = open(FileName.c_str(),O_RDWR);
763 break;
0a8e3465
AL
764
765 case WriteAny:
766 iFd = open(FileName.c_str(),O_RDWR | O_CREAT,Perms);
d38b7b3d 767 break;
f08fcf34
AL
768
769 case WriteTemp:
4decd43c
AL
770 unlink(FileName.c_str());
771 iFd = open(FileName.c_str(),O_RDWR | O_CREAT | O_EXCL,Perms);
f08fcf34 772 break;
578bfd0a
AL
773 }
774
775 if (iFd < 0)
b2e465d6 776 return _error->Errno("open",_("Could not open file %s"),FileName.c_str());
13d87e2e
AL
777
778 this->FileName = FileName;
779 SetCloseExec(iFd,true);
780 return true;
578bfd0a 781}
144c0969
JAK
782
783bool FileFd::OpenDescriptor(int Fd, OpenMode Mode, bool AutoClose)
784{
785 Close();
786 Flags = (AutoClose) ? FileFd::AutoClose : 0;
787 iFd = Fd;
788 if (Mode == ReadOnlyGzip) {
789 gz = gzdopen (iFd, "r");
790 if (gz == NULL) {
791 if (AutoClose)
792 close (iFd);
793 return _error->Errno("gzdopen",_("Could not open file descriptor %d"),
794 Fd);
795 }
796 }
797 this->FileName = "";
798 return true;
799}
578bfd0a 800 /*}}}*/
8e06abb2 801// FileFd::~File - Closes the file /*{{{*/
578bfd0a
AL
802// ---------------------------------------------------------------------
803/* If the proper modes are selected then we close the Fd and possibly
804 unlink the file on error. */
8e06abb2 805FileFd::~FileFd()
578bfd0a
AL
806{
807 Close();
808}
809 /*}}}*/
8e06abb2 810// FileFd::Read - Read a bit of the file /*{{{*/
578bfd0a 811// ---------------------------------------------------------------------
b0db36b1
AL
812/* We are carefull to handle interruption by a signal while reading
813 gracefully. */
650faab0 814bool FileFd::Read(void *To,unsigned long long Size,unsigned long long *Actual)
578bfd0a 815{
b0db36b1
AL
816 int Res;
817 errno = 0;
f604cf55
AL
818 if (Actual != 0)
819 *Actual = 0;
820
b0db36b1 821 do
578bfd0a 822 {
a3a03f5d 823 if (gz != NULL)
824 Res = gzread(gz,To,Size);
825 else
826 Res = read(iFd,To,Size);
b0db36b1
AL
827 if (Res < 0 && errno == EINTR)
828 continue;
829 if (Res < 0)
830 {
831 Flags |= Fail;
b2e465d6 832 return _error->Errno("read",_("Read error"));
b0db36b1 833 }
578bfd0a 834
b0db36b1
AL
835 To = (char *)To + Res;
836 Size -= Res;
f604cf55
AL
837 if (Actual != 0)
838 *Actual += Res;
b0db36b1
AL
839 }
840 while (Res > 0 && Size > 0);
841
842 if (Size == 0)
843 return true;
844
ddc1d8d0 845 // Eof handling
f604cf55 846 if (Actual != 0)
ddc1d8d0
AL
847 {
848 Flags |= HitEof;
849 return true;
850 }
851
b0db36b1 852 Flags |= Fail;
650faab0 853 return _error->Error(_("read, still have %llu to read but none left"), Size);
578bfd0a
AL
854}
855 /*}}}*/
8e06abb2 856// FileFd::Write - Write to the file /*{{{*/
578bfd0a
AL
857// ---------------------------------------------------------------------
858/* */
650faab0 859bool FileFd::Write(const void *From,unsigned long long Size)
578bfd0a 860{
b0db36b1
AL
861 int Res;
862 errno = 0;
863 do
578bfd0a 864 {
a3a03f5d 865 if (gz != NULL)
866 Res = gzwrite(gz,From,Size);
867 else
868 Res = write(iFd,From,Size);
b0db36b1
AL
869 if (Res < 0 && errno == EINTR)
870 continue;
871 if (Res < 0)
872 {
873 Flags |= Fail;
b2e465d6 874 return _error->Errno("write",_("Write error"));
b0db36b1
AL
875 }
876
877 From = (char *)From + Res;
878 Size -= Res;
578bfd0a 879 }
b0db36b1 880 while (Res > 0 && Size > 0);
578bfd0a 881
b0db36b1
AL
882 if (Size == 0)
883 return true;
884
885 Flags |= Fail;
650faab0 886 return _error->Error(_("write, still have %llu to write but couldn't"), Size);
578bfd0a
AL
887}
888 /*}}}*/
8e06abb2 889// FileFd::Seek - Seek in the file /*{{{*/
578bfd0a
AL
890// ---------------------------------------------------------------------
891/* */
650faab0 892bool FileFd::Seek(unsigned long long To)
578bfd0a 893{
a3a03f5d 894 int res;
895 if (gz)
896 res = gzseek(gz,To,SEEK_SET);
897 else
898 res = lseek(iFd,To,SEEK_SET);
899 if (res != (signed)To)
578bfd0a
AL
900 {
901 Flags |= Fail;
650faab0 902 return _error->Error("Unable to seek to %llu", To);
578bfd0a
AL
903 }
904
727f18af
AL
905 return true;
906}
907 /*}}}*/
908// FileFd::Skip - Seek in the file /*{{{*/
909// ---------------------------------------------------------------------
910/* */
650faab0 911bool FileFd::Skip(unsigned long long Over)
727f18af 912{
a3a03f5d 913 int res;
914 if (gz)
915 res = gzseek(gz,Over,SEEK_CUR);
916 else
917 res = lseek(iFd,Over,SEEK_CUR);
918 if (res < 0)
727f18af
AL
919 {
920 Flags |= Fail;
650faab0 921 return _error->Error("Unable to seek ahead %llu",Over);
727f18af
AL
922 }
923
6d5dd02a
AL
924 return true;
925}
926 /*}}}*/
927// FileFd::Truncate - Truncate the file /*{{{*/
928// ---------------------------------------------------------------------
929/* */
650faab0 930bool FileFd::Truncate(unsigned long long To)
6d5dd02a 931{
a3a03f5d 932 if (gz)
933 {
934 Flags |= Fail;
935 return _error->Error("Truncating gzipped files is not implemented (%s)", FileName.c_str());
936 }
6d5dd02a
AL
937 if (ftruncate(iFd,To) != 0)
938 {
939 Flags |= Fail;
650faab0 940 return _error->Error("Unable to truncate to %llu",To);
6d5dd02a
AL
941 }
942
578bfd0a
AL
943 return true;
944}
945 /*}}}*/
7f25bdff
AL
946// FileFd::Tell - Current seek position /*{{{*/
947// ---------------------------------------------------------------------
948/* */
650faab0 949unsigned long long FileFd::Tell()
7f25bdff 950{
a3a03f5d 951 off_t Res;
952 if (gz)
953 Res = gztell(gz);
954 else
955 Res = lseek(iFd,0,SEEK_CUR);
7f25bdff
AL
956 if (Res == (off_t)-1)
957 _error->Errno("lseek","Failed to determine the current file position");
958 return Res;
959}
960 /*}}}*/
4260fd39 961// FileFd::FileSize - Return the size of the file /*{{{*/
578bfd0a
AL
962// ---------------------------------------------------------------------
963/* */
650faab0 964unsigned long long FileFd::FileSize()
578bfd0a
AL
965{
966 struct stat Buf;
9c182afa 967
44dc669e
MV
968 if (fstat(iFd,&Buf) != 0)
969 return _error->Errno("fstat","Unable to determine the file size");
4260fd39
DK
970 return Buf.st_size;
971}
972 /*}}}*/
973// FileFd::Size - Return the size of the content in the file /*{{{*/
974// ---------------------------------------------------------------------
975/* */
650faab0 976unsigned long long FileFd::Size()
4260fd39 977{
650faab0 978 unsigned long long size = FileSize();
44dc669e
MV
979
980 // only check gzsize if we are actually a gzip file, just checking for
981 // "gz" is not sufficient as uncompressed files will be opened with
982 // gzopen in "direct" mode as well
983 if (gz && !gzdirect(gz) && size > 0)
9c182afa
MP
984 {
985 /* unfortunately zlib.h doesn't provide a gzsize(), so we have to do
986 * this ourselves; the original (uncompressed) file size is the last 32
987 * bits of the file */
650faab0 988 // FIXME: Size for gz-files is limited by 32bit… no largefile support
4260fd39 989 off_t orig_pos = lseek(iFd, 0, SEEK_CUR);
9c182afa
MP
990 if (lseek(iFd, -4, SEEK_END) < 0)
991 return _error->Errno("lseek","Unable to seek to end of gzipped file");
f330c0f3 992 size = 0L;
9c182afa
MP
993 if (read(iFd, &size, 4) != 4)
994 return _error->Errno("read","Unable to read original size of gzipped file");
f330c0f3
DK
995
996#ifdef WORDS_BIGENDIAN
2cae0ccb
DK
997 uint32_t tmp_size = size;
998 uint8_t const * const p = (uint8_t const * const) &tmp_size;
999 tmp_size = (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0];
1000 size = tmp_size;
f330c0f3 1001#endif
9c182afa
MP
1002
1003 if (lseek(iFd, orig_pos, SEEK_SET) < 0)
1004 return _error->Errno("lseek","Unable to seek in gzipped file");
1005 return size;
1006 }
1007
44dc669e 1008 return size;
578bfd0a
AL
1009}
1010 /*}}}*/
8e06abb2 1011// FileFd::Close - Close the file if the close flag is set /*{{{*/
578bfd0a
AL
1012// ---------------------------------------------------------------------
1013/* */
8e06abb2 1014bool FileFd::Close()
578bfd0a
AL
1015{
1016 bool Res = true;
1017 if ((Flags & AutoClose) == AutoClose)
d13c2d3f 1018 {
1019 if (gz != NULL) {
3184b4cf 1020 int const e = gzclose(gz);
d13c2d3f 1021 // gzdopen() on empty files always fails with "buffer error" here, ignore that
1022 if (e != 0 && e != Z_BUF_ERROR)
3184b4cf 1023 Res &= _error->Errno("close",_("Problem closing the gzip file %s"), FileName.c_str());
d13c2d3f 1024 } else
1025 if (iFd > 0 && close(iFd) != 0)
3184b4cf 1026 Res &= _error->Errno("close",_("Problem closing the file %s"), FileName.c_str());
d13c2d3f 1027 }
3010fb0e 1028
62d073d9 1029 if ((Flags & Replace) == Replace && iFd >= 0) {
3010fb0e 1030 if (rename(TemporaryFileName.c_str(), FileName.c_str()) != 0)
62d073d9
DK
1031 Res &= _error->Errno("rename",_("Problem renaming the file %s to %s"), TemporaryFileName.c_str(), FileName.c_str());
1032
fd3b761e 1033 FileName = TemporaryFileName; // for the unlink() below.
3010fb0e 1034 }
62d073d9
DK
1035
1036 iFd = -1;
a3a03f5d 1037 gz = NULL;
62d073d9 1038
578bfd0a
AL
1039 if ((Flags & Fail) == Fail && (Flags & DelOnFail) == DelOnFail &&
1040 FileName.empty() == false)
1041 if (unlink(FileName.c_str()) != 0)
62d073d9 1042 Res &= _error->WarningE("unlnk",_("Problem unlinking the file %s"), FileName.c_str());
3010fb0e
JAK
1043
1044
578bfd0a
AL
1045 return Res;
1046}
1047 /*}}}*/
b2e465d6
AL
1048// FileFd::Sync - Sync the file /*{{{*/
1049// ---------------------------------------------------------------------
1050/* */
1051bool FileFd::Sync()
1052{
1053#ifdef _POSIX_SYNCHRONIZED_IO
1054 if (fsync(iFd) != 0)
1055 return _error->Errno("sync",_("Problem syncing the file"));
1056#endif
1057 return true;
1058}
1059 /*}}}*/