From 872bd447163066b331eb12c0fed0d71c0585f47b Mon Sep 17 00:00:00 2001
From: David Kalnischkies <david@kalnischkies.de>
Date: Tue, 16 Feb 2016 15:35:36 +0100
Subject: deal with partially downloaded changelogs
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Changelogs are relatively small and we have no hashes for them, but we
had partial support for them before, so lets stick to it.

This also deletes the (partial) file before moving the downloaded file
into its place – rename(2) should be doing this by itself, but testing
on semaphoreci suggests that this isn't always the case (error is "Stale
file handle") and we don't need an atomic replace here, so be explicit.

Git-Dch: Ignore
---
 apt-pkg/acquire-item.cc | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

(limited to 'apt-pkg')

diff --git a/apt-pkg/acquire-item.cc b/apt-pkg/acquire-item.cc
index 4c1d93be1..62d960633 100644
--- a/apt-pkg/acquire-item.cc
+++ b/apt-pkg/acquire-item.cc
@@ -3145,7 +3145,21 @@ void pkgAcqChangelog::Init(std::string const &DestDir, std::string const &DestFi
 
    DestFile = flCombine(TemporaryDirectory, DestFileName);
    if (DestDir.empty() == false)
+   {
       d->FinalFile = flCombine(DestDir, DestFileName);
+      if (RealFileExists(d->FinalFile))
+      {
+	 FileFd file1, file2;
+	 if (file1.Open(DestFile, FileFd::WriteOnly | FileFd::Create | FileFd::Exclusive) &&
+	       file2.Open(d->FinalFile, FileFd::ReadOnly) && CopyFile(file2, file1))
+	 {
+	    struct timeval times[2];
+	    times[0].tv_sec = times[1].tv_sec = file2.ModificationTime();
+	    times[0].tv_usec = times[1].tv_usec = 0;
+	    utimes(DestFile.c_str(), times);
+	 }
+      }
+   }
 
    Desc.ShortDesc = "Changelog";
    strprintf(Desc.Description, "%s %s %s Changelog", URI::SiteOnly(Desc.URI).c_str(), SrcName.c_str(), SrcVersion.c_str());
@@ -3292,7 +3306,6 @@ void pkgAcqChangelog::Failed(string const &Message, pkgAcquire::MethodConfig con
       ErrorText = errText;
    else
       ErrorText = errText + " (" + ErrorText + ")";
-   return;
 }
 									/*}}}*/
 // AcqChangelog::Done - Item downloaded OK				/*{{{*/
@@ -3301,7 +3314,11 @@ void pkgAcqChangelog::Done(string const &Message,HashStringList const &CalcHashe
 {
    Item::Done(Message,CalcHashes,Cnf);
    if (d->FinalFile.empty() == false)
-      Rename(DestFile, d->FinalFile);
+   {
+      if (RemoveFile("pkgAcqChangelog::Done", d->FinalFile) == false ||
+	    Rename(DestFile, d->FinalFile) == false)
+	 Status = StatError;
+   }
 
    Complete = true;
 }
-- 
cgit v1.2.3