summaryrefslogtreecommitdiff
path: root/apt-pkg/contrib
diff options
context:
space:
mode:
authorDavid Kalnischkies <kalnischkies@gmail.com>2010-07-05 12:06:45 +0200
committerDavid Kalnischkies <kalnischkies@gmail.com>2010-07-05 12:06:45 +0200
commit7ea4af9da25a802516461434b12a61e21743f960 (patch)
tree9277fece2f8bd004ffa8f5ca558423de98afa8d8 /apt-pkg/contrib
parent9643d533f38fa35b345755201eebfd227aa5adbb (diff)
parentfd3b761e8cba6ed626639b50b1221246098c7b3a (diff)
merge with debian-experimental-ma
Diffstat (limited to 'apt-pkg/contrib')
-rw-r--r--apt-pkg/contrib/fileutl.cc20
-rw-r--r--apt-pkg/contrib/fileutl.h3
2 files changed, 17 insertions, 6 deletions
diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc
index e95ddf562..62d42e4da 100644
--- a/apt-pkg/contrib/fileutl.cc
+++ b/apt-pkg/contrib/fileutl.cc
@@ -27,6 +27,7 @@
#include <cstdlib>
#include <cstring>
+#include <cstdio>
#include <iostream>
#include <unistd.h>
@@ -658,10 +659,11 @@ bool FileFd::Open(string FileName,OpenMode Mode, unsigned long Perms)
case WriteEmpty:
{
- 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);
break;
}
@@ -843,11 +845,19 @@ bool FileFd::Close()
if (iFd >= 0 && close(iFd) != 0)
Res &= _error->Errno("close",_("Problem closing the file"));
iFd = -1;
-
+
+ if ((Flags & Replace) == Replace) {
+ if (rename(TemporaryFileName.c_str(), FileName.c_str()) != 0)
+ Res &= _error->Errno("rename",_("Problem renaming the file"));
+ FileName = TemporaryFileName; // for the unlink() below.
+ }
+
if ((Flags & Fail) == Fail && (Flags & DelOnFail) == DelOnFail &&
FileName.empty() == false)
if (unlink(FileName.c_str()) != 0)
Res &= _error->WarningE("unlnk",_("Problem unlinking the file"));
+
+
return Res;
}
/*}}}*/
diff --git a/apt-pkg/contrib/fileutl.h b/apt-pkg/contrib/fileutl.h
index 003bd9b83..528725f89 100644
--- a/apt-pkg/contrib/fileutl.h
+++ b/apt-pkg/contrib/fileutl.h
@@ -34,9 +34,10 @@ class FileFd
int iFd;
enum LocalFlags {AutoClose = (1<<0),Fail = (1<<1),DelOnFail = (1<<2),
- HitEof = (1<<3)};
+ HitEof = (1<<3), Replace = (1<<4) };
unsigned long Flags;
string FileName;
+ string TemporaryFileName;
public:
enum OpenMode {ReadOnly,WriteEmpty,WriteExists,WriteAny,WriteTemp};