1 // -*- mode: cpp; mode: fold -*-
3 // $Id: fileutl.cc,v 1.42 2002/09/14 05:29:22 jgg Exp $
4 /* ######################################################################
8 CopyFile - Buffered copy of a single file
9 GetLock - dpkg compatible lock file manipulation (fcntl)
11 Most of this source is placed in the Public Domain, do with it what
13 It was originally written by Jason Gunthorpe <jgg@debian.org>.
14 FileFd gzip support added by Martin Pitt <martin.pitt@canonical.com>
16 The exception is RunScripts() it is under the GPLv2
18 ##################################################################### */
20 // Include Files /*{{{*/
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>
38 #include <sys/types.h>
47 #ifdef WORDS_BIGENDIAN
56 // RunScripts - Run a set of scripts from a configuration subtree /*{{{*/
57 // ---------------------------------------------------------------------
59 bool RunScripts(const char *Cnf
)
61 Configuration::Item
const *Opts
= _config
->Tree(Cnf
);
62 if (Opts
== 0 || Opts
->Child
== 0)
66 // Fork for running the system calls
67 pid_t Child
= ExecFork();
72 if (_config
->FindDir("DPkg::Chroot-Directory","/") != "/")
74 std::cerr
<< "Chrooting into "
75 << _config
->FindDir("DPkg::Chroot-Directory")
77 if (chroot(_config
->FindDir("DPkg::Chroot-Directory","/").c_str()) != 0)
81 if (chdir("/tmp/") != 0)
84 unsigned int Count
= 1;
85 for (; Opts
!= 0; Opts
= Opts
->Next
, Count
++)
87 if (Opts
->Value
.empty() == true)
90 if (system(Opts
->Value
.c_str()) != 0)
98 while (waitpid(Child
,&Status
,0) != Child
)
102 return _error
->Errno("waitpid","Couldn't wait for subprocess");
105 // Restore sig int/quit
106 signal(SIGQUIT
,SIG_DFL
);
107 signal(SIGINT
,SIG_DFL
);
109 // Check for an error code.
110 if (WIFEXITED(Status
) == 0 || WEXITSTATUS(Status
) != 0)
112 unsigned int Count
= WEXITSTATUS(Status
);
116 for (; Opts
!= 0 && Count
!= 1; Opts
= Opts
->Next
, Count
--);
117 _error
->Error("Problem executing scripts %s '%s'",Cnf
,Opts
->Value
.c_str());
120 return _error
->Error("Sub-process returned an error code");
127 // CopyFile - Buffered copy of a file /*{{{*/
128 // ---------------------------------------------------------------------
129 /* The caller is expected to set things so that failure causes erasure */
130 bool CopyFile(FileFd
&From
,FileFd
&To
)
132 if (From
.IsOpen() == false || To
.IsOpen() == false)
135 // Buffered copy between fds
136 SPtrArray
<unsigned char> Buf
= new unsigned char[64000];
137 unsigned long long Size
= From
.Size();
140 unsigned long long ToRead
= Size
;
144 if (From
.Read(Buf
,ToRead
) == false ||
145 To
.Write(Buf
,ToRead
) == false)
154 // GetLock - Gets a lock file /*{{{*/
155 // ---------------------------------------------------------------------
156 /* This will create an empty file of the given name and lock it. Once this
157 is done all other calls to GetLock in any other process will fail with
158 -1. The return result is the fd of the file, the call should call
159 close at some time. */
160 int GetLock(string File
,bool Errors
)
162 // GetLock() is used in aptitude on directories with public-write access
163 // Use O_NOFOLLOW here to prevent symlink traversal attacks
164 int FD
= open(File
.c_str(),O_RDWR
| O_CREAT
| O_NOFOLLOW
,0640);
167 // Read only .. cant have locking problems there.
170 _error
->Warning(_("Not using locking for read only lock file %s"),File
.c_str());
171 return dup(0); // Need something for the caller to close
175 _error
->Errno("open",_("Could not open lock file %s"),File
.c_str());
177 // Feh.. We do this to distinguish the lock vs open case..
181 SetCloseExec(FD
,true);
183 // Aquire a write lock
186 fl
.l_whence
= SEEK_SET
;
189 if (fcntl(FD
,F_SETLK
,&fl
) == -1)
193 _error
->Warning(_("Not using locking for nfs mounted lock file %s"),File
.c_str());
194 return dup(0); // Need something for the caller to close
197 _error
->Errno("open",_("Could not get lock %s"),File
.c_str());
208 // FileExists - Check if a file exists /*{{{*/
209 // ---------------------------------------------------------------------
210 /* Beware: Directories are also files! */
211 bool FileExists(string File
)
214 if (stat(File
.c_str(),&Buf
) != 0)
219 // RealFileExists - Check if a file exists and if it is really a file /*{{{*/
220 // ---------------------------------------------------------------------
222 bool RealFileExists(string File
)
225 if (stat(File
.c_str(),&Buf
) != 0)
227 return ((Buf
.st_mode
& S_IFREG
) != 0);
230 // DirectoryExists - Check if a directory exists and is really one /*{{{*/
231 // ---------------------------------------------------------------------
233 bool DirectoryExists(string
const &Path
)
236 if (stat(Path
.c_str(),&Buf
) != 0)
238 return ((Buf
.st_mode
& S_IFDIR
) != 0);
241 // CreateDirectory - poor man's mkdir -p guarded by a parent directory /*{{{*/
242 // ---------------------------------------------------------------------
243 /* This method will create all directories needed for path in good old
244 mkdir -p style but refuses to do this if Parent is not a prefix of
245 this Path. Example: /var/cache/ and /var/cache/apt/archives are given,
246 so it will create apt/archives if /var/cache exists - on the other
247 hand if the parent is /var/lib the creation will fail as this path
248 is not a parent of the path to be generated. */
249 bool CreateDirectory(string
const &Parent
, string
const &Path
)
251 if (Parent
.empty() == true || Path
.empty() == true)
254 if (DirectoryExists(Path
) == true)
257 if (DirectoryExists(Parent
) == false)
260 // we are not going to create directories "into the blue"
261 if (Path
.find(Parent
, 0) != 0)
264 vector
<string
> const dirs
= VectorizeString(Path
.substr(Parent
.size()), '/');
265 string progress
= Parent
;
266 for (vector
<string
>::const_iterator d
= dirs
.begin(); d
!= dirs
.end(); ++d
)
268 if (d
->empty() == true)
271 progress
.append("/").append(*d
);
272 if (DirectoryExists(progress
) == true)
275 if (mkdir(progress
.c_str(), 0755) != 0)
281 // CreateAPTDirectoryIfNeeded - ensure that the given directory exists /*{{{*/
282 // ---------------------------------------------------------------------
283 /* a small wrapper around CreateDirectory to check if it exists and to
284 remove the trailing "/apt/" from the parent directory if needed */
285 bool CreateAPTDirectoryIfNeeded(string
const &Parent
, string
const &Path
)
287 if (DirectoryExists(Path
) == true)
290 size_t const len
= Parent
.size();
291 if (len
> 5 && Parent
.find("/apt/", len
- 6, 5) == len
- 5)
293 if (CreateDirectory(Parent
.substr(0,len
-5), Path
) == true)
296 else if (CreateDirectory(Parent
, Path
) == true)
302 // GetListOfFilesInDir - returns a vector of files in the given dir /*{{{*/
303 // ---------------------------------------------------------------------
304 /* If an extension is given only files with this extension are included
305 in the returned vector, otherwise every "normal" file is included. */
306 std::vector
<string
> GetListOfFilesInDir(string
const &Dir
, string
const &Ext
,
307 bool const &SortList
, bool const &AllowNoExt
)
309 std::vector
<string
> ext
;
311 if (Ext
.empty() == false)
313 if (AllowNoExt
== true && ext
.empty() == false)
315 return GetListOfFilesInDir(Dir
, ext
, SortList
);
317 std::vector
<string
> GetListOfFilesInDir(string
const &Dir
, std::vector
<string
> const &Ext
,
318 bool const &SortList
)
320 // Attention debuggers: need to be set with the environment config file!
321 bool const Debug
= _config
->FindB("Debug::GetListOfFilesInDir", false);
324 std::clog
<< "Accept in " << Dir
<< " only files with the following " << Ext
.size() << " extensions:" << std::endl
;
325 if (Ext
.empty() == true)
326 std::clog
<< "\tNO extension" << std::endl
;
328 for (std::vector
<string
>::const_iterator e
= Ext
.begin();
330 std::clog
<< '\t' << (e
->empty() == true ? "NO" : *e
) << " extension" << std::endl
;
333 std::vector
<string
> List
;
335 if (DirectoryExists(Dir
.c_str()) == false)
337 _error
->Error(_("List of files can't be created as '%s' is not a directory"), Dir
.c_str());
341 Configuration::MatchAgainstConfig
SilentIgnore("Dir::Ignore-Files-Silently");
342 DIR *D
= opendir(Dir
.c_str());
345 _error
->Errno("opendir",_("Unable to read %s"),Dir
.c_str());
349 for (struct dirent
*Ent
= readdir(D
); Ent
!= 0; Ent
= readdir(D
))
351 // skip "hidden" files
352 if (Ent
->d_name
[0] == '.')
355 // Make sure it is a file and not something else
356 string
const File
= flCombine(Dir
,Ent
->d_name
);
357 #ifdef _DIRENT_HAVE_D_TYPE
358 if (Ent
->d_type
!= DT_REG
)
361 if (RealFileExists(File
.c_str()) == false)
363 if (SilentIgnore
.Match(Ent
->d_name
) == false)
364 _error
->Notice(_("Ignoring '%s' in directory '%s' as it is not a regular file"), Ent
->d_name
, Dir
.c_str());
369 // check for accepted extension:
370 // no extension given -> periods are bad as hell!
371 // extensions given -> "" extension allows no extension
372 if (Ext
.empty() == false)
374 string d_ext
= flExtension(Ent
->d_name
);
375 if (d_ext
== Ent
->d_name
) // no extension
377 if (std::find(Ext
.begin(), Ext
.end(), "") == Ext
.end())
380 std::clog
<< "Bad file: " << Ent
->d_name
<< " → no extension" << std::endl
;
381 if (SilentIgnore
.Match(Ent
->d_name
) == false)
382 _error
->Notice(_("Ignoring file '%s' in directory '%s' as it has no filename extension"), Ent
->d_name
, Dir
.c_str());
386 else if (std::find(Ext
.begin(), Ext
.end(), d_ext
) == Ext
.end())
389 std::clog
<< "Bad file: " << Ent
->d_name
<< " → bad extension »" << flExtension(Ent
->d_name
) << "«" << std::endl
;
390 if (SilentIgnore
.Match(Ent
->d_name
) == false)
391 _error
->Notice(_("Ignoring file '%s' in directory '%s' as it has an invalid filename extension"), Ent
->d_name
, Dir
.c_str());
396 // Skip bad filenames ala run-parts
397 const char *C
= Ent
->d_name
;
399 if (isalpha(*C
) == 0 && isdigit(*C
) == 0
400 && *C
!= '_' && *C
!= '-') {
401 // no required extension -> dot is a bad character
402 if (*C
== '.' && Ext
.empty() == false)
407 // we don't reach the end of the name -> bad character included
411 std::clog
<< "Bad file: " << Ent
->d_name
<< " → bad character »"
412 << *C
<< "« in filename (period allowed: " << (Ext
.empty() ? "no" : "yes") << ")" << std::endl
;
416 // skip filenames which end with a period. These are never valid
420 std::clog
<< "Bad file: " << Ent
->d_name
<< " → Period as last character" << std::endl
;
425 std::clog
<< "Accept file: " << Ent
->d_name
<< " in " << Dir
<< std::endl
;
426 List
.push_back(File
);
430 if (SortList
== true)
431 std::sort(List
.begin(),List
.end());
435 // SafeGetCWD - This is a safer getcwd that returns a dynamic string /*{{{*/
436 // ---------------------------------------------------------------------
437 /* We return / on failure. */
440 // Stash the current dir.
443 if (getcwd(S
,sizeof(S
)-2) == 0)
445 unsigned int Len
= strlen(S
);
451 // GetModificationTime - Get the mtime of the given file or -1 on error /*{{{*/
452 // ---------------------------------------------------------------------
453 /* We return / on failure. */
454 time_t GetModificationTime(string
const &Path
)
457 if (stat(Path
.c_str(), &St
) < 0)
462 // flNotDir - Strip the directory from the filename /*{{{*/
463 // ---------------------------------------------------------------------
465 string
flNotDir(string File
)
467 string::size_type Res
= File
.rfind('/');
468 if (Res
== string::npos
)
471 return string(File
,Res
,Res
- File
.length());
474 // flNotFile - Strip the file from the directory name /*{{{*/
475 // ---------------------------------------------------------------------
476 /* Result ends in a / */
477 string
flNotFile(string File
)
479 string::size_type Res
= File
.rfind('/');
480 if (Res
== string::npos
)
483 return string(File
,0,Res
);
486 // flExtension - Return the extension for the file /*{{{*/
487 // ---------------------------------------------------------------------
489 string
flExtension(string File
)
491 string::size_type Res
= File
.rfind('.');
492 if (Res
== string::npos
)
495 return string(File
,Res
,Res
- File
.length());
498 // flNoLink - If file is a symlink then deref it /*{{{*/
499 // ---------------------------------------------------------------------
500 /* If the name is not a link then the returned path is the input. */
501 string
flNoLink(string File
)
504 if (lstat(File
.c_str(),&St
) != 0 || S_ISLNK(St
.st_mode
) == 0)
506 if (stat(File
.c_str(),&St
) != 0)
509 /* Loop resolving the link. There is no need to limit the number of
510 loops because the stat call above ensures that the symlink is not
518 if ((Res
= readlink(NFile
.c_str(),Buffer
,sizeof(Buffer
))) <= 0 ||
519 (unsigned)Res
>= sizeof(Buffer
))
522 // Append or replace the previous path
524 if (Buffer
[0] == '/')
527 NFile
= flNotFile(NFile
) + Buffer
;
529 // See if we are done
530 if (lstat(NFile
.c_str(),&St
) != 0)
532 if (S_ISLNK(St
.st_mode
) == 0)
537 // flCombine - Combine a file and a directory /*{{{*/
538 // ---------------------------------------------------------------------
539 /* If the file is an absolute path then it is just returned, otherwise
540 the directory is pre-pended to it. */
541 string
flCombine(string Dir
,string File
)
543 if (File
.empty() == true)
546 if (File
[0] == '/' || Dir
.empty() == true)
548 if (File
.length() >= 2 && File
[0] == '.' && File
[1] == '/')
550 if (Dir
[Dir
.length()-1] == '/')
552 return Dir
+ '/' + File
;
555 // SetCloseExec - Set the close on exec flag /*{{{*/
556 // ---------------------------------------------------------------------
558 void SetCloseExec(int Fd
,bool Close
)
560 if (fcntl(Fd
,F_SETFD
,(Close
== false)?0:FD_CLOEXEC
) != 0)
562 cerr
<< "FATAL -> Could not set close on exec " << strerror(errno
) << endl
;
567 // SetNonBlock - Set the nonblocking flag /*{{{*/
568 // ---------------------------------------------------------------------
570 void SetNonBlock(int Fd
,bool Block
)
572 int Flags
= fcntl(Fd
,F_GETFL
) & (~O_NONBLOCK
);
573 if (fcntl(Fd
,F_SETFL
,Flags
| ((Block
== false)?0:O_NONBLOCK
)) != 0)
575 cerr
<< "FATAL -> Could not set non-blocking flag " << strerror(errno
) << endl
;
580 // WaitFd - Wait for a FD to become readable /*{{{*/
581 // ---------------------------------------------------------------------
582 /* This waits for a FD to become readable using select. It is useful for
583 applications making use of non-blocking sockets. The timeout is
585 bool WaitFd(int Fd
,bool write
,unsigned long timeout
)
598 Res
= select(Fd
+1,0,&Set
,0,(timeout
!= 0?&tv
:0));
600 while (Res
< 0 && errno
== EINTR
);
610 Res
= select(Fd
+1,&Set
,0,0,(timeout
!= 0?&tv
:0));
612 while (Res
< 0 && errno
== EINTR
);
621 // ExecFork - Magical fork that sanitizes the context before execing /*{{{*/
622 // ---------------------------------------------------------------------
623 /* This is used if you want to cleanse the environment for the forked
624 child, it fixes up the important signals and nukes all of the fds,
625 otherwise acts like normal fork. */
628 // Fork off the process
629 pid_t Process
= fork();
632 cerr
<< "FATAL -> Failed to fork." << endl
;
636 // Spawn the subprocess
640 signal(SIGPIPE
,SIG_DFL
);
641 signal(SIGQUIT
,SIG_DFL
);
642 signal(SIGINT
,SIG_DFL
);
643 signal(SIGWINCH
,SIG_DFL
);
644 signal(SIGCONT
,SIG_DFL
);
645 signal(SIGTSTP
,SIG_DFL
);
648 Configuration::Item
const *Opts
= _config
->Tree("APT::Keep-Fds");
649 if (Opts
!= 0 && Opts
->Child
!= 0)
652 for (; Opts
!= 0; Opts
= Opts
->Next
)
654 if (Opts
->Value
.empty() == true)
656 int fd
= atoi(Opts
->Value
.c_str());
661 // Close all of our FDs - just in case
662 for (int K
= 3; K
!= 40; K
++)
664 if(KeepFDs
.find(K
) == KeepFDs
.end())
665 fcntl(K
,F_SETFD
,FD_CLOEXEC
);
672 // ExecWait - Fancy waitpid /*{{{*/
673 // ---------------------------------------------------------------------
674 /* Waits for the given sub process. If Reap is set then no errors are
675 generated. Otherwise a failed subprocess will generate a proper descriptive
677 bool ExecWait(pid_t Pid
,const char *Name
,bool Reap
)
682 // Wait and collect the error code
684 while (waitpid(Pid
,&Status
,0) != Pid
)
692 return _error
->Error(_("Waited for %s but it wasn't there"),Name
);
696 // Check for an error code.
697 if (WIFEXITED(Status
) == 0 || WEXITSTATUS(Status
) != 0)
701 if (WIFSIGNALED(Status
) != 0)
703 if( WTERMSIG(Status
) == SIGSEGV
)
704 return _error
->Error(_("Sub-process %s received a segmentation fault."),Name
);
706 return _error
->Error(_("Sub-process %s received signal %u."),Name
, WTERMSIG(Status
));
709 if (WIFEXITED(Status
) != 0)
710 return _error
->Error(_("Sub-process %s returned an error code (%u)"),Name
,WEXITSTATUS(Status
));
712 return _error
->Error(_("Sub-process %s exited unexpectedly"),Name
);
719 // FileFd::Open - Open a file /*{{{*/
720 // ---------------------------------------------------------------------
721 /* The most commonly used open mode combinations are given with Mode */
722 bool FileFd::Open(string FileName
,OpenMode Mode
,CompressMode Compress
, unsigned long Perms
)
724 if (Mode
== ReadOnlyGzip
)
725 return Open(FileName
, ReadOnly
, Gzip
, Perms
);
729 if (Compress
== Auto
&& (Mode
& WriteOnly
) == WriteOnly
)
730 return _error
->Error("Autodetection on %s only works in ReadOnly openmode!", FileName
.c_str());
731 if ((Mode
& WriteOnly
) != WriteOnly
&& (Mode
& (Atomic
| Create
| Empty
| Exclusive
)) != 0)
732 return _error
->Error("ReadOnly mode for %s doesn't accept additional flags!", FileName
.c_str());
735 #define if_FLAGGED_SET(FLAG, MODE) if ((Mode & FLAG) == FLAG) fileflags |= MODE
736 if_FLAGGED_SET(ReadWrite
, O_RDWR
);
737 else if_FLAGGED_SET(ReadOnly
, O_RDONLY
);
738 else if_FLAGGED_SET(WriteOnly
, O_WRONLY
);
739 else return _error
->Error("No openmode provided in FileFd::Open for %s", FileName
.c_str());
741 if_FLAGGED_SET(Create
, O_CREAT
);
742 if_FLAGGED_SET(Exclusive
, O_EXCL
);
743 else if_FLAGGED_SET(Atomic
, O_EXCL
);
744 if_FLAGGED_SET(Empty
, O_TRUNC
);
745 #undef if_FLAGGED_SET
747 // FIXME: Denote inbuilt compressors somehow - as we don't need to have the binaries for them
748 std::vector
<APT::Configuration::Compressor
> const compressors
= APT::Configuration::getCompressors();
749 std::vector
<APT::Configuration::Compressor
>::const_iterator compressor
= compressors
.begin();
750 if (Compress
== Auto
)
753 for (; compressor
!= compressors
.end(); ++compressor
)
755 std::string file
= std::string(FileName
).append(compressor
->Extension
);
756 if (FileExists(file
) == false)
759 if (compressor
->Binary
== ".")
762 Compress
= Extension
;
766 else if (Compress
== Extension
)
769 std::string ext
= flExtension(FileName
);
773 for (; compressor
!= compressors
.end(); ++compressor
)
774 if (ext
== compressor
->Extension
)
778 else if (Compress
!= None
)
783 case Gzip
: name
= "gzip"; break;
784 case Bzip2
: name
= "bzip2"; break;
785 case Lzma
: name
= "lzma"; break;
786 case Xz
: name
= "xz"; break;
787 default: return _error
->Error("Can't find a match for specified compressor mode for file %s", FileName
.c_str());
789 for (; compressor
!= compressors
.end(); ++compressor
)
790 if (compressor
->Name
== name
)
792 if (compressor
== compressors
.end() && name
!= "gzip")
793 return _error
->Error("Can't find a configured compressor %s for file %s", name
.c_str(), FileName
.c_str());
796 // if we have them, use inbuilt compressors instead of forking
797 if (compressor
!= compressors
.end())
799 if (compressor
->Name
== "gzip")
802 compressor
= compressors
.end();
804 else if (compressor
->Name
== "." || Compress
== None
)
807 compressor
= compressors
.end();
811 if ((Mode
& Atomic
) == Atomic
)
814 char *name
= strdup((FileName
+ ".XXXXXX").c_str());
815 TemporaryFileName
= string(mktemp(name
));
818 else if ((Mode
& (Exclusive
| Create
)) == (Exclusive
| Create
))
820 // for atomic, this will be done by rename in Close()
821 unlink(FileName
.c_str());
823 if ((Mode
& Empty
) == Empty
)
826 if (lstat(FileName
.c_str(),&Buf
) == 0 && S_ISLNK(Buf
.st_mode
))
827 unlink(FileName
.c_str());
830 if (compressor
!= compressors
.end())
832 if ((Mode
& ReadWrite
) == ReadWrite
)
833 _error
->Error("External compressors like %s do not support readwrite mode for file %s", compressor
->Name
.c_str(), FileName
.c_str());
835 _error
->Error("Forking external compressor %s is not implemented for %s", compressor
->Name
.c_str(), FileName
.c_str());
839 if (TemporaryFileName
.empty() == false)
840 iFd
= open(TemporaryFileName
.c_str(), fileflags
, Perms
);
842 iFd
= open(FileName
.c_str(), fileflags
, Perms
);
846 if (OpenInternDescriptor(Mode
, Compress
) == false)
855 return _error
->Errno("open",_("Could not open file %s"),FileName
.c_str());
857 this->FileName
= FileName
;
858 SetCloseExec(iFd
,true);
862 // FileFd::OpenDescriptor - Open a filedescriptor /*{{{*/
863 // ---------------------------------------------------------------------
865 bool FileFd::OpenDescriptor(int Fd
, OpenMode Mode
, CompressMode Compress
, bool AutoClose
)
868 Flags
= (AutoClose
) ? FileFd::AutoClose
: 0;
870 if (OpenInternDescriptor(Mode
, Compress
) == false)
874 return _error
->Errno("gzdopen",_("Could not open file descriptor %d"), Fd
);
879 bool FileFd::OpenInternDescriptor(OpenMode Mode
, CompressMode Compress
)
881 if (Compress
== None
)
883 else if (Compress
== Gzip
)
885 if ((Mode
& ReadWrite
) == ReadWrite
)
886 gz
= gzdopen(iFd
, "r+");
887 else if ((Mode
& WriteOnly
) == WriteOnly
)
888 gz
= gzdopen(iFd
, "w");
890 gz
= gzdopen (iFd
, "r");
899 // FileFd::~File - Closes the file /*{{{*/
900 // ---------------------------------------------------------------------
901 /* If the proper modes are selected then we close the Fd and possibly
902 unlink the file on error. */
908 // FileFd::Read - Read a bit of the file /*{{{*/
909 // ---------------------------------------------------------------------
910 /* We are carefull to handle interruption by a signal while reading
912 bool FileFd::Read(void *To
,unsigned long long Size
,unsigned long long *Actual
)
922 Res
= gzread(gz
,To
,Size
);
924 Res
= read(iFd
,To
,Size
);
925 if (Res
< 0 && errno
== EINTR
)
930 return _error
->Errno("read",_("Read error"));
933 To
= (char *)To
+ Res
;
938 while (Res
> 0 && Size
> 0);
951 return _error
->Error(_("read, still have %llu to read but none left"), Size
);
954 // FileFd::Write - Write to the file /*{{{*/
955 // ---------------------------------------------------------------------
957 bool FileFd::Write(const void *From
,unsigned long long Size
)
964 Res
= gzwrite(gz
,From
,Size
);
966 Res
= write(iFd
,From
,Size
);
967 if (Res
< 0 && errno
== EINTR
)
972 return _error
->Errno("write",_("Write error"));
975 From
= (char *)From
+ Res
;
978 while (Res
> 0 && Size
> 0);
984 return _error
->Error(_("write, still have %llu to write but couldn't"), Size
);
987 // FileFd::Seek - Seek in the file /*{{{*/
988 // ---------------------------------------------------------------------
990 bool FileFd::Seek(unsigned long long To
)
994 res
= gzseek(gz
,To
,SEEK_SET
);
996 res
= lseek(iFd
,To
,SEEK_SET
);
997 if (res
!= (signed)To
)
1000 return _error
->Error("Unable to seek to %llu", To
);
1006 // FileFd::Skip - Seek in the file /*{{{*/
1007 // ---------------------------------------------------------------------
1009 bool FileFd::Skip(unsigned long long Over
)
1013 res
= gzseek(gz
,Over
,SEEK_CUR
);
1015 res
= lseek(iFd
,Over
,SEEK_CUR
);
1019 return _error
->Error("Unable to seek ahead %llu",Over
);
1025 // FileFd::Truncate - Truncate the file /*{{{*/
1026 // ---------------------------------------------------------------------
1028 bool FileFd::Truncate(unsigned long long To
)
1033 return _error
->Error("Truncating gzipped files is not implemented (%s)", FileName
.c_str());
1035 if (ftruncate(iFd
,To
) != 0)
1038 return _error
->Error("Unable to truncate to %llu",To
);
1044 // FileFd::Tell - Current seek position /*{{{*/
1045 // ---------------------------------------------------------------------
1047 unsigned long long FileFd::Tell()
1053 Res
= lseek(iFd
,0,SEEK_CUR
);
1054 if (Res
== (off_t
)-1)
1055 _error
->Errno("lseek","Failed to determine the current file position");
1059 // FileFd::FileSize - Return the size of the file /*{{{*/
1060 // ---------------------------------------------------------------------
1062 unsigned long long FileFd::FileSize()
1066 if (fstat(iFd
,&Buf
) != 0)
1067 return _error
->Errno("fstat","Unable to determine the file size");
1071 // FileFd::Size - Return the size of the content in the file /*{{{*/
1072 // ---------------------------------------------------------------------
1074 unsigned long long FileFd::Size()
1076 unsigned long long size
= FileSize();
1078 // only check gzsize if we are actually a gzip file, just checking for
1079 // "gz" is not sufficient as uncompressed files will be opened with
1080 // gzopen in "direct" mode as well
1081 if (gz
&& !gzdirect(gz
) && size
> 0)
1083 /* unfortunately zlib.h doesn't provide a gzsize(), so we have to do
1084 * this ourselves; the original (uncompressed) file size is the last 32
1085 * bits of the file */
1086 // FIXME: Size for gz-files is limited by 32bit… no largefile support
1087 off_t orig_pos
= lseek(iFd
, 0, SEEK_CUR
);
1088 if (lseek(iFd
, -4, SEEK_END
) < 0)
1089 return _error
->Errno("lseek","Unable to seek to end of gzipped file");
1091 if (read(iFd
, &size
, 4) != 4)
1092 return _error
->Errno("read","Unable to read original size of gzipped file");
1094 #ifdef WORDS_BIGENDIAN
1095 uint32_t tmp_size
= size
;
1096 uint8_t const * const p
= (uint8_t const * const) &tmp_size
;
1097 tmp_size
= (p
[3] << 24) | (p
[2] << 16) | (p
[1] << 8) | p
[0];
1101 if (lseek(iFd
, orig_pos
, SEEK_SET
) < 0)
1102 return _error
->Errno("lseek","Unable to seek in gzipped file");
1109 // FileFd::Close - Close the file if the close flag is set /*{{{*/
1110 // ---------------------------------------------------------------------
1112 bool FileFd::Close()
1115 if ((Flags
& AutoClose
) == AutoClose
)
1118 int const e
= gzclose(gz
);
1119 // gzdopen() on empty files always fails with "buffer error" here, ignore that
1120 if (e
!= 0 && e
!= Z_BUF_ERROR
)
1121 Res
&= _error
->Errno("close",_("Problem closing the gzip file %s"), FileName
.c_str());
1123 if (iFd
> 0 && close(iFd
) != 0)
1124 Res
&= _error
->Errno("close",_("Problem closing the file %s"), FileName
.c_str());
1127 if ((Flags
& Replace
) == Replace
&& iFd
>= 0) {
1128 if (rename(TemporaryFileName
.c_str(), FileName
.c_str()) != 0)
1129 Res
&= _error
->Errno("rename",_("Problem renaming the file %s to %s"), TemporaryFileName
.c_str(), FileName
.c_str());
1131 FileName
= TemporaryFileName
; // for the unlink() below.
1132 TemporaryFileName
.clear();
1138 if ((Flags
& Fail
) == Fail
&& (Flags
& DelOnFail
) == DelOnFail
&&
1139 FileName
.empty() == false)
1140 if (unlink(FileName
.c_str()) != 0)
1141 Res
&= _error
->WarningE("unlnk",_("Problem unlinking the file %s"), FileName
.c_str());
1147 // FileFd::Sync - Sync the file /*{{{*/
1148 // ---------------------------------------------------------------------
1152 #ifdef _POSIX_SYNCHRONIZED_IO
1153 if (fsync(iFd
) != 0)
1154 return _error
->Errno("sync",_("Problem syncing the file"));