]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/contrib/fileutl.cc
* apt-pkg/deb/dpkgpm.cc:
[apt.git] / apt-pkg / contrib / fileutl.cc
index 16f7ce929ca0598b10580cc08f3f3c0768f72c4b..62d42e4da8abe8e84a6706273e13a9da6fac22df 100644 (file)
@@ -27,6 +27,7 @@
 
 #include <cstdlib>
 #include <cstring>
+#include <cstdio>
 
 #include <iostream>
 #include <unistd.h>
@@ -281,6 +282,7 @@ std::vector<string> GetListOfFilesInDir(string const &Dir, std::vector<string> c
    }
 
    std::vector<string> List;
+   Configuration::MatchAgainstConfig SilentIgnore("Dir::Ignore-Files-Silently");
    DIR *D = opendir(Dir.c_str());
    if (D == 0) 
    {
@@ -306,6 +308,7 @@ std::vector<string> GetListOfFilesInDir(string const &Dir, std::vector<string> c
            {
               if (Debug == true)
                  std::clog << "Bad file: " << Ent->d_name << " → no extension" << std::endl;
+              _error->Notice("Ignoring file '%s' in directory '%s' as it has no filename extension", Ent->d_name, Dir.c_str());
               continue;
            }
         }
@@ -313,6 +316,8 @@ std::vector<string> GetListOfFilesInDir(string const &Dir, std::vector<string> c
         {
            if (Debug == true)
               std::clog << "Bad file: " << Ent->d_name << " → bad extension »" << flExtension(Ent->d_name) << "«" << std::endl;
+           if (SilentIgnore.Match(Ent->d_name) == false)
+              _error->Notice("Ignoring file '%s' in directory '%s' as it has an invalid filename extension", Ent->d_name, Dir.c_str());
            continue;
         }
       }
@@ -654,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;
       }
       
@@ -839,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;
 }
                                                                        /*}}}*/