]> git.saurik.com Git - apt.git/commitdiff
ensure FileFd doesn't try to open /dev/null as atomic and co
authorDavid Kalnischkies <david@kalnischkies.de>
Mon, 2 Nov 2015 15:02:44 +0000 (16:02 +0100)
committerDavid Kalnischkies <david@kalnischkies.de>
Wed, 4 Nov 2015 17:42:28 +0000 (18:42 +0100)
The wrapping will fail in the best case and actually end up deleting
/dev/null in the worst case. Given that there is no point in trying to
write atomically to /dev/null as you can't read from it again just
ignore these flags if higher level code ends up trying to use them on
/dev/null.

Git-Dch: Ignore

apt-pkg/contrib/fileutl.cc
test/integration/test-apt-acquire-additional-files
test/libapt/fileutl_test.cc

index 02b27f5cfd2e05a463b9f93518fcc3698d433b8e..368380af7817837f5c37a3ee8afa1c56be94e2ff 100644 (file)
@@ -1124,16 +1124,20 @@ bool FileFd::Open(string FileName,unsigned int const Mode,APT::Configuration::Co
    if ((Mode & ReadWrite) == 0)
       return FileFdError("No openmode provided in FileFd::Open for %s", FileName.c_str());
 
-   if ((Mode & Atomic) == Atomic)
+   unsigned int OpenMode = Mode;
+   if (FileName == "/dev/null")
+      OpenMode = OpenMode & ~(Atomic | Exclusive | Create | Empty);
+
+   if ((OpenMode & Atomic) == Atomic)
    {
       Flags |= Replace;
    }
-   else if ((Mode & (Exclusive | Create)) == (Exclusive | Create))
+   else if ((OpenMode & (Exclusive | Create)) == (Exclusive | Create))
    {
       // for atomic, this will be done by rename in Close()
       unlink(FileName.c_str());
    }
-   if ((Mode & Empty) == Empty)
+   if ((OpenMode & Empty) == Empty)
    {
       struct stat Buf;
       if (lstat(FileName.c_str(),&Buf) == 0 && S_ISLNK(Buf.st_mode))
@@ -1141,7 +1145,7 @@ bool FileFd::Open(string FileName,unsigned int const Mode,APT::Configuration::Co
    }
 
    int fileflags = 0;
-   #define if_FLAGGED_SET(FLAG, MODE) if ((Mode & FLAG) == FLAG) fileflags |= MODE
+   #define if_FLAGGED_SET(FLAG, MODE) if ((OpenMode & FLAG) == FLAG) fileflags |= MODE
    if_FLAGGED_SET(ReadWrite, O_RDWR);
    else if_FLAGGED_SET(ReadOnly, O_RDONLY);
    else if_FLAGGED_SET(WriteOnly, O_WRONLY);
@@ -1151,7 +1155,7 @@ bool FileFd::Open(string FileName,unsigned int const Mode,APT::Configuration::Co
    if_FLAGGED_SET(Exclusive, O_EXCL);
    #undef if_FLAGGED_SET
 
-   if ((Mode & Atomic) == Atomic)
+   if ((OpenMode & Atomic) == Atomic)
    {
       char *name = strdup((FileName + ".XXXXXX").c_str());
 
@@ -1178,7 +1182,7 @@ bool FileFd::Open(string FileName,unsigned int const Mode,APT::Configuration::Co
       iFd = open(FileName.c_str(), fileflags, AccessMode);
 
    this->FileName = FileName;
-   if (iFd == -1 || OpenInternDescriptor(Mode, compressor) == false)
+   if (iFd == -1 || OpenInternDescriptor(OpenMode, compressor) == false)
    {
       if (iFd != -1)
       {
index 552499ea2d228fe6fbd525a6835c78c8f82263c5..0a9653ce69a6349915d60157fa03f203d2096535 100755 (executable)
@@ -31,6 +31,7 @@ Reading package lists..." aptget update
 
 testempty find rootdir/var/lib/apt/lists -name '*Contents*'
 
+msgmsg "Normal Contents file"
 cat > rootdir/etc/apt/apt.conf.d/content-target.conf <<EOF
 Acquire::IndexTargets::deb::Contents {
        MetaKey "\$(COMPONENT)/Contents-\$(ARCHITECTURE)";
@@ -78,6 +79,7 @@ rm ./rootdir/var/lib/apt/lists/localhost:${APTHTTPPORT}_dists_unstable_main_Cont
 testempty aptget indextargets --format '$(FILENAME)' 'Created-By: Contents'
 
 # if we asked for keeping it compressed, keep it
+msgmsg "Normal Contents file with KeepCompressed"
 echo 'Acquire::IndexTargets::deb::Contents::KeepCompressed "true";' >> rootdir/etc/apt/apt.conf.d/content-target.conf
 testsuccessequal "Hit:1 http://localhost:${APTHTTPPORT} unstable InRelease
 Get:2 http://localhost:${APTHTTPPORT} unstable/main amd64 Contents [$(stat -c%s aptarchive/dists/unstable/main/Contents-amd64.gz) B]
@@ -94,6 +96,7 @@ rm ./rootdir/var/lib/apt/lists/localhost:${APTHTTPPORT}_dists_unstable_main_Cont
 rm ./rootdir/var/lib/apt/lists/localhost:${APTHTTPPORT}_dists_unstable_main_Contents-all.gz
 testempty aptget indextargets --format '$(FILENAME)' 'Created-By: Contents'
 
+msgmsg "Compressed Contents file"
 # and no automatic uncompress based on the name please,
 # only if we downloaded a compressed file, but target was uncompressed
 cat > rootdir/etc/apt/apt.conf.d/content-target.conf <<EOF
@@ -125,6 +128,7 @@ testsuccess cmp "rootdir/var/lib/apt/lists/localhost:${APTHTTPPORT}_dists_unstab
 testsuccess cmp "rootdir/var/lib/apt/lists/localhost:${APTHTTPPORT}_dists_unstable_main_Contents-all.gz" 'aptarchive/dists/unstable/main/Contents-all.gz'
 
 rm -f rootdir/etc/apt/apt.conf.d/content-target.conf
+msgmsg "No Contents file"
 
 testequal "'http://localhost:${APTHTTPPORT}/dists/unstable/InRelease' localhost:${APTHTTPPORT}_dists_unstable_InRelease 0 
 'http://localhost:${APTHTTPPORT}/dists/unstable/main/source/Sources.xz' localhost:${APTHTTPPORT}_dists_unstable_main_source_Sources 0 
index b4226171696f29110dfd71ca231ab05d3065910f..2bd090e5e627858a64f938b1f14a0e14c0e4c309 100644 (file)
@@ -298,3 +298,33 @@ TEST(FileUtlTest, flAbsPath)
    res = chdir(cwd.c_str());
    EXPECT_EQ(res, 0);
 }
+
+static void TestDevNullFileFd(unsigned int const filemode)
+{
+   FileFd f("/dev/null", filemode);
+   EXPECT_FALSE(f.Failed());
+   EXPECT_TRUE(f.IsOpen());
+   EXPECT_TRUE(f.IsOpen());
+
+   std::string test = "This is a test!\n";
+   EXPECT_TRUE(f.Write(test.c_str(), test.size()));
+   EXPECT_TRUE(f.IsOpen());
+   EXPECT_FALSE(f.Failed());
+
+   f.Close();
+   EXPECT_FALSE(f.IsOpen());
+   EXPECT_FALSE(f.Failed());
+}
+TEST(FileUtlTest, WorkingWithDevNull)
+{
+   TestDevNullFileFd(FileFd::WriteOnly | FileFd::Create);
+   TestDevNullFileFd(FileFd::WriteOnly | FileFd::Create | FileFd::Empty);
+   TestDevNullFileFd(FileFd::WriteOnly | FileFd::Create | FileFd::Exclusive);
+   TestDevNullFileFd(FileFd::WriteOnly | FileFd::Atomic);
+   TestDevNullFileFd(FileFd::WriteOnly | FileFd::Create | FileFd::Atomic);
+   // short-hands for ReadWrite with these modes
+   TestDevNullFileFd(FileFd::WriteEmpty);
+   TestDevNullFileFd(FileFd::WriteAny);
+   TestDevNullFileFd(FileFd::WriteTemp);
+   TestDevNullFileFd(FileFd::WriteAtomic);
+}