Skip to content

Commit

Permalink
cli: fix up file mode of generated metatypes
Browse files Browse the repository at this point in the history
Since getting umask is hard, it just copies a file mode from the known good
source.

Stebalien/tempfile#137
  • Loading branch information
yuja committed May 23, 2022
1 parent bce7312 commit 9b09e33
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ fn dump_metatypes(args: &DumpMetatypesArgs) -> Result<(), CommandError> {
});

if let Some(metatypes_path) = &args.output_metatypes {
with_output_file(metatypes_path, |out| {
let perm = fs::metadata(&args.input)
.context("failed to get source file permission")?
.permissions(); // assume this is the default file permissions
with_output_file(metatypes_path, perm, |out| {
serde_json::to_writer_pretty(BufWriter::new(out), &units)
})
.with_context(|| format!("failed to dump metatypes to file: {metatypes_path}"))?;
Expand Down Expand Up @@ -274,7 +277,7 @@ fn print_diagnostic(doc: &qmlast::UiDocument, diag: &Diagnostic) -> io::Result<(
report.eprint(Source::from(doc.source()))
}

fn with_output_file<P, F, E>(path: P, f: F) -> anyhow::Result<()>
fn with_output_file<P, F, E>(path: P, perm: fs::Permissions, f: F) -> anyhow::Result<()>
where
P: AsRef<Path>,
F: FnOnce(&mut NamedTempFile) -> Result<(), E>,
Expand All @@ -284,6 +287,7 @@ where
let mut out =
NamedTempFile::new_in(path.parent().ok_or_else(|| anyhow!("invalid file name"))?)?;
f(&mut out)?;
out.as_file().set_permissions(perm)?;
out.persist(path)?;
Ok(())
}

0 comments on commit 9b09e33

Please sign in to comment.