aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/util/filesystem.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/shirabe/src/util/filesystem.rs')
-rw-r--r--crates/shirabe/src/util/filesystem.rs40
1 files changed, 22 insertions, 18 deletions
diff --git a/crates/shirabe/src/util/filesystem.rs b/crates/shirabe/src/util/filesystem.rs
index 2ef51ba1..b79beeae 100644
--- a/crates/shirabe/src/util/filesystem.rs
+++ b/crates/shirabe/src/util/filesystem.rs
@@ -3,14 +3,14 @@
use crate::util::Platform;
use crate::util::ProcessExecutor;
use crate::util::Silencer;
-use shirabe_pcre::Preg;
use shirabe_php_shim::{
- ErrorException, LogicException, PhpMixed, RuntimeException, array_pop, basename, chdir,
- clearstatcache, clearstatcache2, copy, dirname, explode, fclose, feof, file_exists,
+ ErrorException, LogicException, PhpMixed, PregMatches, RuntimeException, array_pop, basename,
+ chdir, clearstatcache, clearstatcache2, copy, dirname, explode, fclose, feof, file_exists,
file_get_contents, file_put_contents, fileatime, filemtime, filesize, fopen, fread,
function_exists, fwrite, implode, is_dir, is_file, is_link, is_readable, lstat, mkdir,
- php_regex, rename, rmdir, rtrim, str_repeat, str_replace, strlen, strpos, strtoupper, strtr,
- substr, substr_count, symlink, touch, unlink, usleep, var_export,
+ php_regex, preg_match2, preg_replace, preg_replace_callback, rename, rmdir, rtrim, str_repeat,
+ str_replace, strlen, strpos, strtoupper, strtr, substr, substr_count, symlink, touch, unlink,
+ usleep, var_export,
};
use shirabe_symfony_filesystem::exception::IOException;
use shirabe_symfony_finder::Finder;
@@ -246,7 +246,7 @@ impl Filesystem {
return Ok(Some(true));
}
- if Preg::is_match3(php_regex!("{^(?:[a-z]:)?[/\\\\]+$}i"), directory).is_some() {
+ if preg_match2(php_regex!("{^(?:[a-z]:)?[/\\\\]+$}i"), directory, 0).is_some() {
return Err(RuntimeException::new(format!("Aborting an attempted deletion of {}, this was probably not intended, if it is a real use case please report it.", directory))
.into());
}
@@ -578,7 +578,7 @@ impl Filesystem {
let mut common_path = to.clone();
while strpos(&format!("{}/", from), &format!("{}/", common_path)) != Some(0)
&& "/" != common_path
- && Preg::is_match3(php_regex!("{^[A-Z]:/?$}i"), &common_path).is_none()
+ && preg_match2(php_regex!("{^[A-Z]:/?$}i"), &common_path, 0).is_none()
{
common_path = strtr(&dirname(&common_path), "\\", "/");
}
@@ -635,7 +635,7 @@ impl Filesystem {
let mut common_path = to.clone();
while strpos(&format!("{}/", from), &format!("{}/", common_path)) != Some(0)
&& "/" != common_path
- && Preg::is_match3(php_regex!("{^[A-Z]:/?$}i"), &common_path).is_none()
+ && preg_match2(php_regex!("{^[A-Z]:/?$}i"), &common_path, 0).is_none()
&& "." != common_path
{
common_path = strtr(&dirname(&common_path), "\\", "/");
@@ -735,9 +735,10 @@ impl Filesystem {
}
// extract a prefix being a protocol://, protocol:, protocol://drive: or simply drive:
- if let Some(prefix_match) = Preg::is_match3(
+ if let Some(prefix_match) = preg_match2(
php_regex!("{^( [0-9a-z]{2,}+: (?: // (?: [a-z]: )? )? | [a-z]: )}ix"),
&path,
+ 0,
) {
prefix = prefix_match.get(1).unwrap_or_default().to_string();
path = substr(&path, strlen(&prefix), None);
@@ -760,14 +761,15 @@ impl Filesystem {
}
// ensure c: is normalized to C:
- prefix = Preg::replace_callback(
+ prefix = preg_replace_callback(
php_regex!("{(^|://)[a-z]:$}i"),
- |m: &shirabe_pcre::PregMatches| -> String {
+ |m: &PregMatches| -> anyhow::Result<String> {
let s = m.get(0).unwrap_or_default().to_string();
- strtoupper(&s)
+ Ok(strtoupper(&s))
},
&prefix,
- );
+ )
+ .expect("the replacement callback cannot fail");
format!("{}{}{}", prefix, absolute, implode("/", &parts))
}
@@ -777,7 +779,7 @@ impl Filesystem {
/// And other possible unforeseen disasters, see https://github.com/composer/composer/pull/9422
pub fn trim_trailing_slash(path: &str) -> String {
let mut path = path.to_string();
- if Preg::is_match3(php_regex!("{^[/\\\\]+$}"), &path).is_none() {
+ if preg_match2(php_regex!("{^[/\\\\]+$}"), &path, 0).is_none() {
path = rtrim(&path, Some("/\\"));
}
@@ -789,18 +791,20 @@ impl Filesystem {
// on windows, \\foo indicates network paths so we exclude those from local paths, however it is unsafe
// on linux as file:////foo (which would be a network path \\foo on windows) will resolve to /foo which could be a local path
if Platform::is_windows() {
- return Preg::is_match3(
+ return preg_match2(
php_regex!(
"{^(file://(?!//)|/(?!/)|/?[a-z]:[\\\\/]|\\.\\.[\\\\/]|[a-z0-9_.-]+[\\\\/])}i"
),
path,
+ 0,
)
.is_some();
}
- Preg::is_match3(
+ preg_match2(
php_regex!("{^(file://|/|/?[a-z]:[\\\\/]|\\.\\.[\\\\/]|[a-z0-9_.-]+[\\\\/])}i"),
path,
+ 0,
)
.is_some()
}
@@ -808,14 +812,14 @@ impl Filesystem {
pub fn get_platform_path(path: &str) -> String {
let mut path = path.to_string();
if Platform::is_windows() {
- path = Preg::replace(
+ path = preg_replace(
php_regex!("{^(?:file:///([a-z]):?/)}i"),
"file://$1:/",
&path,
);
}
- Preg::replace(php_regex!("{^file://}i"), "", &path)
+ preg_replace(php_regex!("{^file://}i"), "", &path)
}
/// Cross-platform safe version of is_readable()