From e795541547877ba6898f1bd31df780f7118d132b Mon Sep 17 00:00:00 2001 From: amokfa <124960413+amokfa@users.noreply.github.com> Date: Mon, 7 Aug 2023 16:50:51 +0530 Subject: [PATCH] fix: create new directories with correct permissions (#265) * create new directories with correct permissions * use create_dir_all on windows --- uplink/src/collector/downloader.rs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/uplink/src/collector/downloader.rs b/uplink/src/collector/downloader.rs index 65da5a949..a36ae0b39 100644 --- a/uplink/src/collector/downloader.rs +++ b/uplink/src/collector/downloader.rs @@ -56,10 +56,11 @@ use serde::{Deserialize, Serialize}; use tokio::time::timeout; use std::collections::HashMap; -use std::fs::{create_dir_all, metadata, remove_dir_all, File}; +use std::fs::{metadata, remove_dir_all, File, Permissions, create_dir, set_permissions, create_dir_all}; use std::sync::Arc; use std::time::Duration; use std::{io::Write, path::PathBuf}; +use std::path::Path; use crate::base::bridge::BridgeTx; use crate::base::DownloaderConfig; @@ -225,12 +226,34 @@ impl FileDownloader { Ok(()) } + #[cfg(unix)] + fn create_dirs_with_perms(&self, path: &Path, perms: Permissions) -> std::io::Result<()> { + let mut current_path = PathBuf::new(); + + for component in path.components() { + current_path.push(component); + + if !current_path.exists() { + create_dir(¤t_path)?; + set_permissions(¤t_path, perms.clone())?; + } + + } + + Ok(()) + } + /// Creates file to download into fn create_file(&self, name: &str, file_name: &str) -> Result<(File, String), Error> { // Ensure that directory for downloading file into, exists let mut download_path = PathBuf::from(self.config.path.clone()); download_path.push(name); - create_dir_all(&download_path)?; + // do manual create_dir_all while setting permissions on each created directory + if cfg!(unix) { + self.create_dirs_with_perms(download_path.as_path(), std::os::unix::fs::PermissionsExt::from_mode(0o777))?; + } else { + create_dir_all(&download_path)?; + } let mut file_path = download_path.to_owned(); file_path.push(file_name);