Skip to content

Commit 44259aa

Browse files
committed
fix: csspell - Rename Mmap to FileRead, use memory-map in comment and alias Mmap as MemoryMap
1 parent df1037c commit 44259aa

2 files changed

Lines changed: 32 additions & 30 deletions

File tree

src/uu/sort/src/merge.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use std::{
2828
};
2929

3030
use compare::Compare;
31-
use memmap2::Mmap;
31+
use memmap2::Mmap as MemoryMap;
3232
use uucore::error::{FromIo, UResult};
3333

3434
use crate::{
@@ -76,9 +76,9 @@ pub fn merge(
7676
output: Output,
7777
tmp_dir: &mut TmpDirWrapper,
7878
) -> UResult<()> {
79-
// If the output file is also listed as an input, pre-mmap it before
79+
// If the output file is also listed as an input, Use memory-map to load it before
8080
// it gets opened for writing. This allows reading the original content
81-
// via mmap while writing to the same file, without needing a temp copy.
81+
// via memory-map while writing to the same file, without needing a temp copy.
8282
let output_as_input = if let Some(name) = output.as_output_name() {
8383
let output_path = Path::new(name).canonicalize()?;
8484
let appears = files
@@ -89,16 +89,17 @@ pub fn merge(
8989
path: output_path.clone(),
9090
error,
9191
})?;
92-
// SAFETY: We keep the read_fd open for the lifetime of the mmap,
92+
// SAFETY: We keep the read_fd open for the lifetime of the memory-map,
9393
// and we only read from it. The file is not modified while the
94-
// mmap exists (writing happens later via a separate FD).
95-
let mmap = Arc::new(unsafe { Mmap::map(&read_fd) }.map_err(|error| {
96-
SortError::ReadFailed {
97-
path: output_path.clone(),
98-
error,
99-
}
100-
})?);
101-
Some((output_path, mmap))
94+
// memory-map exists (writing happens later via a separate FD).
95+
let output_as_input =
96+
Arc::new(unsafe { MemoryMap::map(&read_fd) }.map_err(|error| {
97+
SortError::ReadFailed {
98+
path: output_path.clone(),
99+
error,
100+
}
101+
})?);
102+
Some((output_path, output_as_input))
102103
} else {
103104
None
104105
}
@@ -640,10 +641,11 @@ mod tests {
640641
// Check Opened SortInputs: 7 inputs but only 6 unique sources
641642
let output_canon = out.path().canonicalize().unwrap();
642643
let read_fd = File::open(out.path()).unwrap();
643-
let output_mmap = Arc::new(unsafe { Mmap::map(&read_fd).unwrap() });
644+
let output_as_input = Arc::new(unsafe { MemoryMap::map(&read_fd).unwrap() });
644645

645646
let sort_inputs =
646-
SortInputs::from_files_with_output(&files, Some((output_canon, output_mmap))).unwrap();
647+
SortInputs::from_files_with_output(&files, Some((output_canon, output_as_input)))
648+
.unwrap();
647649

648650
assert_eq!(sort_inputs.len(), 7);
649651
assert_eq!(sort_inputs.unique_count(), 6);
@@ -656,7 +658,7 @@ mod tests {
656658

657659
merge(&mut files_mut, &settings, output, &mut tmp_dir).unwrap();
658660

659-
// If merge succeeded with only 6 unique sources, mmap deduplication worked.
661+
// If merge succeeded with only 6 unique sources, memory-map deduplication worked.
660662
// Verify correctness.
661663
let result = fs::read_to_string(out.path()).unwrap();
662664
assert_eq!(result, "1\n2\n3\n4\n5\n6\n6\n");

src/uu/sort/src/sort_input.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use std::{
1919
},
2020
};
2121

22-
use memmap2::Mmap;
22+
use memmap2::Mmap as MemoryMap;
2323
use uucore::error::UResult;
2424

2525
use crate::{STDIN_FILE, SortError};
@@ -31,8 +31,8 @@ enum SortInputInner {
3131
File(File),
3232
/// A memory-mapped file shared across duplicate paths, with an
3333
/// independent cursor per instance.
34-
Mmap {
35-
data: Arc<Mmap>,
34+
FileRead {
35+
data: Arc<MemoryMap>,
3636
offset: AtomicUsize,
3737
},
3838
Stdin,
@@ -68,9 +68,9 @@ impl SortInput {
6868
}
6969
}
7070

71-
fn from_mmap(data: Arc<Mmap>) -> Self {
71+
fn from_mmap(data: Arc<MemoryMap>) -> Self {
7272
Self {
73-
inner: SortInputInner::Mmap {
73+
inner: SortInputInner::FileRead {
7474
data,
7575
offset: AtomicUsize::new(0),
7676
},
@@ -93,7 +93,7 @@ impl Read for SortInput {
9393

9494
match &mut self.inner {
9595
SortInputInner::File(file) => file.read(buf),
96-
SortInputInner::Mmap { data, offset } => {
96+
SortInputInner::FileRead { data, offset } => {
9797
let pos = offset.load(Ordering::Relaxed);
9898
let available = data.len().saturating_sub(pos);
9999
let to_read = buf.len().min(available);
@@ -142,7 +142,7 @@ impl SortInputs {
142142
/// - `output_as_input`: pre-created mmap used when the output file is also an input.
143143
pub fn from_files_with_output(
144144
files: &[std::ffi::OsString],
145-
output_as_input: Option<(PathBuf, Arc<Mmap>)>,
145+
output_as_input: Option<(PathBuf, Arc<MemoryMap>)>,
146146
) -> UResult<Self> {
147147
let mut inputs = Vec::with_capacity(files.len());
148148

@@ -161,7 +161,7 @@ impl SortInputs {
161161
// - Duplicate files: mmap once, share it
162162
// - Output-as-input: use pre-created mmap
163163
// - Stdin: single occurrence
164-
let mut opened_files: HashMap<PathBuf, Arc<Mmap>> = HashMap::new();
164+
let mut opened_files: HashMap<PathBuf, Arc<MemoryMap>> = HashMap::new();
165165

166166
for file in files {
167167
if file == STDIN_FILE {
@@ -191,7 +191,7 @@ impl SortInputs {
191191
})?;
192192
// SAFETY: We keep the file open for the lifetime of the mmap,
193193
// and we only read from it. The file is not modified.
194-
let mmap = Arc::new(unsafe { Mmap::map(&f) }.map_err(|error| {
194+
let mmap = Arc::new(unsafe { MemoryMap::map(&f) }.map_err(|error| {
195195
SortError::ReadFailed {
196196
path: path.to_owned(),
197197
error,
@@ -244,7 +244,7 @@ impl SortInputs {
244244
SortInputInner::File(_) | SortInputInner::LazyFile(_) => {
245245
file_count += 1;
246246
}
247-
SortInputInner::Mmap { data, .. } => {
247+
SortInputInner::FileRead { data, .. } => {
248248
seen_mmaps.insert(Arc::as_ptr(data));
249249
}
250250
}
@@ -350,7 +350,7 @@ mod tests {
350350
tmpfile.flush().expect("should flush temp file");
351351

352352
let file = File::open(tmpfile.path()).expect("should open temp file");
353-
let mmap = Arc::new(unsafe { Mmap::map(&file).expect("should mmap temp file") });
353+
let mmap = Arc::new(unsafe { MemoryMap::map(&file).expect("should mmap temp file") });
354354
let mut input = SortInput::from_mmap(mmap);
355355

356356
let mut buf = [0u8; 14];
@@ -368,7 +368,7 @@ mod tests {
368368
tmpfile.flush().expect("should flush temp file");
369369

370370
let file = File::open(tmpfile.path()).expect("should open temp file");
371-
let mmap = Arc::new(unsafe { Mmap::map(&file).expect("should mmap temp file") });
371+
let mmap = Arc::new(unsafe { MemoryMap::map(&file).expect("should mmap temp file") });
372372
let input = SortInput::from_mmap(mmap);
373373
let mut reader: Box<dyn Read + Send> = input.into();
374374
let mut buf = [0u8; 9];
@@ -388,7 +388,7 @@ mod tests {
388388
tmpfile.flush().expect("should flush temp file");
389389

390390
let file = File::open(tmpfile.path()).expect("should open temp file");
391-
let mmap = Arc::new(unsafe { Mmap::map(&file).expect("should mmap temp file") });
391+
let mmap = Arc::new(unsafe { MemoryMap::map(&file).expect("should mmap temp file") });
392392

393393
let mut input1 = SortInput::from_mmap(mmap.clone());
394394
let mut input2 = SortInput::from_mmap(mmap);
@@ -493,7 +493,7 @@ mod tests {
493493
let mut buf1 = [0u8; 11];
494494
let mut input1 = SortInput {
495495
inner: match &inputs.iter().next().expect("should get first input").inner {
496-
SortInputInner::Mmap { data, offset } => SortInputInner::Mmap {
496+
SortInputInner::FileRead { data, offset } => SortInputInner::FileRead {
497497
data: data.clone(),
498498
offset: AtomicUsize::new(offset.load(Ordering::Relaxed)),
499499
},
@@ -508,7 +508,7 @@ mod tests {
508508
let mut buf2 = [0u8; 11];
509509
let mut input2 = SortInput {
510510
inner: match &inputs.iter().nth(1).expect("should get second input").inner {
511-
SortInputInner::Mmap { data, offset } => SortInputInner::Mmap {
511+
SortInputInner::FileRead { data, offset } => SortInputInner::FileRead {
512512
data: data.clone(),
513513
offset: AtomicUsize::new(offset.load(Ordering::Relaxed)),
514514
},

0 commit comments

Comments
 (0)