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
55 #ifdef WORDS_BIGENDIAN
73 APT::Configuration::Compressor compressor
;
74 FileFd::OpenMode openmode
;
75 FileFdPrivate() : gz(NULL
), compressor_pid(-1), pipe(false) {};
78 // RunScripts - Run a set of scripts from a configuration subtree /*{{{*/
79 // ---------------------------------------------------------------------
81 bool RunScripts(const char *Cnf
)
83 Configuration::Item
const *Opts
= _config
->Tree(Cnf
);
84 if (Opts
== 0 || Opts
->Child
== 0)
88 // Fork for running the system calls
89 pid_t Child
= ExecFork();
94 if (_config
->FindDir("DPkg::Chroot-Directory","/") != "/")
96 std::cerr
<< "Chrooting into "
97 << _config
->FindDir("DPkg::Chroot-Directory")
99 if (chroot(_config
->FindDir("DPkg::Chroot-Directory","/").c_str()) != 0)
103 if (chdir("/tmp/") != 0)
106 unsigned int Count
= 1;
107 for (; Opts
!= 0; Opts
= Opts
->Next
, Count
++)
109 if (Opts
->Value
.empty() == true)
112 if (system(Opts
->Value
.c_str()) != 0)
118 // Wait for the child
120 while (waitpid(Child
,&Status
,0) != Child
)
124 return _error
->Errno("waitpid","Couldn't wait for subprocess");
127 // Restore sig int/quit
128 signal(SIGQUIT
,SIG_DFL
);
129 signal(SIGINT
,SIG_DFL
);
131 // Check for an error code.
132 if (WIFEXITED(Status
) == 0 || WEXITSTATUS(Status
) != 0)
134 unsigned int Count
= WEXITSTATUS(Status
);
138 for (; Opts
!= 0 && Count
!= 1; Opts
= Opts
->Next
, Count
--);
139 _error
->Error("Problem executing scripts %s '%s'",Cnf
,Opts
->Value
.c_str());
142 return _error
->Error("Sub-process returned an error code");
149 // CopyFile - Buffered copy of a file /*{{{*/
150 // ---------------------------------------------------------------------
151 /* The caller is expected to set things so that failure causes erasure */
152 bool CopyFile(FileFd
&From
,FileFd
&To
)
154 if (From
.IsOpen() == false || To
.IsOpen() == false)
157 // Buffered copy between fds
158 SPtrArray
<unsigned char> Buf
= new unsigned char[64000];
159 unsigned long long Size
= From
.Size();
162 unsigned long long ToRead
= Size
;
166 if (From
.Read(Buf
,ToRead
) == false ||
167 To
.Write(Buf
,ToRead
) == false)
176 // GetLock - Gets a lock file /*{{{*/
177 // ---------------------------------------------------------------------
178 /* This will create an empty file of the given name and lock it. Once this
179 is done all other calls to GetLock in any other process will fail with
180 -1. The return result is the fd of the file, the call should call
181 close at some time. */
182 int GetLock(string File
,bool Errors
)
184 // GetLock() is used in aptitude on directories with public-write access
185 // Use O_NOFOLLOW here to prevent symlink traversal attacks
186 int FD
= open(File
.c_str(),O_RDWR
| O_CREAT
| O_NOFOLLOW
,0640);
189 // Read only .. cant have locking problems there.
192 _error
->Warning(_("Not using locking for read only lock file %s"),File
.c_str());
193 return dup(0); // Need something for the caller to close
197 _error
->Errno("open",_("Could not open lock file %s"),File
.c_str());
199 // Feh.. We do this to distinguish the lock vs open case..
203 SetCloseExec(FD
,true);
205 // Aquire a write lock
208 fl
.l_whence
= SEEK_SET
;
211 if (fcntl(FD
,F_SETLK
,&fl
) == -1)
215 _error
->Warning(_("Not using locking for nfs mounted lock file %s"),File
.c_str());
216 return dup(0); // Need something for the caller to close
219 _error
->Errno("open",_("Could not get lock %s"),File
.c_str());
230 // FileExists - Check if a file exists /*{{{*/
231 // ---------------------------------------------------------------------
232 /* Beware: Directories are also files! */
233 bool FileExists(string File
)
236 if (stat(File
.c_str(),&Buf
) != 0)
241 // RealFileExists - Check if a file exists and if it is really a file /*{{{*/
242 // ---------------------------------------------------------------------
244 bool RealFileExists(string File
)
247 if (stat(File
.c_str(),&Buf
) != 0)
249 return ((Buf
.st_mode
& S_IFREG
) != 0);
252 // DirectoryExists - Check if a directory exists and is really one /*{{{*/
253 // ---------------------------------------------------------------------
255 bool DirectoryExists(string
const &Path
)
258 if (stat(Path
.c_str(),&Buf
) != 0)
260 return ((Buf
.st_mode
& S_IFDIR
) != 0);
263 // CreateDirectory - poor man's mkdir -p guarded by a parent directory /*{{{*/
264 // ---------------------------------------------------------------------
265 /* This method will create all directories needed for path in good old
266 mkdir -p style but refuses to do this if Parent is not a prefix of
267 this Path. Example: /var/cache/ and /var/cache/apt/archives are given,
268 so it will create apt/archives if /var/cache exists - on the other
269 hand if the parent is /var/lib the creation will fail as this path
270 is not a parent of the path to be generated. */
271 bool CreateDirectory(string
const &Parent
, string
const &Path
)
273 if (Parent
.empty() == true || Path
.empty() == true)
276 if (DirectoryExists(Path
) == true)
279 if (DirectoryExists(Parent
) == false)
282 // we are not going to create directories "into the blue"
283 if (Path
.find(Parent
, 0) != 0)
286 vector
<string
> const dirs
= VectorizeString(Path
.substr(Parent
.size()), '/');
287 string progress
= Parent
;
288 for (vector
<string
>::const_iterator d
= dirs
.begin(); d
!= dirs
.end(); ++d
)
290 if (d
->empty() == true)
293 progress
.append("/").append(*d
);
294 if (DirectoryExists(progress
) == true)
297 if (mkdir(progress
.c_str(), 0755) != 0)
303 // CreateAPTDirectoryIfNeeded - ensure that the given directory exists /*{{{*/
304 // ---------------------------------------------------------------------
305 /* a small wrapper around CreateDirectory to check if it exists and to
306 remove the trailing "/apt/" from the parent directory if needed */
307 bool CreateAPTDirectoryIfNeeded(string
const &Parent
, string
const &Path
)
309 if (DirectoryExists(Path
) == true)
312 size_t const len
= Parent
.size();
313 if (len
> 5 && Parent
.find("/apt/", len
- 6, 5) == len
- 5)
315 if (CreateDirectory(Parent
.substr(0,len
-5), Path
) == true)
318 else if (CreateDirectory(Parent
, Path
) == true)
324 // GetListOfFilesInDir - returns a vector of files in the given dir /*{{{*/
325 // ---------------------------------------------------------------------
326 /* If an extension is given only files with this extension are included
327 in the returned vector, otherwise every "normal" file is included. */
328 std::vector
<string
> GetListOfFilesInDir(string
const &Dir
, string
const &Ext
,
329 bool const &SortList
, bool const &AllowNoExt
)
331 std::vector
<string
> ext
;
333 if (Ext
.empty() == false)
335 if (AllowNoExt
== true && ext
.empty() == false)
337 return GetListOfFilesInDir(Dir
, ext
, SortList
);
339 std::vector
<string
> GetListOfFilesInDir(string
const &Dir
, std::vector
<string
> const &Ext
,
340 bool const &SortList
)
342 // Attention debuggers: need to be set with the environment config file!
343 bool const Debug
= _config
->FindB("Debug::GetListOfFilesInDir", false);
346 std::clog
<< "Accept in " << Dir
<< " only files with the following " << Ext
.size() << " extensions:" << std::endl
;
347 if (Ext
.empty() == true)
348 std::clog
<< "\tNO extension" << std::endl
;
350 for (std::vector
<string
>::const_iterator e
= Ext
.begin();
352 std::clog
<< '\t' << (e
->empty() == true ? "NO" : *e
) << " extension" << std::endl
;
355 std::vector
<string
> List
;
357 if (DirectoryExists(Dir
.c_str()) == false)
359 _error
->Error(_("List of files can't be created as '%s' is not a directory"), Dir
.c_str());
363 Configuration::MatchAgainstConfig
SilentIgnore("Dir::Ignore-Files-Silently");
364 DIR *D
= opendir(Dir
.c_str());
367 _error
->Errno("opendir",_("Unable to read %s"),Dir
.c_str());
371 for (struct dirent
*Ent
= readdir(D
); Ent
!= 0; Ent
= readdir(D
))
373 // skip "hidden" files
374 if (Ent
->d_name
[0] == '.')
377 // Make sure it is a file and not something else
378 string
const File
= flCombine(Dir
,Ent
->d_name
);
379 #ifdef _DIRENT_HAVE_D_TYPE
380 if (Ent
->d_type
!= DT_REG
)
383 if (RealFileExists(File
.c_str()) == false)
385 if (SilentIgnore
.Match(Ent
->d_name
) == false)
386 _error
->Notice(_("Ignoring '%s' in directory '%s' as it is not a regular file"), Ent
->d_name
, Dir
.c_str());
391 // check for accepted extension:
392 // no extension given -> periods are bad as hell!
393 // extensions given -> "" extension allows no extension
394 if (Ext
.empty() == false)
396 string d_ext
= flExtension(Ent
->d_name
);
397 if (d_ext
== Ent
->d_name
) // no extension
399 if (std::find(Ext
.begin(), Ext
.end(), "") == Ext
.end())
402 std::clog
<< "Bad file: " << Ent
->d_name
<< " → no extension" << std::endl
;
403 if (SilentIgnore
.Match(Ent
->d_name
) == false)
404 _error
->Notice(_("Ignoring file '%s' in directory '%s' as it has no filename extension"), Ent
->d_name
, Dir
.c_str());
408 else if (std::find(Ext
.begin(), Ext
.end(), d_ext
) == Ext
.end())
411 std::clog
<< "Bad file: " << Ent
->d_name
<< " → bad extension »" << flExtension(Ent
->d_name
) << "«" << std::endl
;
412 if (SilentIgnore
.Match(Ent
->d_name
) == false)
413 _error
->Notice(_("Ignoring file '%s' in directory '%s' as it has an invalid filename extension"), Ent
->d_name
, Dir
.c_str());
418 // Skip bad filenames ala run-parts
419 const char *C
= Ent
->d_name
;
421 if (isalpha(*C
) == 0 && isdigit(*C
) == 0
422 && *C
!= '_' && *C
!= '-') {
423 // no required extension -> dot is a bad character
424 if (*C
== '.' && Ext
.empty() == false)
429 // we don't reach the end of the name -> bad character included
433 std::clog
<< "Bad file: " << Ent
->d_name
<< " → bad character »"
434 << *C
<< "« in filename (period allowed: " << (Ext
.empty() ? "no" : "yes") << ")" << std::endl
;
438 // skip filenames which end with a period. These are never valid
442 std::clog
<< "Bad file: " << Ent
->d_name
<< " → Period as last character" << std::endl
;
447 std::clog
<< "Accept file: " << Ent
->d_name
<< " in " << Dir
<< std::endl
;
448 List
.push_back(File
);
452 if (SortList
== true)
453 std::sort(List
.begin(),List
.end());
457 // SafeGetCWD - This is a safer getcwd that returns a dynamic string /*{{{*/
458 // ---------------------------------------------------------------------
459 /* We return / on failure. */
462 // Stash the current dir.
465 if (getcwd(S
,sizeof(S
)-2) == 0)
467 unsigned int Len
= strlen(S
);
473 // GetModificationTime - Get the mtime of the given file or -1 on error /*{{{*/
474 // ---------------------------------------------------------------------
475 /* We return / on failure. */
476 time_t GetModificationTime(string
const &Path
)
479 if (stat(Path
.c_str(), &St
) < 0)
484 // flNotDir - Strip the directory from the filename /*{{{*/
485 // ---------------------------------------------------------------------
487 string
flNotDir(string File
)
489 string::size_type Res
= File
.rfind('/');
490 if (Res
== string::npos
)
493 return string(File
,Res
,Res
- File
.length());
496 // flNotFile - Strip the file from the directory name /*{{{*/
497 // ---------------------------------------------------------------------
498 /* Result ends in a / */
499 string
flNotFile(string File
)
501 string::size_type Res
= File
.rfind('/');
502 if (Res
== string::npos
)
505 return string(File
,0,Res
);
508 // flExtension - Return the extension for the file /*{{{*/
509 // ---------------------------------------------------------------------
511 string
flExtension(string File
)
513 string::size_type Res
= File
.rfind('.');
514 if (Res
== string::npos
)
517 return string(File
,Res
,Res
- File
.length());
520 // flNoLink - If file is a symlink then deref it /*{{{*/
521 // ---------------------------------------------------------------------
522 /* If the name is not a link then the returned path is the input. */
523 string
flNoLink(string File
)
526 if (lstat(File
.c_str(),&St
) != 0 || S_ISLNK(St
.st_mode
) == 0)
528 if (stat(File
.c_str(),&St
) != 0)
531 /* Loop resolving the link. There is no need to limit the number of
532 loops because the stat call above ensures that the symlink is not
540 if ((Res
= readlink(NFile
.c_str(),Buffer
,sizeof(Buffer
))) <= 0 ||
541 (unsigned)Res
>= sizeof(Buffer
))
544 // Append or replace the previous path
546 if (Buffer
[0] == '/')
549 NFile
= flNotFile(NFile
) + Buffer
;
551 // See if we are done
552 if (lstat(NFile
.c_str(),&St
) != 0)
554 if (S_ISLNK(St
.st_mode
) == 0)
559 // flCombine - Combine a file and a directory /*{{{*/
560 // ---------------------------------------------------------------------
561 /* If the file is an absolute path then it is just returned, otherwise
562 the directory is pre-pended to it. */
563 string
flCombine(string Dir
,string File
)
565 if (File
.empty() == true)
568 if (File
[0] == '/' || Dir
.empty() == true)
570 if (File
.length() >= 2 && File
[0] == '.' && File
[1] == '/')
572 if (Dir
[Dir
.length()-1] == '/')
574 return Dir
+ '/' + File
;
577 // SetCloseExec - Set the close on exec flag /*{{{*/
578 // ---------------------------------------------------------------------
580 void SetCloseExec(int Fd
,bool Close
)
582 if (fcntl(Fd
,F_SETFD
,(Close
== false)?0:FD_CLOEXEC
) != 0)
584 cerr
<< "FATAL -> Could not set close on exec " << strerror(errno
) << endl
;
589 // SetNonBlock - Set the nonblocking flag /*{{{*/
590 // ---------------------------------------------------------------------
592 void SetNonBlock(int Fd
,bool Block
)
594 int Flags
= fcntl(Fd
,F_GETFL
) & (~O_NONBLOCK
);
595 if (fcntl(Fd
,F_SETFL
,Flags
| ((Block
== false)?0:O_NONBLOCK
)) != 0)
597 cerr
<< "FATAL -> Could not set non-blocking flag " << strerror(errno
) << endl
;
602 // WaitFd - Wait for a FD to become readable /*{{{*/
603 // ---------------------------------------------------------------------
604 /* This waits for a FD to become readable using select. It is useful for
605 applications making use of non-blocking sockets. The timeout is
607 bool WaitFd(int Fd
,bool write
,unsigned long timeout
)
620 Res
= select(Fd
+1,0,&Set
,0,(timeout
!= 0?&tv
:0));
622 while (Res
< 0 && errno
== EINTR
);
632 Res
= select(Fd
+1,&Set
,0,0,(timeout
!= 0?&tv
:0));
634 while (Res
< 0 && errno
== EINTR
);
643 // ExecFork - Magical fork that sanitizes the context before execing /*{{{*/
644 // ---------------------------------------------------------------------
645 /* This is used if you want to cleanse the environment for the forked
646 child, it fixes up the important signals and nukes all of the fds,
647 otherwise acts like normal fork. */
650 // Fork off the process
651 pid_t Process
= fork();
654 cerr
<< "FATAL -> Failed to fork." << endl
;
658 // Spawn the subprocess
662 signal(SIGPIPE
,SIG_DFL
);
663 signal(SIGQUIT
,SIG_DFL
);
664 signal(SIGINT
,SIG_DFL
);
665 signal(SIGWINCH
,SIG_DFL
);
666 signal(SIGCONT
,SIG_DFL
);
667 signal(SIGTSTP
,SIG_DFL
);
670 Configuration::Item
const *Opts
= _config
->Tree("APT::Keep-Fds");
671 if (Opts
!= 0 && Opts
->Child
!= 0)
674 for (; Opts
!= 0; Opts
= Opts
->Next
)
676 if (Opts
->Value
.empty() == true)
678 int fd
= atoi(Opts
->Value
.c_str());
683 // Close all of our FDs - just in case
684 for (int K
= 3; K
!= 40; K
++)
686 if(KeepFDs
.find(K
) == KeepFDs
.end())
687 fcntl(K
,F_SETFD
,FD_CLOEXEC
);
694 // ExecWait - Fancy waitpid /*{{{*/
695 // ---------------------------------------------------------------------
696 /* Waits for the given sub process. If Reap is set then no errors are
697 generated. Otherwise a failed subprocess will generate a proper descriptive
699 bool ExecWait(pid_t Pid
,const char *Name
,bool Reap
)
704 // Wait and collect the error code
706 while (waitpid(Pid
,&Status
,0) != Pid
)
714 return _error
->Error(_("Waited for %s but it wasn't there"),Name
);
718 // Check for an error code.
719 if (WIFEXITED(Status
) == 0 || WEXITSTATUS(Status
) != 0)
723 if (WIFSIGNALED(Status
) != 0)
725 if( WTERMSIG(Status
) == SIGSEGV
)
726 return _error
->Error(_("Sub-process %s received a segmentation fault."),Name
);
728 return _error
->Error(_("Sub-process %s received signal %u."),Name
, WTERMSIG(Status
));
731 if (WIFEXITED(Status
) != 0)
732 return _error
->Error(_("Sub-process %s returned an error code (%u)"),Name
,WEXITSTATUS(Status
));
734 return _error
->Error(_("Sub-process %s exited unexpectedly"),Name
);
741 // ExecCompressor - Open a de/compressor pipe /*{{{*/
742 // ---------------------------------------------------------------------
743 /* This opens the compressor, either in compress mode or decompress
744 mode. FileFd is always the compressor input/output file,
745 OutFd is the created pipe, Input for Compress, Output for Decompress. */
746 bool ExecCompressor(APT::Configuration::Compressor
const &Prog
,
747 pid_t
*Pid
, int const FileFd
, int &OutFd
, bool const Comp
)
753 if (Prog
.Binary
.empty() == true)
759 // Handle 'decompression' of empty files
764 if (Buf
.st_size
== 0 && S_ISFIFO(Buf
.st_mode
) == false)
771 // Create a data pipe
772 int Pipe
[2] = {-1,-1};
774 return _error
->Errno("pipe",_("Failed to create subprocess IPC"));
775 for (int J
= 0; J
!= 2; J
++)
776 SetCloseExec(Pipe
[J
],true);
784 pid_t child
= ExecFork();
791 dup2(FileFd
,STDOUT_FILENO
);
792 dup2(Pipe
[0],STDIN_FILENO
);
796 dup2(FileFd
,STDIN_FILENO
);
797 dup2(Pipe
[1],STDOUT_FILENO
);
800 SetCloseExec(STDOUT_FILENO
,false);
801 SetCloseExec(STDIN_FILENO
,false);
803 std::vector
<char const*> Args
;
804 Args
.push_back(Prog
.Binary
.c_str());
805 std::vector
<std::string
> const * const addArgs
=
806 (Comp
== true) ? &(Prog
.CompressArgs
) : &(Prog
.UncompressArgs
);
807 for (std::vector
<std::string
>::const_iterator a
= addArgs
->begin();
808 a
!= addArgs
->end(); ++a
)
809 Args
.push_back(a
->c_str());
810 Args
.push_back(NULL
);
812 execvp(Args
[0],(char **)&Args
[0]);
813 cerr
<< _("Failed to exec compressor ") << Args
[0] << endl
;
822 ExecWait(child
, Prog
.Binary
.c_str(), true);
826 bool ExecCompressor(APT::Configuration::Compressor
const &Prog
,
827 pid_t
*Pid
, std::string
const &FileName
, int &OutFd
, bool const Comp
)
833 if (Prog
.Binary
.empty() == true)
836 OutFd
= open(FileName
.c_str(), O_WRONLY
, 0666);
838 OutFd
= open(FileName
.c_str(), O_RDONLY
);
842 // Handle 'decompression' of empty files
846 stat(FileName
.c_str(), &Buf
);
847 if (Buf
.st_size
== 0)
849 OutFd
= open(FileName
.c_str(), O_RDONLY
);
854 // Create a data pipe
855 int Pipe
[2] = {-1,-1};
857 return _error
->Errno("pipe",_("Failed to create subprocess IPC"));
858 for (int J
= 0; J
!= 2; J
++)
859 SetCloseExec(Pipe
[J
],true);
867 pid_t child
= ExecFork();
874 dup2(Pipe
[0],STDIN_FILENO
);
875 SetCloseExec(STDIN_FILENO
,false);
879 dup2(Pipe
[1],STDOUT_FILENO
);
880 SetCloseExec(STDOUT_FILENO
,false);
883 std::vector
<char const*> Args
;
884 Args
.push_back(Prog
.Binary
.c_str());
885 std::vector
<std::string
> const * const addArgs
=
886 (Comp
== true) ? &(Prog
.CompressArgs
) : &(Prog
.UncompressArgs
);
887 for (std::vector
<std::string
>::const_iterator a
= addArgs
->begin();
888 a
!= addArgs
->end(); ++a
)
889 Args
.push_back(a
->c_str());
890 Args
.push_back("--stdout");
891 Args
.push_back(FileName
.c_str());
892 Args
.push_back(NULL
);
894 execvp(Args
[0],(char **)&Args
[0]);
895 cerr
<< _("Failed to exec compressor ") << Args
[0] << endl
;
904 ExecWait(child
, Prog
.Binary
.c_str(), false);
910 // FileFd::Open - Open a file /*{{{*/
911 // ---------------------------------------------------------------------
912 /* The most commonly used open mode combinations are given with Mode */
913 bool FileFd::Open(string FileName
,OpenMode Mode
,CompressMode Compress
, unsigned long const Perms
)
915 if (Mode
== ReadOnlyGzip
)
916 return Open(FileName
, ReadOnly
, Gzip
, Perms
);
918 d
= new FileFdPrivate
;
922 if (Compress
== Auto
&& (Mode
& WriteOnly
) == WriteOnly
)
923 return _error
->Error("Autodetection on %s only works in ReadOnly openmode!", FileName
.c_str());
924 if ((Mode
& WriteOnly
) != WriteOnly
&& (Mode
& (Atomic
| Create
| Empty
| Exclusive
)) != 0)
925 return _error
->Error("ReadOnly mode for %s doesn't accept additional flags!", FileName
.c_str());
928 #define if_FLAGGED_SET(FLAG, MODE) if ((Mode & FLAG) == FLAG) fileflags |= MODE
929 if_FLAGGED_SET(ReadWrite
, O_RDWR
);
930 else if_FLAGGED_SET(ReadOnly
, O_RDONLY
);
931 else if_FLAGGED_SET(WriteOnly
, O_WRONLY
);
932 else return _error
->Error("No openmode provided in FileFd::Open for %s", FileName
.c_str());
934 if_FLAGGED_SET(Create
, O_CREAT
);
935 if_FLAGGED_SET(Exclusive
, O_EXCL
);
936 else if_FLAGGED_SET(Atomic
, O_EXCL
);
937 if_FLAGGED_SET(Empty
, O_TRUNC
);
938 #undef if_FLAGGED_SET
940 // FIXME: Denote inbuilt compressors somehow - as we don't need to have the binaries for them
941 std::vector
<APT::Configuration::Compressor
> const compressors
= APT::Configuration::getCompressors();
942 std::vector
<APT::Configuration::Compressor
>::const_iterator compressor
= compressors
.begin();
943 if (Compress
== Auto
)
946 for (; compressor
!= compressors
.end(); ++compressor
)
948 std::string file
= std::string(FileName
).append(compressor
->Extension
);
949 if (FileExists(file
) == false)
952 if (compressor
->Binary
== ".")
955 Compress
= Extension
;
959 else if (Compress
== Extension
)
962 std::string ext
= flExtension(FileName
);
966 for (; compressor
!= compressors
.end(); ++compressor
)
967 if (ext
== compressor
->Extension
)
971 else if (Compress
!= None
)
976 case Gzip
: name
= "gzip"; break;
977 case Bzip2
: name
= "bzip2"; break;
978 case Lzma
: name
= "lzma"; break;
979 case Xz
: name
= "xz"; break;
980 default: return _error
->Error("Can't find a match for specified compressor mode for file %s", FileName
.c_str());
982 for (; compressor
!= compressors
.end(); ++compressor
)
983 if (compressor
->Name
== name
)
985 if (compressor
== compressors
.end() && name
!= "gzip")
986 return _error
->Error("Can't find a configured compressor %s for file %s", name
.c_str(), FileName
.c_str());
989 // if we have them, use inbuilt compressors instead of forking
990 if (compressor
!= compressors
.end())
993 if (compressor
->Name
== "gzip")
996 compressor
= compressors
.end();
1000 if (compressor
->Name
== ".")
1003 compressor
= compressors
.end();
1007 if ((Mode
& Atomic
) == Atomic
)
1010 char *name
= strdup((FileName
+ ".XXXXXX").c_str());
1011 TemporaryFileName
= string(mktemp(name
));
1014 else if ((Mode
& (Exclusive
| Create
)) == (Exclusive
| Create
))
1016 // for atomic, this will be done by rename in Close()
1017 unlink(FileName
.c_str());
1019 if ((Mode
& Empty
) == Empty
)
1022 if (lstat(FileName
.c_str(),&Buf
) == 0 && S_ISLNK(Buf
.st_mode
))
1023 unlink(FileName
.c_str());
1026 if (compressor
!= compressors
.end())
1028 if ((Mode
& ReadWrite
) == ReadWrite
)
1029 return _error
->Error("External compressors like %s do not support readwrite mode for file %s", compressor
->Name
.c_str(), FileName
.c_str());
1031 if (ExecCompressor(*compressor
, NULL
/*d->compressor_pid*/, FileName
, iFd
, ((Mode
& ReadOnly
) != ReadOnly
)) == false)
1032 return _error
->Error("Forking external compressor %s is not implemented for %s", compressor
->Name
.c_str(), FileName
.c_str());
1034 d
->compressor
= *compressor
;
1038 if (TemporaryFileName
.empty() == false)
1039 iFd
= open(TemporaryFileName
.c_str(), fileflags
, Perms
);
1041 iFd
= open(FileName
.c_str(), fileflags
, Perms
);
1045 if (OpenInternDescriptor(Mode
, Compress
) == false)
1054 return _error
->Errno("open",_("Could not open file %s"),FileName
.c_str());
1056 this->FileName
= FileName
;
1057 SetCloseExec(iFd
,true);
1061 // FileFd::OpenDescriptor - Open a filedescriptor /*{{{*/
1062 // ---------------------------------------------------------------------
1064 bool FileFd::OpenDescriptor(int Fd
, OpenMode Mode
, CompressMode Compress
, bool AutoClose
)
1067 d
= new FileFdPrivate
;
1069 Flags
= (AutoClose
) ? FileFd::AutoClose
: 0;
1071 if (OpenInternDescriptor(Mode
, Compress
) == false)
1075 return _error
->Errno("gzdopen",_("Could not open file descriptor %d"), Fd
);
1077 this->FileName
= "";
1080 bool FileFd::OpenInternDescriptor(OpenMode Mode
, CompressMode Compress
)
1082 if (Compress
== None
)
1085 else if (Compress
== Gzip
)
1087 if ((Mode
& ReadWrite
) == ReadWrite
)
1088 d
->gz
= gzdopen(iFd
, "r+");
1089 else if ((Mode
& WriteOnly
) == WriteOnly
)
1090 d
->gz
= gzdopen(iFd
, "w");
1092 d
->gz
= gzdopen (iFd
, "r");
1095 Flags
|= Compressed
;
1103 case Gzip
: name
= "gzip"; break;
1104 case Bzip2
: name
= "bzip2"; break;
1105 case Lzma
: name
= "lzma"; break;
1106 case Xz
: name
= "xz"; break;
1107 default: return _error
->Error("Can't find a match for specified compressor mode for file %s", FileName
.c_str());
1109 std::vector
<APT::Configuration::Compressor
> const compressors
= APT::Configuration::getCompressors();
1110 std::vector
<APT::Configuration::Compressor
>::const_iterator compressor
= compressors
.begin();
1111 for (; compressor
!= compressors
.end(); ++compressor
)
1112 if (compressor
->Name
== name
)
1114 if (compressor
== compressors
.end() ||
1115 ExecCompressor(*compressor
, NULL
/*&(d->compressor_pid)*/,
1116 FileName
, iFd
, ((Mode
& ReadOnly
) != ReadOnly
)) == false)
1117 return _error
->Error("Forking external compressor %s is not implemented for %s", name
.c_str(), FileName
.c_str());
1123 // FileFd::~File - Closes the file /*{{{*/
1124 // ---------------------------------------------------------------------
1125 /* If the proper modes are selected then we close the Fd and possibly
1126 unlink the file on error. */
1132 // FileFd::Read - Read a bit of the file /*{{{*/
1133 // ---------------------------------------------------------------------
1134 /* We are carefull to handle interruption by a signal while reading
1136 bool FileFd::Read(void *To
,unsigned long long Size
,unsigned long long *Actual
)
1142 *((char *)To
) = '\0';
1147 Res
= gzread(d
->gz
,To
,Size
);
1150 Res
= read(iFd
,To
,Size
);
1151 if (Res
< 0 && errno
== EINTR
)
1156 return _error
->Errno("read",_("Read error"));
1159 To
= (char *)To
+ Res
;
1164 while (Res
> 0 && Size
> 0);
1177 return _error
->Error(_("read, still have %llu to read but none left"), Size
);
1180 // FileFd::ReadLine - Read a complete line from the file /*{{{*/
1181 // ---------------------------------------------------------------------
1182 /* Beware: This method can be quiet slow for big buffers on UNcompressed
1183 files because of the naive implementation! */
1184 char* FileFd::ReadLine(char *To
, unsigned long long const Size
)
1189 return gzgets(d
->gz
, To
, Size
);
1192 unsigned long long read
= 0;
1193 if (Read(To
, Size
, &read
) == false)
1196 for (; *c
!= '\n' && *c
!= '\0' && read
!= 0; --read
, ++c
)
1197 ; // find the end of the line
1201 Seek(Tell() - read
);
1205 // FileFd::Write - Write to the file /*{{{*/
1206 // ---------------------------------------------------------------------
1208 bool FileFd::Write(const void *From
,unsigned long long Size
)
1216 Res
= gzwrite(d
->gz
,From
,Size
);
1219 Res
= write(iFd
,From
,Size
);
1220 if (Res
< 0 && errno
== EINTR
)
1225 return _error
->Errno("write",_("Write error"));
1228 From
= (char *)From
+ Res
;
1231 while (Res
> 0 && Size
> 0);
1237 return _error
->Error(_("write, still have %llu to write but couldn't"), Size
);
1240 // FileFd::Seek - Seek in the file /*{{{*/
1241 // ---------------------------------------------------------------------
1243 bool FileFd::Seek(unsigned long long To
)
1245 if (d
->pipe
== true)
1247 // FIXME: What about OpenDescriptor() stuff here?
1249 bool result
= ExecCompressor(d
->compressor
, NULL
, FileName
, iFd
, (d
->openmode
& ReadOnly
) != ReadOnly
);
1250 if (result
== true && To
!= 0)
1257 res
= gzseek(d
->gz
,To
,SEEK_SET
);
1260 res
= lseek(iFd
,To
,SEEK_SET
);
1261 if (res
!= (signed)To
)
1264 return _error
->Error("Unable to seek to %llu", To
);
1270 // FileFd::Skip - Seek in the file /*{{{*/
1271 // ---------------------------------------------------------------------
1273 bool FileFd::Skip(unsigned long long Over
)
1278 res
= gzseek(d
->gz
,Over
,SEEK_CUR
);
1281 res
= lseek(iFd
,Over
,SEEK_CUR
);
1285 return _error
->Error("Unable to seek ahead %llu",Over
);
1291 // FileFd::Truncate - Truncate the file /*{{{*/
1292 // ---------------------------------------------------------------------
1294 bool FileFd::Truncate(unsigned long long To
)
1299 return _error
->Error("Truncating gzipped files is not implemented (%s)", FileName
.c_str());
1301 if (ftruncate(iFd
,To
) != 0)
1304 return _error
->Error("Unable to truncate to %llu",To
);
1310 // FileFd::Tell - Current seek position /*{{{*/
1311 // ---------------------------------------------------------------------
1313 unsigned long long FileFd::Tell()
1318 Res
= gztell(d
->gz
);
1321 Res
= lseek(iFd
,0,SEEK_CUR
);
1322 if (Res
== (off_t
)-1)
1323 _error
->Errno("lseek","Failed to determine the current file position");
1327 // FileFd::FileSize - Return the size of the file /*{{{*/
1328 // ---------------------------------------------------------------------
1330 unsigned long long FileFd::FileSize()
1333 if (d
->pipe
== false && fstat(iFd
,&Buf
) != 0)
1334 return _error
->Errno("fstat","Unable to determine the file size");
1336 // for compressor pipes st_size is undefined and at 'best' zero
1337 if (d
->pipe
== true || S_ISFIFO(Buf
.st_mode
))
1339 // we set it here, too, as we get the info here for free
1340 // in theory the Open-methods should take care of it already
1342 if (stat(FileName
.c_str(), &Buf
) != 0)
1343 return _error
->Errno("stat","Unable to determine the file size");
1349 // FileFd::Size - Return the size of the content in the file /*{{{*/
1350 // ---------------------------------------------------------------------
1352 unsigned long long FileFd::Size()
1354 unsigned long long size
= FileSize();
1356 // for compressor pipes st_size is undefined and at 'best' zero,
1357 // so we 'read' the content and 'seek' back - see there
1358 if (d
->pipe
== true)
1360 // FIXME: If we have read first and then FileSize() the report is wrong
1363 unsigned long long read
= 0;
1365 Read(ignore
, sizeof(ignore
), &read
);
1371 // only check gzsize if we are actually a gzip file, just checking for
1372 // "gz" is not sufficient as uncompressed files could be opened with
1373 // gzopen in "direct" mode as well
1374 else if (d
->gz
&& !gzdirect(d
->gz
) && size
> 0)
1376 /* unfortunately zlib.h doesn't provide a gzsize(), so we have to do
1377 * this ourselves; the original (uncompressed) file size is the last 32
1378 * bits of the file */
1379 // FIXME: Size for gz-files is limited by 32bit… no largefile support
1380 off_t orig_pos
= lseek(iFd
, 0, SEEK_CUR
);
1381 if (lseek(iFd
, -4, SEEK_END
) < 0)
1382 return _error
->Errno("lseek","Unable to seek to end of gzipped file");
1384 if (read(iFd
, &size
, 4) != 4)
1385 return _error
->Errno("read","Unable to read original size of gzipped file");
1387 #ifdef WORDS_BIGENDIAN
1388 uint32_t tmp_size
= size
;
1389 uint8_t const * const p
= (uint8_t const * const) &tmp_size
;
1390 tmp_size
= (p
[3] << 24) | (p
[2] << 16) | (p
[1] << 8) | p
[0];
1394 if (lseek(iFd
, orig_pos
, SEEK_SET
) < 0)
1395 return _error
->Errno("lseek","Unable to seek in gzipped file");
1403 // FileFd::ModificationTime - Return the time of last touch /*{{{*/
1404 // ---------------------------------------------------------------------
1406 time_t FileFd::ModificationTime()
1409 if (d
->pipe
== false && fstat(iFd
,&Buf
) != 0)
1411 _error
->Errno("fstat","Unable to determine the modification time of file %s", FileName
.c_str());
1415 // for compressor pipes st_size is undefined and at 'best' zero
1416 if (d
->pipe
== true || S_ISFIFO(Buf
.st_mode
))
1418 // we set it here, too, as we get the info here for free
1419 // in theory the Open-methods should take care of it already
1421 if (stat(FileName
.c_str(), &Buf
) != 0)
1423 _error
->Errno("fstat","Unable to determine the modification time of file %s", FileName
.c_str());
1428 return Buf
.st_mtime
;
1431 // FileFd::Close - Close the file if the close flag is set /*{{{*/
1432 // ---------------------------------------------------------------------
1434 bool FileFd::Close()
1440 if ((Flags
& AutoClose
) == AutoClose
)
1443 if (d
!= NULL
&& d
->gz
!= NULL
) {
1444 int const e
= gzclose(d
->gz
);
1445 // gzdopen() on empty files always fails with "buffer error" here, ignore that
1446 if (e
!= 0 && e
!= Z_BUF_ERROR
)
1447 Res
&= _error
->Errno("close",_("Problem closing the gzip file %s"), FileName
.c_str());
1450 if (iFd
> 0 && close(iFd
) != 0)
1451 Res
&= _error
->Errno("close",_("Problem closing the file %s"), FileName
.c_str());
1454 if ((Flags
& Replace
) == Replace
&& iFd
>= 0) {
1455 if (rename(TemporaryFileName
.c_str(), FileName
.c_str()) != 0)
1456 Res
&= _error
->Errno("rename",_("Problem renaming the file %s to %s"), TemporaryFileName
.c_str(), FileName
.c_str());
1458 FileName
= TemporaryFileName
; // for the unlink() below.
1459 TemporaryFileName
.clear();
1464 if ((Flags
& Fail
) == Fail
&& (Flags
& DelOnFail
) == DelOnFail
&&
1465 FileName
.empty() == false)
1466 if (unlink(FileName
.c_str()) != 0)
1467 Res
&= _error
->WarningE("unlnk",_("Problem unlinking the file %s"), FileName
.c_str());
1471 // if (d->compressor_pid != -1)
1472 // ExecWait(d->compressor_pid, "FileFdCompressor", true);
1480 // FileFd::Sync - Sync the file /*{{{*/
1481 // ---------------------------------------------------------------------
1485 #ifdef _POSIX_SYNCHRONIZED_IO
1486 if (fsync(iFd
) != 0)
1487 return _error
->Errno("sync",_("Problem syncing the file"));
1493 gzFile
FileFd::gzFd() { return (gzFile
) d
->gz
; }