summaryrefslogtreecommitdiff
path: root/src/path_list_editor.rs
blob: 1ae48162eba412968b93e3d2353cb12a7945b0a6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//! Utilities for editing null-separated path lists.
//!
//! This module operates on the same null-separated path-list format consumed by `repo2markdown`.
//! The functions are intended for small Unix-style filter binaries such as `r2md-append`,
//! `r2md-prepend`, and `r2md-remove`.
//!
//! Paths that are kept from stdin are written back using their original byte representation.
//! Removal compares paths after normalization relative to a supplied origin base.

use std::{
    collections::HashSet,
    ffi::OsString,
    io::{Read, Write},
    os::unix::ffi::OsStrExt,
    path::{Path, PathBuf},
};

use crate::{normalizer::Normalizer, util::path_list::paths_from_null_separated_bytes};

const NUL: &[u8] = b"\0";

/// Copies a null-separated path list from `input`, then appends `new_paths`.
///
/// Each path in `new_paths` is written as a NUL-terminated entry.
///
/// This function is defensive about unterminated input: if `input` is non-empty and does not end
/// with a NUL byte, a NUL byte is inserted before appended paths are written. If `new_paths` is
/// empty, this still has the effect of repairing a non-empty unterminated input stream into a
/// NUL-terminated one.
///
/// This function does not normalize, deduplicate, or validate paths.
pub fn append_paths<R: Read, W: Write>(
    input: R,
    mut output: W,
    new_paths: &[OsString],
) -> std::io::Result<()> {
    let last_byte_in_input = copy_while_tracking_last_byte(input, &mut output)?;
    // Defensively ensure non-empty input is NUL-terminated.
    if matches!(last_byte_in_input, Some(byte) if byte != 0) {
        output.write_all(NUL)?;
    }
    for path in new_paths {
        output.write_all(path.as_bytes())?;
        output.write_all(NUL)?;
    }
    Ok(())
}

/// Copies all bytes from `input` to `output`, returning the last byte copied.
///
/// Returns `Ok(None)` when `input` is empty.
///
/// This helper is used by [`append_paths`] to preserve streaming behavior while still detecting
/// whether the input path list ended with a NUL byte.
fn copy_while_tracking_last_byte<R: Read, W: Write>(
    mut input: R,
    mut output: W,
) -> std::io::Result<Option<u8>> {
    let mut buf = [0; 8192];
    let mut last = None;

    loop {
        let read_bytes = input.read(&mut buf)?;
        if read_bytes == 0 {
            break;
        }

        last = Some(buf[read_bytes - 1]);
        output.write_all(&buf[..read_bytes])?;
    }
    Ok(last)
}

/// Prepends `new_paths` before the null-separated path list from `input`.
///
/// Each path in `new_paths` is written as a NUL-terminated entry before the original input stream
/// is copied unchanged.
///
/// This function does not normalize, deduplicate, validate, or repair the input stream. In
/// particular, unlike [`append_paths`], it does not need to inspect whether the input stream is
/// NUL-terminated because no new path is written after stdin.
pub fn prepend_paths<R: Read, W: Write>(
    mut input: R,
    mut output: W,
    new_paths: &[OsString],
) -> std::io::Result<()> {
    for path in new_paths {
        output.write_all(path.as_bytes())?;
        output.write_all(NUL)?;
    }
    std::io::copy(&mut input, &mut output)?;
    Ok(())
}

