Skip to content

Commit fd0a9c0

Browse files
authored
CLI: skip setting file mode for Windows (#1591)
When the CLI creates a config file for the user, we use `600` unix permissions to ensure only the user can read/write the file. Setting the file mode like this is only possible like this on unix-like systems so this breaks the build for Windows. This diff splits the opening of the file into platform-specific functions, one for `windows` which doesn't set the file mode. The `unix` flavored function does what we had in the first place. In <https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfile2> there's mention of ACL being inherited from the parent directory. My understanding is `%APPDATA%` should only be accessible by the named user, so I'm hoping the defaults are correct for our use case.
2 parents 78fe3a1 + 894985e commit fd0a9c0

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

svix-cli/src/config.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{
2+
fs::{File, OpenOptions},
23
io::Write,
3-
os::unix::fs::OpenOptionsExt,
44
path::{Path, PathBuf},
55
};
66

@@ -30,6 +30,26 @@ pub struct Config {
3030
pub relay_disable_security: Option<bool>,
3131
}
3232

33+
fn config_file_open_opts() -> OpenOptions {
34+
OpenOptions::new()
35+
.create(true)
36+
.truncate(true)
37+
.write(true)
38+
.to_owned()
39+
}
40+
41+
#[cfg(windows)]
42+
fn open_config_file(path: &Path) -> Result<File> {
43+
Ok(config_file_open_opts().open(path)?)
44+
}
45+
46+
#[cfg(unix)]
47+
fn open_config_file(path: &Path) -> Result<File> {
48+
use std::os::unix::fs::OpenOptionsExt;
49+
const FILE_MODE: u32 = 0o600;
50+
Ok(config_file_open_opts().mode(FILE_MODE).open(path)?)
51+
}
52+
3353
impl Config {
3454
pub fn load() -> Result<Config> {
3555
let cfg_file = get_folder()?.join(FILE_NAME);
@@ -41,12 +61,7 @@ impl Config {
4161
}
4262

4363
pub fn save_to_disk(&self, path: &Path) -> Result<()> {
44-
let mut fh = std::fs::OpenOptions::new()
45-
.create(true)
46-
.truncate(true)
47-
.write(true)
48-
.mode(FILE_MODE)
49-
.open(path)?;
64+
let mut fh = open_config_file(path)?;
5065

5166
let source = &toml::to_string_pretty(self)?;
5267
fh.write_all(source.as_bytes())?;
@@ -64,7 +79,6 @@ impl Config {
6479
}
6580

6681
const FILE_NAME: &str = "config.toml";
67-
const FILE_MODE: u32 = 0o600;
6882

6983
fn get_folder() -> Result<PathBuf> {
7084
let config_path = if cfg!(windows) {

0 commit comments

Comments
 (0)