uv-extract 0.0.76

This is an internal component crate of uv
Documentation
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
//! Directory hashing while extracting seekable ZIP archives.

use std::path::{Path, PathBuf};
use std::pin::pin;
use std::sync::Mutex;

use crate::vendor::CloneableSeekableReader;
use crate::{Error, insecure_no_validate};
use async_zip::StoredZipEntry;
use async_zip::base::read::seek::ZipFileReader;
use async_zip::error::ZipError;
use futures::executor::block_on;
use futures::io::{AllowStdIo, AsyncReadExt, AsyncWriteExt};
use rayon::prelude::*;
use rustc_hash::FxHashSet;
use tokio_util::compat::{FuturesAsyncReadCompatExt, FuturesAsyncWriteCompatExt};
use tracing::warn;
use uv_configuration::initialize_rayon_once;

use super::{
    DirhashTree, HashedFile, UnhashedFile, UnzipOutput, blake3_copy, directory_tree_from_extracted,
};
use crate::archive_path::SanitizedArchivePath;

/// A successfully extracted file, or an explicit directory that can affect the digest.
enum ExtractedEntry {
    File {
        path: SanitizedArchivePath,
        size: u64,
        digest: Option<blake3::Hash>,
        executable: bool,
    },
    Directory(SanitizedArchivePath),
}

/// Unzip a `.zip` archive into the target directory.
pub(crate) fn unzip(reader: fs_err::File, target: &Path) -> Result<Vec<UnhashedFile>, Error> {
    let UnzipOutput::Unhashed(files) = unzip_inner(reader, target, false)? else {
        return Err(Error::Io(std::io::Error::other(
            "seekable ZIP hash tree was unexpectedly computed",
        )));
    };
    Ok(files)
}

/// Unzip a `.zip` archive into the target directory while computing a hash tree of the extracted
/// files.
///
/// Returns the list of unpacked files and their sizes, along with a hash tree containing the
/// canonicalized extracted file paths, contents, and empty directories.
pub(crate) fn unzip_and_hash(
    reader: fs_err::File,
    target: &Path,
) -> Result<(Vec<HashedFile>, DirhashTree), Error> {
    let UnzipOutput::Hashed { files, tree } = unzip_inner(reader, target, true)? else {
        return Err(Error::Io(std::io::Error::other(
            "seekable ZIP hash tree was not computed",
        )));
    };
    Ok((files, tree))
}

fn unzip_inner(
    reader: fs_err::File,
    target: &Path,
    hash_contents: bool,
) -> Result<UnzipOutput, Error> {
    let (reader, _) = reader.into_parts();

    // Parse the central directory once, then clone the archive reader per Rayon worker so
    // extraction stays parallel for already-downloaded wheels. AllowStdIo adapts synchronous
    // file I/O to async_zip; extraction itself runs on blocking and Rayon threads.
    let archive = block_on(ZipFileReader::new(AllowStdIo::new(
        CloneableSeekableReader::new(reader),
    )))?;
    if hash_contents {
        validate_unique_output_paths(archive.file().entries())?;
    }

    let directories = Mutex::new(FxHashSet::default());
    let skip_validation = insecure_no_validate();
    // Initialize the threadpool with the user settings.
    initialize_rayon_once();
    let extract = |file_number| {
        let mut archive = archive.clone();
        extract_entry(
            &mut archive,
            file_number,
            target,
            &directories,
            skip_validation,
            hash_contents,
        )
    };

    if !hash_contents {
        let files = (0..archive.file().entries().len())
            .into_par_iter()
            .map(extract)
            .filter_map(|result| match result {
                Ok(Some(ExtractedEntry::File { path, size, .. })) => {
                    Some(Ok(UnhashedFile::new(path.into_path_buf(), size)))
                }
                Ok(Some(ExtractedEntry::Directory(_)) | None) => None,
                Err(err) => Some(Err(err)),
            })
            .collect::<Result<_, Error>>()?;
        return Ok(UnzipOutput::Unhashed(files));
    }

    let extracted = (0..archive.file().entries().len())
        .into_par_iter()
        .map(extract)
        // Filter out skipped dangerous paths, then collect files and directory candidates.
        .filter_map(Result::transpose)
        .collect::<Result<Vec<_>, Error>>()?;

    let mut hashed_files = Vec::with_capacity(extracted.len());
    let mut digest_directories = FxHashSet::default();
    for extracted in extracted {
        match extracted {
            ExtractedEntry::File {
                path,
                size,
                digest,
                executable,
            } => {
                if let Some(digest) = digest {
                    hashed_files.push(HashedFile::new(path, size, digest, executable));
                }
            }
            ExtractedEntry::Directory(path) => {
                digest_directories.insert(path);
            }
        }
    }
    let tree = directory_tree_from_extracted(&hashed_files, &digest_directories)?;
    Ok(UnzipOutput::Hashed {
        files: hashed_files,
        tree,
    })
}

