From 55367a253e9f1bb81bed316a6e9312caa208b81e Mon Sep 17 00:00:00 2001 From: William Date: Sun, 10 Sep 2023 19:49:04 +0100 Subject: [PATCH] Ignore hidden files in GMA creation --- fastgmad-lib/src/create/mod.rs | 7 ++++++- fastgmad-lib/src/util.rs | 23 ++++++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/fastgmad-lib/src/create/mod.rs b/fastgmad-lib/src/create/mod.rs index 03aae16..cfe98f4 100644 --- a/fastgmad-lib/src/create/mod.rs +++ b/fastgmad-lib/src/create/mod.rs @@ -1,6 +1,6 @@ use crate::{ error::{fastgmad_error, fastgmad_io_error, FastGmadError}, - util::WriteEx, + util::{WriteEx, self}, whitelist, }; use std::{ @@ -182,11 +182,16 @@ fn discover_entries(folder: &Path, ignore: &[String], warn_invalid: bool) -> Res fastgmad_io_error!(while "walking directory", error: std::io::Error::new(std::io::ErrorKind::Other, "unknown"), path: path) } })?; + if !entry.file_type().is_file() { continue; } let path = entry.path(); + if matches!(util::is_hidden_file(path), Ok(true) | Err(_)) { + continue; + } + let relative_path = path .strip_prefix(folder) .map_err(|_| fastgmad_io_error!(error: std::io::Error::new(std::io::ErrorKind::InvalidData, "File not in addon directory"), path: path))? diff --git a/fastgmad-lib/src/util.rs b/fastgmad-lib/src/util.rs index 1820351..8f5f901 100644 --- a/fastgmad-lib/src/util.rs +++ b/fastgmad-lib/src/util.rs @@ -1,4 +1,25 @@ -use std::io::{BufRead, Write}; +use std::{io::{BufRead, Write}, path::Path}; + +pub fn is_hidden_file(path: &Path) -> Result { + let hidden; + + #[cfg(unix)] { + if let Some(file_name) = path.file_name() { + use std::os::unix::prelude::OsStrExt; + hidden = file_name.as_bytes().starts_with(b"."); + } else { + hidden = false; + } + } + + #[cfg(windows)] { + use std::os::windows::fs::MetadataExt; + const HIDDEN: u32 = 0x00000002; + hidden = std::fs::metadata(path)?.file_attributes() & HIDDEN != 0; + } + + Ok(hidden) +} pub trait WriteEx: Write { fn write_nul_str(&mut self, bytes: &[u8]) -> Result<(), std::io::Error>;