mirror of
https://codeberg.org/postscriptum/snac2nex.git
synced 2026-03-31 13:15:27 +00:00
init filesystem-dir-permissions option
This commit is contained in:
parent
f9df7c33c9
commit
fc4d0c2b3b
3 changed files with 41 additions and 13 deletions
|
|
@ -67,6 +67,9 @@ snac2nex -s /path/to/snac/storage -t /path/to/nex -u user1 -u user2
|
||||||
--filesystem-sync-time
|
--filesystem-sync-time
|
||||||
Sync file meta with Snac entry time (e.g. time modified)
|
Sync file meta with Snac entry time (e.g. time modified)
|
||||||
|
|
||||||
|
--filesystem-dir-permissions <FILESYSTEM_DIR_PERMISSIONS>
|
||||||
|
Set new directory permissions (macos, linux only)
|
||||||
|
|
||||||
--filesystem-file-permissions <FILESYSTEM_FILE_PERMISSIONS>
|
--filesystem-file-permissions <FILESYSTEM_FILE_PERMISSIONS>
|
||||||
Set new file permissions (macos, linux only)
|
Set new file permissions (macos, linux only)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -52,6 +52,10 @@ pub struct Config {
|
||||||
#[arg(long, default_value_t = false)]
|
#[arg(long, default_value_t = false)]
|
||||||
pub filesystem_sync_time: bool,
|
pub filesystem_sync_time: bool,
|
||||||
|
|
||||||
|
/// Set new directory permissions (macos, linux only)
|
||||||
|
#[arg(long, value_parser = permissions)]
|
||||||
|
pub filesystem_dir_permissions: Option<u32>,
|
||||||
|
|
||||||
/// Set new file permissions (macos, linux only)
|
/// Set new file permissions (macos, linux only)
|
||||||
#[arg(long, value_parser = permissions)]
|
#[arg(long, value_parser = permissions)]
|
||||||
pub filesystem_file_permissions: Option<u32>,
|
pub filesystem_file_permissions: Option<u32>,
|
||||||
|
|
@ -78,7 +82,9 @@ fn test() {
|
||||||
"cmd",
|
"cmd",
|
||||||
"--source=p",
|
"--source=p",
|
||||||
"--target=p",
|
"--target=p",
|
||||||
|
"--filesystem-dir-permissions=0755",
|
||||||
"--filesystem-file-permissions=0644",
|
"--filesystem-file-permissions=0644",
|
||||||
]);
|
]);
|
||||||
|
assert!(a.filesystem_dir_permissions.is_some_and(|p| p == 0o755));
|
||||||
assert!(a.filesystem_file_permissions.is_some_and(|p| p == 0o644))
|
assert!(a.filesystem_file_permissions.is_some_and(|p| p == 0o644))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
45
src/nex.rs
45
src/nex.rs
|
|
@ -18,6 +18,7 @@ use template::Template;
|
||||||
|
|
||||||
pub struct Nex {
|
pub struct Nex {
|
||||||
attachment: Attachment,
|
attachment: Attachment,
|
||||||
|
dir_permissions: Option<Permissions>,
|
||||||
file_permissions: Option<Permissions>,
|
file_permissions: Option<Permissions>,
|
||||||
filename: String,
|
filename: String,
|
||||||
is_cleanup: bool,
|
is_cleanup: bool,
|
||||||
|
|
@ -42,12 +43,18 @@ impl Nex {
|
||||||
if !target.is_dir() {
|
if !target.is_dir() {
|
||||||
bail!("Target location is not directory!")
|
bail!("Target location is not directory!")
|
||||||
}
|
}
|
||||||
|
// init permissions
|
||||||
|
let dir_permissions = permissions(config.filesystem_dir_permissions, 0o755);
|
||||||
|
let file_permissions = permissions(config.filesystem_file_permissions, 0o644);
|
||||||
// init locations for each user
|
// init locations for each user
|
||||||
let mut users = HashMap::with_capacity(config.user.len());
|
let mut users = HashMap::with_capacity(config.user.len());
|
||||||
for u in &config.user {
|
for u in &config.user {
|
||||||
let mut p = PathBuf::from(&target);
|
let mut p = PathBuf::from(&target);
|
||||||
p.push(u);
|
p.push(u);
|
||||||
fs::create_dir_all(&p)?;
|
fs::create_dir_all(&p)?;
|
||||||
|
if let Some(ref permissions) = dir_permissions {
|
||||||
|
fs::set_permissions(&p, permissions.clone())?
|
||||||
|
}
|
||||||
users.insert(u.clone(), p);
|
users.insert(u.clone(), p);
|
||||||
}
|
}
|
||||||
// init document template formatter
|
// init document template formatter
|
||||||
|
|
@ -55,19 +62,8 @@ impl Nex {
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
attachment: Attachment::init(config.attachment.as_ref())?,
|
attachment: Attachment::init(config.attachment.as_ref())?,
|
||||||
file_permissions: {
|
dir_permissions,
|
||||||
#[cfg(any(target_os = "linux", target_os = "macos"))]
|
file_permissions,
|
||||||
{
|
|
||||||
use std::{fs::Permissions, os::unix::fs::PermissionsExt};
|
|
||||||
Some(Permissions::from_mode(
|
|
||||||
config.filesystem_file_permissions.unwrap_or(0o644),
|
|
||||||
))
|
|
||||||
}
|
|
||||||
#[cfg(not(any(target_os = "linux", target_os = "macos",)))]
|
|
||||||
{
|
|
||||||
None // @TODO
|
|
||||||
}
|
|
||||||
},
|
|
||||||
filename: config.format_filename.clone(),
|
filename: config.format_filename.clone(),
|
||||||
is_cleanup: !config.keep,
|
is_cleanup: !config.keep,
|
||||||
is_debug: !config.daemon,
|
is_debug: !config.daemon,
|
||||||
|
|
@ -117,6 +113,9 @@ impl Nex {
|
||||||
}
|
}
|
||||||
|
|
||||||
fs::create_dir_all(&p)?;
|
fs::create_dir_all(&p)?;
|
||||||
|
if let Some(ref permissions) = self.dir_permissions {
|
||||||
|
fs::set_permissions(&p, permissions.clone())?
|
||||||
|
}
|
||||||
|
|
||||||
// init shared post ID once
|
// init shared post ID once
|
||||||
let id = published
|
let id = published
|
||||||
|
|
@ -133,6 +132,9 @@ impl Nex {
|
||||||
let mut d = PathBuf::from(&p);
|
let mut d = PathBuf::from(&p);
|
||||||
d.push(format!(".{id}"));
|
d.push(format!(".{id}"));
|
||||||
fs::create_dir_all(&d)?;
|
fs::create_dir_all(&d)?;
|
||||||
|
if let Some(ref permissions) = self.dir_permissions {
|
||||||
|
fs::set_permissions(&p, permissions.clone())?
|
||||||
|
}
|
||||||
|
|
||||||
// create system meta file to track post time updated
|
// create system meta file to track post time updated
|
||||||
// if this meta file exists and has timestamp changed, also cleanup attachment files to update
|
// if this meta file exists and has timestamp changed, also cleanup attachment files to update
|
||||||
|
|
@ -143,6 +145,9 @@ impl Nex {
|
||||||
if !fs::read_to_string(&s).is_ok_and(|this| this == state) {
|
if !fs::read_to_string(&s).is_ok_and(|this| this == state) {
|
||||||
fs::remove_dir_all(&d)?;
|
fs::remove_dir_all(&d)?;
|
||||||
fs::create_dir_all(&d)?;
|
fs::create_dir_all(&d)?;
|
||||||
|
if let Some(ref permissions) = self.dir_permissions {
|
||||||
|
fs::set_permissions(&p, permissions.clone())?
|
||||||
|
}
|
||||||
self.persist(&s, ×tamp, state.as_bytes())?;
|
self.persist(&s, ×tamp, state.as_bytes())?;
|
||||||
} else {
|
} else {
|
||||||
if let Some(ref mut i) = index {
|
if let Some(ref mut i) = index {
|
||||||
|
|
@ -299,3 +304,17 @@ impl Nex {
|
||||||
Ok(status)
|
Ok(status)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn permissions(config: Option<u32>, default: u32) -> Option<Permissions> {
|
||||||
|
{
|
||||||
|
#[cfg(any(target_os = "linux", target_os = "macos"))]
|
||||||
|
{
|
||||||
|
use std::{fs::Permissions, os::unix::fs::PermissionsExt};
|
||||||
|
Some(Permissions::from_mode(config.unwrap_or(default)))
|
||||||
|
}
|
||||||
|
#[cfg(not(any(target_os = "linux", target_os = "macos",)))]
|
||||||
|
{
|
||||||
|
None // @TODO
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue