]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/contrib/fileutl.cc
give a descriptive error for pipe tries with 'false'
[apt.git] / apt-pkg / contrib / fileutl.cc
index cde005eb52bc674bfdeff6efaa6c883e52ca9bcb..c623f1a2a5bbac58d2b6c264d04018f76db7b5e4 100644 (file)
@@ -1535,7 +1535,7 @@ public:
         return false;
 
       unsigned int flags = (Mode & (FileFd::WriteOnly|FileFd::ReadOnly));
-      if (backend.OpenDescriptor(iFd, flags) == false)
+      if (backend.OpenDescriptor(iFd, flags, FileFd::None, true) == false)
         return false;
 
       // Write the file header
@@ -1626,11 +1626,14 @@ public:
 
       if (cctx != nullptr)
       {
-        res = LZ4F_compressEnd(cctx, lz4_buffer.buffer, lz4_buffer.buffersize_max, nullptr);
-        if (LZ4F_isError(res) || backend.Write(lz4_buffer.buffer, res) == false)
-           return false;
-        if (!backend.Flush())
-           return false;
+        if (filefd->Failed() == false)
+        {
+           res = LZ4F_compressEnd(cctx, lz4_buffer.buffer, lz4_buffer.buffersize_max, nullptr);
+           if (LZ4F_isError(res) || backend.Write(lz4_buffer.buffer, res) == false)
+              return false;
+           if (!backend.Flush())
+              return false;
+        }
         if (!backend.Close())
            return false;
 
@@ -1643,6 +1646,11 @@ public:
         res = LZ4F_freeDecompressionContext(dctx);
         dctx = nullptr;
       }
+      if (backend.IsOpen())
+      {
+        backend.Close();
+        filefd->iFd = -1;
+      }
 
       return LZ4F_isError(res) == false;
    }
@@ -1658,16 +1666,17 @@ class APT_HIDDEN LzmaFileFdPrivate: public FileFdPrivate {                              /*{{{*/
 #ifdef HAVE_LZMA
    struct LZMAFILE {
       FILE* file;
+      FileFd * const filefd;
       uint8_t buffer[4096];
       lzma_stream stream;
       lzma_ret err;
       bool eof;
       bool compressing;
 
-      LZMAFILE() : file(nullptr), eof(false), compressing(false) { buffer[0] = '\0'; }
+      LZMAFILE(FileFd * const fd) : file(nullptr), filefd(fd), eof(false), compressing(false) { buffer[0] = '\0'; }
       ~LZMAFILE()
       {
-        if (compressing == true)
+        if (compressing == true && filefd->Failed() == false)
         {
            size_t constexpr buffersize = sizeof(buffer)/sizeof(buffer[0]);
            while(true)
@@ -1728,7 +1737,7 @@ public:
         return filefd->FileFdError("ReadWrite mode is not supported for lzma/xz files %s", filefd->FileName.c_str());
 
       if (lzma == nullptr)
-        lzma = new LzmaFileFdPrivate::LZMAFILE;
+        lzma = new LzmaFileFdPrivate::LZMAFILE(filefd);
       if ((Mode & FileFd::WriteOnly) == FileFd::WriteOnly)
         lzma->file = fdopen(iFd, "w");
       else
@@ -1873,6 +1882,9 @@ public:
 
       if ((Mode & FileFd::ReadWrite) == FileFd::ReadWrite)
         return filefd->FileFdError("ReadWrite mode is not supported for file %s", filefd->FileName.c_str());
+      if (compressor.Binary == "false")
+        return filefd->FileFdError("libapt has inbuilt support for the %s compression,"
+              " but was forced to ignore it in favor of an external binary – which isn't installed.", compressor.Name.c_str());
 
       bool const Comp = (Mode & FileFd::WriteOnly) == FileFd::WriteOnly;
       if (Comp == false)
@@ -1974,6 +1986,11 @@ public:
    virtual bool InternalClose(std::string const &) APT_OVERRIDE
    {
       bool Ret = true;
+      if (filefd->iFd != -1)
+      {
+        close(filefd->iFd);
+        filefd->iFd = -1;
+      }
       if (compressor_pid > 0)
         Ret &= ExecWait(compressor_pid, "FileFdCompressor", true);
       compressor_pid = -1;
@@ -2589,7 +2606,7 @@ unsigned long long FileFd::Size()
 /* */
 bool FileFd::Close()
 {
-   if (Flush() == false)
+   if (Failed() == false && Flush() == false)
       return false;
    if (iFd == -1)
       return true;
@@ -2609,7 +2626,7 @@ bool FileFd::Close()
    }
 
    if ((Flags & Replace) == Replace) {
-      if (rename(TemporaryFileName.c_str(), FileName.c_str()) != 0)
+      if (Failed() == false && rename(TemporaryFileName.c_str(), FileName.c_str()) != 0)
         Res &= _error->Errno("rename",_("Problem renaming the file %s to %s"), TemporaryFileName.c_str(), FileName.c_str());
 
       FileName = TemporaryFileName; // for the unlink() below.