/// Reject entries that would write to the same sanitized output path.
///
/// Duplicate paths can otherwise race to determine which contents are persisted or hashed.
fn validate_unique_output_paths(entries: &[StoredZipEntry]) -> Result<(), Error> {
    let mut paths = FxHashSet::default();
    for (file_number, entry) in entries.iter().enumerate() {
        let file_name = entry_file_name(entry, file_number)?;
        let Ok(Some(path)) = SanitizedArchivePath::from_archive_member(file_name) else {
            continue;
        };
        if !paths.insert(path.clone()) {
            return Err(Error::DuplicateOutputPath {
                path: path.into_path_buf(),
            });
        }
    }
    Ok(())
}

/// Extract a single central-directory entry from a seekable ZIP archive.
fn extract_entry<R>(
    archive: &mut ZipFileReader<AllowStdIo<R>>,
    file_number: usize,
    target: &Path,
    directories: &Mutex<FxHashSet<PathBuf>>,
    skip_validation: bool,
    hash_contents: bool,
) -> Result<Option<ExtractedEntry>, Error>
where
    R: std::io::BufRead + std::io::Seek + Unpin,
{
    let entry = archive.file().entries()[file_number].clone();
    let file_name = entry_file_name(&entry, file_number)?;
    let enclosed_name = match SanitizedArchivePath::from_archive_member(file_name) {
        Ok(path) => path,
        Err(_) if skip_validation => None,
        Err(err) => return Err(err),
    };
    let Some(enclosed_name) = enclosed_name else {
        warn!("Skipping unsafe file name: {file_name}");
        return Ok(None);
    };

    let path = target.join(enclosed_name.as_path());
    if entry.dir()? {
        create_directory_once(directories, &path)?;
        if hash_contents {
            validate_directory_entry(&entry, enclosed_name.as_path(), skip_validation)?;
        }
        return Ok(Some(ExtractedEntry::Directory(enclosed_name)));
    }

    if let Some(parent) = path.parent() {
        create_directory_once(directories, parent)?;
    }

    extract_file_entry(
        archive,
        &entry,
        file_number,
        enclosed_name,
        &path,
        skip_validation,
        hash_contents,
    )
    .map(Some)
}

/// Return an entry file name from the central directory.
fn entry_file_name(entry: &StoredZipEntry, file_number: usize) -> Result<&str, Error> {
    match entry.filename().as_str() {
        Ok(file_name) => Ok(file_name),
        Err(ZipError::StringNotUtf8) => Err(Error::CentralDirectoryEntryNotUtf8 {
            index: file_number as u64,
        }),
        Err(err) => Err(err.into()),
    }
}

/// Create a directory once across parallel extraction workers.
fn create_directory_once(
    directories: &Mutex<FxHashSet<PathBuf>>,
    path: &Path,
) -> Result<(), Error> {
    let mut directories = directories.lock().map_err(|_| directory_lock_error())?;
    if directories.insert(path.to_path_buf()) {
        fs_err::create_dir_all(path).map_err(Error::Io)?;
    }

    Ok(())
}

/// Validate the metadata for a directory entry.
fn validate_directory_entry(
    entry: &StoredZipEntry,
    path: &Path,
    skip_validation: bool,
) -> Result<(), Error> {
    if skip_validation {
        return Ok(());
    }

    if entry.crc32() != 0 {
        return Err(Error::BadCrc32 {
            path: path.to_path_buf(),
            computed: 0,
            expected: entry.crc32(),
        });
    }

    if entry.uncompressed_size() != 0 {
        return Err(Error::BadUncompressedSize {
            path: path.to_path_buf(),
            computed: 0,
            expected: entry.uncompressed_size(),
        });
    }

    Ok(())
}

