fix: ignore possibly functionality breaking files

This commit is contained in:
daimond113 2024-08-11 22:57:42 +02:00
parent 7267dc2300
commit 08e808b4b9
No known key found for this signature in database
GPG key ID: 3A8ECE51328B513C
6 changed files with 85 additions and 25 deletions

View file

@ -18,6 +18,7 @@ use pesde::{
pesde::{IndexFile, IndexFileEntry, ScopeInfo, SCOPE_INFO_FILE}, pesde::{IndexFile, IndexFileEntry, ScopeInfo, SCOPE_INFO_FILE},
specifiers::DependencySpecifiers, specifiers::DependencySpecifiers,
version_id::VersionId, version_id::VersionId,
IGNORED_DIRS, IGNORED_FILES,
}, },
DEFAULT_INDEX_NAME, MANIFEST_FILE_NAME, DEFAULT_INDEX_NAME, MANIFEST_FILE_NAME,
}; };
@ -53,8 +54,7 @@ fn get_refspec(repo: &Repository, remote: &mut Remote) -> Result<String, git2::E
Ok(refspec.to_string()) Ok(refspec.to_string())
} }
const FORBIDDEN_FILES: &[&str] = &[".DS_Store", "default.project.json"]; const ADDITIONAL_FORBIDDEN_FILES: &[&str] = &["default.project.json"];
const FORBIDDEN_DIRECTORIES: &[&str] = &[".git"];
pub async fn publish_package( pub async fn publish_package(
app_state: web::Data<AppState>, app_state: web::Data<AppState>,
@ -85,17 +85,22 @@ pub async fn publish_package(
for entry in entries { for entry in entries {
let mut entry = entry?; let mut entry = entry?;
let path = entry.path()?; let path = entry.path()?;
let path = path.to_str().ok_or(Error::InvalidArchive)?;
if entry.header().entry_type().is_dir() { if entry.header().entry_type().is_dir() {
if FORBIDDEN_DIRECTORIES.contains(&path) { if path.components().next().is_some_and(|ct| {
ct.as_os_str()
.to_str()
.map_or(true, |s| IGNORED_DIRS.contains(&s))
}) {
return Err(Error::InvalidArchive); return Err(Error::InvalidArchive);
} }
continue; continue;
} }
if FORBIDDEN_FILES.contains(&path) { let path = path.to_str().ok_or(Error::InvalidArchive)?;
if IGNORED_FILES.contains(&path) || ADDITIONAL_FORBIDDEN_FILES.contains(&path) {
return Err(Error::InvalidArchive); return Err(Error::InvalidArchive);
} }

View file

@ -12,7 +12,10 @@ use tempfile::tempfile;
use pesde::{ use pesde::{
manifest::target::Target, manifest::target::Target,
scripts::ScriptName, scripts::ScriptName,
source::{pesde::PesdePackageSource, specifiers::DependencySpecifiers, traits::PackageSource}, source::{
pesde::PesdePackageSource, specifiers::DependencySpecifiers, traits::PackageSource,
IGNORED_DIRS, IGNORED_FILES,
},
Project, DEFAULT_INDEX_NAME, MANIFEST_FILE_NAME, Project, DEFAULT_INDEX_NAME, MANIFEST_FILE_NAME,
}; };
@ -98,6 +101,19 @@ impl PublishCommand {
); );
} }
for ignored_path in IGNORED_FILES.iter().chain(IGNORED_DIRS.iter()) {
if manifest.includes.remove(*ignored_path) {
println!(
r#"{}: {ignored_path} was in includes, removing it.
{}: if this was a toolchain manager's manifest file, do not include it due to it possibly messing with user scripts
{}: otherwise, the file was deemed unnecessary, if you don't understand why, please contact the maintainers"#,
"warn".yellow().bold(),
"info".blue().bold(),
"info".blue().bold()
);
}
}
for (name, path) in [("lib path", lib_path), ("bin path", bin_path)] { for (name, path) in [("lib path", lib_path), ("bin path", bin_path)] {
let Some(export_path) = path else { continue }; let Some(export_path) = path else { continue };

View file

@ -10,7 +10,7 @@ use crate::{
fs::{store_in_cas, FSEntry, PackageFS}, fs::{store_in_cas, FSEntry, PackageFS},
git::{pkg_ref::GitPackageRef, specifier::GitDependencySpecifier}, git::{pkg_ref::GitPackageRef, specifier::GitDependencySpecifier},
git_index::GitBasedSource, git_index::GitBasedSource,
PackageSource, ResolveResult, VersionId, PackageSource, ResolveResult, VersionId, IGNORED_DIRS, IGNORED_FILES,
}, },
util::hash, util::hash,
Project, MANIFEST_FILE_NAME, Project, MANIFEST_FILE_NAME,
@ -282,11 +282,23 @@ impl PackageSource for GitPackageSource {
})?; })?;
if matches!(object.kind, gix::object::Kind::Tree) { if matches!(object.kind, gix::object::Kind::Tree) {
if path
.components()
.next()
.is_some_and(|ct| IGNORED_DIRS.contains(&ct.as_str()))
{
continue;
}
entries.insert(path, FSEntry::Directory); entries.insert(path, FSEntry::Directory);
continue; continue;
} }
if IGNORED_FILES.contains(&path.as_str()) {
continue;
}
let data = object.into_blob().data.clone(); let data = object.into_blob().data.clone();
let hash = store_in_cas(project.cas_dir(), &data)?.0; let hash = store_in_cas(project.cas_dir(), &data)?.0;

View file