/// Removes paths from a null-separated path list.
///
/// The `input` stream is interpreted as a NUL-separated sequence of path entries. Each entry is
/// normalized relative to `origin_base` and compared against the normalized form of each path in
/// `unwanted_paths`.
///
/// Entries whose normalized path matches one of the normalized `unwanted_paths` are skipped.
/// Entries that are kept are written to `output` using their original byte form, followed by a NUL
/// byte.
pub fn remove_paths<R: Read, W: Write>(
    mut input: R,
    mut output: W,
    unwanted_paths: &[PathBuf],
    origin_base: &Path,
) -> Result<(), Box<dyn std::error::Error>> {
    // The root is arbitrary here because root_relative is not user-facing.
    // It only needs to be consistent across both sides of the comparison.
    let normalizer = Normalizer::new(Path::new("."), origin_base)?;
    // Normalize unwanted paths once and place them in a HashSet for efficient lookup.
    let unwanted_paths = unwanted_paths
        .iter()
        .map(|path| normalizer.normalize(path))
        .collect::<Result<HashSet<_>, _>>()?;

    let mut input_buf = Vec::new();
    input.read_to_end(&mut input_buf)?;
    for path in paths_from_null_separated_bytes(&input_buf) {
        let normalized_path = normalizer.normalize(path)?;
        if !unwanted_paths.contains(&normalized_path) {
            output.write_all(path.as_os_str().as_bytes())?;
            output.write_all(NUL)?;
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use std::{
        ffi::OsString,
        os::unix::ffi::OsStrExt,
        path::{Path, PathBuf},
    };

    use super::{append_paths, prepend_paths, remove_paths};

    #[test]
    fn append_paths_works_with_empty_stdin_and_args() {
        let input = b"";
        let mut output = Vec::new();

        append_paths(&input[..], &mut output, &[]).unwrap();

        assert_eq!(output, b"");
    }

    #[test]
    fn append_paths_works_with_empty_args() {
        let input = b"README.md\0";
        let mut output = Vec::new();

        append_paths(&input[..], &mut output, &[]).unwrap();

        assert_eq!(output, b"README.md\0");
    }

    #[test]
    fn append_paths_works_with_empty_stdin() {
        let input = b"";
        let mut output = Vec::new();

        append_paths(&input[..], &mut output, &[OsString::from("README.md")]).unwrap();

        assert_eq!(output, b"README.md\0");
    }

    #[test]
    fn append_paths_works_with_unique_stdin_and_args() {
        let input = b"a.rs\0b.rs\0";
        let mut output = Vec::new();

        append_paths(&input[..], &mut output, &[OsString::from("c.rs")]).unwrap();

        assert_eq!(output, b"a.rs\0b.rs\0c.rs\0");
    }

    #[test]
    fn append_paths_keeps_duplicate_entries() {
        let input = b"a.rs\0b.rs\0";
        let mut output = Vec::new();

        append_paths(&input[..], &mut output, &[OsString::from("a.rs")]).unwrap();

        assert_eq!(output, b"a.rs\0b.rs\0a.rs\0");
    }

    #[test]
    fn append_paths_inserts_separator_when_stdin_is_not_null_terminated() {
        let input = b"a.rs";
        let mut output = Vec::new();

        append_paths(&input[..], &mut output, &[OsString::from("b.rs")]).unwrap();

        assert_eq!(output, b"a.rs\0b.rs\0");
    }

    #[test]
    fn append_paths_does_not_insert_extra_separator_when_stdin_is_null_terminated() {
        let input = b"a.rs\0";
        let mut output = Vec::new();

        append_paths(&input[..], &mut output, &[OsString::from("b.rs")]).unwrap();

        assert_eq!(output, b"a.rs\0b.rs\0");
    }

    #[test]
    fn append_paths_handles_partially_terminated_input_sequence() {
        let input = b"a.rs\0b.rs";
        let mut output = Vec::new();

        append_paths(&input[..], &mut output, &[OsString::from("c.rs")]).unwrap();

        assert_eq!(output, b"a.rs\0b.rs\0c.rs\0");
    }

    #[test]
    fn prepend_paths_works_with_empty_stdin_and_args() {
        let input = b"";
        let mut output = Vec::new();

        prepend_paths(&input[..], &mut output, &[]).unwrap();

        assert_eq!(output, b"");
    }

    #[test]
    fn prepend_paths_works_with_empty_args() {
        let input = b"README.md\0";
        let mut output = Vec::new();

        prepend_paths(&input[..], &mut output, &[]).unwrap();

        assert_eq!(output, b"README.md\0");
    }

    #[test]
    fn prepend_paths_works_with_empty_stdin() {
        let input = b"";
        let mut output = Vec::new();

        prepend_paths(&input[..], &mut output, &[OsString::from("README.md")]).unwrap();

        assert_eq!(output, b"README.md\0");
    }

    #[test]
    fn prepend_paths_works_with_unique_stdin_and_args() {
        let input = b"a.rs\0b.rs\0";
        let mut output = Vec::new();

        prepend_paths(&input[..], &mut output, &[OsString::from("c.rs")]).unwrap();

        assert_eq!(output, b"c.rs\0a.rs\0b.rs\0");
    }

    #[test]
    fn prepend_paths_keeps_duplicate_entries() {
        let input = b"a.rs\0b.rs\0";
        let mut output = Vec::new();

        prepend_paths(&input[..], &mut output, &[OsString::from("a.rs")]).unwrap();

        assert_eq!(output, b"a.rs\0a.rs\0b.rs\0");
    }

    #[test]
    fn remove_paths_works_with_empty_stdin_and_args() {
        let input = b"";
        let mut output = Vec::new();

        remove_paths(&input[..], &mut output, &[], Path::new(".")).unwrap();

        assert_eq!(output, b"");
    }

    #[test]
    fn remove_paths_works_with_empty_args() {
        let input = b"README.md\0";
        let mut output = Vec::new();

        remove_paths(&input[..], &mut output, &[], Path::new(".")).unwrap();

        assert_eq!(output, b"README.md\0");
    }

    #[test]
    fn remove_paths_works_with_empty_stdin() {
        let input = b"";
        let mut output = Vec::new();

        remove_paths(
            &input[..],
            &mut output,
            &[PathBuf::from("README.md")],
            Path::new("."),
        )
        .unwrap();

        assert_eq!(output, b"");
    }

    #[test]
    fn remove_paths_filters_matching_entries() {
        let input = b"a.rs\0b.rs\0c.rs\0";
        let mut output = Vec::new();

        remove_paths(
            &input[..],
            &mut output,
            &[PathBuf::from("b.rs")],
            Path::new("."),
        )
        .unwrap();

        assert_eq!(output, b"a.rs\0c.rs\0");
    }

    #[test]
    fn remove_paths_removes_all_matching_entries() {
        let input = b"a.rs\0b.rs\0b.rs\0c.rs\0";
        let mut output = Vec::new();

        remove_paths(
            &input[..],
            &mut output,
            &[PathBuf::from("b.rs")],
            Path::new("."),
        )
        .unwrap();

        assert_eq!(output, b"a.rs\0c.rs\0");
    }

    #[test]
    fn remove_paths_matches_dot_normalized_entries() {
        let input = b"src/./main.rs\0src/lib.rs\0";
        let mut output = Vec::new();

        remove_paths(
            &input[..],
            &mut output,
            &[PathBuf::from("src/main.rs")],
            Path::new("."),
        )
        .unwrap();

        assert_eq!(output, b"src/lib.rs\0");
    }

    #[test]
    fn remove_paths_matches_parent_dir_normalized_entries() {
        let input = b"src/../a.rs\0b.rs\0";
        let mut output = Vec::new();

        remove_paths(
            &input[..],
            &mut output,
            &[PathBuf::from("a.rs")],
            Path::new("."),
        )
        .unwrap();

        assert_eq!(output, b"b.rs\0");
    }

    #[test]
    fn remove_paths_matches_when_unwanted_path_needs_normalization() {
        let input = b"a.rs\0b.rs\0";
        let mut output = Vec::new();

        remove_paths(
            &input[..],
            &mut output,
            &[PathBuf::from("src/../a.rs")],
            Path::new("."),
        )
        .unwrap();

        assert_eq!(output, b"b.rs\0");
    }

    #[test]
    fn remove_paths_matches_when_both_sides_need_normalization() {
        let input = b"src/../a.rs\0b.rs\0";
        let mut output = Vec::new();

        remove_paths(
            &input[..],
            &mut output,
            &[PathBuf::from("./a.rs")],
            Path::new("."),
        )
        .unwrap();

        assert_eq!(output, b"b.rs\0");
    }

    #[test]
    fn remove_paths_preserves_original_bytes_for_kept_paths() {
        let input = b"src/./main.rs\0src/lib.rs\0";
        let mut output = Vec::new();

        remove_paths(
            &input[..],
            &mut output,
            &[PathBuf::from("other.rs")],
            Path::new("."),
        )
        .unwrap();

        assert_eq!(output, b"src/./main.rs\0src/lib.rs\0");
    }

    #[test]
    fn remove_paths_matches_relative_unwanted_path_against_absolute_input_using_origin_base() {
        let temp_dir = tempfile::tempdir().unwrap();
        let origin = temp_dir.path();
        let absolute_path = origin.join("a.rs");

        let mut input = Vec::new();
        input.extend(absolute_path.as_os_str().as_bytes());
        input.push(0);
        input.extend(b"b.rs\0");

        let mut output = Vec::new();

        remove_paths(&input[..], &mut output, &[PathBuf::from("a.rs")], origin).unwrap();

        assert_eq!(output, b"b.rs\0");
    }
}