/// Extract a regular file entry and return its digest metadata.
fn extract_file_entry<R>(
    archive: &mut ZipFileReader<AllowStdIo<R>>,
    entry: &StoredZipEntry,
    file_number: usize,
    enclosed_name: SanitizedArchivePath,
    path: &Path,
    skip_validation: bool,
    hash_contents: bool,
) -> Result<ExtractedEntry, Error>
where
    R: std::io::BufRead + std::io::Seek + Unpin,
{
    let outfile = if hash_contents {
        fs_err::OpenOptions::new()
            .write(true)
            .create_new(true)
            .open(path)
    } else {
        fs_err::File::create(path)
    }
    .map_err(Error::Io)?;
    let size = entry.uncompressed_size();
    let writer = buffered_file_writer(outfile, size);

    // Keep the hashing state out of ordinary extraction, and pin both futures here to avoid
    // moving their large state into `block_on`.
    let (copied, computed_crc32, digest) = if hash_contents {
        let (copied, computed_crc32, digest) =
            block_on(pin!(copy_and_hash_entry(archive, file_number, writer)))?;
        (copied, computed_crc32, Some(digest))
    } else {
        let (copied, computed_crc32) = block_on(pin!(copy_entry(archive, file_number, writer)))?;
        (copied, computed_crc32, None)
    };
    validate_file_entry(
        enclosed_name.as_path(),
        copied,
        size,
        computed_crc32,
        entry.crc32(),
        skip_validation,
    )?;
    #[cfg(unix)]
    preserve_executable_bit(path, entry.unix_permissions())?;

    Ok(ExtractedEntry::File {
        path: enclosed_name,
        size,
        digest,
        executable: entry
            .unix_permissions()
            .is_some_and(|mode| mode & 0o111 != 0),
    })
}

/// Build a buffered writer sized for the expected entry contents.
fn buffered_file_writer(file: fs_err::File, size: u64) -> std::io::BufWriter<fs_err::File> {
    if let Ok(size) = usize::try_from(size) {
        std::io::BufWriter::with_capacity(std::cmp::min(size, 1024 * 1024), file)
    } else {
        std::io::BufWriter::new(file)
    }
}

/// Validate the copied size and CRC for a file entry.
fn validate_file_entry(
    path: &Path,
    copied: u64,
    expected_size: u64,
    computed_crc32: u32,
    expected_crc32: u32,
    skip_validation: bool,
) -> Result<(), Error> {
    if skip_validation {
        return Ok(());
    }

    if copied != expected_size {
        return Err(Error::BadUncompressedSize {
            path: path.to_path_buf(),
            computed: copied,
            expected: expected_size,
        });
    }

    if computed_crc32 != expected_crc32 {
        return Err(Error::BadCrc32 {
            path: path.to_path_buf(),
            computed: computed_crc32,
            expected: expected_crc32,
        });
    }

    Ok(())
}

#[cfg(unix)]
/// Preserve executable permissions according to pip's wheel extraction behavior.
fn preserve_executable_bit(path: &Path, unix_permissions: Option<u16>) -> Result<(), Error> {
    use std::fs::Permissions;
    use std::os::unix::fs::PermissionsExt;

    let Some(mode) = unix_permissions else {
        return Ok(());
    };

    // https://github.com/pypa/pip/blob/3898741e29b7279e7bffe044ecfbe20f6a438b1e/src/pip/_internal/utils/unpacking.py#L88-L100
    if mode & 0o111 == 0 {
        return Ok(());
    }

    let permissions = fs_err::metadata(path).map_err(Error::Io)?.permissions();
    if permissions.mode() & 0o111 == 0o111 {
        return Ok(());
    }

    fs_err::set_permissions(path, Permissions::from_mode(permissions.mode() | 0o111))
        .map_err(Error::Io)
}

/// Return an error for a poisoned directory memoization lock.
fn directory_lock_error() -> Error {
    Error::Io(std::io::Error::other("directory set lock poisoned"))
}

/// Copy an entry without computing a content digest.
async fn copy_entry<R>(
    archive: &mut ZipFileReader<AllowStdIo<R>>,
    file_number: usize,
    writer: std::io::BufWriter<fs_err::File>,
) -> Result<(u64, u32), Error>
where
    R: std::io::BufRead + std::io::Seek + Unpin,
{
    let mut file = archive.reader_with_entry(file_number).await?;
    let mut writer = AllowStdIo::new(writer);

    let mut copied = 0;
    let mut buffer = vec![0; 128 * 1024];
    loop {
        let read = file.read(&mut buffer).await.map_err(Error::io_or_zip)?;
        if read == 0 {
            break;
        }
        writer.write_all(&buffer[..read]).await.map_err(Error::Io)?;
        copied += read as u64;
    }
    writer.flush().await.map_err(Error::Io)?;
    Ok((copied, file.compute_hash()))
}

/// Copy an entry while hashing the same uncompressed bytes written to disk.
async fn copy_and_hash_entry<R>(
    archive: &mut ZipFileReader<AllowStdIo<R>>,
    file_number: usize,
    writer: std::io::BufWriter<fs_err::File>,
) -> Result<(u64, u32, blake3::Hash), Error>
where
    R: std::io::BufRead + std::io::Seek + Unpin,
{
    let mut file = archive.reader_with_entry(file_number).await?;
    let mut writer = AllowStdIo::new(writer);
    let (copied, digest) = blake3_copy((&mut file).compat(), (&mut writer).compat_write())
        .await
        .map_err(Error::io_or_zip)?;
    Ok((copied, file.compute_hash(), digest))
}