Skip to content

Commit ccb6752

Browse files
committed
fixes
1 parent 96c4693 commit ccb6752

File tree

3 files changed

+48
-50
lines changed

3 files changed

+48
-50
lines changed

src/cache.rs

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
//! Functions to deal with the build cache
22
use std::{
33
collections::{BTreeMap, HashSet},
4-
io,
5-
path::Path,
4+
path::{Path, PathBuf},
65
};
76

87
use content_inspector::ContentType;
@@ -14,13 +13,7 @@ use sha2::{Digest, Sha256};
1413
use crate::{
1514
env_vars,
1615
metadata::{Output, build_reindexed_channels},
17-
packaging::{
18-
Files,
19-
metadata::{
20-
contains_prefix_binary, contains_prefix_text,
21-
rewrite_prefix_in_file,
22-
},
23-
},
16+
packaging::{Files, contains_prefix_binary, contains_prefix_text, rewrite_prefix_in_file},
2417
recipe::{
2518
Jinja,
2619
parser::{Dependency, Requirements, Source},
@@ -37,11 +30,15 @@ use crate::{
3730

3831
/// Check if a file contains the prefix and determine if it's binary or text
3932
/// Returns (has_prefix, is_text)
40-
fn check_file_for_prefix(file_path: &Path, prefix: &Path, prefix_str: &str) -> (bool, bool) {
41-
let content_type = content_inspector::inspect_path(file_path);
33+
fn check_file_for_prefix(file_path: &Path, prefix: &Path) -> (bool, bool) {
34+
let content = match fs::read(file_path) {
35+
Ok(content) => content,
36+
Err(_) => return (false, false),
37+
};
38+
let content_type = content_inspector::inspect(&content);
4239
let is_text = content_type.is_text()
4340
&& matches!(content_type, ContentType::UTF_8 | ContentType::UTF_8_BOM);
44-
41+
4542
if is_text {
4643
match contains_prefix_text(file_path, prefix) {
4744
Ok(Some(_)) => (true, true),
@@ -60,15 +57,19 @@ fn check_file_for_prefix(file_path: &Path, prefix: &Path, prefix_str: &str) -> (
6057
{
6158
if let Ok(contents) = fs::read(file_path) {
6259
let prefix_bytes = prefix.to_string_lossy().as_bytes();
63-
(contents.windows(prefix_bytes.len()).any(|window| window == prefix_bytes), false)
60+
(
61+
contents
62+
.windows(prefix_bytes.len())
63+
.any(|window| window == prefix_bytes),
64+
false,
65+
)
6466
} else {
6567
(false, false)
6668
}
6769
}
6870
}
6971
}
7072

71-
7273
/// Error type for cache key generation
7374
#[derive(Debug, thiserror::Error)]
7475
pub enum CacheKeyError {
@@ -314,7 +315,11 @@ impl Output {
314315
// If the cache was built under a different prefix, rewrite occurrences of
315316
// the old prefix in restored text and binary files.
316317
if cache.prefix != *self.prefix() {
317-
for rel in cache.files_with_prefix.iter().chain(cache.binary_files_with_prefix.iter()) {
318+
for rel in cache
319+
.files_with_prefix
320+
.iter()
321+
.chain(cache.binary_files_with_prefix.iter())
322+
{
318323
for base in [
319324
self.prefix(),
320325
&self.build_configuration.directories.work_dir,
@@ -323,13 +328,9 @@ impl Output {
323328
if !path.exists() {
324329
continue;
325330
}
326-
331+
327332
if let Err(e) = rewrite_prefix_in_file(&path, &cache.prefix, self.prefix()) {
328-
tracing::warn!(
329-
"Failed to rewrite restored file {}: {}",
330-
path.display(),
331-
e
332-
);
333+
tracing::warn!("Failed to rewrite restored file {}: {}", path.display(), e);
333334
}
334335
}
335336
}
@@ -345,7 +346,11 @@ impl Output {
345346
if !path.exists() {
346347
continue;
347348
}
348-
if let Err(e) = rewrite_prefix_in_file(&path, &cache.work_dir, &self.build_configuration.directories.work_dir) {
349+
if let Err(e) = rewrite_prefix_in_file(
350+
&path,
351+
&cache.work_dir,
352+
&self.build_configuration.directories.work_dir,
353+
) {
349354
tracing::warn!(
350355
"Failed to rewrite restored work_dir file {}: {}",
351356
path.display(),
@@ -501,7 +506,6 @@ impl Output {
501506
// Track files that contain the old prefix for later path rewriting
502507
let mut files_with_prefix: Vec<PathBuf> = Vec::new();
503508
let mut binary_files_with_prefix: Vec<PathBuf> = Vec::new();
504-
let old_prefix_str = self.prefix().to_string_lossy().to_string();
505509

506510
for file in &new_files.new_files {
507511
if file.is_dir() && !file.is_symlink() {
@@ -511,8 +515,8 @@ impl Output {
511515
let dest = prefix_cache_dir.join(stripped);
512516
copy_file(file, &dest, &mut creation_cache, &copy_options).into_diagnostic()?;
513517
copied_files.push(stripped.to_path_buf());
514-
let (has_prefix, is_text) = check_file_for_prefix(file, self.prefix(), &old_prefix_str);
515-
518+
let (has_prefix, is_text) = check_file_for_prefix(file, self.prefix());
519+
516520
if has_prefix {
517521
match is_text {
518522
true => files_with_prefix.push(stripped.to_path_buf()),
@@ -541,21 +545,16 @@ impl Output {
541545
binary_files_with_prefix,
542546
files_with_work_dir: {
543547
let mut files = Vec::new();
544-
let old_work_dir_str = self
545-
.build_configuration
546-
.directories
547-
.work_dir
548-
.to_string_lossy()
549-
.to_string();
550548
for rel in work_dir_files.copied_paths() {
551549
let abs = self.build_configuration.directories.work_dir.join(rel);
552550
if abs.is_dir() {
553551
continue;
554552
}
555-
match contains_prefix_text(&abs, &self.build_configuration.directories.work_dir) {
553+
match contains_prefix_text(&abs, &self.build_configuration.directories.work_dir)
554+
{
556555
Ok(Some(_)) => files.push(rel.to_path_buf()),
557-
Ok(None) => {},
558-
Err(_) => {},
556+
Ok(None) => {}
557+
Err(_) => {}
559558
}
560559
}
561560
files
@@ -729,7 +728,6 @@ impl Output {
729728
let copy_options = CopyOptions::default();
730729
let mut files_with_prefix: Vec<PathBuf> = Vec::new();
731730
let mut binary_files_with_prefix: Vec<PathBuf> = Vec::new();
732-
let old_prefix_str = self.prefix().to_string_lossy().to_string();
733731

734732
for file in &new_files.new_files {
735733
// skip directories (if they are not a symlink)
@@ -743,7 +741,7 @@ impl Output {
743741
let dest = &prefix_cache_dir.join(stripped);
744742
copy_file(file, dest, &mut creation_cache, &copy_options).into_diagnostic()?;
745743
copied_files.push(stripped.to_path_buf());
746-
let (has_prefix, is_text) = check_file_for_prefix(file, self.prefix(), &old_prefix_str);
744+
let (has_prefix, is_text) = check_file_for_prefix(file, self.prefix());
747745
if has_prefix {
748746
match is_text {
749747
true => files_with_prefix.push(stripped.to_path_buf()),

src/packaging.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ mod file_finder;
2121
mod file_mapper;
2222
mod metadata;
2323
pub use file_finder::{Files, TempFiles, content_type};
24-
pub use metadata::{contains_prefix_binary, contains_prefix_text, create_prefix_placeholder};
24+
pub use metadata::{
25+
contains_prefix_binary, contains_prefix_text, create_prefix_placeholder, rewrite_prefix_in_file,
26+
};
2527
use tempfile::NamedTempFile;
2628

2729
use crate::{

src/packaging/metadata.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -124,40 +124,38 @@ pub fn rewrite_prefix_in_file(
124124
old_prefix: &Path,
125125
new_prefix: &Path,
126126
) -> Result<(), PackagingError> {
127-
let content_type = content_inspector::inspect_path(file_path);
127+
let content = fs::read(file_path)?;
128+
let content_type = content_inspector::inspect(&content);
128129
let is_text = content_type.is_text()
129130
&& matches!(content_type, ContentType::UTF_8 | ContentType::UTF_8_BOM);
130-
131+
131132
match is_text {
132133
true => {
133134
let old_prefix_str = old_prefix.to_string_lossy();
134135
let new_prefix_str = new_prefix.to_string_lossy();
135136
let contents = fs::read_to_string(file_path)?;
136-
let replaced = contents.replace(&old_prefix_str, &new_prefix_str);
137+
let replaced = contents.replace(&*old_prefix_str, &new_prefix_str);
137138
fs::write(file_path, replaced)?;
138139
Ok(())
139140
}
140141
false => {
141142
#[cfg(target_family = "unix")]
142143
{
143144
use std::os::unix::prelude::OsStrExt;
144-
145+
145146
let old_prefix_bytes = old_prefix.as_os_str().as_bytes();
146147
let new_prefix_bytes = new_prefix.as_os_str().as_bytes();
147-
148+
148149
if new_prefix_bytes.len() > old_prefix_bytes.len() {
149150
return Err(PackagingError::IoError(std::io::Error::new(
150151
std::io::ErrorKind::InvalidInput,
151152
"New prefix is longer than old prefix, cannot replace in binary file",
152153
)));
153154
}
154-
155-
let mut file = File::options()
156-
.read(true)
157-
.write(true)
158-
.open(file_path)?;
159-
160-
let mmap = unsafe { memmap2::MmapOptions::new().map_mut(&file) }?;
155+
156+
let file = File::options().read(true).write(true).open(file_path)?;
157+
158+
let mut mmap = unsafe { memmap2::MmapOptions::new().map_mut(&file) }?;
161159
let mut pos = 0;
162160
while pos <= mmap.len().saturating_sub(old_prefix_bytes.len()) {
163161
if &mmap[pos..pos + old_prefix_bytes.len()] == old_prefix_bytes {
@@ -169,7 +167,7 @@ pub fn rewrite_prefix_in_file(
169167
}
170168
pos += 1;
171169
}
172-
170+
173171
Ok(())
174172
}
175173
#[cfg(target_family = "windows")]

0 commit comments

Comments
 (0)