oci-unpack 0.1.1

Download and unpack OCI images.
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
use std::io::{
    self, BufReader,
    ErrorKind::{AlreadyExists, NotFound},
    Read, Seek,
};

use std::{
    cell::Cell,
    ffi::OsStr,
    fs::File,
    os::unix::ffi::OsStrExt,
    path::{Path, PathBuf},
};

use rustix::{
    fd::{AsFd, BorrowedFd, OwnedFd},
    fs::Mode,
};

use crate::{
    fs::{normalize_path, DirFdCache, Directory},
    manifests::Blob,
    EventHandler, MediaType,
};

use super::{try_io, DirectoryMetadata, UnpackError};

const WHITEOUT_PREFIX: &[u8] = b".wh.";

const WHITEOUT_OPAQUE: &[u8] = b".wh..opq";

pub(crate) fn unpack_layer<E: EventHandler>(
    blob_id: &str,
    event_handler: &E,
    target: &Directory,
    blob: &Blob,
    mut tarball: File,
    dirs_metadata: &mut DirectoryMetadata,
) -> Result<(), UnpackError> {
    let archive_len = try_io!(blob_id, {
        let len = tarball.seek(io::SeekFrom::End(0))?;
        tarball.rewind()?;
        len
    });

    // Track position (in bytes) to send progress notifications.
    let tarball_position = Cell::new(0);
    let tarball = PositionTracker {
        count: &tarball_position,
        reader: tarball,
    };

    // Uncompress and extract files from the archive.
    let reader: Box<dyn Read> = match blob.media_type {
        MediaType::DockerImageV1 | MediaType::OciConfig => {
            // Configuration files are just written to disk.
            return Ok(());
        }

        MediaType::DockerFsTarGzip | MediaType::OciFsTarGzip => {
            Box::new(flate2::read::GzDecoder::new(tarball))
        }

        #[cfg(feature = "zstd")]
        MediaType::OciFsTarZstd => {
            let reader = zstd::stream::read::Decoder::new(tarball)
                .map_err(|e| UnpackError::Io(e, format!("blob:{blob_id}").into()))?;

            Box::new(reader)
        }

        MediaType::OciFsTar => Box::new(BufReader::new(tarball)),

        unknown => return Err(UnpackError::InvalidContentType(unknown)),
    };

    event_handler.layer_start(archive_len);

    let mut archive = tar::Archive::new(reader);
    let mut ctx = Context::new(event_handler, blob_id, target, dirs_metadata);

    for entry in try_io!(blob_id, archive.entries()) {
        event_handler.layer_progress(tarball_position.get());
        ctx.unpack(entry)?;
    }

    event_handler.layer_progress(tarball_position.get());

    Ok(())
}

struct InvalidEntryType(tar::EntryType);

impl std::fmt::Display for InvalidEntryType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Invalid entry type: {:?}", self.0)
    }
}

/// Count how many bytes have been read from `reader`.
struct PositionTracker<'a, R> {
    count: &'a Cell<usize>,
    reader: R,
}

impl<T: Read> Read for PositionTracker<'_, T> {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        let n = self.reader.read(buf)?;
        self.count.set(self.count.get() + n);
        Ok(n)
    }
}

struct Context<'a, E> {
    event_handler: &'a E,
    blob_id: &'a str,
    target: &'a Directory,
    dirs_cache: DirFdCache<'a>,
    dirs_metadata: &'a mut DirectoryMetadata,
    cached_link_dirfd: Option<(PathBuf, OwnedFd)>,
}

impl<'a, E: EventHandler> Context<'a, E> {
    fn new(
        event_handler: &'a E,
        blob_id: &'a str,
        target: &'a Directory,
        dirs_metadata: &'a mut DirectoryMetadata,
    ) -> Self {
        Self {
            event_handler,
            blob_id,
            target,
            dirs_cache: DirFdCache::new(target),
            dirs_metadata,
            cached_link_dirfd: None,
        }
    }

