]>
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>
49 // RunScripts - Run a set of scripts from a configuration subtree /*{{{*/
50 // ---------------------------------------------------------------------
52 bool RunScripts(const char *Cnf
)
54 Configuration::Item
const *Opts
= _config
->Tree(Cnf
);
55 if (Opts
== 0 || Opts
->Child
== 0)
59 // Fork for running the system calls
60 pid_t Child
= ExecFork();
65 if (_config
->FindDir("DPkg::Chroot-Directory","/") != "/")
67 std::cerr
<< "Chrooting into "
68 << _config
->FindDir("DPkg::Chroot-Directory")
70 if (chroot(_config
->FindDir("DPkg::Chroot-Directory","/").c_str()) != 0)
74 if (chdir("/tmp/") != 0)
77 unsigned int Count
= 1;
78 for (; Opts
!= 0; Opts
= Opts
->Next
, Count
++)
80 if (Opts
->Value
.empty() == true)
83 if (system(Opts
->Value
.c_str()) != 0)
91 while (waitpid(Child
,&Status
,0) != Child
)
95 return _error
->Errno("waitpid","Couldn't wait for subprocess");
98 // Restore sig int/quit
99 signal(SIGQUIT
,SIG_DFL
);
100 signal(SIGINT
,SIG_DFL
);
102 // Check for an error code.
103 if (WIFEXITED(Status
) == 0 || WEXITSTATUS(Status
) != 0)
105 unsigned int Count
= WEXITSTATUS(Status
);
109 for (; Opts
!= 0 && Count
!= 1; Opts
= Opts
->Next
, Count
--);
110 _error
->Error("Problem executing scripts %s '%s'",Cnf
,Opts
->Value
.c_str());
113 return _error
->Error("Sub-process returned an error code");
120 // CopyFile - Buffered copy of a file /*{{{*/
121 // ---------------------------------------------------------------------
122 /* The caller is expected to set things so that failure causes erasure */
123 bool CopyFile(FileFd
&From
,FileFd
&To
)
125 if (From
.IsOpen() == false || To
.IsOpen() == false)
128 // Buffered copy between fds
129 SPtrArray
<unsigned char> Buf
= new unsigned char[64000];
130 unsigned long Size
= From
.Size();
133 unsigned long ToRead
= Size
;
137 if (From
.Read(Buf
,ToRead
) == false ||
138 To
.Write(Buf
,ToRead
) == false)
147 // GetLock - Gets a lock file /*{{{*/
148 // ---------------------------------------------------------------------
149 /* This will create an empty file of the given name and lock it. Once this
150 is done all other calls to GetLock in any other process will fail with
151 -1. The return result is the fd of the file, the call should call
152 close at some time. */
153 int GetLock(string File
,bool Errors
)
155 // GetLock() is used in aptitude on directories with public-write access
156 // Use O_NOFOLLOW here to prevent symlink traversal attacks
157 int FD
= open(File
.c_str(),O_RDWR
| O_CREAT
| O_NOFOLLOW
,0640);
160 // Read only .. cant have locking problems there.
163 _error
->Warning(_("Not using locking for read only lock file %s"),File
.c_str());
164 return dup(0); // Need something for the caller to close
168 _error
->Errno("open",_("Could not open lock file %s"),File
.c_str());
170 // Feh.. We do this to distinguish the lock vs open case..
174 SetCloseExec(FD
,true);
176 // Aquire a write lock
179 fl
.l_whence
= SEEK_SET
;
182 if (fcntl(FD
,F_SETLK
,&fl
) == -1)
186 _error
->Warning(_("Not using locking for nfs mounted lock file %s"),File
.c_str());
187 return dup(0); // Need something for the caller to close
190 _error
->Errno("open",_("Could not get lock %s"),File
.c_str());
201 // FileExists - Check if a file exists /*{{{*/
202 // ---------------------------------------------------------------------
203 /* Beware: Directories are also files! */
204 bool FileExists(string File
)
207 if (stat(File
.c_str(),&Buf
) != 0)
212 // RealFileExists - Check if a file exists and if it is really a file /*{{{*/
213 // ---------------------------------------------------------------------
215 bool RealFileExists(string File
)
218 if (stat(File
.c_str(),&Buf
) != 0)
220 return ((Buf
.st_mode
& S_IFREG
) != 0);
223 // DirectoryExists - Check if a directory exists and is really one /*{{{*/
224 // ---------------------------------------------------------------------
226 bool DirectoryExists(string
const &Path
)
229 if (stat(Path
.c_str(),&Buf
) != 0)
231 return ((Buf
.st_mode
& S_IFDIR
) != 0);
234 // CreateDirectory - poor man's mkdir -p guarded by a parent directory /*{{{*/
235 // ---------------------------------------------------------------------
236 /* This method will create all directories needed for path in good old
237 mkdir -p style but refuses to do this if Parent is not a prefix of
238 this Path. Example: /var/cache/ and /var/cache/apt/archives are given,
239 so it will create apt/archives if /var/cache exists - on the other
240 hand if the parent is /var/lib the creation will fail as this path
241 is not a parent of the path to be generated. */
242 bool CreateDirectory(string
const &Parent
, string
const &Path
)
244 if (Parent
.empty() == true || Path
.empty() == true)
247 if (DirectoryExists(Path
) == true)
250 if (DirectoryExists(Parent
) == false)
253 // we are not going to create directories "into the blue"
254 if (Path
.find(Parent
, 0) != 0)
257 vector
<string
> const dirs
= VectorizeString(Path
.substr(Parent
.size()), '/');
258 string progress
= Parent
;
259 for (vector
<string
>::const_iterator d
= dirs
.begin(); d
!= dirs
.end(); ++d
)
261 if (d
->empty() == true)
264 progress
.append("/").append(*d
);
265 if (DirectoryExists(progress
) == true)
268 if (mkdir(progress
.c_str(), 0755) != 0)
274 // CreateAPTDirectoryIfNeeded - ensure that the given directory exists /*{{{*/
275 // ---------------------------------------------------------------------
276 /* a small wrapper around CreateDirectory to check if it exists and to
277 remove the trailing "/apt/" from the parent directory if needed */
278 bool CreateAPTDirectoryIfNeeded(string
const &Parent
, string
const &Path
)
280 if (DirectoryExists(Path
) == true)
283 size_t const len
= Parent
.size();
284 if (len
> 5 && Parent
.find("/apt/", len
- 6, 5) == len
- 5)
286 if (CreateDirectory(Parent
.substr(0,len
-5), Path
) == true)
289 else if (CreateDirectory(Parent
, Path
) == true)
295 // GetListOfFilesInDir - returns a vector of files in the given dir /*{{{*/
296 // ---------------------------------------------------------------------
297 /* If an extension is given only files with this extension are included
298 in the returned vector, otherwise every "normal" file is included. */
299 std::vector
<string
> GetListOfFilesInDir(string
const &Dir
, string
const &Ext
,
300 bool const &SortList
, bool const &AllowNoExt
)
302 std::vector
<string
> ext
;
304 if (Ext
.empty() == false)
306 if (AllowNoExt
== true && ext
.empty() == false)
308 return GetListOfFilesInDir(Dir
, ext
, SortList
);
310 std::vector
<string
> GetListOfFilesInDir(string
const &Dir
, std::vector
<string
> const &Ext
,
311 bool const &SortList
)
313 // Attention debuggers: need to be set with the environment config file!
314 bool const Debug
= _config
->FindB("Debug::GetListOfFilesInDir", false);
317 std::clog
<< "Accept in " << Dir
<< " only files with the following " << Ext
.size() << " extensions:" << std::endl
;
318 if (Ext
.empty() == true)
319 std::clog
<< "\tNO extension" << std::endl
;
321 for (std::vector
<string
>::const_iterator e
= Ext
.begin();
323 std::clog
<< '\t' << (e
->empty() == true ? "NO" : *e
) << " extension" << std::endl
;
326 std::vector
<string
> List
;
328 if (DirectoryExists(Dir
.c_str()) == false)
330 _error
->Error(_("List of files can't be created as '%s' is not a directory"), Dir
.c_str());
334 Configuration::MatchAgainstConfig
SilentIgnore("Dir::Ignore-Files-Silently");
335 DIR *D
= opendir(Dir
.c_str());
338 _error
->Errno("opendir",_("Unable to read %s"),Dir
.c_str());
342 for (struct dirent
*Ent
= readdir(D
); Ent
!= 0; Ent
= readdir(D
))
344 // skip "hidden" files
345 if (Ent
->d_name
[0] == '.')
348 // Make sure it is a file and not something else
349 string
const File
= flCombine(Dir
,Ent
->d_name
);
350 #ifdef _DIRENT_HAVE_D_TYPE
351 if (Ent
->d_type
!= DT_REG
)
354 if (RealFileExists(File
.c_str()) == false)
356 if (SilentIgnore
.Match(Ent
->d_name
) == false)
357 _error
->Notice(_("Ignoring '%s' in directory '%s' as it is not a regular file"), Ent
->d_name
, Dir
.c_str());
362 // check for accepted extension:
363 // no extension given -> periods are bad as hell!
364 // extensions given -> "" extension allows no extension
365 if (Ext
.empty() == false)
367 string d_ext
= flExtension(Ent
->d_name
);
368 if (d_ext
== Ent
->d_name
) // no extension
370 if (std::find(Ext
.begin(), Ext
.end(), "") == Ext
.end())
373 std::clog
<< "Bad file: " << Ent
->d_name
<< " → no extension" << std::endl
;
374 if (SilentIgnore
.Match(Ent
->d_name
) == false)
375 _error
->Notice(_("Ignoring file '%s' in directory '%s' as it has no filename extension"), Ent
->d_name
, Dir
.c_str());
379 else if (std::find(Ext
.begin(), Ext
.end(), d_ext
) == Ext
.end())
382 std::clog
<< "Bad file: " << Ent
->d_name
<< " → bad extension »" << flExtension(Ent
->d_name
) << "«" << std::endl
;
383 if (SilentIgnore
.Match(Ent
->d_name
) == false)
384 _error
->Notice(_("Ignoring file '%s' in directory '%s' as it has an invalid filename extension"), Ent
->d_name
, Dir
.c_str());
389 // Skip bad filenames ala run-parts
390 const char *C
= Ent
->d_name
;
392 if (isalpha(*C
) == 0 && isdigit(*C
) == 0
393 && *C
!= '_' && *C
!= '-') {
394 // no required extension -> dot is a bad character
395 if (*C
== '.' && Ext
.empty() == false)
400 // we don't reach the end of the name -> bad character included
404 std::clog
<< "Bad file: " << Ent
->d_name
<< " → bad character »"
405 << *C
<< "« in filename (period allowed: " << (Ext
.empty() ? "no" : "yes") << ")" << std::endl
;
409 // skip filenames which end with a period. These are never valid
413 std::clog
<< "Bad file: " << Ent
->d_name
<< " → Period as last character" << std::endl
;
418 std::clog
<< "Accept file: " << Ent
->d_name
<< " in " << Dir
<< std::endl
;
419 List
.push_back(File
);
423 if (SortList
== true)
424 std::sort(List
.begin(),List
.end());
428 // SafeGetCWD - This is a safer getcwd that returns a dynamic string /*{{{*/
429 // ---------------------------------------------------------------------
430 /* We return / on failure. */
433 // Stash the current dir.
436 if (getcwd(S
,sizeof(S
)-2) == 0)
438 unsigned int Len
= strlen(S
);
444 // flNotDir - Strip the directory from the filename /*{{{*/
445 // ---------------------------------------------------------------------
447 string
flNotDir(string File
)
449 string::size_type Res
= File
.rfind('/');
450 if (Res
== string::npos
)
453 return string(File
,Res
,Res
- File
.length());
456 // flNotFile - Strip the file from the directory name /*{{{*/
457 // ---------------------------------------------------------------------
458 /* Result ends in a / */
459 string
flNotFile(string File
)
461 string::size_type Res
= File
.rfind('/');
462 if (Res
== string::npos
)
465 return string(File
,0,Res
);
468 // flExtension - Return the extension for the file /*{{{*/
469 // ---------------------------------------------------------------------
471 string
flExtension(string File
)
473 string::size_type Res
= File
.rfind('.');
474 if (Res
== string::npos
)
477 return string(File
,Res
,Res
- File
.length());
480 // flNoLink - If file is a symlink then deref it /*{{{*/
481 // ---------------------------------------------------------------------
482 /* If the name is not a link then the returned path is the input. */
483 string
flNoLink(string File
)
486 if (lstat(File
.c_str(),&St
) != 0 || S_ISLNK(St
.st_mode
) == 0)
488 if (stat(File
.c_str(),&St
) != 0)
491 /* Loop resolving the link. There is no need to limit the number of
492 loops because the stat call above ensures that the symlink is not
500 if ((Res
= readlink(NFile
.c_str(),Buffer
,sizeof(Buffer
))) <= 0 ||
501 (unsigned)Res
>= sizeof(Buffer
))
504 // Append or replace the previous path
506 if (Buffer
[0] == '/')
509 NFile
= flNotFile(NFile
) + Buffer
;
511 // See if we are done
512 if (lstat(NFile
.c_str(),&St
) != 0)
514 if (S_ISLNK(St
.st_mode
) == 0)
519 // flCombine - Combine a file and a directory /*{{{*/
520 // ---------------------------------------------------------------------
521 /* If the file is an absolute path then it is just returned, otherwise
522 the directory is pre-pended to it. */
523 string
flCombine(string Dir
,string File
)
525 if (File
.empty() == true)
528 if (File
[0] == '/' || Dir
.empty() == true)
530 if (File
.length() >= 2 && File
[0] == '.' && File
[1] == '/')
532 if (Dir
[Dir
.length()-1] == '/')
534 return Dir
+ '/' + File
;
537 // SetCloseExec - Set the close on exec flag /*{{{*/
538 // ---------------------------------------------------------------------
540 void SetCloseExec(int Fd
,bool Close
)
542 if (fcntl(Fd
,F_SETFD
,(Close
== false)?0:FD_CLOEXEC
) != 0)
544 cerr
<< "FATAL -> Could not set close on exec " << strerror(errno
) << endl
;
549 // SetNonBlock - Set the nonblocking flag /*{{{*/
550 // ---------------------------------------------------------------------
552 void SetNonBlock(int Fd
,bool Block
)
554 int Flags
= fcntl(Fd
,F_GETFL
) & (~O_NONBLOCK
);
555 if (fcntl(Fd
,F_SETFL
,Flags
| ((Block
== false)?0:O_NONBLOCK
)) != 0)
557 cerr
<< "FATAL -> Could not set non-blocking flag " << strerror(errno
) << endl
;
562 // WaitFd - Wait for a FD to become readable /*{{{*/
563 // ---------------------------------------------------------------------
564 /* This waits for a FD to become readable using select. It is useful for
565 applications making use of non-blocking sockets. The timeout is
567 bool WaitFd(int Fd
,bool write
,unsigned long timeout
)
580 Res
= select(Fd
+1,0,&Set
,0,(timeout
!= 0?&tv
:0));
582 while (Res
< 0 && errno
== EINTR
);
592 Res
= select(Fd
+1,&Set
,0,0,(timeout
!= 0?&tv
:0));
594 while (Res
< 0 && errno
== EINTR
);
603 // ExecFork - Magical fork that sanitizes the context before execing /*{{{*/
604 // ---------------------------------------------------------------------
605 /* This is used if you want to cleanse the environment for the forked
606 child, it fixes up the important signals and nukes all of the fds,
607 otherwise acts like normal fork. */
610 // Fork off the process
611 pid_t Process
= fork();
614 cerr
<< "FATAL -> Failed to fork." << endl
;
618 // Spawn the subprocess
622 signal(SIGPIPE
,SIG_DFL
);
623 signal(SIGQUIT
,SIG_DFL
);
624 signal(SIGINT
,SIG_DFL
);
625 signal(SIGWINCH
,SIG_DFL
);
626 signal(SIGCONT
,SIG_DFL
);
627 signal(SIGTSTP
,SIG_DFL
);
630 Configuration::Item
const *Opts
= _config
->Tree("APT::Keep-Fds");
631 if (Opts
!= 0 && Opts
->Child
!= 0)
634 for (; Opts
!= 0; Opts
= Opts
->Next
)
636 if (Opts
->Value
.empty() == true)
638 int fd
= atoi(Opts
->Value
.c_str());
643 // Close all of our FDs - just in case
644 for (int K
= 3; K
!= 40; K
++)
646 if(KeepFDs
.find(K
) == KeepFDs
.end())
647 fcntl(K
,F_SETFD
,FD_CLOEXEC
);
654 // ExecWait - Fancy waitpid /*{{{*/
655 // ---------------------------------------------------------------------
656 /* Waits for the given sub process. If Reap is set then no errors are
657 generated. Otherwise a failed subprocess will generate a proper descriptive
659 bool ExecWait(pid_t Pid
,const char *Name
,bool Reap
)
664 // Wait and collect the error code
666 while (waitpid(Pid
,&Status
,0) != Pid
)
674 return _error
->Error(_("Waited for %s but it wasn't there"),Name
);
678 // Check for an error code.
679 if (WIFEXITED(Status
) == 0 || WEXITSTATUS(Status
) != 0)
683 if (WIFSIGNALED(Status
) != 0)
685 if( WTERMSIG(Status
) == SIGSEGV
)
686 return _error
->Error(_("Sub-process %s received a segmentation fault."),Name
);
688 return _error
->Error(_("Sub-process %s received signal %u."),Name
, WTERMSIG(Status
));
691 if (WIFEXITED(Status
) != 0)
692 return _error
->Error(_("Sub-process %s returned an error code (%u)"),Name
,WEXITSTATUS(Status
));
694 return _error
->Error(_("Sub-process %s exited unexpectedly"),Name
);
701 // FileFd::Open - Open a file /*{{{*/
702 // ---------------------------------------------------------------------
703 /* The most commonly used open mode combinations are given with Mode */
704 bool FileFd::Open(string FileName
,OpenMode Mode
, unsigned long Perms
)
711 iFd
= open(FileName
.c_str(),O_RDONLY
);
715 iFd
= open(FileName
.c_str(),O_RDONLY
);
717 gz
= gzdopen (iFd
, "r");
728 char *name
= strdup((FileName
+ ".XXXXXX").c_str());
729 TemporaryFileName
= string(mktemp(name
));
730 iFd
= open(TemporaryFileName
.c_str(),O_RDWR
| O_CREAT
| O_EXCL
,Perms
);
738 if (lstat(FileName
.c_str(),&Buf
) == 0 && S_ISLNK(Buf
.st_mode
))
739 unlink(FileName
.c_str());
740 iFd
= open(FileName
.c_str(),O_RDWR
| O_CREAT
| O_TRUNC
,Perms
);
745 iFd
= open(FileName
.c_str(),O_RDWR
);
749 iFd
= open(FileName
.c_str(),O_RDWR
| O_CREAT
,Perms
);
753 unlink(FileName
.c_str());
754 iFd
= open(FileName
.c_str(),O_RDWR
| O_CREAT
| O_EXCL
,Perms
);
759 return _error
->Errno("open",_("Could not open file %s"),FileName
.c_str());
761 this->FileName
= FileName
;
762 SetCloseExec(iFd
,true);
766 bool FileFd::OpenDescriptor(int Fd
, OpenMode Mode
, bool AutoClose
)
769 Flags
= (AutoClose
) ? FileFd::AutoClose
: 0;
771 if (Mode
== ReadOnlyGzip
) {
772 gz
= gzdopen (iFd
, "r");
776 return _error
->Errno("gzdopen",_("Could not open file descriptor %d"),
784 // FileFd::~File - Closes the file /*{{{*/
785 // ---------------------------------------------------------------------
786 /* If the proper modes are selected then we close the Fd and possibly
787 unlink the file on error. */
793 // FileFd::Read - Read a bit of the file /*{{{*/
794 // ---------------------------------------------------------------------
795 /* We are carefull to handle interruption by a signal while reading
797 bool FileFd::Read(void *To
,unsigned long Size
,unsigned long *Actual
)
807 Res
= gzread(gz
,To
,Size
);
809 Res
= read(iFd
,To
,Size
);
810 if (Res
< 0 && errno
== EINTR
)
815 return _error
->Errno("read",_("Read error"));
818 To
= (char *)To
+ Res
;
823 while (Res
> 0 && Size
> 0);
836 return _error
->Error(_("read, still have %lu to read but none left"),Size
);
839 // FileFd::Write - Write to the file /*{{{*/
840 // ---------------------------------------------------------------------
842 bool FileFd::Write(const void *From
,unsigned long Size
)
849 Res
= gzwrite(gz
,From
,Size
);
851 Res
= write(iFd
,From
,Size
);
852 if (Res
< 0 && errno
== EINTR
)
857 return _error
->Errno("write",_("Write error"));
860 From
= (char *)From
+ Res
;
863 while (Res
> 0 && Size
> 0);
869 return _error
->Error(_("write, still have %lu to write but couldn't"),Size
);
872 // FileFd::Seek - Seek in the file /*{{{*/
873 // ---------------------------------------------------------------------
875 bool FileFd::Seek(unsigned long To
)
879 res
= gzseek(gz
,To
,SEEK_SET
);
881 res
= lseek(iFd
,To
,SEEK_SET
);
882 if (res
!= (signed)To
)
885 return _error
->Error("Unable to seek to %lu",To
);
891 // FileFd::Skip - Seek in the file /*{{{*/
892 // ---------------------------------------------------------------------
894 bool FileFd::Skip(unsigned long Over
)
898 res
= gzseek(gz
,Over
,SEEK_CUR
);
900 res
= lseek(iFd
,Over
,SEEK_CUR
);
904 return _error
->Error("Unable to seek ahead %lu",Over
);
910 // FileFd::Truncate - Truncate the file /*{{{*/
911 // ---------------------------------------------------------------------
913 bool FileFd::Truncate(unsigned long To
)
918 return _error
->Error("Truncating gzipped files is not implemented (%s)", FileName
.c_str());
920 if (ftruncate(iFd
,To
) != 0)
923 return _error
->Error("Unable to truncate to %lu",To
);
929 // FileFd::Tell - Current seek position /*{{{*/
930 // ---------------------------------------------------------------------
932 unsigned long FileFd::Tell()
938 Res
= lseek(iFd
,0,SEEK_CUR
);
939 if (Res
== (off_t
)-1)
940 _error
->Errno("lseek","Failed to determine the current file position");
944 // FileFd::FileSize - Return the size of the file /*{{{*/
945 // ---------------------------------------------------------------------
947 unsigned long FileFd::FileSize()
951 if (fstat(iFd
,&Buf
) != 0)
952 return _error
->Errno("fstat","Unable to determine the file size");
956 // FileFd::Size - Return the size of the content in the file /*{{{*/
957 // ---------------------------------------------------------------------
959 unsigned long FileFd::Size()
961 unsigned long size
= FileSize();
963 // only check gzsize if we are actually a gzip file, just checking for
964 // "gz" is not sufficient as uncompressed files will be opened with
965 // gzopen in "direct" mode as well
966 if (gz
&& !gzdirect(gz
) && size
> 0)
968 /* unfortunately zlib.h doesn't provide a gzsize(), so we have to do
969 * this ourselves; the original (uncompressed) file size is the last 32
970 * bits of the file */
971 off_t orig_pos
= lseek(iFd
, 0, SEEK_CUR
);
972 if (lseek(iFd
, -4, SEEK_END
) < 0)
973 return _error
->Errno("lseek","Unable to seek to end of gzipped file");
974 if (read(iFd
, &size
, 4) != 4)
975 return _error
->Errno("read","Unable to read original size of gzipped file");
978 if (lseek(iFd
, orig_pos
, SEEK_SET
) < 0)
979 return _error
->Errno("lseek","Unable to seek in gzipped file");
986 // FileFd::Close - Close the file if the close flag is set /*{{{*/
987 // ---------------------------------------------------------------------
992 if ((Flags
& AutoClose
) == AutoClose
)
995 int const e
= gzclose(gz
);
996 // gzdopen() on empty files always fails with "buffer error" here, ignore that
997 if (e
!= 0 && e
!= Z_BUF_ERROR
)
998 Res
&= _error
->Errno("close",_("Problem closing the gzip file %s"), FileName
.c_str());
1000 if (iFd
> 0 && close(iFd
) != 0)
1001 Res
&= _error
->Errno("close",_("Problem closing the file %s"), FileName
.c_str());
1004 if ((Flags
& Replace
) == Replace
&& iFd
>= 0) {
1005 if (rename(TemporaryFileName
.c_str(), FileName
.c_str()) != 0)
1006 Res
&= _error
->Errno("rename",_("Problem renaming the file %s to %s"), TemporaryFileName
.c_str(), FileName
.c_str());
1008 FileName
= TemporaryFileName
; // for the unlink() below.
1014 if ((Flags
& Fail
) == Fail
&& (Flags
& DelOnFail
) == DelOnFail
&&
1015 FileName
.empty() == false)
1016 if (unlink(FileName
.c_str()) != 0)
1017 Res
&= _error
->WarningE("unlnk",_("Problem unlinking the file %s"), FileName
.c_str());
1023 // FileFd::Sync - Sync the file /*{{{*/
1024 // ---------------------------------------------------------------------
1028 #ifdef _POSIX_SYNCHRONIZED_IO
1029 if (fsync(iFd
) != 0)
1030 return _error
->Errno("sync",_("Problem syncing the file"));