aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/util
diff options
context:
space:
mode:
Diffstat (limited to 'crates/shirabe/src/util')
-rw-r--r--crates/shirabe/src/util/error_handler.rs2
-rw-r--r--crates/shirabe/src/util/perforce.rs30
-rw-r--r--crates/shirabe/src/util/remote_filesystem.rs6
-rw-r--r--crates/shirabe/src/util/tar.rs17
4 files changed, 32 insertions, 23 deletions
diff --git a/crates/shirabe/src/util/error_handler.rs b/crates/shirabe/src/util/error_handler.rs
index 7a9cf9c2..2e574fe0 100644
--- a/crates/shirabe/src/util/error_handler.rs
+++ b/crates/shirabe/src/util/error_handler.rs
@@ -125,7 +125,7 @@ impl ErrorHandler {
}
if output_even_without_io {
- fwrite(&STDERR, &format!("Warning: {}{}", message, PHP_EOL), None);
+ fwrite(&STDERR, format!("Warning: {}{}", message, PHP_EOL), None);
}
}
}
diff --git a/crates/shirabe/src/util/perforce.rs b/crates/shirabe/src/util/perforce.rs
index fa448b35..9cd804a4 100644
--- a/crates/shirabe/src/util/perforce.rs
+++ b/crates/shirabe/src/util/perforce.rs
@@ -417,12 +417,12 @@ impl Perforce {
pub fn write_client_spec_to_file(&mut self, spec: &PhpResource) {
fwrite(
spec,
- &format!("Client: {}{}{}", self.get_client(), PHP_EOL, PHP_EOL),
+ format!("Client: {}{}{}", self.get_client(), PHP_EOL, PHP_EOL),
None,
);
fwrite(
spec,
- &format!(
+ format!(
"Update: {}{}{}",
date("Y/m/d H:i:s", None),
PHP_EOL,
@@ -432,12 +432,12 @@ impl Perforce {
);
fwrite(
spec,
- &format!("Access: {}{}", date("Y/m/d H:i:s", None), PHP_EOL),
+ format!("Access: {}{}", date("Y/m/d H:i:s", None), PHP_EOL),
None,
);
fwrite(
spec,
- &format!(
+ format!(
"Owner: {}{}{}",
self.get_user().unwrap_or_default(),
PHP_EOL,
@@ -445,10 +445,10 @@ impl Perforce {
),
None,
);
- fwrite(spec, &format!("Description:{}", PHP_EOL), None);
+ fwrite(spec, format!("Description:{}", PHP_EOL), None);
fwrite(
spec,
- &format!(
+ format!(
" Created by {} from composer.{}{}",
self.get_user().unwrap_or_default(),
PHP_EOL,
@@ -458,12 +458,12 @@ impl Perforce {
);
fwrite(
spec,
- &format!("Root: {}{}{}", self.get_path(), PHP_EOL, PHP_EOL),
+ format!("Root: {}{}{}", self.get_path(), PHP_EOL, PHP_EOL),
None,
);
fwrite(
spec,
- &format!(
+ format!(
"Options: noallwrite noclobber nocompress unlocked modtime rmdir{}{}",
PHP_EOL, PHP_EOL
),
@@ -471,20 +471,16 @@ impl Perforce {
);
fwrite(
spec,
- &format!("SubmitOptions: revertunchanged{}{}", PHP_EOL, PHP_EOL),
- None,
- );
- fwrite(
- spec,
- &format!("LineEnd: local{}{}", PHP_EOL, PHP_EOL),
+ format!("SubmitOptions: revertunchanged{}{}", PHP_EOL, PHP_EOL),
None,
);
+ fwrite(spec, format!("LineEnd: local{}{}", PHP_EOL, PHP_EOL), None);
if self.is_stream() {
- fwrite(spec, &format!("Stream:{}", PHP_EOL), None);
+ fwrite(spec, format!("Stream:{}", PHP_EOL), None);
let stream_clone = self.p4_stream.clone().unwrap_or_default();
fwrite(
spec,
- &format!(
+ format!(
" {}{}",
self.get_stream_without_label(&stream_clone),
PHP_EOL
@@ -496,7 +492,7 @@ impl Perforce {
let client = self.get_client();
fwrite(
spec,
- &format!("View: {}/... //{}/... {}", stream, client, PHP_EOL),
+ format!("View: {}/... //{}/... {}", stream, client, PHP_EOL),
None,
);
}
diff --git a/crates/shirabe/src/util/remote_filesystem.rs b/crates/shirabe/src/util/remote_filesystem.rs
index a244c9f9..2dafc0d3 100644
--- a/crates/shirabe/src/util/remote_filesystem.rs
+++ b/crates/shirabe/src/util/remote_filesystem.rs
@@ -1036,10 +1036,12 @@ impl RemoteFilesystem {
.unwrap_or(false);
if decode {
- let decoded = zlib_decode(result.as_deref().unwrap_or(""));
+ let decoded = zlib_decode(result.as_deref().unwrap_or("").as_bytes());
result = match decoded {
- Some(d) => Some(d),
+ // TODO(phase-e): byte-string semantics — the response body travels through
+ // RemoteFilesystem as a String; from_utf8_lossy can corrupt binary payloads
+ Some(d) => Some(String::from_utf8_lossy(&d).into_owned()),
None => {
return Err(anyhow::anyhow!(TransportException::new(
"Failed to decode zlib stream".to_string(),
diff --git a/crates/shirabe/src/util/tar.rs b/crates/shirabe/src/util/tar.rs
index 55b6a960..792d1c4f 100644
--- a/crates/shirabe/src/util/tar.rs
+++ b/crates/shirabe/src/util/tar.rs
@@ -7,7 +7,7 @@ pub struct Tar;
impl Tar {
pub fn get_composer_json(path_to_archive: &str) -> anyhow::Result<Option<String>> {
- let phar = PharData::new(path_to_archive.to_string());
+ let phar = PharData::new(path_to_archive.to_string())?;
if !phar.valid() {
return Ok(None);
@@ -16,9 +16,20 @@ impl Tar {
Ok(Some(Self::extract_composer_json_from_folder(&phar)?))
}
+ /// The content bytes are decoded strictly: a composer.json that is not valid
+ /// UTF-8 could never survive the JSON parsing that follows in PHP either.
+ fn content_to_string(content: Vec<u8>) -> anyhow::Result<String> {
+ String::from_utf8(content).map_err(|_| {
+ anyhow::anyhow!(RuntimeException {
+ message: "composer.json in the archive is not valid UTF-8".to_string(),
+ code: 0,
+ })
+ })
+ }
+
fn extract_composer_json_from_folder(phar: &PharData) -> anyhow::Result<String> {
if let Some(file) = phar.get("composer.json") {
- return Ok(file.get_content());
+ return Self::content_to_string(file.get_content());
}
let mut top_level_paths: IndexMap<String, bool> = IndexMap::new();
@@ -49,7 +60,7 @@ impl Tar {
if !top_level_paths.is_empty()
&& let Some(file) = phar.get(&composer_json_path)
{
- return Ok(file.get_content());
+ return Self::content_to_string(file.get_content());
}
Err(anyhow::anyhow!(RuntimeException {