    fn path_fd(&mut self, path: impl AsRef<Path>) -> io::Result<BorrowedFd<'_>> {
        Ok(self.dirs_cache.get(path, true)?)
    }

    fn unpack(&mut self, entry: io::Result<tar::Entry<impl Read>>) -> Result<(), UnpackError> {
        let entry = try_io!(self.blob_id, entry);

        let entry_path = try_io!(self.blob_id, entry.path());
        let entry_path = entry_path.as_ref();

        let (parent_path, file_name) = try_io!(entry_path, normalize_path(entry_path));

        // Handle whiteout entries.
        //
        // The cache for directory file descriptors is reset after removing
        // any entry.
        if let Some(whiteout) = file_name
            .as_os_str()
            .as_bytes()
            .strip_prefix(WHITEOUT_PREFIX)
        {
            let parent_fd = try_io!(parent_path, self.path_fd(&parent_path));
            try_io!(entry_path, Self::process_whiteout(parent_fd, whiteout));
            self.dirs_cache.clear();
            return Ok(());
        }

        // Unpack the entry.
        try_io!(file_name, {
            match entry.header().entry_type() {
                tar::EntryType::Directory => self.unpack_dir(parent_path, &file_name, entry)?,

                tar::EntryType::Regular => self.unpack_regular(parent_path, &file_name, entry)?,

                tar::EntryType::Symlink | tar::EntryType::Link => {
                    self.unpack_link(self.target.as_fd(), parent_path, &file_name, entry)?
                }

                other => {
                    self.event_handler
                        .layer_entry_skipped(entry.path()?.as_ref(), &InvalidEntryType(other));
                }
            }
        });

        Ok(())
    }

    fn unpack_dir(
        &mut self,
        parent_path: impl AsRef<Path>,
        file_name: &Path,
        entry: tar::Entry<impl Read>,
    ) -> io::Result<()> {
        use rustix::fs;

        let header = entry.header();

        let parent_fd = self.path_fd(parent_path)?;
        let result = fs::mkdirat(parent_fd, file_name, Mode::from_raw_mode(0o700));

        if let Err(e) = result {
            // Ignore the error if the directory already exists.
            if e.kind() == AlreadyExists && !Self::is_directory(parent_fd, file_name)? {
                return Err(e.into());
            }
        }

        let (uid, gid) = Self::get_entry_owner(header)?;

        // Store mtime/mode metadata to be applied later.
        if let Ok(mtime) = header.mtime() {
            let (mut path, b) = normalize_path(entry.path()?)?;
            path.push(b);

            let key = super::DirectoryMetadataEntry::key(path);
            let entry = super::DirectoryMetadataEntry {
                mode: Mode::from_bits_retain(header.mode()?),
                mtime,
                uid,
                gid,
            };

            self.dirs_metadata.insert(key, entry);
        }

        Ok(())
    }

    fn unpack_regular(
        &mut self,
        parent_path: impl AsRef<Path>,
        file_name: &Path,
        mut entry: tar::Entry<impl Read>,
    ) -> io::Result<()> {
        use rustix::fs;

        let mode = Mode::from_bits_retain(entry.header().mode()? & 0o7777);

        let parent_path = parent_path.as_ref();
        let parent_fd = self.dirs_cache.get(parent_path, true)?;

        let mut output = loop {
            let result = fs::openat2(
                parent_fd,
                file_name,
                fs::OFlags::CREATE | fs::OFlags::EXCL | fs::OFlags::WRONLY,
                mode,
                fs::ResolveFlags::BENEATH,
            );

            match result {
                Ok(f) => break File::from(f),

                Err(e) if e.kind() == AlreadyExists => {
                    let removed = crate::fs::remove_entry(parent_fd, file_name)?;

                    // Remove the entry from dirs_metadata if the entry
                    // was a directory.
                    if removed == crate::fs::RemovedEntry::Directory {
                        let path = parent_path.join(file_name);
                        let key = super::DirectoryMetadataEntry::key(path);
                        self.dirs_metadata.remove(&key);
                    }
                }

                Err(e) => return Err(e.into()),
            }
        };

        io::copy(&mut entry, &mut output)?;

        drop(output);

        Self::set_owner(parent_fd, file_name, entry.header())?;

        let mtime = Self::make_timestamps(entry.header().mtime()?);
        fs::utimensat(parent_fd, file_name, &mtime, fs::AtFlags::SYMLINK_NOFOLLOW)?;

        Ok(())
    }

    /// Unpack hard and symbolic links.
    fn unpack_link(
        &mut self,
        root_fd: BorrowedFd,
        parent_path: impl AsRef<Path>,
        file_name: &Path,
        entry: tar::Entry<impl Read>,
    ) -> io::Result<()> {
        use rustix::fs;

        let dest = match entry.link_name()? {
            Some(dest) => dest,
            None => return Err(io::Error::new(NotFound, "Missing link")),
        };

        let parent_fd = self.dirs_cache.get(parent_path, true)?;

        let is_symlink = entry.header().entry_type().is_symlink();

        loop {
            let result = if is_symlink {
                fs::symlinkat(dest.as_ref(), parent_fd, file_name)
            } else {
                // To create hard-links, get a file descriptor of the directory of
                // the source (`old_path`). The descriptor is cached because some OCI
                // images have multiple consecutive links in the same directory.

                let (old_parent, old_name) = normalize_path(&dest)?;

                let old_dirfd = match self.cached_link_dirfd.take() {
                    Some((cached_path, fd)) if cached_path == old_parent => fd,

                    _ => fs::openat2(
                        root_fd,
                        &old_parent,
                        fs::OFlags::PATH | fs::OFlags::NOFOLLOW,
                        fs::Mode::empty(),
                        fs::ResolveFlags::IN_ROOT | fs::ResolveFlags::NO_MAGICLINKS,
                    )?,
                };

                let result = fs::linkat(
                    &old_dirfd,
                    old_name,
                    parent_fd,
                    file_name,
                    fs::AtFlags::empty(),
                );

                self.cached_link_dirfd = Some((old_parent, old_dirfd));

                result
            };

            match result {
                Ok(_) => break,

                Err(e) if e.kind() == AlreadyExists => {
                    fs::unlinkat(parent_fd, file_name, fs::AtFlags::empty())?;
                }

                Err(e) => return Err(e.into()),
            }
        }

        if is_symlink {
            let header = entry.header();

            let mtime = Self::make_timestamps(header.mtime()?);
            fs::utimensat(parent_fd, file_name, &mtime, fs::AtFlags::SYMLINK_NOFOLLOW)?;

            Self::set_owner(parent_fd, file_name, header)?;
        }

        Ok(())
    }

    fn is_directory(parent: BorrowedFd, file_name: &Path) -> io::Result<bool> {
        let stat = rustix::fs::statat(parent, file_name, rustix::fs::AtFlags::empty())?;
        Ok(stat.st_mode & libc::S_IFDIR != 0)
    }

    fn make_timestamps(mtime: u64) -> rustix::fs::Timestamps {
        let mtime = rustix::fs::Timespec {
            tv_sec: i64::try_from(mtime).unwrap_or_default(),
            tv_nsec: 0,
        };

        rustix::fs::Timestamps {
            last_access: mtime,
            last_modification: mtime,
        }
    }

    /// Return the `uid, gid` of the entry.
    ///
    /// The owner is ignored if it is `0` (`root`).
    fn get_entry_owner(header: &tar::Header) -> io::Result<(Option<u32>, Option<u32>)> {
        Ok((
            match header.uid().map(|id| id.try_into()) {
                Ok(Ok(id)) if id > 0 => Some(id),
                _ => None,
            },
            match header.gid().map(|id| id.try_into()) {
                Ok(Ok(id)) if id > 0 => Some(id),
                _ => None,
            },
        ))
    }

    /// Set user/group of an entry.
    ///
    /// Errors from `fchownat` are ignored.
    fn set_owner(parent_fd: BorrowedFd, file_name: &Path, header: &tar::Header) -> io::Result<()> {
        let (uid, gid) = Self::get_entry_owner(header)?;
        crate::fs::change_owner(parent_fd, file_name, uid, gid, true)?;
        Ok(())
    }

    /// Process whiteout entries, by removing files in the directory.
    ///
    /// The [1]specification indicates that whiteout entries _should_
    /// appear before regular files. This implementation assumes that
    /// the layer was built in such way.
    ///
    /// [1]: https://github.com/opencontainers/image-spec/blob/v1.0/layer.md#whiteouts
    fn process_whiteout(dir: BorrowedFd, whiteout: &[u8]) -> io::Result<()> {
        let path = Path::new(OsStr::from_bytes(whiteout));

        if whiteout == WHITEOUT_OPAQUE {
            crate::fs::remove_subtree(dir, Path::new("."))?;
        } else {
            crate::fs::remove_entry(dir, path)?;
        }

        Ok(())
    }
}