Skip to content

Commit

Permalink
fix: create new directories with correct permissions (#265)
Browse files Browse the repository at this point in the history
* create new directories with correct permissions

* use create_dir_all on windows
  • Loading branch information
amokfa authored Aug 7, 2023
1 parent c9cd830 commit e795541
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions uplink/src/collector/downloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(&current_path)?;
set_permissions(&current_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);
Expand Down

0 comments on commit e795541

Please sign in to comment.