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>
54 #ifdef WORDS_BIGENDIAN
78 APT::Configuration::Compressor compressor
;
79 unsigned int openmode
;
80 unsigned long long seekpos
;
81 FileFdPrivate() : gz(NULL
), bz2(NULL
),
82 compressed_fd(-1), compressor_pid(-1), pipe(false),
83 openmode(0), seekpos(0) {};
86 // RunScripts - Run a set of scripts from a configuration subtree /*{{{*/
87 // ---------------------------------------------------------------------
89 bool RunScripts(const char *Cnf
)
91 Configuration::Item
const *Opts
= _config
->Tree(Cnf
);
92 if (Opts
== 0 || Opts
->Child
== 0)
96 // Fork for running the system calls
97 pid_t Child
= ExecFork();
102 if (_config
->FindDir("DPkg::Chroot-Directory","/") != "/")
104 std::cerr
<< "Chrooting into "
105 << _config
->FindDir("DPkg::Chroot-Directory")
107 if (chroot(_config
->FindDir("DPkg::Chroot-Directory","/").c_str()) != 0)
111 if (chdir("/tmp/") != 0)
114 unsigned int Count
= 1;
115 for (; Opts
!= 0; Opts
= Opts
->Next
, Count
++)
117 if (Opts
->Value
.empty() == true)
120 if (system(Opts
->Value
.c_str()) != 0)
126 // Wait for the child
128 while (waitpid(Child
,&Status
,0) != Child
)
132 return _error
->Errno("waitpid","Couldn't wait for subprocess");
135 // Restore sig int/quit
136 signal(SIGQUIT
,SIG_DFL
);
137 signal(SIGINT
,SIG_DFL
);
139 // Check for an error code.
140 if (WIFEXITED(Status
) == 0 || WEXITSTATUS(Status
) != 0)
142 unsigned int Count
= WEXITSTATUS(Status
);
146 for (; Opts
!= 0 && Count
!= 1; Opts
= Opts
->Next
, Count
--);
147 _error
->Error("Problem executing scripts %s '%s'",Cnf
,Opts
->Value
.c_str());
150 return _error
->Error("Sub-process returned an error code");
157 // CopyFile - Buffered copy of a file /*{{{*/
158 // ---------------------------------------------------------------------
159 /* The caller is expected to set things so that failure causes erasure */
160 bool CopyFile(FileFd
&From
,FileFd
&To
)
162 if (From
.IsOpen() == false || To
.IsOpen() == false)
165 // Buffered copy between fds
166 SPtrArray
<unsigned char> Buf
= new unsigned char[64000];
167 unsigned long long Size
= From
.Size();
170 unsigned long long ToRead
= Size
;
174 if (From
.Read(Buf
,ToRead
) == false ||
175 To
.Write(Buf
,ToRead
) == false)
184 // GetLock - Gets a lock file /*{{{*/
185 // ---------------------------------------------------------------------
186 /* This will create an empty file of the given name and lock it. Once this
187 is done all other calls to GetLock in any other process will fail with
188 -1. The return result is the fd of the file, the call should call
189 close at some time. */
190 int GetLock(string File
,bool Errors
)
192 // GetLock() is used in aptitude on directories with public-write access
193 // Use O_NOFOLLOW here to prevent symlink traversal attacks
194 int FD
= open(File
.c_str(),O_RDWR
| O_CREAT
| O_NOFOLLOW
,0640);
197 // Read only .. cant have locking problems there.
200 _error
->Warning(_("Not using locking for read only lock file %s"),File
.c_str());
201 return dup(0); // Need something for the caller to close
205 _error
->Errno("open",_("Could not open lock file %s"),File
.c_str());
207 // Feh.. We do this to distinguish the lock vs open case..
211 SetCloseExec(FD
,true);
213 // Aquire a write lock
216 fl
.l_whence
= SEEK_SET
;
219 if (fcntl(FD
,F_SETLK
,&fl
) == -1)
223 _error
->Warning(_("Not using locking for nfs mounted lock file %s"),File
.c_str());
224 return dup(0); // Need something for the caller to close
227 _error
->Errno("open",_("Could not get lock %s"),File
.c_str());
238 // FileExists - Check if a file exists /*{{{*/
239 // ---------------------------------------------------------------------
240 /* Beware: Directories are also files! */
241 bool FileExists(string File
)
244 if (stat(File
.c_str(),&Buf
) != 0)
249 // RealFileExists - Check if a file exists and if it is really a file /*{{{*/
250 // ---------------------------------------------------------------------
252 bool RealFileExists(string File
)
255 if (stat(File
.c_str(),&Buf
) != 0)
257 return ((Buf
.st_mode
& S_IFREG
) != 0);
260 // DirectoryExists - Check if a directory exists and is really one /*{{{*/
261 // ---------------------------------------------------------------------
263 bool DirectoryExists(string
const &Path
)
266 if (stat(Path
.c_str(),&Buf
) != 0)
268 return ((Buf
.st_mode
& S_IFDIR
) != 0);
271 // CreateDirectory - poor man's mkdir -p guarded by a parent directory /*{{{*/
272 // ---------------------------------------------------------------------
273 /* This method will create all directories needed for path in good old
274 mkdir -p style but refuses to do this if Parent is not a prefix of
275 this Path. Example: /var/cache/ and /var/cache/apt/archives are given,
276 so it will create apt/archives if /var/cache exists - on the other
277 hand if the parent is /var/lib the creation will fail as this path
278 is not a parent of the path to be generated. */
279 bool CreateDirectory(string
const &Parent
, string
const &Path
)
281 if (Parent
.empty() == true || Path
.empty() == true)
284 if (DirectoryExists(Path
) == true)
287 if (DirectoryExists(Parent
) == false)
290 // we are not going to create directories "into the blue"
291 if (Path
.find(Parent
, 0) != 0)
294 vector
<string
> const dirs
= VectorizeString(Path
.substr(Parent
.size()), '/');
295 string progress
= Parent
;
296 for (vector
<string
>::const_iterator d
= dirs
.begin(); d
!= dirs
.end(); ++d
)
298 if (d
->empty() == true)
301 progress
.append("/").append(*d
);
302 if (DirectoryExists(progress
) == true)
305 if (mkdir(progress
.c_str(), 0755) != 0)
311 // CreateAPTDirectoryIfNeeded - ensure that the given directory exists /*{{{*/
312 // ---------------------------------------------------------------------
313 /* a small wrapper around CreateDirectory to check if it exists and to
314 remove the trailing "/apt/" from the parent directory if needed */
315 bool CreateAPTDirectoryIfNeeded(string
const &Parent
, string
const &Path
)
317 if (DirectoryExists(Path
) == true)
320 size_t const len
= Parent
.size();
321 if (len
> 5 && Parent
.find("/apt/", len
- 6, 5) == len
- 5)
323 if (CreateDirectory(Parent
.substr(0,len
-5), Path
) == true)
326 else if (CreateDirectory(Parent
, Path
) == true)
332 // GetListOfFilesInDir - returns a vector of files in the given dir /*{{{*/
333 // ---------------------------------------------------------------------
334 /* If an extension is given only files with this extension are included
335 in the returned vector, otherwise every "normal" file is included. */
336 std::vector
<string
> GetListOfFilesInDir(string
const &Dir
, string
const &Ext
,
337 bool const &SortList
, bool const &AllowNoExt
)
339 std::vector
<string
> ext
;
341 if (Ext
.empty() == false)
343 if (AllowNoExt
== true && ext
.empty() == false)
345 return GetListOfFilesInDir(Dir
, ext
, SortList
);
347 std::vector
<string
> GetListOfFilesInDir(string
const &Dir
, std::vector
<string
> const &Ext
,
348 bool const &SortList
)
350 // Attention debuggers: need to be set with the environment config file!
351 bool const Debug
= _config
->FindB("Debug::GetListOfFilesInDir", false);
354 std::clog
<< "Accept in " << Dir
<< " only files with the following " << Ext
.size() << " extensions:" << std::endl
;
355 if (Ext
.empty() == true)
356 std::clog
<< "\tNO extension" << std::endl
;
358 for (std::vector
<string
>::const_iterator e
= Ext
.begin();
360 std::clog
<< '\t' << (e
->empty() == true ? "NO" : *e
) << " extension" << std::endl
;
363 std::vector
<string
> List
;
365 if (DirectoryExists(Dir
.c_str()) == false)
367 _error
->Error(_("List of files can't be created as '%s' is not a directory"), Dir
.c_str());
371 Configuration::MatchAgainstConfig
SilentIgnore("Dir::Ignore-Files-Silently");
372 DIR *D
= opendir(Dir
.c_str());
375 _error
->Errno("opendir",_("Unable to read %s"),Dir
.c_str());
379 for (struct dirent
*Ent
= readdir(D
); Ent
!= 0; Ent
= readdir(D
))
381 // skip "hidden" files
382 if (Ent
->d_name
[0] == '.')
385 // Make sure it is a file and not something else
386 string
const File
= flCombine(Dir
,Ent
->d_name
);
387 #ifdef _DIRENT_HAVE_D_TYPE
388 if (Ent
->d_type
!= DT_REG
)
391 if (RealFileExists(File
.c_str()) == false)
393 // do not show ignoration warnings for directories
395 #ifdef _DIRENT_HAVE_D_TYPE
396 Ent
->d_type
== DT_DIR
||
398 DirectoryExists(File
.c_str()) == true)
400 if (SilentIgnore
.Match(Ent
->d_name
) == false)
401 _error
->Notice(_("Ignoring '%s' in directory '%s' as it is not a regular file"), Ent
->d_name
, Dir
.c_str());
406 // check for accepted extension:
407 // no extension given -> periods are bad as hell!
408 // extensions given -> "" extension allows no extension
409 if (Ext
.empty() == false)
411 string d_ext
= flExtension(Ent
->d_name
);
412 if (d_ext
== Ent
->d_name
) // no extension
414 if (std::find(Ext
.begin(), Ext
.end(), "") == Ext
.end())
417 std::clog
<< "Bad file: " << Ent
->d_name
<< " → no extension" << std::endl
;
418 if (SilentIgnore
.Match(Ent
->d_name
) == false)
419 _error
->Notice(_("Ignoring file '%s' in directory '%s' as it has no filename extension"), Ent
->d_name
, Dir
.c_str());
423 else if (std::find(Ext
.begin(), Ext
.end(), d_ext
) == Ext
.end())
426 std::clog
<< "Bad file: " << Ent
->d_name
<< " → bad extension »" << flExtension(Ent
->d_name
) << "«" << std::endl
;
427 if (SilentIgnore
.Match(Ent
->d_name
) == false)
428 _error
->Notice(_("Ignoring file '%s' in directory '%s' as it has an invalid filename extension"), Ent
->d_name
, Dir
.c_str());
433 // Skip bad filenames ala run-parts
434 const char *C
= Ent
->d_name
;
436 if (isalpha(*C
) == 0 && isdigit(*C
) == 0
437 && *C
!= '_' && *C
!= '-') {
438 // no required extension -> dot is a bad character
439 if (*C
== '.' && Ext
.empty() == false)
444 // we don't reach the end of the name -> bad character included
448 std::clog
<< "Bad file: " << Ent
->d_name
<< " → bad character »"
449 << *C
<< "« in filename (period allowed: " << (Ext
.empty() ? "no" : "yes") << ")" << std::endl
;
453 // skip filenames which end with a period. These are never valid
457 std::clog
<< "Bad file: " << Ent
->d_name
<< " → Period as last character" << std::endl
;
462 std::clog
<< "Accept file: " << Ent
->d_name
<< " in " << Dir
<< std::endl
;
463 List
.push_back(File
);
467 if (SortList
== true)
468 std::sort(List
.begin(),List
.end());
471 std::vector
<string
> GetListOfFilesInDir(string
const &Dir
, bool SortList
)
473 bool const Debug
= _config
->FindB("Debug::GetListOfFilesInDir", false);
475 std::clog
<< "Accept in " << Dir
<< " all regular files" << std::endl
;
477 std::vector
<string
> List
;
479 if (DirectoryExists(Dir
.c_str()) == false)
481 _error
->Error(_("List of files can't be created as '%s' is not a directory"), Dir
.c_str());
485 DIR *D
= opendir(Dir
.c_str());
488 _error
->Errno("opendir",_("Unable to read %s"),Dir
.c_str());
492 for (struct dirent
*Ent
= readdir(D
); Ent
!= 0; Ent
= readdir(D
))
494 // skip "hidden" files
495 if (Ent
->d_name
[0] == '.')
498 // Make sure it is a file and not something else
499 string
const File
= flCombine(Dir
,Ent
->d_name
);
500 #ifdef _DIRENT_HAVE_D_TYPE
501 if (Ent
->d_type
!= DT_REG
)
504 if (RealFileExists(File
.c_str()) == false)
507 std::clog
<< "Bad file: " << Ent
->d_name
<< " → it is not a real file" << std::endl
;
512 // Skip bad filenames ala run-parts
513 const char *C
= Ent
->d_name
;
515 if (isalpha(*C
) == 0 && isdigit(*C
) == 0
516 && *C
!= '_' && *C
!= '-' && *C
!= '.')
519 // we don't reach the end of the name -> bad character included
523 std::clog
<< "Bad file: " << Ent
->d_name
<< " → bad character »" << *C
<< "« in filename" << std::endl
;
527 // skip filenames which end with a period. These are never valid
531 std::clog
<< "Bad file: " << Ent
->d_name
<< " → Period as last character" << std::endl
;
536 std::clog
<< "Accept file: " << Ent
->d_name
<< " in " << Dir
<< std::endl
;
537 List
.push_back(File
);
541 if (SortList
== true)
542 std::sort(List
.begin(),List
.end());
546 // SafeGetCWD - This is a safer getcwd that returns a dynamic string /*{{{*/
547 // ---------------------------------------------------------------------
548 /* We return / on failure. */
551 // Stash the current dir.
554 if (getcwd(S
,sizeof(S
)-2) == 0)
556 unsigned int Len
= strlen(S
);
562 // GetModificationTime - Get the mtime of the given file or -1 on error /*{{{*/
563 // ---------------------------------------------------------------------
564 /* We return / on failure. */
565 time_t GetModificationTime(string
const &Path
)
568 if (stat(Path
.c_str(), &St
) < 0)
573 // flNotDir - Strip the directory from the filename /*{{{*/
574 // ---------------------------------------------------------------------
576 string
flNotDir(string File
)
578 string::size_type Res
= File
.rfind('/');
579 if (Res
== string::npos
)
582 return string(File
,Res
,Res
- File
.length());
585 // flNotFile - Strip the file from the directory name /*{{{*/
586 // ---------------------------------------------------------------------
587 /* Result ends in a / */
588 string
flNotFile(string File
)
590 string::size_type Res
= File
.rfind('/');
591 if (Res
== string::npos
)
594 return string(File
,0,Res
);
597 // flExtension - Return the extension for the file /*{{{*/
598 // ---------------------------------------------------------------------
600 string
flExtension(string File
)
602 string::size_type Res
= File
.rfind('.');
603 if (Res
== string::npos
)
606 return string(File
,Res
,Res
- File
.length());
609 // flNoLink - If file is a symlink then deref it /*{{{*/
610 // ---------------------------------------------------------------------
611 /* If the name is not a link then the returned path is the input. */
612 string
flNoLink(string File
)
615 if (lstat(File
.c_str(),&St
) != 0 || S_ISLNK(St
.st_mode
) == 0)
617 if (stat(File
.c_str(),&St
) != 0)
620 /* Loop resolving the link. There is no need to limit the number of
621 loops because the stat call above ensures that the symlink is not
629 if ((Res
= readlink(NFile
.c_str(),Buffer
,sizeof(Buffer
))) <= 0 ||
630 (unsigned)Res
>= sizeof(Buffer
))
633 // Append or replace the previous path
635 if (Buffer
[0] == '/')
638 NFile
= flNotFile(NFile
) + Buffer
;
640 // See if we are done
641 if (lstat(NFile
.c_str(),&St
) != 0)
643 if (S_ISLNK(St
.st_mode
) == 0)
648 // flCombine - Combine a file and a directory /*{{{*/
649 // ---------------------------------------------------------------------
650 /* If the file is an absolute path then it is just returned, otherwise
651 the directory is pre-pended to it. */
652 string
flCombine(string Dir
,string File
)
654 if (File
.empty() == true)
657 if (File
[0] == '/' || Dir
.empty() == true)
659 if (File
.length() >= 2 && File
[0] == '.' && File
[1] == '/')
661 if (Dir
[Dir
.length()-1] == '/')
663 return Dir
+ '/' + File
;
666 // SetCloseExec - Set the close on exec flag /*{{{*/
667 // ---------------------------------------------------------------------
669 void SetCloseExec(int Fd
,bool Close
)
671 if (fcntl(Fd
,F_SETFD
,(Close
== false)?0:FD_CLOEXEC
) != 0)
673 cerr
<< "FATAL -> Could not set close on exec " << strerror(errno
) << endl
;
678 // SetNonBlock - Set the nonblocking flag /*{{{*/
679 // ---------------------------------------------------------------------
681 void SetNonBlock(int Fd
,bool Block
)
683 int Flags
= fcntl(Fd
,F_GETFL
) & (~O_NONBLOCK
);
684 if (fcntl(Fd
,F_SETFL
,Flags
| ((Block
== false)?0:O_NONBLOCK
)) != 0)
686 cerr
<< "FATAL -> Could not set non-blocking flag " << strerror(errno
) << endl
;
691 // WaitFd - Wait for a FD to become readable /*{{{*/
692 // ---------------------------------------------------------------------
693 /* This waits for a FD to become readable using select. It is useful for
694 applications making use of non-blocking sockets. The timeout is
696 bool WaitFd(int Fd
,bool write
,unsigned long timeout
)
709 Res
= select(Fd
+1,0,&Set
,0,(timeout
!= 0?&tv
:0));
711 while (Res
< 0 && errno
== EINTR
);
721 Res
= select(Fd
+1,&Set
,0,0,(timeout
!= 0?&tv
:0));
723 while (Res
< 0 && errno
== EINTR
);
732 // ExecFork - Magical fork that sanitizes the context before execing /*{{{*/
733 // ---------------------------------------------------------------------
734 /* This is used if you want to cleanse the environment for the forked
735 child, it fixes up the important signals and nukes all of the fds,
736 otherwise acts like normal fork. */
739 // Fork off the process
740 pid_t Process
= fork();
743 cerr
<< "FATAL -> Failed to fork." << endl
;
747 // Spawn the subprocess
751 signal(SIGPIPE
,SIG_DFL
);
752 signal(SIGQUIT
,SIG_DFL
);
753 signal(SIGINT
,SIG_DFL
);
754 signal(SIGWINCH
,SIG_DFL
);
755 signal(SIGCONT
,SIG_DFL
);
756 signal(SIGTSTP
,SIG_DFL
);
759 Configuration::Item
const *Opts
= _config
->Tree("APT::Keep-Fds");
760 if (Opts
!= 0 && Opts
->Child
!= 0)
763 for (; Opts
!= 0; Opts
= Opts
->Next
)
765 if (Opts
->Value
.empty() == true)
767 int fd
= atoi(Opts
->Value
.c_str());
772 // Close all of our FDs - just in case
773 for (int K
= 3; K
!= 40; K
++)
775 if(KeepFDs
.find(K
) == KeepFDs
.end())
776 fcntl(K
,F_SETFD
,FD_CLOEXEC
);
783 // ExecWait - Fancy waitpid /*{{{*/
784 // ---------------------------------------------------------------------
785 /* Waits for the given sub process. If Reap is set then no errors are
786 generated. Otherwise a failed subprocess will generate a proper descriptive
788 bool ExecWait(pid_t Pid
,const char *Name
,bool Reap
)
793 // Wait and collect the error code
795 while (waitpid(Pid
,&Status
,0) != Pid
)
803 return _error
->Error(_("Waited for %s but it wasn't there"),Name
);
807 // Check for an error code.
808 if (WIFEXITED(Status
) == 0 || WEXITSTATUS(Status
) != 0)
812 if (WIFSIGNALED(Status
) != 0)
814 if( WTERMSIG(Status
) == SIGSEGV
)
815 return _error
->Error(_("Sub-process %s received a segmentation fault."),Name
);
817 return _error
->Error(_("Sub-process %s received signal %u."),Name
, WTERMSIG(Status
));
820 if (WIFEXITED(Status
) != 0)
821 return _error
->Error(_("Sub-process %s returned an error code (%u)"),Name
,WEXITSTATUS(Status
));
823 return _error
->Error(_("Sub-process %s exited unexpectedly"),Name
);
830 // FileFd::Open - Open a file /*{{{*/
831 // ---------------------------------------------------------------------
832 /* The most commonly used open mode combinations are given with Mode */
833 bool FileFd::Open(string FileName
,unsigned int const Mode
,CompressMode Compress
, unsigned long const Perms
)
835 if (Mode
== ReadOnlyGzip
)
836 return Open(FileName
, ReadOnly
, Gzip
, Perms
);
838 if (Compress
== Auto
&& (Mode
& WriteOnly
) == WriteOnly
)
839 return _error
->Error("Autodetection on %s only works in ReadOnly openmode!", FileName
.c_str());
841 std::vector
<APT::Configuration::Compressor
> const compressors
= APT::Configuration::getCompressors();
842 std::vector
<APT::Configuration::Compressor
>::const_iterator compressor
= compressors
.begin();
843 if (Compress
== Auto
)
845 for (; compressor
!= compressors
.end(); ++compressor
)
847 std::string file
= std::string(FileName
).append(compressor
->Extension
);
848 if (FileExists(file
) == false)
854 else if (Compress
== Extension
)
856 std::string::size_type
const found
= FileName
.find_last_of('.');
858 if (found
!= std::string::npos
)
860 ext
= FileName
.substr(found
);
861 if (ext
== ".new" || ext
== ".bak")
863 std::string::size_type
const found2
= FileName
.find_last_of('.', found
- 1);
864 if (found2
!= std::string::npos
)
865 ext
= FileName
.substr(found2
, found
- found2
);
870 for (; compressor
!= compressors
.end(); ++compressor
)
871 if (ext
== compressor
->Extension
)
873 // no matching extension - assume uncompressed (imagine files like 'example.org_Packages')
874 if (compressor
== compressors
.end())
875 for (compressor
= compressors
.begin(); compressor
!= compressors
.end(); ++compressor
)
876 if (compressor
->Name
== ".")
884 case None
: name
= "."; break;
885 case Gzip
: name
= "gzip"; break;
886 case Bzip2
: name
= "bzip2"; break;
887 case Lzma
: name
= "lzma"; break;
888 case Xz
: name
= "xz"; break;
892 return _error
->Error("Opening File %s in None, Auto or Extension should be already handled?!?", FileName
.c_str());
894 for (; compressor
!= compressors
.end(); ++compressor
)
895 if (compressor
->Name
== name
)
897 if (compressor
== compressors
.end())
898 return _error
->Error("Can't find a configured compressor %s for file %s", name
.c_str(), FileName
.c_str());
901 if (compressor
== compressors
.end())
902 return _error
->Error("Can't find a match for specified compressor mode for file %s", FileName
.c_str());
903 return Open(FileName
, Mode
, *compressor
, Perms
);
905 bool FileFd::Open(string FileName
,unsigned int const Mode
,APT::Configuration::Compressor
const &compressor
, unsigned long const Perms
)
908 d
= new FileFdPrivate
;
912 if ((Mode
& WriteOnly
) != WriteOnly
&& (Mode
& (Atomic
| Create
| Empty
| Exclusive
)) != 0)
913 return _error
->Error("ReadOnly mode for %s doesn't accept additional flags!", FileName
.c_str());
914 if ((Mode
& ReadWrite
) == 0)
915 return _error
->Error("No openmode provided in FileFd::Open for %s", FileName
.c_str());
917 if ((Mode
& Atomic
) == Atomic
)
920 char *name
= strdup((FileName
+ ".XXXXXX").c_str());
921 TemporaryFileName
= string(mktemp(name
));
924 else if ((Mode
& (Exclusive
| Create
)) == (Exclusive
| Create
))
926 // for atomic, this will be done by rename in Close()
927 unlink(FileName
.c_str());
929 if ((Mode
& Empty
) == Empty
)
932 if (lstat(FileName
.c_str(),&Buf
) == 0 && S_ISLNK(Buf
.st_mode
))
933 unlink(FileName
.c_str());
937 #define if_FLAGGED_SET(FLAG, MODE) if ((Mode & FLAG) == FLAG) fileflags |= MODE
938 if_FLAGGED_SET(ReadWrite
, O_RDWR
);
939 else if_FLAGGED_SET(ReadOnly
, O_RDONLY
);
940 else if_FLAGGED_SET(WriteOnly
, O_WRONLY
);
942 if_FLAGGED_SET(Create
, O_CREAT
);
943 if_FLAGGED_SET(Empty
, O_TRUNC
);
944 if_FLAGGED_SET(Exclusive
, O_EXCL
);
945 else if_FLAGGED_SET(Atomic
, O_EXCL
);
946 #undef if_FLAGGED_SET
948 if (TemporaryFileName
.empty() == false)
949 iFd
= open(TemporaryFileName
.c_str(), fileflags
, Perms
);
951 iFd
= open(FileName
.c_str(), fileflags
, Perms
);
953 this->FileName
= FileName
;
954 if (iFd
== -1 || OpenInternDescriptor(Mode
, compressor
) == false)
961 return _error
->Errno("open",_("Could not open file %s"), FileName
.c_str());
964 SetCloseExec(iFd
,true);
968 // FileFd::OpenDescriptor - Open a filedescriptor /*{{{*/
969 // ---------------------------------------------------------------------
971 bool FileFd::OpenDescriptor(int Fd
, unsigned int const Mode
, CompressMode Compress
, bool AutoClose
)
973 std::vector
<APT::Configuration::Compressor
> const compressors
= APT::Configuration::getCompressors();
974 std::vector
<APT::Configuration::Compressor
>::const_iterator compressor
= compressors
.begin();
977 // compat with the old API
978 if (Mode
== ReadOnlyGzip
&& Compress
== None
)
983 case None
: name
= "."; break;
984 case Gzip
: name
= "gzip"; break;
985 case Bzip2
: name
= "bzip2"; break;
986 case Lzma
: name
= "lzma"; break;
987 case Xz
: name
= "xz"; break;
990 return _error
->Error("Opening Fd %d in Auto or Extension compression mode is not supported", Fd
);
992 for (; compressor
!= compressors
.end(); ++compressor
)
993 if (compressor
->Name
== name
)
995 if (compressor
== compressors
.end())
996 return _error
->Error("Can't find a configured compressor %s for file %s", name
.c_str(), FileName
.c_str());
998 return OpenDescriptor(Fd
, Mode
, *compressor
, AutoClose
);
1000 bool FileFd::OpenDescriptor(int Fd
, unsigned int const Mode
, APT::Configuration::Compressor
const &compressor
, bool AutoClose
)
1003 d
= new FileFdPrivate
;
1005 Flags
= (AutoClose
) ? FileFd::AutoClose
: 0;
1007 this->FileName
= "";
1008 if (OpenInternDescriptor(Mode
, compressor
) == false)
1012 return _error
->Errno("gzdopen",_("Could not open file descriptor %d"), Fd
);
1016 bool FileFd::OpenInternDescriptor(unsigned int const Mode
, APT::Configuration::Compressor
const &compressor
)
1018 d
->compressor
= compressor
;
1019 if (compressor
.Name
== "." || compressor
.Binary
.empty() == true)
1022 else if (compressor
.Name
== "gzip")
1024 if ((Mode
& ReadWrite
) == ReadWrite
)
1025 d
->gz
= gzdopen(iFd
, "r+");
1026 else if ((Mode
& WriteOnly
) == WriteOnly
)
1027 d
->gz
= gzdopen(iFd
, "w");
1029 d
->gz
= gzdopen(iFd
, "r");
1032 Flags
|= Compressed
;
1037 else if (compressor
.Name
== "bzip2")
1039 if ((Mode
& ReadWrite
) == ReadWrite
)
1040 d
->bz2
= BZ2_bzdopen(iFd
, "r+");
1041 else if ((Mode
& WriteOnly
) == WriteOnly
)
1042 d
->bz2
= BZ2_bzdopen(iFd
, "w");
1044 d
->bz2
= BZ2_bzdopen(iFd
, "r");
1047 Flags
|= Compressed
;
1053 if ((Mode
& ReadWrite
) == ReadWrite
)
1054 return _error
->Error("ReadWrite mode is not supported for file %s", FileName
.c_str());
1056 bool const Comp
= (Mode
& WriteOnly
) == WriteOnly
;
1057 // Handle 'decompression' of empty files
1062 if (Buf
.st_size
== 0 && S_ISFIFO(Buf
.st_mode
) == false)
1065 // We don't need the file open - instead let the compressor open it
1066 // as he properly knows better how to efficiently read from 'his' file
1067 if (FileName
.empty() == false)
1074 // Create a data pipe
1075 int Pipe
[2] = {-1,-1};
1076 if (pipe(Pipe
) != 0)
1077 return _error
->Errno("pipe",_("Failed to create subprocess IPC"));
1078 for (int J
= 0; J
!= 2; J
++)
1079 SetCloseExec(Pipe
[J
],true);
1081 d
->compressed_fd
= iFd
;
1090 d
->compressor_pid
= ExecFork();
1091 if (d
->compressor_pid
== 0)
1095 dup2(d
->compressed_fd
,STDOUT_FILENO
);
1096 dup2(Pipe
[0],STDIN_FILENO
);
1100 if (FileName
.empty() == true)
1101 dup2(d
->compressed_fd
,STDIN_FILENO
);
1102 dup2(Pipe
[1],STDOUT_FILENO
);
1104 int const nullfd
= open("/dev/null", O_WRONLY
);
1107 dup2(nullfd
,STDERR_FILENO
);
1111 SetCloseExec(STDOUT_FILENO
,false);
1112 SetCloseExec(STDIN_FILENO
,false);
1114 std::vector
<char const*> Args
;
1115 Args
.push_back(compressor
.Binary
.c_str());
1116 std::vector
<std::string
> const * const addArgs
=
1117 (Comp
== true) ? &(compressor
.CompressArgs
) : &(compressor
.UncompressArgs
);
1118 for (std::vector
<std::string
>::const_iterator a
= addArgs
->begin();
1119 a
!= addArgs
->end(); ++a
)
1120 Args
.push_back(a
->c_str());
1121 if (Comp
== false && FileName
.empty() == false)
1123 Args
.push_back("--stdout");
1124 if (TemporaryFileName
.empty() == false)
1125 Args
.push_back(TemporaryFileName
.c_str());
1127 Args
.push_back(FileName
.c_str());
1129 Args
.push_back(NULL
);
1131 execvp(Args
[0],(char **)&Args
[0]);
1132 cerr
<< _("Failed to exec compressor ") << Args
[0] << endl
;
1139 if ((Comp
== true || FileName
.empty() == true) && d
->compressed_fd
!= -1)
1140 close(d
->compressed_fd
);
1145 // FileFd::~File - Closes the file /*{{{*/
1146 // ---------------------------------------------------------------------
1147 /* If the proper modes are selected then we close the Fd and possibly
1148 unlink the file on error. */
1154 // FileFd::Read - Read a bit of the file /*{{{*/
1155 // ---------------------------------------------------------------------
1156 /* We are carefull to handle interruption by a signal while reading
1158 bool FileFd::Read(void *To
,unsigned long long Size
,unsigned long long *Actual
)
1164 *((char *)To
) = '\0';
1169 Res
= gzread(d
->gz
,To
,Size
);
1174 Res
= BZ2_bzread(d
->bz2
,To
,Size
);
1177 Res
= read(iFd
,To
,Size
);
1188 char const * const errmsg
= gzerror(d
->gz
, &err
);
1190 return _error
->Error("gzread: %s (%d: %s)", _("Read error"), err
, errmsg
);
1197 char const * const errmsg
= BZ2_bzerror(d
->bz2
, &err
);
1198 if (err
!= BZ_IO_ERROR
)
1199 return _error
->Error("BZ2_bzread: %s (%d: %s)", _("Read error"), err
, errmsg
);
1202 return _error
->Errno("read",_("Read error"));
1205 To
= (char *)To
+ Res
;
1211 while (Res
> 0 && Size
> 0);
1224 return _error
->Error(_("read, still have %llu to read but none left"), Size
);
1227 // FileFd::ReadLine - Read a complete line from the file /*{{{*/
1228 // ---------------------------------------------------------------------
1229 /* Beware: This method can be quiet slow for big buffers on UNcompressed
1230 files because of the naive implementation! */
1231 char* FileFd::ReadLine(char *To
, unsigned long long const Size
)
1236 return gzgets(d
->gz
, To
, Size
);
1239 unsigned long long read
= 0;
1240 while ((Size
- 1) != read
)
1242 unsigned long long done
= 0;
1243 if (Read(To
+ read
, 1, &done
) == false)
1247 if (To
[read
++] == '\n')
1256 // FileFd::Write - Write to the file /*{{{*/
1257 // ---------------------------------------------------------------------
1259 bool FileFd::Write(const void *From
,unsigned long long Size
)
1267 Res
= gzwrite(d
->gz
,From
,Size
);
1272 Res
= BZ2_bzwrite(d
->bz2
,(void*)From
,Size
);
1275 Res
= write(iFd
,From
,Size
);
1276 if (Res
< 0 && errno
== EINTR
)
1285 char const * const errmsg
= gzerror(d
->gz
, &err
);
1287 return _error
->Error("gzwrite: %s (%d: %s)", _("Write error"), err
, errmsg
);
1294 char const * const errmsg
= BZ2_bzerror(d
->bz2
, &err
);
1295 if (err
!= BZ_IO_ERROR
)
1296 return _error
->Error("BZ2_bzwrite: %s (%d: %s)", _("Write error"), err
, errmsg
);
1299 return _error
->Errno("write",_("Write error"));
1302 From
= (char *)From
+ Res
;
1306 while (Res
> 0 && Size
> 0);
1312 return _error
->Error(_("write, still have %llu to write but couldn't"), Size
);
1314 bool FileFd::Write(int Fd
, const void *From
, unsigned long long Size
)
1320 Res
= write(Fd
,From
,Size
);
1321 if (Res
< 0 && errno
== EINTR
)
1324 return _error
->Errno("write",_("Write error"));
1326 From
= (char *)From
+ Res
;
1329 while (Res
> 0 && Size
> 0);
1334 return _error
->Error(_("write, still have %llu to write but couldn't"), Size
);
1337 // FileFd::Seek - Seek in the file /*{{{*/
1338 // ---------------------------------------------------------------------
1340 bool FileFd::Seek(unsigned long long To
)
1348 // Our poor man seeking in pipes is costly, so try to avoid it
1349 unsigned long long seekpos
= Tell();
1352 else if (seekpos
< To
)
1353 return Skip(To
- seekpos
);
1355 if ((d
->openmode
& ReadOnly
) != ReadOnly
)
1356 return _error
->Error("Reopen is only implemented for read-only files!");
1359 BZ2_bzclose(d
->bz2
);
1364 if (TemporaryFileName
.empty() == false)
1365 iFd
= open(TemporaryFileName
.c_str(), O_RDONLY
);
1366 else if (FileName
.empty() == false)
1367 iFd
= open(FileName
.c_str(), O_RDONLY
);
1370 if (d
->compressed_fd
> 0)
1371 if (lseek(d
->compressed_fd
, 0, SEEK_SET
) != 0)
1372 iFd
= d
->compressed_fd
;
1374 return _error
->Error("Reopen is not implemented for pipes opened with FileFd::OpenDescriptor()!");
1377 if (OpenInternDescriptor(d
->openmode
, d
->compressor
) == false)
1378 return _error
->Error("Seek on file %s because it couldn't be reopened", FileName
.c_str());
1389 res
= gzseek(d
->gz
,To
,SEEK_SET
);
1392 res
= lseek(iFd
,To
,SEEK_SET
);
1393 if (res
!= (signed)To
)
1396 return _error
->Error("Unable to seek to %llu", To
);
1403 // FileFd::Skip - Seek in the file /*{{{*/
1404 // ---------------------------------------------------------------------
1406 bool FileFd::Skip(unsigned long long Over
)
1418 unsigned long long toread
= std::min((unsigned long long) sizeof(buffer
), Over
);
1419 if (Read(buffer
, toread
) == false)
1420 return _error
->Error("Unable to seek ahead %llu",Over
);
1429 res
= gzseek(d
->gz
,Over
,SEEK_CUR
);
1432 res
= lseek(iFd
,Over
,SEEK_CUR
);
1436 return _error
->Error("Unable to seek ahead %llu",Over
);
1443 // FileFd::Truncate - Truncate the file /*{{{*/
1444 // ---------------------------------------------------------------------
1446 bool FileFd::Truncate(unsigned long long To
)
1448 #if defined HAVE_ZLIB || defined HAVE_BZ2
1449 if (d
->gz
!= NULL
|| d
->bz2
!= NULL
)
1452 return _error
->Error("Truncating compressed files is not implemented (%s)", FileName
.c_str());
1455 if (ftruncate(iFd
,To
) != 0)
1458 return _error
->Error("Unable to truncate to %llu",To
);
1464 // FileFd::Tell - Current seek position /*{{{*/
1465 // ---------------------------------------------------------------------
1467 unsigned long long FileFd::Tell()
1469 // In theory, we could just return seekpos here always instead of
1470 // seeking around, but not all users of FileFd use always Seek() and co
1471 // so d->seekpos isn't always true and we can just use it as a hint if
1472 // we have nothing else, but not always as an authority…
1483 Res
= gztell(d
->gz
);
1486 Res
= lseek(iFd
,0,SEEK_CUR
);
1487 if (Res
== (off_t
)-1)
1488 _error
->Errno("lseek","Failed to determine the current file position");
1493 // FileFd::FileSize - Return the size of the file /*{{{*/
1494 // ---------------------------------------------------------------------
1496 unsigned long long FileFd::FileSize()
1499 if (d
->pipe
== false && fstat(iFd
,&Buf
) != 0)
1500 return _error
->Errno("fstat","Unable to determine the file size");
1502 // for compressor pipes st_size is undefined and at 'best' zero
1503 if (d
->pipe
== true || S_ISFIFO(Buf
.st_mode
))
1505 // we set it here, too, as we get the info here for free
1506 // in theory the Open-methods should take care of it already
1508 if (stat(FileName
.c_str(), &Buf
) != 0)
1509 return _error
->Errno("stat","Unable to determine the file size");
1515 // FileFd::Size - Return the size of the content in the file /*{{{*/
1516 // ---------------------------------------------------------------------
1518 unsigned long long FileFd::Size()
1520 unsigned long long size
= FileSize();
1522 // for compressor pipes st_size is undefined and at 'best' zero,
1523 // so we 'read' the content and 'seek' back - see there
1526 || (d
->bz2
&& size
> 0)
1530 unsigned long long const oldSeek
= Tell();
1532 unsigned long long read
= 0;
1534 Read(ignore
, sizeof(ignore
), &read
);
1540 // only check gzsize if we are actually a gzip file, just checking for
1541 // "gz" is not sufficient as uncompressed files could be opened with
1542 // gzopen in "direct" mode as well
1543 else if (d
->gz
&& !gzdirect(d
->gz
) && size
> 0)
1545 off_t
const oldPos
= lseek(iFd
,0,SEEK_CUR
);
1546 /* unfortunately zlib.h doesn't provide a gzsize(), so we have to do
1547 * this ourselves; the original (uncompressed) file size is the last 32
1548 * bits of the file */
1549 // FIXME: Size for gz-files is limited by 32bit… no largefile support
1550 if (lseek(iFd
, -4, SEEK_END
) < 0)
1551 return _error
->Errno("lseek","Unable to seek to end of gzipped file");
1553 if (read(iFd
, &size
, 4) != 4)
1554 return _error
->Errno("read","Unable to read original size of gzipped file");
1556 #ifdef WORDS_BIGENDIAN
1557 uint32_t tmp_size
= size
;
1558 uint8_t const * const p
= (uint8_t const * const) &tmp_size
;
1559 tmp_size
= (p
[3] << 24) | (p
[2] << 16) | (p
[1] << 8) | p
[0];
1563 if (lseek(iFd
, oldPos
, SEEK_SET
) < 0)
1564 return _error
->Errno("lseek","Unable to seek in gzipped file");
1573 // FileFd::ModificationTime - Return the time of last touch /*{{{*/
1574 // ---------------------------------------------------------------------
1576 time_t FileFd::ModificationTime()
1579 if (d
->pipe
== false && fstat(iFd
,&Buf
) != 0)
1581 _error
->Errno("fstat","Unable to determine the modification time of file %s", FileName
.c_str());
1585 // for compressor pipes st_size is undefined and at 'best' zero
1586 if (d
->pipe
== true || S_ISFIFO(Buf
.st_mode
))
1588 // we set it here, too, as we get the info here for free
1589 // in theory the Open-methods should take care of it already
1591 if (stat(FileName
.c_str(), &Buf
) != 0)
1593 _error
->Errno("fstat","Unable to determine the modification time of file %s", FileName
.c_str());
1598 return Buf
.st_mtime
;
1601 // FileFd::Close - Close the file if the close flag is set /*{{{*/
1602 // ---------------------------------------------------------------------
1604 bool FileFd::Close()
1610 if ((Flags
& AutoClose
) == AutoClose
)
1613 if (d
!= NULL
&& d
->gz
!= NULL
) {
1614 int const e
= gzclose(d
->gz
);
1615 // gzdclose() on empty files always fails with "buffer error" here, ignore that
1616 if (e
!= 0 && e
!= Z_BUF_ERROR
)
1617 Res
&= _error
->Errno("close",_("Problem closing the gzip file %s"), FileName
.c_str());
1621 if (d
!= NULL
&& d
->bz2
!= NULL
)
1622 BZ2_bzclose(d
->bz2
);
1625 if (iFd
> 0 && close(iFd
) != 0)
1626 Res
&= _error
->Errno("close",_("Problem closing the file %s"), FileName
.c_str());
1629 if ((Flags
& Replace
) == Replace
&& iFd
>= 0) {
1630 if (rename(TemporaryFileName
.c_str(), FileName
.c_str()) != 0)
1631 Res
&= _error
->Errno("rename",_("Problem renaming the file %s to %s"), TemporaryFileName
.c_str(), FileName
.c_str());
1633 FileName
= TemporaryFileName
; // for the unlink() below.
1634 TemporaryFileName
.clear();
1639 if ((Flags
& Fail
) == Fail
&& (Flags
& DelOnFail
) == DelOnFail
&&
1640 FileName
.empty() == false)
1641 if (unlink(FileName
.c_str()) != 0)
1642 Res
&= _error
->WarningE("unlnk",_("Problem unlinking the file %s"), FileName
.c_str());
1646 if (d
->compressor_pid
> 0)
1647 ExecWait(d
->compressor_pid
, "FileFdCompressor", true);
1655 // FileFd::Sync - Sync the file /*{{{*/
1656 // ---------------------------------------------------------------------
1660 #ifdef _POSIX_SYNCHRONIZED_IO
1661 if (fsync(iFd
) != 0)
1662 return _error
->Errno("sync",_("Problem syncing the file"));
1668 gzFile
FileFd::gzFd() { return (gzFile
) d
->gz
; }