]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/fileutl.cc
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 /*{{{*/
21 #include <apt-pkg/fileutl.h>
22 #include <apt-pkg/strutl.h>
23 #include <apt-pkg/error.h>
24 #include <apt-pkg/sptr.h>
25 #include <apt-pkg/configuration.h>
37 #include <sys/types.h>
47 #ifdef WORDS_BIGENDIAN
54 // RunScripts - Run a set of scripts from a configuration subtree /*{{{*/
55 // ---------------------------------------------------------------------
57 bool RunScripts(const char *Cnf
)
59 Configuration::Item
const *Opts
= _config
->Tree(Cnf
);
60 if (Opts
== 0 || Opts
->Child
== 0)
64 // Fork for running the system calls
65 pid_t Child
= ExecFork();
70 if (_config
->FindDir("DPkg::Chroot-Directory","/") != "/")
72 std::cerr
<< "Chrooting into "
73 << _config
->FindDir("DPkg::Chroot-Directory")
75 if (chroot(_config
->FindDir("DPkg::Chroot-Directory","/").c_str()) != 0)
79 if (chdir("/tmp/") != 0)
82 unsigned int Count
= 1;
83 for (; Opts
!= 0; Opts
= Opts
->Next
, Count
++)
85 if (Opts
->Value
.empty() == true)
88 if (system(Opts
->Value
.c_str()) != 0)
96 while (waitpid(Child
,&Status
,0) != Child
)
100 return _error
->Errno("waitpid","Couldn't wait for subprocess");
103 // Restore sig int/quit
104 signal(SIGQUIT
,SIG_DFL
);
105 signal(SIGINT
,SIG_DFL
);
107 // Check for an error code.
108 if (WIFEXITED(Status
) == 0 || WEXITSTATUS(Status
) != 0)
110 unsigned int Count
= WEXITSTATUS(Status
);
114 for (; Opts
!= 0 && Count
!= 1; Opts
= Opts
->Next
, Count
--);
115 _error
->Error("Problem executing scripts %s '%s'",Cnf
,Opts
->Value
.c_str());
118 return _error
->Error("Sub-process returned an error code");
125 // CopyFile - Buffered copy of a file /*{{{*/
126 // ---------------------------------------------------------------------
127 /* The caller is expected to set things so that failure causes erasure */
128 bool CopyFile(FileFd
&From
,FileFd
&To
)
130 if (From
.IsOpen() == false || To
.IsOpen() == false)
133 // Buffered copy between fds
134 SPtrArray
<unsigned char> Buf
= new unsigned char[64000];
135 unsigned long Size
= From
.Size();
138 unsigned long ToRead
= Size
;
142 if (From
.Read(Buf
,ToRead
) == false ||
143 To
.Write(Buf
,ToRead
) == false)
152 // GetLock - Gets a lock file /*{{{*/
153 // ---------------------------------------------------------------------
154 /* This will create an empty file of the given name and lock it. Once this
155 is done all other calls to GetLock in any other process will fail with
156 -1. The return result is the fd of the file, the call should call
157 close at some time. */
158 int GetLock(string File
,bool Errors
)
160 // GetLock() is used in aptitude on directories with public-write access
161 // Use O_NOFOLLOW here to prevent symlink traversal attacks
162 int FD
= open(File
.c_str(),O_RDWR
| O_CREAT
| O_NOFOLLOW
,0640);
165 // Read only .. cant have locking problems there.
168 _error
->Warning(_("Not using locking for read only lock file %s"),File
.c_str());
169 return dup(0); // Need something for the caller to close
173 _error
->Errno("open",_("Could not open lock file %s"),File
.c_str());
175 // Feh.. We do this to distinguish the lock vs open case..
179 SetCloseExec(FD
,true);
181 // Aquire a write lock
184 fl
.l_whence
= SEEK_SET
;
187 if (fcntl(FD
,F_SETLK
,&fl
) == -1)
191 _error
->Warning(_("Not using locking for nfs mounted lock file %s"),File
.c_str());
192 return dup(0); // Need something for the caller to close
195 _error
->Errno("open",_("Could not get lock %s"),File
.c_str());
206 // FileExists - Check if a file exists /*{{{*/
207 // ---------------------------------------------------------------------
208 /* Beware: Directories are also files! */
209 bool FileExists(string File
)
212 if (stat(File
.c_str(),&Buf
) != 0)
217 // RealFileExists - Check if a file exists and if it is really a file /*{{{*/
218 // ---------------------------------------------------------------------
220 bool RealFileExists(string File
)
223 if (stat(File
.c_str(),&Buf
) != 0)
225 return ((Buf
.st_mode
& S_IFREG
) != 0);
228 // DirectoryExists - Check if a directory exists and is really one /*{{{*/
229 // ---------------------------------------------------------------------
231 bool DirectoryExists(string
const &Path
)
234 if (stat(Path
.c_str(),&Buf
) != 0)
236 return ((Buf
.st_mode
& S_IFDIR
) != 0);
239 // CreateDirectory - poor man's mkdir -p guarded by a parent directory /*{{{*/
240 // ---------------------------------------------------------------------
241 /* This method will create all directories needed for path in good old
242 mkdir -p style but refuses to do this if Parent is not a prefix of
243 this Path. Example: /var/cache/ and /var/cache/apt/archives are given,
244 so it will create apt/archives if /var/cache exists - on the other
245 hand if the parent is /var/lib the creation will fail as this path
246 is not a parent of the path to be generated. */
247 bool CreateDirectory(string
const &Parent
, string
const &Path
)
249 if (Parent
.empty() == true || Path
.empty() == true)
252 if (DirectoryExists(Path
) == true)
255 if (DirectoryExists(Parent
) == false)
258 // we are not going to create directories "into the blue"
259 if (Path
.find(Parent
, 0) != 0)
262 vector
<string
> const dirs
= VectorizeString(Path
.substr(Parent
.size()), '/');
263 string progress
= Parent
;
264 for (vector
<string
>::const_iterator d
= dirs
.begin(); d
!= dirs
.end(); ++d
)
266 if (d
->empty() == true)
269 progress
.append("/").append(*d
);
270 if (DirectoryExists(progress
) == true)
273 if (mkdir(progress
.c_str(), 0755) != 0)
279 // CreateAPTDirectoryIfNeeded - ensure that the given directory exists /*{{{*/
280 // ---------------------------------------------------------------------
281 /* a small wrapper around CreateDirectory to check if it exists and to
282 remove the trailing "/apt/" from the parent directory if needed */
283 bool CreateAPTDirectoryIfNeeded(string
const &Parent
, string
const &Path
)
285 if (DirectoryExists(Path
) == true)
288 size_t const len
= Parent
.size();
289 if (len
> 5 && Parent
.find("/apt/", len
- 6, 5) == len
- 5)
291 if (CreateDirectory(Parent
.substr(0,len
-5), Path
) == true)
294 else if (CreateDirectory(Parent
, Path
) == true)
300 // GetListOfFilesInDir - returns a vector of files in the given dir /*{{{*/
301 // ---------------------------------------------------------------------
302 /* If an extension is given only files with this extension are included
303 in the returned vector, otherwise every "normal" file is included. */
304 std::vector
<string
> GetListOfFilesInDir(string
const &Dir
, string
const &Ext
,
305 bool const &SortList
, bool const &AllowNoExt
)
307 std::vector
<string
> ext
;
309 if (Ext
.empty() == false)
311 if (AllowNoExt
== true && ext
.empty() == false)
313 return GetListOfFilesInDir(Dir
, ext
, SortList
);
315 std::vector
<string
> GetListOfFilesInDir(string
const &Dir
, std::vector
<string
> const &Ext
,
316 bool const &SortList
)
318 // Attention debuggers: need to be set with the environment config file!
319 bool const Debug
= _config
->FindB("Debug::GetListOfFilesInDir", false);
322 std::clog
<< "Accept in " << Dir
<< " only files with the following " << Ext
.size() << " extensions:" << std::endl
;
323 if (Ext
.empty() == true)
324 std::clog
<< "\tNO extension" << std::endl
;
326 for (std::vector
<string
>::const_iterator e
= Ext
.begin();
328 std::clog
<< '\t' << (e
->empty() == true ? "NO" : *e
) << " extension" << std::endl
;
331 std::vector
<string
> List
;
333 if (DirectoryExists(Dir
.c_str()) == false)
335 _error
->Error(_("List of files can't be created as '%s' is not a directory"), Dir
.c_str());
339 Configuration::MatchAgainstConfig
SilentIgnore("Dir::Ignore-Files-Silently");
340 DIR *D
= opendir(Dir
.c_str());
343 _error
->Errno("opendir",_("Unable to read %s"),Dir
.c_str());
347 for (struct dirent
*Ent
= readdir(D
); Ent
!= 0; Ent
= readdir(D
))
349 // skip "hidden" files
350 if (Ent
->d_name
[0] == '.')
353 // Make sure it is a file and not something else
354 string
const File
= flCombine(Dir
,Ent
->d_name
);
355 #ifdef _DIRENT_HAVE_D_TYPE
356 if (Ent
->d_type
!= DT_REG
)
359 if (RealFileExists(File
.c_str()) == false)
361 if (SilentIgnore
.Match(Ent
->d_name
) == false)
362 _error
->Notice(_("Ignoring '%s' in directory '%s' as it is not a regular file"), Ent
->d_name
, Dir
.c_str());
367 // check for accepted extension:
368 // no extension given -> periods are bad as hell!
369 // extensions given -> "" extension allows no extension
370 if (Ext
.empty() == false)
372 string d_ext
= flExtension(Ent
->d_name
);
373 if (d_ext
== Ent
->d_name
) // no extension
375 if (std::find(Ext
.begin(), Ext
.end(), "") == Ext
.end())
378 std::clog
<< "Bad file: " << Ent
->d_name
<< " → no extension" << std::endl
;
379 if (SilentIgnore
.Match(Ent
->d_name
) == false)
380 _error
->Notice(_("Ignoring file '%s' in directory '%s' as it has no filename extension"), Ent
->d_name
, Dir
.c_str());
384 else if (std::find(Ext
.begin(), Ext
.end(), d_ext
) == Ext
.end())
387 std::clog
<< "Bad file: " << Ent
->d_name
<< " → bad extension »" << flExtension(Ent
->d_name
) << "«" << std::endl
;
388 if (SilentIgnore
.Match(Ent
->d_name
) == false)
389 _error
->Notice(_("Ignoring file '%s' in directory '%s' as it has an invalid filename extension"), Ent
->d_name
, Dir
.c_str());
394 // Skip bad filenames ala run-parts
395 const char *C
= Ent
->d_name
;
397 if (isalpha(*C
) == 0 && isdigit(*C
) == 0
398 && *C
!= '_' && *C
!= '-') {
399 // no required extension -> dot is a bad character
400 if (*C
== '.' && Ext
.empty() == false)
405 // we don't reach the end of the name -> bad character included
409 std::clog
<< "Bad file: " << Ent
->d_name
<< " → bad character »"
410 << *C
<< "« in filename (period allowed: " << (Ext
.empty() ? "no" : "yes") << ")" << std::endl
;
414 // skip filenames which end with a period. These are never valid
418 std::clog
<< "Bad file: " << Ent
->d_name
<< " → Period as last character" << std::endl
;
423 std::clog
<< "Accept file: " << Ent
->d_name
<< " in " << Dir
<< std::endl
;
424 List
.push_back(File
);
428 if (SortList
== true)
429 std::sort(List
.begin(),List
.end());
433 // SafeGetCWD - This is a safer getcwd that returns a dynamic string /*{{{*/
434 // ---------------------------------------------------------------------
435 /* We return / on failure. */
438 // Stash the current dir.
441 if (getcwd(S
,sizeof(S
)-2) == 0)
443 unsigned int Len
= strlen(S
);
449 // GetModificationTime - Get the mtime of the given file or -1 on error /*{{{*/
450 // ---------------------------------------------------------------------
451 /* We return / on failure. */
452 time_t GetModificationTime(string
const &Path
)
455 if (stat(Path
.c_str(), &St
) < 0)
460 // flNotDir - Strip the directory from the filename /*{{{*/
461 // ---------------------------------------------------------------------
463 string
flNotDir(string File
)
465 string::size_type Res
= File
.rfind('/');
466 if (Res
== string::npos
)
469 return string(File
,Res
,Res
- File
.length());
472 // flNotFile - Strip the file from the directory name /*{{{*/
473 // ---------------------------------------------------------------------
474 /* Result ends in a / */
475 string
flNotFile(string File
)
477 string::size_type Res
= File
.rfind('/');
478 if (Res
== string::npos
)
481 return string(File
,0,Res
);
484 // flExtension - Return the extension for the file /*{{{*/
485 // ---------------------------------------------------------------------
487 string
flExtension(string File
)
489 string::size_type Res
= File
.rfind('.');
490 if (Res
== string::npos
)
493 return string(File
,Res
,Res
- File
.length());
496 // flNoLink - If file is a symlink then deref it /*{{{*/
497 // ---------------------------------------------------------------------
498 /* If the name is not a link then the returned path is the input. */
499 string
flNoLink(string File
)
502 if (lstat(File
.c_str(),&St
) != 0 || S_ISLNK(St
.st_mode
) == 0)
504 if (stat(File
.c_str(),&St
) != 0)
507 /* Loop resolving the link. There is no need to limit the number of
508 loops because the stat call above ensures that the symlink is not
516 if ((Res
= readlink(NFile
.c_str(),Buffer
,sizeof(Buffer
))) <= 0 ||
517 (unsigned)Res
>= sizeof(Buffer
))
520 // Append or replace the previous path
522 if (Buffer
[0] == '/')
525 NFile
= flNotFile(NFile
) + Buffer
;
527 // See if we are done
528 if (lstat(NFile
.c_str(),&St
) != 0)
530 if (S_ISLNK(St
.st_mode
) == 0)
535 // flCombine - Combine a file and a directory /*{{{*/
536 // ---------------------------------------------------------------------
537 /* If the file is an absolute path then it is just returned, otherwise
538 the directory is pre-pended to it. */
539 string
flCombine(string Dir
,string File
)
541 if (File
.empty() == true)
544 if (File
[0] == '/' || Dir
.empty() == true)
546 if (File
.length() >= 2 && File
[0] == '.' && File
[1] == '/')
548 if (Dir
[Dir
.length()-1] == '/')
550 return Dir
+ '/' + File
;
553 // SetCloseExec - Set the close on exec flag /*{{{*/
554 // ---------------------------------------------------------------------
556 void SetCloseExec(int Fd
,bool Close
)
558 if (fcntl(Fd
,F_SETFD
,(Close
== false)?0:FD_CLOEXEC
) != 0)
560 cerr
<< "FATAL -> Could not set close on exec " << strerror(errno
) << endl
;
565 // SetNonBlock - Set the nonblocking flag /*{{{*/
566 // ---------------------------------------------------------------------
568 void SetNonBlock(int Fd
,bool Block
)
570 int Flags
= fcntl(Fd
,F_GETFL
) & (~O_NONBLOCK
);
571 if (fcntl(Fd
,F_SETFL
,Flags
| ((Block
== false)?0:O_NONBLOCK
)) != 0)
573 cerr
<< "FATAL -> Could not set non-blocking flag " << strerror(errno
) << endl
;
578 // WaitFd - Wait for a FD to become readable /*{{{*/
579 // ---------------------------------------------------------------------
580 /* This waits for a FD to become readable using select. It is useful for
581 applications making use of non-blocking sockets. The timeout is
583 bool WaitFd(int Fd
,bool write
,unsigned long timeout
)
596 Res
= select(Fd
+1,0,&Set
,0,(timeout
!= 0?&tv
:0));
598 while (Res
< 0 && errno
== EINTR
);
608 Res
= select(Fd
+1,&Set
,0,0,(timeout
!= 0?&tv
:0));
610 while (Res
< 0 && errno
== EINTR
);
619 // ExecFork - Magical fork that sanitizes the context before execing /*{{{*/
620 // ---------------------------------------------------------------------
621 /* This is used if you want to cleanse the environment for the forked
622 child, it fixes up the important signals and nukes all of the fds,
623 otherwise acts like normal fork. */
626 // Fork off the process
627 pid_t Process
= fork();
630 cerr
<< "FATAL -> Failed to fork." << endl
;
634 // Spawn the subprocess
638 signal(SIGPIPE
,SIG_DFL
);
639 signal(SIGQUIT
,SIG_DFL
);
640 signal(SIGINT
,SIG_DFL
);
641 signal(SIGWINCH
,SIG_DFL
);
642 signal(SIGCONT
,SIG_DFL
);
643 signal(SIGTSTP
,SIG_DFL
);
646 Configuration::Item
const *Opts
= _config
->Tree("APT::Keep-Fds");
647 if (Opts
!= 0 && Opts
->Child
!= 0)
650 for (; Opts
!= 0; Opts
= Opts
->Next
)
652 if (Opts
->Value
.empty() == true)
654 int fd
= atoi(Opts
->Value
.c_str());
659 // Close all of our FDs - just in case
660 for (int K
= 3; K
!= 40; K
++)
662 if(KeepFDs
.find(K
) == KeepFDs
.end())
663 fcntl(K
,F_SETFD
,FD_CLOEXEC
);
670 // ExecWait - Fancy waitpid /*{{{*/
671 // ---------------------------------------------------------------------
672 /* Waits for the given sub process. If Reap is set then no errors are
673 generated. Otherwise a failed subprocess will generate a proper descriptive
675 bool ExecWait(pid_t Pid
,const char *Name
,bool Reap
)
680 // Wait and collect the error code
682 while (waitpid(Pid
,&Status
,0) != Pid
)
690 return _error
->Error(_("Waited for %s but it wasn't there"),Name
);
694 // Check for an error code.
695 if (WIFEXITED(Status
) == 0 || WEXITSTATUS(Status
) != 0)
699 if (WIFSIGNALED(Status
) != 0)
701 if( WTERMSIG(Status
) == SIGSEGV
)
702 return _error
->Error(_("Sub-process %s received a segmentation fault."),Name
);
704 return _error
->Error(_("Sub-process %s received signal %u."),Name
, WTERMSIG(Status
));
707 if (WIFEXITED(Status
) != 0)
708 return _error
->Error(_("Sub-process %s returned an error code (%u)"),Name
,WEXITSTATUS(Status
));
710 return _error
->Error(_("Sub-process %s exited unexpectedly"),Name
);
717 // FileFd::Open - Open a file /*{{{*/
718 // ---------------------------------------------------------------------
719 /* The most commonly used open mode combinations are given with Mode */
720 bool FileFd::Open(string FileName
,OpenMode Mode
, unsigned long Perms
)
727 iFd
= open(FileName
.c_str(),O_RDONLY
);
731 iFd
= open(FileName
.c_str(),O_RDONLY
);
733 gz
= gzdopen (iFd
, "r");
744 char *name
= strdup((FileName
+ ".XXXXXX").c_str());
745 TemporaryFileName
= string(mktemp(name
));
746 iFd
= open(TemporaryFileName
.c_str(),O_RDWR
| O_CREAT
| O_EXCL
,Perms
);
754 if (lstat(FileName
.c_str(),&Buf
) == 0 && S_ISLNK(Buf
.st_mode
))
755 unlink(FileName
.c_str());
756 iFd
= open(FileName
.c_str(),O_RDWR
| O_CREAT
| O_TRUNC
,Perms
);
761 iFd
= open(FileName
.c_str(),O_RDWR
);
765 iFd
= open(FileName
.c_str(),O_RDWR
| O_CREAT
,Perms
);
769 unlink(FileName
.c_str());
770 iFd
= open(FileName
.c_str(),O_RDWR
| O_CREAT
| O_EXCL
,Perms
);
775 return _error
->Errno("open",_("Could not open file %s"),FileName
.c_str());
777 this->FileName
= FileName
;
778 SetCloseExec(iFd
,true);
782 bool FileFd::OpenDescriptor(int Fd
, OpenMode Mode
, bool AutoClose
)
785 Flags
= (AutoClose
) ? FileFd::AutoClose
: 0;
787 if (Mode
== ReadOnlyGzip
) {
788 gz
= gzdopen (iFd
, "r");
792 return _error
->Errno("gzdopen",_("Could not open file descriptor %d"),
800 // FileFd::~File - Closes the file /*{{{*/
801 // ---------------------------------------------------------------------
802 /* If the proper modes are selected then we close the Fd and possibly
803 unlink the file on error. */
809 // FileFd::Read - Read a bit of the file /*{{{*/
810 // ---------------------------------------------------------------------
811 /* We are carefull to handle interruption by a signal while reading
813 bool FileFd::Read(void *To
,unsigned long Size
,unsigned long *Actual
)
823 Res
= gzread(gz
,To
,Size
);
825 Res
= read(iFd
,To
,Size
);
826 if (Res
< 0 && errno
== EINTR
)
831 return _error
->Errno("read",_("Read error"));
834 To
= (char *)To
+ Res
;
839 while (Res
> 0 && Size
> 0);
852 return _error
->Error(_("read, still have %lu to read but none left"),Size
);
855 // FileFd::Write - Write to the file /*{{{*/
856 // ---------------------------------------------------------------------
858 bool FileFd::Write(const void *From
,unsigned long Size
)
865 Res
= gzwrite(gz
,From
,Size
);
867 Res
= write(iFd
,From
,Size
);
868 if (Res
< 0 && errno
== EINTR
)
873 return _error
->Errno("write",_("Write error"));
876 From
= (char *)From
+ Res
;
879 while (Res
> 0 && Size
> 0);
885 return _error
->Error(_("write, still have %lu to write but couldn't"),Size
);
888 // FileFd::Seek - Seek in the file /*{{{*/
889 // ---------------------------------------------------------------------
891 bool FileFd::Seek(unsigned long To
)
895 res
= gzseek(gz
,To
,SEEK_SET
);
897 res
= lseek(iFd
,To
,SEEK_SET
);
898 if (res
!= (signed)To
)
901 return _error
->Error("Unable to seek to %lu",To
);
907 // FileFd::Skip - Seek in the file /*{{{*/
908 // ---------------------------------------------------------------------
910 bool FileFd::Skip(unsigned long Over
)
914 res
= gzseek(gz
,Over
,SEEK_CUR
);
916 res
= lseek(iFd
,Over
,SEEK_CUR
);
920 return _error
->Error("Unable to seek ahead %lu",Over
);
926 // FileFd::Truncate - Truncate the file /*{{{*/
927 // ---------------------------------------------------------------------
929 bool FileFd::Truncate(unsigned long To
)
934 return _error
->Error("Truncating gzipped files is not implemented (%s)", FileName
.c_str());
936 if (ftruncate(iFd
,To
) != 0)
939 return _error
->Error("Unable to truncate to %lu",To
);
945 // FileFd::Tell - Current seek position /*{{{*/
946 // ---------------------------------------------------------------------
948 unsigned long FileFd::Tell()
954 Res
= lseek(iFd
,0,SEEK_CUR
);
955 if (Res
== (off_t
)-1)
956 _error
->Errno("lseek","Failed to determine the current file position");
960 // FileFd::FileSize - Return the size of the file /*{{{*/
961 // ---------------------------------------------------------------------
963 unsigned long FileFd::FileSize()
967 if (fstat(iFd
,&Buf
) != 0)
968 return _error
->Errno("fstat","Unable to determine the file size");
972 // FileFd::Size - Return the size of the content in the file /*{{{*/
973 // ---------------------------------------------------------------------
975 unsigned long FileFd::Size()
977 unsigned long size
= FileSize();
979 // only check gzsize if we are actually a gzip file, just checking for
980 // "gz" is not sufficient as uncompressed files will be opened with
981 // gzopen in "direct" mode as well
982 if (gz
&& !gzdirect(gz
) && size
> 0)
984 /* unfortunately zlib.h doesn't provide a gzsize(), so we have to do
985 * this ourselves; the original (uncompressed) file size is the last 32
986 * bits of the file */
987 off_t orig_pos
= lseek(iFd
, 0, SEEK_CUR
);
988 if (lseek(iFd
, -4, SEEK_END
) < 0)
989 return _error
->Errno("lseek","Unable to seek to end of gzipped file");
991 if (read(iFd
, &size
, 4) != 4)
992 return _error
->Errno("read","Unable to read original size of gzipped file");
994 #ifdef WORDS_BIGENDIAN
995 uint32_t tmp_size
= size
;
996 uint8_t const * const p
= (uint8_t const * const) &tmp_size
;
997 tmp_size
= (p
[3] << 24) | (p
[2] << 16) | (p
[1] << 8) | p
[0];
1001 if (lseek(iFd
, orig_pos
, SEEK_SET
) < 0)
1002 return _error
->Errno("lseek","Unable to seek in gzipped file");
1009 // FileFd::Close - Close the file if the close flag is set /*{{{*/
1010 // ---------------------------------------------------------------------
1012 bool FileFd::Close()
1015 if ((Flags
& AutoClose
) == AutoClose
)
1018 int const e
= gzclose(gz
);
1019 // gzdopen() on empty files always fails with "buffer error" here, ignore that
1020 if (e
!= 0 && e
!= Z_BUF_ERROR
)
1021 Res
&= _error
->Errno("close",_("Problem closing the gzip file %s"), FileName
.c_str());
1023 if (iFd
> 0 && close(iFd
) != 0)
1024 Res
&= _error
->Errno("close",_("Problem closing the file %s"), FileName
.c_str());
1027 if ((Flags
& Replace
) == Replace
&& iFd
>= 0) {
1028 if (rename(TemporaryFileName
.c_str(), FileName
.c_str()) != 0)
1029 Res
&= _error
->Errno("rename",_("Problem renaming the file %s to %s"), TemporaryFileName
.c_str(), FileName
.c_str());
1031 FileName
= TemporaryFileName
; // for the unlink() below.
1037 if ((Flags
& Fail
) == Fail
&& (Flags
& DelOnFail
) == DelOnFail
&&
1038 FileName
.empty() == false)
1039 if (unlink(FileName
.c_str()) != 0)
1040 Res
&= _error
->WarningE("unlnk",_("Problem unlinking the file %s"), FileName
.c_str());
1046 // FileFd::Sync - Sync the file /*{{{*/
1047 // ---------------------------------------------------------------------
1051 #ifdef _POSIX_SYNCHRONIZED_IO
1052 if (fsync(iFd
) != 0)
1053 return _error
->Errno("sync",_("Problem syncing the file"));