[librsvg/rustify-rsvg-convert] rsvg-convert: Overwrite existing files by default



commit dac42df6d0d8d7b9b190b63231047ae2efd37dcb
Author: Federico Mena Quintero <federico gnome org>
Date:   Mon Jan 25 14:48:47 2021 -0600

    rsvg-convert: Overwrite existing files by default
    
    This is the same behavior as rsvg-convert.c, which used just
    fopen(name, "wb").
    
    Since here we use GFile, we want file.replace() instead of
    file.create() - the latter uses O_EXCL and returns an error if the
    output file already exists.
    
    I don't think we want FileCreateFlags::REPLACE_DESTINATION, since that
    one (for example) will remove a symlink and replace it with an actual
    file.  However, fopen() as above wouldn't do that.  Hence, this uses
    FileCreateFlags::NONE.

 src/bin/rsvg-convert.rs           |  2 +-
 tests/src/cmdline/rsvg_convert.rs | 21 +++++++++++++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)
---
diff --git a/src/bin/rsvg-convert.rs b/src/bin/rsvg-convert.rs
index 7df81ef6..ba402ac7 100644
--- a/src/bin/rsvg-convert.rs
+++ b/src/bin/rsvg-convert.rs
@@ -434,7 +434,7 @@ impl Converter {
             Output::Path(ref p) => {
                 let file = gio::File::new_for_path(p);
                 let stream = file
-                    .create(FileCreateFlags::NONE, None::<&Cancellable>)
+                    .replace(None, false, FileCreateFlags::NONE, None::<&Cancellable>)
                     .unwrap_or_else(|e| exit!("Error opening output \"{}\": {}", self.output, e));
                 stream.upcast::<OutputStream>()
             }
diff --git a/tests/src/cmdline/rsvg_convert.rs b/tests/src/cmdline/rsvg_convert.rs
index d63e3d02..578d46d6 100644
--- a/tests/src/cmdline/rsvg_convert.rs
+++ b/tests/src/cmdline/rsvg_convert.rs
@@ -189,6 +189,27 @@ fn output_file_short_option() {
     std::fs::remove_file(&output).unwrap();
 }
 
+#[test]
+fn overwrites_existing_output_file() {
+    let output = {
+        let tempfile = Builder::new().suffix(".png").tempfile().unwrap();
+        tempfile.path().to_path_buf()
+    };
+    assert!(predicates::path::is_file().not().eval(&output));
+
+    for _ in 0..2 {
+        RsvgConvert::new_with_input("tests/fixtures/dimensions/521-with-viewbox.svg")
+            .arg(format!("--output={}", output.display()))
+            .assert()
+            .success()
+            .stdout(is_empty());
+
+        assert!(predicates::path::is_file().eval(&output));
+    }
+
+    std::fs::remove_file(&output).unwrap();
+}
+
 #[test]
 fn empty_input_yields_error() {
     let starts_with = starts_with("Error reading SVG");


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]