[pbs-devel] [PATCH backup v2 1/2] use truncate whenever we create files
Maximiliano Sandoval
m.sandoval at proxmox.com
Mon Jan 27 10:24:39 CET 2025
Fixes the suspicious_open_options clippy lint, for example:
```
warning: file opened with `create`, but `truncate` behavior not defined
--> src/api2/tape/restore.rs:1713:18
|
1713 | .create(true)
| ^^^^^^^^^^^^- help: add: `.truncate(true)`
|
= help: if you intend to overwrite an existing file entirely, call `.truncate(true)`
= help: if you instead know that you may want to keep some parts of the old file, call `.truncate(false)`
= help: alternatively, use `.append(true)` to append to the file instead of overwriting it
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#suspicious_open_options
```
Signed-off-by: Maximiliano Sandoval <m.sandoval at proxmox.com>
---
Differences from v1:
- Do not add truncate/create when create_new is used
pbs-client/src/pxar/extract.rs | 1 +
src/api2/tape/restore.rs | 1 +
src/server/sync.rs | 1 +
3 files changed, 3 insertions(+)
diff --git a/pbs-client/src/pxar/extract.rs b/pbs-client/src/pxar/extract.rs
index 64754006..1f017d81 100644
--- a/pbs-client/src/pxar/extract.rs
+++ b/pbs-client/src/pxar/extract.rs
@@ -135,6 +135,7 @@ where
if let Some(entry) = prelude {
let mut prelude_file = OpenOptions::new()
.create(true)
+ .truncate(true)
.write(true)
.open(path)
.with_context(|| format!("error creating prelude file '{path:?}'"))?;
diff --git a/src/api2/tape/restore.rs b/src/api2/tape/restore.rs
index 93a6053b..2cc1baab 100644
--- a/src/api2/tape/restore.rs
+++ b/src/api2/tape/restore.rs
@@ -1711,6 +1711,7 @@ fn try_restore_snapshot_archive<R: pxar::decoder::SeqRead>(
let mut tmpfile = std::fs::OpenOptions::new()
.write(true)
.create(true)
+ .truncate(true)
.read(true)
.open(&tmp_path)
.map_err(|err| format_err!("restore {:?} failed - {}", tmp_path, err))?;
diff --git a/src/server/sync.rs b/src/server/sync.rs
index 5e3fbdcd..4dd46c5a 100644
--- a/src/server/sync.rs
+++ b/src/server/sync.rs
@@ -159,6 +159,7 @@ impl SyncSourceReader for RemoteSourceReader {
let tmpfile = std::fs::OpenOptions::new()
.write(true)
.create(true)
+ .truncate(true)
.read(true)
.open(&tmp_path)?;
--
2.39.5
More information about the pbs-devel
mailing list