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 // FIXME: Compressor Fds have some speed disadvantages and are a bit buggy currently,
48 // so while the current implementation satisfies the testcases it is not a real option
49 // to disable it for now
50 #define APT_USE_ZLIB 1
54 #pragma message "Usage of zlib is DISABLED!"
57 #ifdef WORDS_BIGENDIAN
76 APT::Configuration::Compressor compressor
;
77 unsigned int openmode
;
78 unsigned long long seekpos
;
79 FileFdPrivate() : gz(NULL
), compressed_fd(-1), compressor_pid(-1), pipe(false),
80 openmode(0), seekpos(0) {};
83 // RunScripts - Run a set of scripts from a configuration subtree /*{{{*/
84 // ---------------------------------------------------------------------
86 bool RunScripts(const char *Cnf
)
88 Configuration::Item
const *Opts
= _config
->Tree(Cnf
);
89 if (Opts
== 0 || Opts
->Child
== 0)
93 // Fork for running the system calls
94 pid_t Child
= ExecFork();
99 if (_config
->FindDir("DPkg::Chroot-Directory","/") != "/")
101 std::cerr
<< "Chrooting into "
102 << _config
->FindDir("DPkg::Chroot-Directory")
104 if (chroot(_config
->FindDir("DPkg::Chroot-Directory","/").c_str()) != 0)
108 if (chdir("/tmp/") != 0)
111 unsigned int Count
= 1;
112 for (; Opts
!= 0; Opts
= Opts
->Next
, Count
++)
114 if (Opts
->Value
.empty() == true)
117 if (system(Opts
->Value
.c_str()) != 0)
123 // Wait for the child
125 while (waitpid(Child
,&Status
,0) != Child
)
129 return _error
->Errno("waitpid","Couldn't wait for subprocess");
132 // Restore sig int/quit
133 signal(SIGQUIT
,SIG_DFL
);
134 signal(SIGINT
,SIG_DFL
);
136 // Check for an error code.
137 if (WIFEXITED(Status
) == 0 || WEXITSTATUS(Status
) != 0)
139 unsigned int Count
= WEXITSTATUS(Status
);
143 for (; Opts
!= 0 && Count
!= 1; Opts
= Opts
->Next
, Count
--);
144 _error
->Error("Problem executing scripts %s '%s'",Cnf
,Opts
->Value
.c_str());
147 return _error
->Error("Sub-process returned an error code");
154 // CopyFile - Buffered copy of a file /*{{{*/
155 // ---------------------------------------------------------------------
156 /* The caller is expected to set things so that failure causes erasure */
157 bool CopyFile(FileFd
&From
,FileFd
&To
)
159 if (From
.IsOpen() == false || To
.IsOpen() == false)
162 // Buffered copy between fds
163 SPtrArray
<unsigned char> Buf
= new unsigned char[64000];
164 unsigned long long Size
= From
.Size();
167 unsigned long long ToRead
= Size
;
171 if (From
.Read(Buf
,ToRead
) == false ||
172 To
.Write(Buf
,ToRead
) == false)
181 // GetLock - Gets a lock file /*{{{*/
182 // ---------------------------------------------------------------------
183 /* This will create an empty file of the given name and lock it. Once this
184 is done all other calls to GetLock in any other process will fail with
185 -1. The return result is the fd of the file, the call should call
186 close at some time. */
187 int GetLock(string File
,bool Errors
)
189 // GetLock() is used in aptitude on directories with public-write access
190 // Use O_NOFOLLOW here to prevent symlink traversal attacks
191 int FD
= open(File
.c_str(),O_RDWR
| O_CREAT
| O_NOFOLLOW
,0640);
194 // Read only .. cant have locking problems there.
197 _error
->Warning(_("Not using locking for read only lock file %s"),File
.c_str());
198 return dup(0); // Need something for the caller to close
202 _error
->Errno("open",_("Could not open lock file %s"),File
.c_str());
204 // Feh.. We do this to distinguish the lock vs open case..
208 SetCloseExec(FD
,true);
210 // Aquire a write lock
213 fl
.l_whence
= SEEK_SET
;
216 if (fcntl(FD
,F_SETLK
,&fl
) == -1)
220 _error
->Warning(_("Not using locking for nfs mounted lock file %s"),File
.c_str());
221 return dup(0); // Need something for the caller to close
224 _error
->Errno("open",_("Could not get lock %s"),File
.c_str());
235 // FileExists - Check if a file exists /*{{{*/
236 // ---------------------------------------------------------------------
237 /* Beware: Directories are also files! */
238 bool FileExists(string File
)
241 if (stat(File
.c_str(),&Buf
) != 0)
246 // RealFileExists - Check if a file exists and if it is really a file /*{{{*/
247 // ---------------------------------------------------------------------
249 bool RealFileExists(string File
)
252 if (stat(File
.c_str(),&Buf
) != 0)
254 return ((Buf
.st_mode
& S_IFREG
) != 0);
257 // DirectoryExists - Check if a directory exists and is really one /*{{{*/
258 // ---------------------------------------------------------------------
260 bool DirectoryExists(string
const &Path
)
263 if (stat(Path
.c_str(),&Buf
) != 0)
265 return ((Buf
.st_mode
& S_IFDIR
) != 0);
268 // CreateDirectory - poor man's mkdir -p guarded by a parent directory /*{{{*/
269 // ---------------------------------------------------------------------
270 /* This method will create all directories needed for path in good old
271 mkdir -p style but refuses to do this if Parent is not a prefix of
272 this Path. Example: /var/cache/ and /var/cache/apt/archives are given,
273 so it will create apt/archives if /var/cache exists - on the other
274 hand if the parent is /var/lib the creation will fail as this path
275 is not a parent of the path to be generated. */
276 bool CreateDirectory(string
const &Parent
, string
const &Path
)
278 if (Parent
.empty() == true || Path
.empty() == true)
281 if (DirectoryExists(Path
) == true)
284 if (DirectoryExists(Parent
) == false)
287 // we are not going to create directories "into the blue"
288 if (Path
.find(Parent
, 0) != 0)
291 vector
<string
> const dirs
= VectorizeString(Path
.substr(Parent
.size()), '/');
292 string progress
= Parent
;
293 for (vector
<string
>::const_iterator d
= dirs
.begin(); d
!= dirs
.end(); ++d
)
295 if (d
->empty() == true)
298 progress
.append("/").append(*d
);
299 if (DirectoryExists(progress
) == true)
302 if (mkdir(progress
.c_str(), 0755) != 0)
308 // CreateAPTDirectoryIfNeeded - ensure that the given directory exists /*{{{*/
309 // ---------------------------------------------------------------------
310 /* a small wrapper around CreateDirectory to check if it exists and to
311 remove the trailing "/apt/" from the parent directory if needed */
312 bool CreateAPTDirectoryIfNeeded(string
const &Parent
, string
const &Path
)
314 if (DirectoryExists(Path
) == true)
317 size_t const len
= Parent
.size();
318 if (len
> 5 && Parent
.find("/apt/", len
- 6, 5) == len
- 5)
320 if (CreateDirectory(Parent
.substr(0,len
-5), Path
) == true)
323 else if (CreateDirectory(Parent
, Path
) == true)
329 // GetListOfFilesInDir - returns a vector of files in the given dir /*{{{*/
330 // ---------------------------------------------------------------------
331 /* If an extension is given only files with this extension are included
332 in the returned vector, otherwise every "normal" file is included. */
333 std::vector
<string
> GetListOfFilesInDir(string
const &Dir
, string
const &Ext
,
334 bool const &SortList
, bool const &AllowNoExt
)
336 std::vector
<string
> ext
;
338 if (Ext
.empty() == false)
340 if (AllowNoExt
== true && ext
.empty() == false)
342 return GetListOfFilesInDir(Dir
, ext
, SortList
);
344 std::vector
<string
> GetListOfFilesInDir(string
const &Dir
, std::vector
<string
> const &Ext
,
345 bool const &SortList
)
347 // Attention debuggers: need to be set with the environment config file!
348 bool const Debug
= _config
->FindB("Debug::GetListOfFilesInDir", false);
351 std::clog
<< "Accept in " << Dir
<< " only files with the following " << Ext
.size() << " extensions:" << std::endl
;
352 if (Ext
.empty() == true)
353 std::clog
<< "\tNO extension" << std::endl
;
355 for (std::vector
<string
>::const_iterator e
= Ext
.begin();
357 std::clog
<< '\t' << (e
->empty() == true ? "NO" : *e
) << " extension" << std::endl
;
360 std::vector
<string
> List
;
362 if (DirectoryExists(Dir
.c_str()) == false)
364 _error
->Error(_("List of files can't be created as '%s' is not a directory"), Dir
.c_str());
368 Configuration::MatchAgainstConfig
SilentIgnore("Dir::Ignore-Files-Silently");
369 DIR *D
= opendir(Dir
.c_str());
372 _error
->Errno("opendir",_("Unable to read %s"),Dir
.c_str());
376 for (struct dirent
*Ent
= readdir(D
); Ent
!= 0; Ent
= readdir(D
))
378 // skip "hidden" files
379 if (Ent
->d_name
[0] == '.')
382 // Make sure it is a file and not something else
383 string
const File
= flCombine(Dir
,Ent
->d_name
);
384 #ifdef _DIRENT_HAVE_D_TYPE
385 if (Ent
->d_type
!= DT_REG
)
388 if (RealFileExists(File
.c_str()) == false)
390 // do not show ignoration warnings for directories
392 #ifdef _DIRENT_HAVE_D_TYPE
393 Ent
->d_type
== DT_DIR
||
395 DirectoryExists(File
.c_str()) == true)
397 if (SilentIgnore
.Match(Ent
->d_name
) == false)
398 _error
->Notice(_("Ignoring '%s' in directory '%s' as it is not a regular file"), Ent
->d_name
, Dir
.c_str());
403 // check for accepted extension:
404 // no extension given -> periods are bad as hell!
405 // extensions given -> "" extension allows no extension
406 if (Ext
.empty() == false)
408 string d_ext
= flExtension(Ent
->d_name
);
409 if (d_ext
== Ent
->d_name
) // no extension
411 if (std::find(Ext
.begin(), Ext
.end(), "") == Ext
.end())
414 std::clog
<< "Bad file: " << Ent
->d_name
<< " → no extension" << std::endl
;
415 if (SilentIgnore
.Match(Ent
->d_name
) == false)
416 _error
->Notice(_("Ignoring file '%s' in directory '%s' as it has no filename extension"), Ent
->d_name
, Dir
.c_str());
420 else if (std::find(Ext
.begin(), Ext
.end(), d_ext
) == Ext
.end())
423 std::clog
<< "Bad file: " << Ent
->d_name
<< " → bad extension »" << flExtension(Ent
->d_name
) << "«" << std::endl
;
424 if (SilentIgnore
.Match(Ent
->d_name
) == false)
425 _error
->Notice(_("Ignoring file '%s' in directory '%s' as it has an invalid filename extension"), Ent
->d_name
, Dir
.c_str());
430 // Skip bad filenames ala run-parts
431 const char *C
= Ent
->d_name
;
433 if (isalpha(*C
) == 0 && isdigit(*C
) == 0
434 && *C
!= '_' && *C
!= '-') {
435 // no required extension -> dot is a bad character
436 if (*C
== '.' && Ext
.empty() == false)
441 // we don't reach the end of the name -> bad character included
445 std::clog
<< "Bad file: " << Ent
->d_name
<< " → bad character »"
446 << *C
<< "« in filename (period allowed: " << (Ext
.empty() ? "no" : "yes") << ")" << std::endl
;
450 // skip filenames which end with a period. These are never valid
454 std::clog
<< "Bad file: " << Ent
->d_name
<< " → Period as last character" << std::endl
;
459 std::clog
<< "Accept file: " << Ent
->d_name
<< " in " << Dir
<< std::endl
;
460 List
.push_back(File
);
464 if (SortList
== true)
465 std::sort(List
.begin(),List
.end());
468 std::vector
<string
> GetListOfFilesInDir(string
const &Dir
, bool SortList
)
470 bool const Debug
= _config
->FindB("Debug::GetListOfFilesInDir", false);
472 std::clog
<< "Accept in " << Dir
<< " all regular files" << std::endl
;
474 std::vector
<string
> List
;
476 if (DirectoryExists(Dir
.c_str()) == false)
478 _error
->Error(_("List of files can't be created as '%s' is not a directory"), Dir
.c_str());
482 DIR *D
= opendir(Dir
.c_str());
485 _error
->Errno("opendir",_("Unable to read %s"),Dir
.c_str());
489 for (struct dirent
*Ent
= readdir(D
); Ent
!= 0; Ent
= readdir(D
))
491 // skip "hidden" files
492 if (Ent
->d_name
[0] == '.')
495 // Make sure it is a file and not something else
496 string
const File
= flCombine(Dir
,Ent
->d_name
);
497 #ifdef _DIRENT_HAVE_D_TYPE
498 if (Ent
->d_type
!= DT_REG
)
501 if (RealFileExists(File
.c_str()) == false)
504 std::clog
<< "Bad file: " << Ent
->d_name
<< " → it is not a real file" << std::endl
;
509 // Skip bad filenames ala run-parts
510 const char *C
= Ent
->d_name
;
512 if (isalpha(*C
) == 0 && isdigit(*C
) == 0
513 && *C
!= '_' && *C
!= '-' && *C
!= '.')
516 // we don't reach the end of the name -> bad character included
520 std::clog
<< "Bad file: " << Ent
->d_name
<< " → bad character »" << *C
<< "« in filename" << std::endl
;
524 // skip filenames which end with a period. These are never valid
528 std::clog
<< "Bad file: " << Ent
->d_name
<< " → Period as last character" << std::endl
;
533 std::clog
<< "Accept file: " << Ent
->d_name
<< " in " << Dir
<< std::endl
;
534 List
.push_back(File
);
538 if (SortList
== true)
539 std::sort(List
.begin(),List
.end());
543 // SafeGetCWD - This is a safer getcwd that returns a dynamic string /*{{{*/
544 // ---------------------------------------------------------------------
545 /* We return / on failure. */
548 // Stash the current dir.
551 if (getcwd(S
,sizeof(S
)-2) == 0)
553 unsigned int Len
= strlen(S
);
559 // GetModificationTime - Get the mtime of the given file or -1 on error /*{{{*/
560 // ---------------------------------------------------------------------
561 /* We return / on failure. */
562 time_t GetModificationTime(string
const &Path
)
565 if (stat(Path
.c_str(), &St
) < 0)
570 // flNotDir - Strip the directory from the filename /*{{{*/
571 // ---------------------------------------------------------------------
573 string
flNotDir(string File
)
575 string::size_type Res
= File
.rfind('/');
576 if (Res
== string::npos
)
579 return string(File
,Res
,Res
- File
.length());
582 // flNotFile - Strip the file from the directory name /*{{{*/
583 // ---------------------------------------------------------------------
584 /* Result ends in a / */
585 string
flNotFile(string File
)
587 string::size_type Res
= File
.rfind('/');
588 if (Res
== string::npos
)
591 return string(File
,0,Res
);
594 // flExtension - Return the extension for the file /*{{{*/
595 // ---------------------------------------------------------------------
597 string
flExtension(string File
)
599 string::size_type Res
= File
.rfind('.');
600 if (Res
== string::npos
)
603 return string(File
,Res
,Res
- File
.length());
606 // flNoLink - If file is a symlink then deref it /*{{{*/
607 // ---------------------------------------------------------------------
608 /* If the name is not a link then the returned path is the input. */
609 string
flNoLink(string File
)
612 if (lstat(File
.c_str(),&St
) != 0 || S_ISLNK(St
.st_mode
) == 0)
614 if (stat(File
.c_str(),&St
) != 0)
617 /* Loop resolving the link. There is no need to limit the number of
618 loops because the stat call above ensures that the symlink is not
626 if ((Res
= readlink(NFile
.c_str(),Buffer
,sizeof(Buffer
))) <= 0 ||
627 (unsigned)Res
>= sizeof(Buffer
))
630 // Append or replace the previous path
632 if (Buffer
[0] == '/')
635 NFile
= flNotFile(NFile
) + Buffer
;
637 // See if we are done
638 if (lstat(NFile
.c_str(),&St
) != 0)
640 if (S_ISLNK(St
.st_mode
) == 0)
645 // flCombine - Combine a file and a directory /*{{{*/
646 // ---------------------------------------------------------------------
647 /* If the file is an absolute path then it is just returned, otherwise
648 the directory is pre-pended to it. */
649 string
flCombine(string Dir
,string File
)
651 if (File
.empty() == true)
654 if (File
[0] == '/' || Dir
.empty() == true)
656 if (File
.length() >= 2 && File
[0] == '.' && File
[1] == '/')
658 if (Dir
[Dir
.length()-1] == '/')
660 return Dir
+ '/' + File
;
663 // SetCloseExec - Set the close on exec flag /*{{{*/
664 // ---------------------------------------------------------------------
666 void SetCloseExec(int Fd
,bool Close
)
668 if (fcntl(Fd
,F_SETFD
,(Close
== false)?0:FD_CLOEXEC
) != 0)
670 cerr
<< "FATAL -> Could not set close on exec " << strerror(errno
) << endl
;
675 // SetNonBlock - Set the nonblocking flag /*{{{*/
676 // ---------------------------------------------------------------------
678 void SetNonBlock(int Fd
,bool Block
)
680 int Flags
= fcntl(Fd
,F_GETFL
) & (~O_NONBLOCK
);
681 if (fcntl(Fd
,F_SETFL
,Flags
| ((Block
== false)?0:O_NONBLOCK
)) != 0)
683 cerr
<< "FATAL -> Could not set non-blocking flag " << strerror(errno
) << endl
;
688 // WaitFd - Wait for a FD to become readable /*{{{*/
689 // ---------------------------------------------------------------------
690 /* This waits for a FD to become readable using select. It is useful for
691 applications making use of non-blocking sockets. The timeout is
693 bool WaitFd(int Fd
,bool write
,unsigned long timeout
)
706 Res
= select(Fd
+1,0,&Set
,0,(timeout
!= 0?&tv
:0));
708 while (Res
< 0 && errno
== EINTR
);
718 Res
= select(Fd
+1,&Set
,0,0,(timeout
!= 0?&tv
:0));
720 while (Res
< 0 && errno
== EINTR
);
729 // ExecFork - Magical fork that sanitizes the context before execing /*{{{*/
730 // ---------------------------------------------------------------------
731 /* This is used if you want to cleanse the environment for the forked
732 child, it fixes up the important signals and nukes all of the fds,
733 otherwise acts like normal fork. */
736 // Fork off the process
737 pid_t Process
= fork();
740 cerr
<< "FATAL -> Failed to fork." << endl
;
744 // Spawn the subprocess
748 signal(SIGPIPE
,SIG_DFL
);
749 signal(SIGQUIT
,SIG_DFL
);
750 signal(SIGINT
,SIG_DFL
);
751 signal(SIGWINCH
,SIG_DFL
);
752 signal(SIGCONT
,SIG_DFL
);
753 signal(SIGTSTP
,SIG_DFL
);
756 Configuration::Item
const *Opts
= _config
->Tree("APT::Keep-Fds");
757 if (Opts
!= 0 && Opts
->Child
!= 0)
760 for (; Opts
!= 0; Opts
= Opts
->Next
)
762 if (Opts
->Value
.empty() == true)
764 int fd
= atoi(Opts
->Value
.c_str());
769 // Close all of our FDs - just in case
770 for (int K
= 3; K
!= 40; K
++)
772 if(KeepFDs
.find(K
) == KeepFDs
.end())
773 fcntl(K
,F_SETFD
,FD_CLOEXEC
);
780 // ExecWait - Fancy waitpid /*{{{*/
781 // ---------------------------------------------------------------------
782 /* Waits for the given sub process. If Reap is set then no errors are
783 generated. Otherwise a failed subprocess will generate a proper descriptive
785 bool ExecWait(pid_t Pid
,const char *Name
,bool Reap
)
790 // Wait and collect the error code
792 while (waitpid(Pid
,&Status
,0) != Pid
)
800 return _error
->Error(_("Waited for %s but it wasn't there"),Name
);
804 // Check for an error code.
805 if (WIFEXITED(Status
) == 0 || WEXITSTATUS(Status
) != 0)
809 if (WIFSIGNALED(Status
) != 0)
811 if( WTERMSIG(Status
) == SIGSEGV
)
812 return _error
->Error(_("Sub-process %s received a segmentation fault."),Name
);
814 return _error
->Error(_("Sub-process %s received signal %u."),Name
, WTERMSIG(Status
));
817 if (WIFEXITED(Status
) != 0)
818 return _error
->Error(_("Sub-process %s returned an error code (%u)"),Name
,WEXITSTATUS(Status
));
820 return _error
->Error(_("Sub-process %s exited unexpectedly"),Name
);
827 // FileFd::Open - Open a file /*{{{*/
828 // ---------------------------------------------------------------------
829 /* The most commonly used open mode combinations are given with Mode */
830 bool FileFd::Open(string FileName
,unsigned int const Mode
,CompressMode Compress
, unsigned long const Perms
)
832 if (Mode
== ReadOnlyGzip
)
833 return Open(FileName
, ReadOnly
, Gzip
, Perms
);
835 if (Compress
== Auto
&& (Mode
& WriteOnly
) == WriteOnly
)
836 return _error
->Error("Autodetection on %s only works in ReadOnly openmode!", FileName
.c_str());
838 // FIXME: Denote inbuilt compressors somehow - as we don't need to have the binaries for them
839 std::vector
<APT::Configuration::Compressor
> const compressors
= APT::Configuration::getCompressors();
840 std::vector
<APT::Configuration::Compressor
>::const_iterator compressor
= compressors
.begin();
841 if (Compress
== Auto
)
843 for (; compressor
!= compressors
.end(); ++compressor
)
845 std::string file
= std::string(FileName
).append(compressor
->Extension
);
846 if (FileExists(file
) == false)
852 else if (Compress
== Extension
)
854 std::string::size_type
const found
= FileName
.find_last_of('.');
856 if (found
!= std::string::npos
)
858 ext
= FileName
.substr(found
);
859 if (ext
== ".new" || ext
== ".bak")
861 std::string::size_type
const found2
= FileName
.find_last_of('.', found
- 1);
862 if (found2
!= std::string::npos
)
863 ext
= FileName
.substr(found2
, found
- found2
);
868 for (; compressor
!= compressors
.end(); ++compressor
)
869 if (ext
== compressor
->Extension
)
871 // no matching extension - assume uncompressed (imagine files like 'example.org_Packages')
872 if (compressor
== compressors
.end())
873 for (compressor
= compressors
.begin(); compressor
!= compressors
.end(); ++compressor
)
874 if (compressor
->Name
== ".")
882 case None
: name
= "."; break;
883 case Gzip
: name
= "gzip"; break;
884 case Bzip2
: name
= "bzip2"; break;
885 case Lzma
: name
= "lzma"; break;
886 case Xz
: name
= "xz"; break;
890 return _error
->Error("Opening File %s in None, Auto or Extension should be already handled?!?", FileName
.c_str());
892 for (; compressor
!= compressors
.end(); ++compressor
)
893 if (compressor
->Name
== name
)
895 if (compressor
== compressors
.end())
896 return _error
->Error("Can't find a configured compressor %s for file %s", name
.c_str(), FileName
.c_str());
899 if (compressor
== compressors
.end())
900 return _error
->Error("Can't find a match for specified compressor mode for file %s", FileName
.c_str());
901 return Open(FileName
, Mode
, *compressor
, Perms
);
903 bool FileFd::Open(string FileName
,unsigned int const Mode
,APT::Configuration::Compressor
const &compressor
, unsigned long const Perms
)
906 d
= new FileFdPrivate
;
910 if ((Mode
& WriteOnly
) != WriteOnly
&& (Mode
& (Atomic
| Create
| Empty
| Exclusive
)) != 0)
911 return _error
->Error("ReadOnly mode for %s doesn't accept additional flags!", FileName
.c_str());
912 if ((Mode
& ReadWrite
) == 0)
913 return _error
->Error("No openmode provided in FileFd::Open for %s", FileName
.c_str());
915 if ((Mode
& Atomic
) == Atomic
)
918 char *name
= strdup((FileName
+ ".XXXXXX").c_str());
919 TemporaryFileName
= string(mktemp(name
));
922 else if ((Mode
& (Exclusive
| Create
)) == (Exclusive
| Create
))
924 // for atomic, this will be done by rename in Close()
925 unlink(FileName
.c_str());
927 if ((Mode
& Empty
) == Empty
)
930 if (lstat(FileName
.c_str(),&Buf
) == 0 && S_ISLNK(Buf
.st_mode
))
931 unlink(FileName
.c_str());
935 #define if_FLAGGED_SET(FLAG, MODE) if ((Mode & FLAG) == FLAG) fileflags |= MODE
936 if_FLAGGED_SET(ReadWrite
, O_RDWR
);
937 else if_FLAGGED_SET(ReadOnly
, O_RDONLY
);
938 else if_FLAGGED_SET(WriteOnly
, O_WRONLY
);
940 if_FLAGGED_SET(Create
, O_CREAT
);
941 if_FLAGGED_SET(Empty
, O_TRUNC
);
942 if_FLAGGED_SET(Exclusive
, O_EXCL
);
943 else if_FLAGGED_SET(Atomic
, O_EXCL
);
944 #undef if_FLAGGED_SET
946 if (TemporaryFileName
.empty() == false)
947 iFd
= open(TemporaryFileName
.c_str(), fileflags
, Perms
);
949 iFd
= open(FileName
.c_str(), fileflags
, Perms
);
951 this->FileName
= FileName
;
952 if (iFd
== -1 || OpenInternDescriptor(Mode
, compressor
) == false)
959 return _error
->Errno("open",_("Could not open file %s"), FileName
.c_str());
962 SetCloseExec(iFd
,true);
966 // FileFd::OpenDescriptor - Open a filedescriptor /*{{{*/
967 // ---------------------------------------------------------------------
969 bool FileFd::OpenDescriptor(int Fd
, unsigned int const Mode
, CompressMode Compress
, bool AutoClose
)
971 std::vector
<APT::Configuration::Compressor
> const compressors
= APT::Configuration::getCompressors();
972 std::vector
<APT::Configuration::Compressor
>::const_iterator compressor
= compressors
.begin();
975 // compat with the old API
976 if (Mode
== ReadOnlyGzip
&& Compress
== None
)
981 case None
: name
= "."; break;
982 case Gzip
: name
= "gzip"; break;
983 case Bzip2
: name
= "bzip2"; break;
984 case Lzma
: name
= "lzma"; break;
985 case Xz
: name
= "xz"; break;
988 return _error
->Error("Opening Fd %d in Auto or Extension compression mode is not supported", Fd
);
990 for (; compressor
!= compressors
.end(); ++compressor
)
991 if (compressor
->Name
== name
)
993 if (compressor
== compressors
.end())
994 return _error
->Error("Can't find a configured compressor %s for file %s", name
.c_str(), FileName
.c_str());
996 return OpenDescriptor(Fd
, Mode
, *compressor
, AutoClose
);
998 bool FileFd::OpenDescriptor(int Fd
, unsigned int const Mode
, APT::Configuration::Compressor
const &compressor
, bool AutoClose
)
1001 d
= new FileFdPrivate
;
1003 Flags
= (AutoClose
) ? FileFd::AutoClose
: 0;
1005 this->FileName
= "";
1006 if (OpenInternDescriptor(Mode
, compressor
) == false)
1010 return _error
->Errno("gzdopen",_("Could not open file descriptor %d"), Fd
);
1014 bool FileFd::OpenInternDescriptor(unsigned int const Mode
, APT::Configuration::Compressor
const &compressor
)
1016 d
->compressor
= compressor
;
1017 if (compressor
.Name
== "." || compressor
.Binary
.empty() == true)
1020 else if (compressor
.Name
== "gzip")
1022 if ((Mode
& ReadWrite
) == ReadWrite
)
1023 d
->gz
= gzdopen(iFd
, "r+");
1024 else if ((Mode
& WriteOnly
) == WriteOnly
)
1025 d
->gz
= gzdopen(iFd
, "w");
1027 d
->gz
= gzdopen (iFd
, "r");
1030 Flags
|= Compressed
;
1035 if ((Mode
& ReadWrite
) == ReadWrite
)
1036 return _error
->Error("ReadWrite mode is not supported for file %s", FileName
.c_str());
1038 bool const Comp
= (Mode
& WriteOnly
) == WriteOnly
;
1039 // Handle 'decompression' of empty files
1044 if (Buf
.st_size
== 0 && S_ISFIFO(Buf
.st_mode
) == false)
1047 // We don't need the file open - instead let the compressor open it
1048 // as he properly knows better how to efficiently read from 'his' file
1049 if (FileName
.empty() == false)
1053 // Create a data pipe
1054 int Pipe
[2] = {-1,-1};
1055 if (pipe(Pipe
) != 0)
1056 return _error
->Errno("pipe",_("Failed to create subprocess IPC"));
1057 for (int J
= 0; J
!= 2; J
++)
1058 SetCloseExec(Pipe
[J
],true);
1060 d
->compressed_fd
= iFd
;
1069 d
->compressor_pid
= ExecFork();
1070 if (d
->compressor_pid
== 0)
1074 dup2(d
->compressed_fd
,STDOUT_FILENO
);
1075 dup2(Pipe
[0],STDIN_FILENO
);
1079 if (FileName
.empty() == true)
1080 dup2(d
->compressed_fd
,STDIN_FILENO
);
1081 dup2(Pipe
[1],STDOUT_FILENO
);
1084 SetCloseExec(STDOUT_FILENO
,false);
1085 SetCloseExec(STDIN_FILENO
,false);
1087 std::vector
<char const*> Args
;
1088 Args
.push_back(compressor
.Binary
.c_str());
1089 std::vector
<std::string
> const * const addArgs
=
1090 (Comp
== true) ? &(compressor
.CompressArgs
) : &(compressor
.UncompressArgs
);
1091 for (std::vector
<std::string
>::const_iterator a
= addArgs
->begin();
1092 a
!= addArgs
->end(); ++a
)
1093 Args
.push_back(a
->c_str());
1094 if (Comp
== false && FileName
.empty() == false)
1096 Args
.push_back("--stdout");
1097 if (TemporaryFileName
.empty() == false)
1098 Args
.push_back(TemporaryFileName
.c_str());
1100 Args
.push_back(FileName
.c_str());
1102 Args
.push_back(NULL
);
1104 execvp(Args
[0],(char **)&Args
[0]);
1105 cerr
<< _("Failed to exec compressor ") << Args
[0] << endl
;
1112 if (Comp
== true || FileName
.empty() == true)
1113 close(d
->compressed_fd
);
1118 // FileFd::~File - Closes the file /*{{{*/
1119 // ---------------------------------------------------------------------
1120 /* If the proper modes are selected then we close the Fd and possibly
1121 unlink the file on error. */
1127 // FileFd::Read - Read a bit of the file /*{{{*/
1128 // ---------------------------------------------------------------------
1129 /* We are carefull to handle interruption by a signal while reading
1131 bool FileFd::Read(void *To
,unsigned long long Size
,unsigned long long *Actual
)
1137 *((char *)To
) = '\0';
1142 Res
= gzread(d
->gz
,To
,Size
);
1145 Res
= read(iFd
,To
,Size
);
1156 char const * const errmsg
= gzerror(d
->gz
, &err
);
1158 return _error
->Error("gzread: %s (%d: %s)", _("Read error"), err
, errmsg
);
1161 return _error
->Errno("read",_("Read error"));
1164 To
= (char *)To
+ Res
;
1170 while (Res
> 0 && Size
> 0);
1183 return _error
->Error(_("read, still have %llu to read but none left"), Size
);
1186 // FileFd::ReadLine - Read a complete line from the file /*{{{*/
1187 // ---------------------------------------------------------------------
1188 /* Beware: This method can be quiet slow for big buffers on UNcompressed
1189 files because of the naive implementation! */
1190 char* FileFd::ReadLine(char *To
, unsigned long long const Size
)
1195 return gzgets(d
->gz
, To
, Size
);
1198 unsigned long long read
= 0;
1199 while ((Size
- 1) != read
)
1201 unsigned long long done
= 0;
1202 if (Read(To
+ read
, 1, &done
) == false)
1206 if (To
[read
++] == '\n')
1215 // FileFd::Write - Write to the file /*{{{*/
1216 // ---------------------------------------------------------------------
1218 bool FileFd::Write(const void *From
,unsigned long long Size
)
1226 Res
= gzwrite(d
->gz
,From
,Size
);
1229 Res
= write(iFd
,From
,Size
);
1230 if (Res
< 0 && errno
== EINTR
)
1235 return _error
->Errno("write",_("Write error"));
1238 From
= (char *)From
+ Res
;
1242 while (Res
> 0 && Size
> 0);
1248 return _error
->Error(_("write, still have %llu to write but couldn't"), Size
);
1251 // FileFd::Seek - Seek in the file /*{{{*/
1252 // ---------------------------------------------------------------------
1254 bool FileFd::Seek(unsigned long long To
)
1256 if (d
->pipe
== true)
1258 // Our poor man seeking in pipes is costly, so try to avoid it
1259 unsigned long long seekpos
= Tell();
1262 else if (seekpos
< To
)
1263 return Skip(To
- seekpos
);
1265 if ((d
->openmode
& ReadOnly
) != ReadOnly
)
1266 return _error
->Error("Reopen is only implemented for read-only files!");
1269 if (TemporaryFileName
.empty() == false)
1270 iFd
= open(TemporaryFileName
.c_str(), O_RDONLY
);
1271 else if (FileName
.empty() == false)
1272 iFd
= open(FileName
.c_str(), O_RDONLY
);
1275 if (d
->compressed_fd
> 0)
1276 if (lseek(d
->compressed_fd
, 0, SEEK_SET
) != 0)
1277 iFd
= d
->compressed_fd
;
1279 return _error
->Error("Reopen is not implemented for pipes opened with FileFd::OpenDescriptor()!");
1282 if (OpenInternDescriptor(d
->openmode
, d
->compressor
) == false)
1283 return _error
->Error("Seek on file %s because it couldn't be reopened", FileName
.c_str());
1294 res
= gzseek(d
->gz
,To
,SEEK_SET
);
1297 res
= lseek(iFd
,To
,SEEK_SET
);
1298 if (res
!= (signed)To
)
1301 return _error
->Error("Unable to seek to %llu", To
);
1308 // FileFd::Skip - Seek in the file /*{{{*/
1309 // ---------------------------------------------------------------------
1311 bool FileFd::Skip(unsigned long long Over
)
1313 if (d
->pipe
== true)
1319 unsigned long long toread
= std::min((unsigned long long) sizeof(buffer
), Over
);
1320 if (Read(buffer
, toread
) == false)
1321 return _error
->Error("Unable to seek ahead %llu",Over
);
1330 res
= gzseek(d
->gz
,Over
,SEEK_CUR
);
1333 res
= lseek(iFd
,Over
,SEEK_CUR
);
1337 return _error
->Error("Unable to seek ahead %llu",Over
);
1344 // FileFd::Truncate - Truncate the file /*{{{*/
1345 // ---------------------------------------------------------------------
1347 bool FileFd::Truncate(unsigned long long To
)
1352 return _error
->Error("Truncating gzipped files is not implemented (%s)", FileName
.c_str());
1354 if (ftruncate(iFd
,To
) != 0)
1357 return _error
->Error("Unable to truncate to %llu",To
);
1363 // FileFd::Tell - Current seek position /*{{{*/
1364 // ---------------------------------------------------------------------
1366 unsigned long long FileFd::Tell()
1368 // In theory, we could just return seekpos here always instead of
1369 // seeking around, but not all users of FileFd use always Seek() and co
1370 // so d->seekpos isn't always true and we can just use it as a hint if
1371 // we have nothing else, but not always as an authority…
1372 if (d
->pipe
== true)
1378 Res
= gztell(d
->gz
);
1381 Res
= lseek(iFd
,0,SEEK_CUR
);
1382 if (Res
== (off_t
)-1)
1383 _error
->Errno("lseek","Failed to determine the current file position");
1388 // FileFd::FileSize - Return the size of the file /*{{{*/
1389 // ---------------------------------------------------------------------
1391 unsigned long long FileFd::FileSize()
1394 if (d
->pipe
== false && fstat(iFd
,&Buf
) != 0)
1395 return _error
->Errno("fstat","Unable to determine the file size");
1397 // for compressor pipes st_size is undefined and at 'best' zero
1398 if (d
->pipe
== true || S_ISFIFO(Buf
.st_mode
))
1400 // we set it here, too, as we get the info here for free
1401 // in theory the Open-methods should take care of it already
1403 if (stat(FileName
.c_str(), &Buf
) != 0)
1404 return _error
->Errno("stat","Unable to determine the file size");
1410 // FileFd::Size - Return the size of the content in the file /*{{{*/
1411 // ---------------------------------------------------------------------
1413 unsigned long long FileFd::Size()
1415 unsigned long long size
= FileSize();
1417 // for compressor pipes st_size is undefined and at 'best' zero,
1418 // so we 'read' the content and 'seek' back - see there
1419 if (d
->pipe
== true)
1421 unsigned long long const oldSeek
= Tell();
1423 unsigned long long read
= 0;
1425 Read(ignore
, sizeof(ignore
), &read
);
1431 // only check gzsize if we are actually a gzip file, just checking for
1432 // "gz" is not sufficient as uncompressed files could be opened with
1433 // gzopen in "direct" mode as well
1434 else if (d
->gz
&& !gzdirect(d
->gz
) && size
> 0)
1436 off_t
const oldPos
= lseek(iFd
,0,SEEK_CUR
);
1437 /* unfortunately zlib.h doesn't provide a gzsize(), so we have to do
1438 * this ourselves; the original (uncompressed) file size is the last 32
1439 * bits of the file */
1440 // FIXME: Size for gz-files is limited by 32bit… no largefile support
1441 if (lseek(iFd
, -4, SEEK_END
) < 0)
1442 return _error
->Errno("lseek","Unable to seek to end of gzipped file");
1444 if (read(iFd
, &size
, 4) != 4)
1445 return _error
->Errno("read","Unable to read original size of gzipped file");
1447 #ifdef WORDS_BIGENDIAN
1448 uint32_t tmp_size
= size
;
1449 uint8_t const * const p
= (uint8_t const * const) &tmp_size
;
1450 tmp_size
= (p
[3] << 24) | (p
[2] << 16) | (p
[1] << 8) | p
[0];
1454 if (lseek(iFd
, oldPos
, SEEK_SET
) < 0)
1455 return _error
->Errno("lseek","Unable to seek in gzipped file");
1464 // FileFd::ModificationTime - Return the time of last touch /*{{{*/
1465 // ---------------------------------------------------------------------
1467 time_t FileFd::ModificationTime()
1470 if (d
->pipe
== false && fstat(iFd
,&Buf
) != 0)
1472 _error
->Errno("fstat","Unable to determine the modification time of file %s", FileName
.c_str());
1476 // for compressor pipes st_size is undefined and at 'best' zero
1477 if (d
->pipe
== true || S_ISFIFO(Buf
.st_mode
))
1479 // we set it here, too, as we get the info here for free
1480 // in theory the Open-methods should take care of it already
1482 if (stat(FileName
.c_str(), &Buf
) != 0)
1484 _error
->Errno("fstat","Unable to determine the modification time of file %s", FileName
.c_str());
1489 return Buf
.st_mtime
;
1492 // FileFd::Close - Close the file if the close flag is set /*{{{*/
1493 // ---------------------------------------------------------------------
1495 bool FileFd::Close()
1501 if ((Flags
& AutoClose
) == AutoClose
)
1504 if (d
!= NULL
&& d
->gz
!= NULL
) {
1505 int const e
= gzclose(d
->gz
);
1506 // gzdclose() on empty files always fails with "buffer error" here, ignore that
1507 if (e
!= 0 && e
!= Z_BUF_ERROR
)
1508 Res
&= _error
->Errno("close",_("Problem closing the gzip file %s"), FileName
.c_str());
1511 if (iFd
> 0 && close(iFd
) != 0)
1512 Res
&= _error
->Errno("close",_("Problem closing the file %s"), FileName
.c_str());
1515 if ((Flags
& Replace
) == Replace
&& iFd
>= 0) {
1516 if (rename(TemporaryFileName
.c_str(), FileName
.c_str()) != 0)
1517 Res
&= _error
->Errno("rename",_("Problem renaming the file %s to %s"), TemporaryFileName
.c_str(), FileName
.c_str());
1519 FileName
= TemporaryFileName
; // for the unlink() below.
1520 TemporaryFileName
.clear();
1525 if ((Flags
& Fail
) == Fail
&& (Flags
& DelOnFail
) == DelOnFail
&&
1526 FileName
.empty() == false)
1527 if (unlink(FileName
.c_str()) != 0)
1528 Res
&= _error
->WarningE("unlnk",_("Problem unlinking the file %s"), FileName
.c_str());
1532 if (d
->compressor_pid
> 0)
1533 ExecWait(d
->compressor_pid
, "FileFdCompressor", true);
1541 // FileFd::Sync - Sync the file /*{{{*/
1542 // ---------------------------------------------------------------------
1546 #ifdef _POSIX_SYNCHRONIZED_IO
1547 if (fsync(iFd
) != 0)
1548 return _error
->Errno("sync",_("Problem syncing the file"));
1554 gzFile
FileFd::gzFd() { return (gzFile
) d
->gz
; }