]>
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>
46 #ifndef WORDS_BIGENDIAN
53 // RunScripts - Run a set of scripts from a configuration subtree /*{{{*/
54 // ---------------------------------------------------------------------
56 bool RunScripts(const char *Cnf
)
58 Configuration::Item
const *Opts
= _config
->Tree(Cnf
);
59 if (Opts
== 0 || Opts
->Child
== 0)
63 // Fork for running the system calls
64 pid_t Child
= ExecFork();
69 if (chdir("/tmp/") != 0)
72 unsigned int Count
= 1;
73 for (; Opts
!= 0; Opts
= Opts
->Next
, Count
++)
75 if (Opts
->Value
.empty() == true)
78 if (system(Opts
->Value
.c_str()) != 0)
86 while (waitpid(Child
,&Status
,0) != Child
)
90 return _error
->Errno("waitpid","Couldn't wait for subprocess");
93 // Restore sig int/quit
94 signal(SIGQUIT
,SIG_DFL
);
95 signal(SIGINT
,SIG_DFL
);
97 // Check for an error code.
98 if (WIFEXITED(Status
) == 0 || WEXITSTATUS(Status
) != 0)
100 unsigned int Count
= WEXITSTATUS(Status
);
104 for (; Opts
!= 0 && Count
!= 1; Opts
= Opts
->Next
, Count
--);
105 _error
->Error("Problem executing scripts %s '%s'",Cnf
,Opts
->Value
.c_str());
108 return _error
->Error("Sub-process returned an error code");
115 // CopyFile - Buffered copy of a file /*{{{*/
116 // ---------------------------------------------------------------------
117 /* The caller is expected to set things so that failure causes erasure */
118 bool CopyFile(FileFd
&From
,FileFd
&To
)
120 if (From
.IsOpen() == false || To
.IsOpen() == false)
123 // Buffered copy between fds
124 SPtrArray
<unsigned char> Buf
= new unsigned char[64000];
125 unsigned long Size
= From
.Size();
128 unsigned long ToRead
= Size
;
132 if (From
.Read(Buf
,ToRead
) == false ||
133 To
.Write(Buf
,ToRead
) == false)
142 // GetLock - Gets a lock file /*{{{*/
143 // ---------------------------------------------------------------------
144 /* This will create an empty file of the given name and lock it. Once this
145 is done all other calls to GetLock in any other process will fail with
146 -1. The return result is the fd of the file, the call should call
147 close at some time. */
148 int GetLock(string File
,bool Errors
)
150 // GetLock() is used in aptitude on directories with public-write access
151 // Use O_NOFOLLOW here to prevent symlink traversal attacks
152 int FD
= open(File
.c_str(),O_RDWR
| O_CREAT
| O_NOFOLLOW
,0640);
155 // Read only .. cant have locking problems there.
158 _error
->Warning(_("Not using locking for read only lock file %s"),File
.c_str());
159 return dup(0); // Need something for the caller to close
163 _error
->Errno("open",_("Could not open lock file %s"),File
.c_str());
165 // Feh.. We do this to distinguish the lock vs open case..
169 SetCloseExec(FD
,true);
171 // Aquire a write lock
174 fl
.l_whence
= SEEK_SET
;
177 if (fcntl(FD
,F_SETLK
,&fl
) == -1)
181 _error
->Warning(_("Not using locking for nfs mounted lock file %s"),File
.c_str());
182 return dup(0); // Need something for the caller to close
185 _error
->Errno("open",_("Could not get lock %s"),File
.c_str());
196 // FileExists - Check if a file exists /*{{{*/
197 // ---------------------------------------------------------------------
198 /* Beware: Directories are also files! */
199 bool FileExists(string File
)
202 if (stat(File
.c_str(),&Buf
) != 0)
207 // RealFileExists - Check if a file exists and if it is really a file /*{{{*/
208 // ---------------------------------------------------------------------
210 bool RealFileExists(string File
)
213 if (stat(File
.c_str(),&Buf
) != 0)
215 return ((Buf
.st_mode
& S_IFREG
) != 0);
218 // DirectoryExists - Check if a directory exists and is really one /*{{{*/
219 // ---------------------------------------------------------------------
221 bool DirectoryExists(string
const &Path
)
224 if (stat(Path
.c_str(),&Buf
) != 0)
226 return ((Buf
.st_mode
& S_IFDIR
) != 0);
229 // CreateDirectory - poor man's mkdir -p guarded by a parent directory /*{{{*/
230 // ---------------------------------------------------------------------
231 /* This method will create all directories needed for path in good old
232 mkdir -p style but refuses to do this if Parent is not a prefix of
233 this Path. Example: /var/cache/ and /var/cache/apt/archives are given,
234 so it will create apt/archives if /var/cache exists - on the other
235 hand if the parent is /var/lib the creation will fail as this path
236 is not a parent of the path to be generated. */
237 bool CreateDirectory(string
const &Parent
, string
const &Path
)
239 if (Parent
.empty() == true || Path
.empty() == true)
242 if (DirectoryExists(Path
) == true)
245 if (DirectoryExists(Parent
) == false)
248 // we are not going to create directories "into the blue"
249 if (Path
.find(Parent
, 0) != 0)
252 vector
<string
> const dirs
= VectorizeString(Path
.substr(Parent
.size()), '/');
253 string progress
= Parent
;
254 for (vector
<string
>::const_iterator d
= dirs
.begin(); d
!= dirs
.end(); ++d
)
256 if (d
->empty() == true)
259 progress
.append("/").append(*d
);
260 if (DirectoryExists(progress
) == true)
263 if (mkdir(progress
.c_str(), 0755) != 0)
269 // CreateAPTDirectoryIfNeeded - ensure that the given directory exists /*{{{*/
270 // ---------------------------------------------------------------------
271 /* a small wrapper around CreateDirectory to check if it exists and to
272 remove the trailing "/apt/" from the parent directory if needed */
273 bool CreateAPTDirectoryIfNeeded(string
const &Parent
, string
const &Path
)
275 if (DirectoryExists(Path
) == true)
278 size_t const len
= Parent
.size();
279 if (len
> 5 && Parent
.find("/apt/", len
- 6, 5) == len
- 5)
281 if (CreateDirectory(Parent
.substr(0,len
-5), Path
) == true)
284 else if (CreateDirectory(Parent
, Path
) == true)
290 // GetListOfFilesInDir - returns a vector of files in the given dir /*{{{*/
291 // ---------------------------------------------------------------------
292 /* If an extension is given only files with this extension are included
293 in the returned vector, otherwise every "normal" file is included. */
294 std::vector
<string
> GetListOfFilesInDir(string
const &Dir
, string
const &Ext
,
295 bool const &SortList
, bool const &AllowNoExt
)
297 std::vector
<string
> ext
;
299 if (Ext
.empty() == false)
301 if (AllowNoExt
== true && ext
.empty() == false)
303 return GetListOfFilesInDir(Dir
, ext
, SortList
);
305 std::vector
<string
> GetListOfFilesInDir(string
const &Dir
, std::vector
<string
> const &Ext
,
306 bool const &SortList
)
308 // Attention debuggers: need to be set with the environment config file!
309 bool const Debug
= _config
->FindB("Debug::GetListOfFilesInDir", false);
312 std::clog
<< "Accept in " << Dir
<< " only files with the following " << Ext
.size() << " extensions:" << std::endl
;
313 if (Ext
.empty() == true)
314 std::clog
<< "\tNO extension" << std::endl
;
316 for (std::vector
<string
>::const_iterator e
= Ext
.begin();
318 std::clog
<< '\t' << (e
->empty() == true ? "NO" : *e
) << " extension" << std::endl
;
321 std::vector
<string
> List
;
323 if (DirectoryExists(Dir
.c_str()) == false)
325 _error
->Error(_("List of files can't be created as '%s' is not a directory"), Dir
.c_str());
329 Configuration::MatchAgainstConfig
SilentIgnore("Dir::Ignore-Files-Silently");
330 DIR *D
= opendir(Dir
.c_str());
333 _error
->Errno("opendir",_("Unable to read %s"),Dir
.c_str());
337 for (struct dirent
*Ent
= readdir(D
); Ent
!= 0; Ent
= readdir(D
))
339 // skip "hidden" files
340 if (Ent
->d_name
[0] == '.')
343 // Make sure it is a file and not something else
344 string
const File
= flCombine(Dir
,Ent
->d_name
);
345 #ifdef _DIRENT_HAVE_D_TYPE
346 if (Ent
->d_type
!= DT_REG
)
349 if (RealFileExists(File
.c_str()) == false)
351 if (SilentIgnore
.Match(Ent
->d_name
) == false)
352 _error
->Notice(_("Ignoring '%s' in directory '%s' as it is not a regular file"), Ent
->d_name
, Dir
.c_str());
357 // check for accepted extension:
358 // no extension given -> periods are bad as hell!
359 // extensions given -> "" extension allows no extension
360 if (Ext
.empty() == false)
362 string d_ext
= flExtension(Ent
->d_name
);
363 if (d_ext
== Ent
->d_name
) // no extension
365 if (std::find(Ext
.begin(), Ext
.end(), "") == Ext
.end())
368 std::clog
<< "Bad file: " << Ent
->d_name
<< " → no extension" << std::endl
;
369 if (SilentIgnore
.Match(Ent
->d_name
) == false)
370 _error
->Notice(_("Ignoring file '%s' in directory '%s' as it has no filename extension"), Ent
->d_name
, Dir
.c_str());
374 else if (std::find(Ext
.begin(), Ext
.end(), d_ext
) == Ext
.end())
377 std::clog
<< "Bad file: " << Ent
->d_name
<< " → bad extension »" << flExtension(Ent
->d_name
) << "«" << std::endl
;
378 if (SilentIgnore
.Match(Ent
->d_name
) == false)
379 _error
->Notice(_("Ignoring file '%s' in directory '%s' as it has an invalid filename extension"), Ent
->d_name
, Dir
.c_str());
384 // Skip bad filenames ala run-parts
385 const char *C
= Ent
->d_name
;
387 if (isalpha(*C
) == 0 && isdigit(*C
) == 0
388 && *C
!= '_' && *C
!= '-') {
389 // no required extension -> dot is a bad character
390 if (*C
== '.' && Ext
.empty() == false)
395 // we don't reach the end of the name -> bad character included
399 std::clog
<< "Bad file: " << Ent
->d_name
<< " → bad character »"
400 << *C
<< "« in filename (period allowed: " << (Ext
.empty() ? "no" : "yes") << ")" << std::endl
;
404 // skip filenames which end with a period. These are never valid
408 std::clog
<< "Bad file: " << Ent
->d_name
<< " → Period as last character" << std::endl
;
413 std::clog
<< "Accept file: " << Ent
->d_name
<< " in " << Dir
<< std::endl
;
414 List
.push_back(File
);
418 if (SortList
== true)
419 std::sort(List
.begin(),List
.end());
423 // SafeGetCWD - This is a safer getcwd that returns a dynamic string /*{{{*/
424 // ---------------------------------------------------------------------
425 /* We return / on failure. */
428 // Stash the current dir.
431 if (getcwd(S
,sizeof(S
)-2) == 0)
433 unsigned int Len
= strlen(S
);
439 // flNotDir - Strip the directory from the filename /*{{{*/
440 // ---------------------------------------------------------------------
442 string
flNotDir(string File
)
444 string::size_type Res
= File
.rfind('/');
445 if (Res
== string::npos
)
448 return string(File
,Res
,Res
- File
.length());
451 // flNotFile - Strip the file from the directory name /*{{{*/
452 // ---------------------------------------------------------------------
453 /* Result ends in a / */
454 string
flNotFile(string File
)
456 string::size_type Res
= File
.rfind('/');
457 if (Res
== string::npos
)
460 return string(File
,0,Res
);
463 // flExtension - Return the extension for the file /*{{{*/
464 // ---------------------------------------------------------------------
466 string
flExtension(string File
)
468 string::size_type Res
= File
.rfind('.');
469 if (Res
== string::npos
)
472 return string(File
,Res
,Res
- File
.length());
475 // flNoLink - If file is a symlink then deref it /*{{{*/
476 // ---------------------------------------------------------------------
477 /* If the name is not a link then the returned path is the input. */
478 string
flNoLink(string File
)
481 if (lstat(File
.c_str(),&St
) != 0 || S_ISLNK(St
.st_mode
) == 0)
483 if (stat(File
.c_str(),&St
) != 0)
486 /* Loop resolving the link. There is no need to limit the number of
487 loops because the stat call above ensures that the symlink is not
495 if ((Res
= readlink(NFile
.c_str(),Buffer
,sizeof(Buffer
))) <= 0 ||
496 (unsigned)Res
>= sizeof(Buffer
))
499 // Append or replace the previous path
501 if (Buffer
[0] == '/')
504 NFile
= flNotFile(NFile
) + Buffer
;
506 // See if we are done
507 if (lstat(NFile
.c_str(),&St
) != 0)
509 if (S_ISLNK(St
.st_mode
) == 0)
514 // flCombine - Combine a file and a directory /*{{{*/
515 // ---------------------------------------------------------------------
516 /* If the file is an absolute path then it is just returned, otherwise
517 the directory is pre-pended to it. */
518 string
flCombine(string Dir
,string File
)
520 if (File
.empty() == true)
523 if (File
[0] == '/' || Dir
.empty() == true)
525 if (File
.length() >= 2 && File
[0] == '.' && File
[1] == '/')
527 if (Dir
[Dir
.length()-1] == '/')
529 return Dir
+ '/' + File
;
532 // SetCloseExec - Set the close on exec flag /*{{{*/
533 // ---------------------------------------------------------------------
535 void SetCloseExec(int Fd
,bool Close
)
537 if (fcntl(Fd
,F_SETFD
,(Close
== false)?0:FD_CLOEXEC
) != 0)
539 cerr
<< "FATAL -> Could not set close on exec " << strerror(errno
) << endl
;
544 // SetNonBlock - Set the nonblocking flag /*{{{*/
545 // ---------------------------------------------------------------------
547 void SetNonBlock(int Fd
,bool Block
)
549 int Flags
= fcntl(Fd
,F_GETFL
) & (~O_NONBLOCK
);
550 if (fcntl(Fd
,F_SETFL
,Flags
| ((Block
== false)?0:O_NONBLOCK
)) != 0)
552 cerr
<< "FATAL -> Could not set non-blocking flag " << strerror(errno
) << endl
;
557 // WaitFd - Wait for a FD to become readable /*{{{*/
558 // ---------------------------------------------------------------------
559 /* This waits for a FD to become readable using select. It is useful for
560 applications making use of non-blocking sockets. The timeout is
562 bool WaitFd(int Fd
,bool write
,unsigned long timeout
)
575 Res
= select(Fd
+1,0,&Set
,0,(timeout
!= 0?&tv
:0));
577 while (Res
< 0 && errno
== EINTR
);
587 Res
= select(Fd
+1,&Set
,0,0,(timeout
!= 0?&tv
:0));
589 while (Res
< 0 && errno
== EINTR
);
598 // ExecFork - Magical fork that sanitizes the context before execing /*{{{*/
599 // ---------------------------------------------------------------------
600 /* This is used if you want to cleanse the environment for the forked
601 child, it fixes up the important signals and nukes all of the fds,
602 otherwise acts like normal fork. */
605 // Fork off the process
606 pid_t Process
= fork();
609 cerr
<< "FATAL -> Failed to fork." << endl
;
613 // Spawn the subprocess
617 signal(SIGPIPE
,SIG_DFL
);
618 signal(SIGQUIT
,SIG_DFL
);
619 signal(SIGINT
,SIG_DFL
);
620 signal(SIGWINCH
,SIG_DFL
);
621 signal(SIGCONT
,SIG_DFL
);
622 signal(SIGTSTP
,SIG_DFL
);
625 Configuration::Item
const *Opts
= _config
->Tree("APT::Keep-Fds");
626 if (Opts
!= 0 && Opts
->Child
!= 0)
629 for (; Opts
!= 0; Opts
= Opts
->Next
)
631 if (Opts
->Value
.empty() == true)
633 int fd
= atoi(Opts
->Value
.c_str());
638 // Close all of our FDs - just in case
639 for (int K
= 3; K
!= 40; K
++)
641 if(KeepFDs
.find(K
) == KeepFDs
.end())
642 fcntl(K
,F_SETFD
,FD_CLOEXEC
);
649 // ExecWait - Fancy waitpid /*{{{*/
650 // ---------------------------------------------------------------------
651 /* Waits for the given sub process. If Reap is set then no errors are
652 generated. Otherwise a failed subprocess will generate a proper descriptive
654 bool ExecWait(pid_t Pid
,const char *Name
,bool Reap
)
659 // Wait and collect the error code
661 while (waitpid(Pid
,&Status
,0) != Pid
)
669 return _error
->Error(_("Waited for %s but it wasn't there"),Name
);
673 // Check for an error code.
674 if (WIFEXITED(Status
) == 0 || WEXITSTATUS(Status
) != 0)
678 if (WIFSIGNALED(Status
) != 0)
680 if( WTERMSIG(Status
) == SIGSEGV
)
681 return _error
->Error(_("Sub-process %s received a segmentation fault."),Name
);
683 return _error
->Error(_("Sub-process %s received signal %u."),Name
, WTERMSIG(Status
));
686 if (WIFEXITED(Status
) != 0)
687 return _error
->Error(_("Sub-process %s returned an error code (%u)"),Name
,WEXITSTATUS(Status
));
689 return _error
->Error(_("Sub-process %s exited unexpectedly"),Name
);
696 // FileFd::Open - Open a file /*{{{*/
697 // ---------------------------------------------------------------------
698 /* The most commonly used open mode combinations are given with Mode */
699 bool FileFd::Open(string FileName
,OpenMode Mode
, unsigned long Perms
)
706 iFd
= open(FileName
.c_str(),O_RDONLY
);
710 iFd
= open(FileName
.c_str(),O_RDONLY
);
712 gz
= gzdopen (iFd
, "r");
723 char *name
= strdup((FileName
+ ".XXXXXX").c_str());
724 TemporaryFileName
= string(mktemp(name
));
725 iFd
= open(TemporaryFileName
.c_str(),O_RDWR
| O_CREAT
| O_EXCL
,Perms
);
733 if (lstat(FileName
.c_str(),&Buf
) == 0 && S_ISLNK(Buf
.st_mode
))
734 unlink(FileName
.c_str());
735 iFd
= open(FileName
.c_str(),O_RDWR
| O_CREAT
| O_TRUNC
,Perms
);
740 iFd
= open(FileName
.c_str(),O_RDWR
);
744 iFd
= open(FileName
.c_str(),O_RDWR
| O_CREAT
,Perms
);
748 unlink(FileName
.c_str());
749 iFd
= open(FileName
.c_str(),O_RDWR
| O_CREAT
| O_EXCL
,Perms
);
754 return _error
->Errno("open",_("Could not open file %s"),FileName
.c_str());
756 this->FileName
= FileName
;
757 SetCloseExec(iFd
,true);
761 bool FileFd::OpenDescriptor(int Fd
, OpenMode Mode
, bool AutoClose
)
764 Flags
= (AutoClose
) ? FileFd::AutoClose
: 0;
766 if (Mode
== ReadOnlyGzip
) {
767 gz
= gzdopen (iFd
, "r");
771 return _error
->Errno("gzdopen",_("Could not open file descriptor %d"),
779 // FileFd::~File - Closes the file /*{{{*/
780 // ---------------------------------------------------------------------
781 /* If the proper modes are selected then we close the Fd and possibly
782 unlink the file on error. */
788 // FileFd::Read - Read a bit of the file /*{{{*/
789 // ---------------------------------------------------------------------
790 /* We are carefull to handle interruption by a signal while reading
792 bool FileFd::Read(void *To
,unsigned long Size
,unsigned long *Actual
)
802 Res
= gzread(gz
,To
,Size
);
804 Res
= read(iFd
,To
,Size
);
805 if (Res
< 0 && errno
== EINTR
)
810 return _error
->Errno("read",_("Read error"));
813 To
= (char *)To
+ Res
;
818 while (Res
> 0 && Size
> 0);
831 return _error
->Error(_("read, still have %lu to read but none left"),Size
);
834 // FileFd::Write - Write to the file /*{{{*/
835 // ---------------------------------------------------------------------
837 bool FileFd::Write(const void *From
,unsigned long Size
)
844 Res
= gzwrite(gz
,From
,Size
);
846 Res
= write(iFd
,From
,Size
);
847 if (Res
< 0 && errno
== EINTR
)
852 return _error
->Errno("write",_("Write error"));
855 From
= (char *)From
+ Res
;
858 while (Res
> 0 && Size
> 0);
864 return _error
->Error(_("write, still have %lu to write but couldn't"),Size
);
867 // FileFd::Seek - Seek in the file /*{{{*/
868 // ---------------------------------------------------------------------
870 bool FileFd::Seek(unsigned long To
)
874 res
= gzseek(gz
,To
,SEEK_SET
);
876 res
= lseek(iFd
,To
,SEEK_SET
);
877 if (res
!= (signed)To
)
880 return _error
->Error("Unable to seek to %lu",To
);
886 // FileFd::Skip - Seek in the file /*{{{*/
887 // ---------------------------------------------------------------------
889 bool FileFd::Skip(unsigned long Over
)
893 res
= gzseek(gz
,Over
,SEEK_CUR
);
895 res
= lseek(iFd
,Over
,SEEK_CUR
);
899 return _error
->Error("Unable to seek ahead %lu",Over
);
905 // FileFd::Truncate - Truncate the file /*{{{*/
906 // ---------------------------------------------------------------------
908 bool FileFd::Truncate(unsigned long To
)
913 return _error
->Error("Truncating gzipped files is not implemented (%s)", FileName
.c_str());
915 if (ftruncate(iFd
,To
) != 0)
918 return _error
->Error("Unable to truncate to %lu",To
);
924 // FileFd::Tell - Current seek position /*{{{*/
925 // ---------------------------------------------------------------------
927 unsigned long FileFd::Tell()
933 Res
= lseek(iFd
,0,SEEK_CUR
);
934 if (Res
== (off_t
)-1)
935 _error
->Errno("lseek","Failed to determine the current file position");
939 // FileFd::FileSize - Return the size of the file /*{{{*/
940 // ---------------------------------------------------------------------
942 unsigned long FileFd::FileSize()
946 if (fstat(iFd
,&Buf
) != 0)
947 return _error
->Errno("fstat","Unable to determine the file size");
951 // FileFd::Size - Return the size of the content in the file /*{{{*/
952 // ---------------------------------------------------------------------
954 unsigned long FileFd::Size()
956 unsigned long size
= FileSize();
958 // only check gzsize if we are actually a gzip file, just checking for
959 // "gz" is not sufficient as uncompressed files will be opened with
960 // gzopen in "direct" mode as well
961 if (gz
&& !gzdirect(gz
) && size
> 0)
963 /* unfortunately zlib.h doesn't provide a gzsize(), so we have to do
964 * this ourselves; the original (uncompressed) file size is the last 32
965 * bits of the file */
966 off_t orig_pos
= lseek(iFd
, 0, SEEK_CUR
);
967 if (lseek(iFd
, -4, SEEK_END
) < 0)
968 return _error
->Errno("lseek","Unable to seek to end of gzipped file");
970 if (read(iFd
, &size
, 4) != 4)
971 return _error
->Errno("read","Unable to read original size of gzipped file");
973 #ifdef WORDS_BIGENDIAN
974 uint32_t tmp_size
= size
;
975 uint8_t const * const p
= (uint8_t const * const) &tmp_size
;
976 tmp_size
= (p
[3] << 24) | (p
[2] << 16) | (p
[1] << 8) | p
[0];
980 if (lseek(iFd
, orig_pos
, SEEK_SET
) < 0)
981 return _error
->Errno("lseek","Unable to seek in gzipped file");
988 // FileFd::Close - Close the file if the close flag is set /*{{{*/
989 // ---------------------------------------------------------------------
994 if ((Flags
& AutoClose
) == AutoClose
)
997 int const e
= gzclose(gz
);
998 // gzdopen() on empty files always fails with "buffer error" here, ignore that
999 if (e
!= 0 && e
!= Z_BUF_ERROR
)
1000 Res
&= _error
->Errno("close",_("Problem closing the gzip file %s"), FileName
.c_str());
1002 if (iFd
> 0 && close(iFd
) != 0)
1003 Res
&= _error
->Errno("close",_("Problem closing the file %s"), FileName
.c_str());
1006 if ((Flags
& Replace
) == Replace
&& iFd
>= 0) {
1007 if (rename(TemporaryFileName
.c_str(), FileName
.c_str()) != 0)
1008 Res
&= _error
->Errno("rename",_("Problem renaming the file %s to %s"), TemporaryFileName
.c_str(), FileName
.c_str());
1010 FileName
= TemporaryFileName
; // for the unlink() below.
1016 if ((Flags
& Fail
) == Fail
&& (Flags
& DelOnFail
) == DelOnFail
&&
1017 FileName
.empty() == false)
1018 if (unlink(FileName
.c_str()) != 0)
1019 Res
&= _error
->WarningE("unlnk",_("Problem unlinking the file %s"), FileName
.c_str());
1025 // FileFd::Sync - Sync the file /*{{{*/
1026 // ---------------------------------------------------------------------
1030 #ifdef _POSIX_SYNCHRONIZED_IO
1031 if (fsync(iFd
) != 0)
1032 return _error
->Errno("sync",_("Problem syncing the file"));