From c14ace0d68de0b31d8c8cd986a4911384ef3fd3f Mon Sep 17 00:00:00 2001 From: Kenneth Hsu Date: Tue, 20 Apr 2021 16:05:35 -0700 Subject: [PATCH] Make File backend truncate existing files when overwriting. The implementations of ISystemIO.FileOpenWrite call File.OpenWrite, which does not truncate the existing file. As such, if we overwrite a larger file with a smaller one, tail remnants of the existing file will remain. Since most of the put operations involve unique filenames, this is only a problem when overwriting existing files (e.g., duplicati-verification.json). This fixes a regression introduced in revision 71c6aca8a7 ("Fixed IO calls in Filebackend") from pull request #3456. The previous implementation called File.Open with FileMode.Create, which truncates existing files. This fixes #4486. --- Duplicati/Library/Backend/File/FileBackend.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Duplicati/Library/Backend/File/FileBackend.cs b/Duplicati/Library/Backend/File/FileBackend.cs index 1dd9752cd..3b41484c9 100644 --- a/Duplicati/Library/Backend/File/FileBackend.cs +++ b/Duplicati/Library/Backend/File/FileBackend.cs @@ -187,7 +187,7 @@ namespace Duplicati.Library.Backend private static Random random = new Random(); public async Task Put(string remotename, System.IO.Stream stream, CancellationToken cancelToken) { - using(System.IO.FileStream writestream = systemIO.FileOpenWrite(GetRemoteName(remotename))) + using(System.IO.FileStream writestream = systemIO.FileCreate(GetRemoteName(remotename))) { if (random.NextDouble() > 0.6666) throw new Exception("Random upload failure"); @@ -197,7 +197,7 @@ namespace Duplicati.Library.Backend #else public async Task PutAsync(string remotename, System.IO.Stream stream, CancellationToken cancelToken) { - using (System.IO.FileStream writestream = systemIO.FileOpenWrite(GetRemoteName(remotename))) + using (System.IO.FileStream writestream = systemIO.FileCreate(GetRemoteName(remotename))) await Utility.Utility.CopyStreamAsync(stream, writestream, true, cancelToken, m_copybuffer); } #endif