@ -30,6 +30,12 @@ pub mod version_id;
#[cfg(feature = "wally-compat")] #[cfg(feature = "wally-compat")]
pub mod wally; pub mod wally;
/// Files that will not be stored when downloading a package. These are only files which break pesde's functionality, or are meaningless and possibly heavy (e.g. `.DS_Store`)
pub const IGNORED_FILES: &[&str] = &["foreman.toml", "aftman.toml", "rokit.toml", ".DS_Store"];
/// Directories that will not be stored when downloading a package. These are only directories which break pesde's functionality, or are meaningless and possibly heavy
pub const IGNORED_DIRS: &[&str] = &[".git"];
/// The result of resolving a package /// The result of resolving a package
pub type ResolveResult<Ref> = (PackageNames, BTreeMap<VersionId, Ref>); pub type ResolveResult<Ref> = (PackageNames, BTreeMap<VersionId, Ref>);

View file

@ -1,3 +1,18 @@
use std::{
collections::{BTreeMap, BTreeSet},
fmt::Debug,
hash::Hash,
path::PathBuf,
};
use gix::Url;
use relative_path::RelativePathBuf;
use reqwest::header::ACCEPT;
use serde::{Deserialize, Serialize};
use pkg_ref::PesdePackageRef;
use specifier::PesdeDependencySpecifier;
use crate::{ use crate::{
manifest::{ manifest::{
target::{Target, TargetKind}, target::{Target, TargetKind},
@ -7,23 +22,11 @@ use crate::{
source::{ source::{
fs::{store_reader_in_cas, FSEntry, PackageFS}, fs::{store_reader_in_cas, FSEntry, PackageFS},
git_index::GitBasedSource, git_index::GitBasedSource,
DependencySpecifiers, PackageSource, ResolveResult, VersionId, DependencySpecifiers, PackageSource, ResolveResult, VersionId, IGNORED_DIRS, IGNORED_FILES,
}, },
util::hash, util::hash,
Project, Project,
}; };
use gix::Url;
use pkg_ref::PesdePackageRef;
use relative_path::RelativePathBuf;
use reqwest::header::ACCEPT;
use serde::{Deserialize, Serialize};
use specifier::PesdeDependencySpecifier;
use std::{
collections::{BTreeMap, BTreeSet},
fmt::Debug,
hash::Hash,
path::PathBuf,
};
/// The pesde package reference /// The pesde package reference
pub mod pkg_ref; pub mod pkg_ref;
@ -292,11 +295,23 @@ impl PackageSource for PesdePackageSource {
let path = RelativePathBuf::from_path(entry.path()?).unwrap(); let path = RelativePathBuf::from_path(entry.path()?).unwrap();
if entry.header().entry_type().is_dir() { if entry.header().entry_type().is_dir() {
if path
.components()
.next()
.is_some_and(|ct| IGNORED_DIRS.contains(&ct.as_str()))
{
continue;
}
entries.insert(path, FSEntry::Directory); entries.insert(path, FSEntry::Directory);
continue; continue;
} }
if IGNORED_FILES.contains(&path.as_str()) {
continue;
}
let hash = store_reader_in_cas(project.cas_dir(), &mut entry)?; let hash = store_reader_in_cas(project.cas_dir(), &mut entry)?;
entries.insert(path, FSEntry::File(hash)); entries.insert(path, FSEntry::File(hash));
} }
@ -385,9 +400,10 @@ pub type IndexFile = BTreeMap<VersionId, IndexFileEntry>;
pub mod errors { pub mod errors {
use std::path::PathBuf; use std::path::PathBuf;
use crate::source::git_index::errors::{ReadFile, TreeError};
use thiserror::Error; use thiserror::Error;
use crate::source::git_index::errors::{ReadFile, TreeError};
/// Errors that can occur when resolving a package from a pesde package source /// Errors that can occur when resolving a package from a pesde package source
#[derive(Debug, Error)] #[derive(Debug, Error)]
#[non_exhaustive] #[non_exhaustive]

View file

@ -17,6 +17,7 @@ use crate::{
traits::PackageSource, traits::PackageSource,
version_id::VersionId, version_id::VersionId,
wally::{compat_util::get_target, manifest::WallyManifest, pkg_ref::WallyPackageRef}, wally::{compat_util::get_target, manifest::WallyManifest, pkg_ref::WallyPackageRef},
IGNORED_DIRS, IGNORED_FILES,
}, },
util::hash, util::hash,
Project, Project,
@ -202,17 +203,21 @@ impl PackageSource for WallyPackageSource {
let path = let path =
RelativePathBuf::from_path(entry.path().strip_prefix(tempdir.path())?).unwrap(); RelativePathBuf::from_path(entry.path().strip_prefix(tempdir.path())?).unwrap();
if path == ".git" {
continue;
}
if entry.file_type()?.is_dir() { if entry.file_type()?.is_dir() {
if IGNORED_DIRS.contains(&path.as_str()) {
continue;
}
entries.insert(path, FSEntry::Directory); entries.insert(path, FSEntry::Directory);
dir_entries.extend(std::fs::read_dir(entry.path())?); dir_entries.extend(std::fs::read_dir(entry.path())?);
continue; continue;
} }
if IGNORED_FILES.contains(&path.as_str()) {
continue;
}
let mut file = std::fs::File::open(entry.path())?; let mut file = std::fs::File::open(entry.path())?;
let hash = store_reader_in_cas(project.cas_dir(), &mut file)?; let hash = store_reader_in_cas(project.cas_dir(), &mut file)?;
entries.insert(path, FSEntry::File(hash)); entries.insert(path, FSEntry::File(hash));