- Make FileFd replace files atomically in WriteTemp mode (for cache, etc).
#include <cstdlib>
#include <cstring>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <unistd.h>
#include <iostream>
#include <unistd.h>
- struct stat Buf;
- if (lstat(FileName.c_str(),&Buf) == 0 && S_ISLNK(Buf.st_mode))
- unlink(FileName.c_str());
- iFd = open(FileName.c_str(),O_RDWR | O_CREAT | O_TRUNC,Perms);
+ Flags |= Replace;
+ char *name = strdup((FileName + ".XXXXXX").c_str());
+ TemporaryFileName = string(mktemp(name));
+ iFd = open(TemporaryFileName.c_str(),O_RDWR | O_CREAT | O_EXCL,Perms);
+ free(name);
if (iFd >= 0 && close(iFd) != 0)
Res &= _error->Errno("close",_("Problem closing the file"));
iFd = -1;
if (iFd >= 0 && close(iFd) != 0)
Res &= _error->Errno("close",_("Problem closing the file"));
iFd = -1;
+
+ if ((Flags & Replace) == Replace) {
+ FileName = TemporaryFileName; // for the unlink() below.
+ if (rename(TemporaryFileName.c_str(), FileName.c_str()) != 0)
+ Res &= _error->Errno("rename",_("Problem renaming the file"));
+ }
+
if ((Flags & Fail) == Fail && (Flags & DelOnFail) == DelOnFail &&
FileName.empty() == false)
if (unlink(FileName.c_str()) != 0)
Res &= _error->WarningE("unlnk",_("Problem unlinking the file"));
if ((Flags & Fail) == Fail && (Flags & DelOnFail) == DelOnFail &&
FileName.empty() == false)
if (unlink(FileName.c_str()) != 0)
Res &= _error->WarningE("unlnk",_("Problem unlinking the file"));
int iFd;
enum LocalFlags {AutoClose = (1<<0),Fail = (1<<1),DelOnFail = (1<<2),
int iFd;
enum LocalFlags {AutoClose = (1<<0),Fail = (1<<1),DelOnFail = (1<<2),
+ HitEof = (1<<3), Replace = (1<<4) };
unsigned long Flags;
string FileName;
unsigned long Flags;
string FileName;
+ string TemporaryFileName;
public:
enum OpenMode {ReadOnly,WriteEmpty,WriteExists,WriteAny,WriteTemp};
public:
enum OpenMode {ReadOnly,WriteEmpty,WriteExists,WriteAny,WriteTemp};
- Support matching pins by regular expressions or glob() like patterns,
regular expressions have to be put between to slashes; for example,
/.*/.
- Support matching pins by regular expressions or glob() like patterns,
regular expressions have to be put between to slashes; for example,
/.*/.
+ * apt-pkg/contrib/fileutl.cc:
+ - Make FileFd replace files atomically in WriteTemp mode (for cache, etc).
* debian/control:
- Set Standards-Version to 3.9.0
* debian/control:
- Set Standards-Version to 3.9.0