docker-registry 0.9.0

A pure-Rust asynchronous library for Docker Registry HTTP API v2
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
433
434
435
436
//! Render a docker image.

// Docker image format is specified at
// https://github.com/moby/moby/blob/v17.05.0-ce/image/spec/v1.md

use std::{fs, io, path};

use libflate::gzip;

/// Zstd magic number: 0x28B52FFD
const ZSTD_MAGIC: [u8; 4] = [0x28, 0xB5, 0x2F, 0xFD];

fn is_zstd(data: &[u8]) -> bool {
  data.len() >= 4 && data[..4] == ZSTD_MAGIC
}

#[derive(Debug, thiserror::Error)]
pub enum RenderError {
  #[error("wrong target path {}: must be absolute path to existing directory", _0.display())]
  WrongTargetPath(path::PathBuf),
  #[error("io error")]
  Io(#[from] std::io::Error),
}

/// Unpack an ordered list of layers to a target directory.
///
/// Layers must be provided as gzip-compressed tar archives, with lower layers
/// coming first. Target directory must be an existing absolute path.
pub fn unpack(layers: &[Vec<u8>], target_dir: &path::Path) -> Result<(), RenderError> {
  _unpack(layers, target_dir, |mut archive, target_dir| {
    Ok(archive.unpack(target_dir)?)
  })
}

/// Unpack an ordered list of layers to a target directory, filtering
/// file entries by path.
///
/// Layers must be provided as gzip-compressed tar archives, with lower layers
/// coming first. Target directory must be an existing absolute path.
pub fn filter_unpack<P>(layers: &[Vec<u8>], target_dir: &path::Path, predicate: P) -> Result<(), RenderError>
where
  P: Fn(&path::Path) -> bool,
{
  _unpack(layers, target_dir, |mut archive, target_dir| {
    for entry in archive.entries()? {
      let mut entry = entry?;
      let path = entry.path()?;

      if predicate(&path) {
        entry.unpack_in(target_dir)?;
      }
    }

    Ok(())
  })
}

fn decompress(data: &[u8]) -> Result<Box<dyn io::Read + '_>, RenderError> {
  if is_zstd(data) {
    Ok(Box::new(zstd::Decoder::new(data)?))
  } else {
    Ok(Box::new(gzip::Decoder::new(data)?))
  }
}

fn _unpack<U>(layers: &[Vec<u8>], target_dir: &path::Path, unpacker: U) -> Result<(), RenderError>
where
  U: Fn(tar::Archive<Box<dyn io::Read + '_>>, &path::Path) -> Result<(), RenderError>,
{
  if !target_dir.is_absolute() || !target_dir.exists() || !target_dir.is_dir() {
    return Err(RenderError::WrongTargetPath(target_dir.to_path_buf()));
  }
  for l in layers {
    // Pre-pass: handle opaque whiteouts BEFORE unpacking so that
    // new files from the current layer survive the directory clearing.
    let reader = decompress(l.as_slice())?;
    let mut archive = tar::Archive::new(reader);
    for entry in archive.entries()? {
      let file = entry?;
      let path = file.path()?;
      let parent = path.parent().unwrap_or_else(|| path::Path::new("/"));
      if let Some(fname) = path.file_name() {
        if fname.to_string_lossy() == ".wh..wh..opq" {
          let rel_parent = path::PathBuf::from("./".to_string() + &parent.to_string_lossy());
          let abs_parent = target_dir.join(&rel_parent);
          if abs_parent.is_dir() {
            for dir_entry in fs::read_dir(&abs_parent)? {
              let dir_entry = dir_entry?;
              if dir_entry.path().is_dir() {
                fs::remove_dir_all(dir_entry.path())?;
              } else {
                fs::remove_file(dir_entry.path())?;
              }
            }
          }
        }
      }
    }

    // Unpack layers
    let reader = decompress(l.as_slice())?;
    let mut archive = tar::Archive::new(reader);
    archive.set_preserve_permissions(true);
    archive.set_unpack_xattrs(true);
    unpacker(archive, target_dir)?;

    // Clean whiteouts
    let reader = decompress(l.as_slice())?;
    let mut archive = tar::Archive::new(reader);
    for entry in archive.entries()? {
      let file = entry?;
      let path = file.path()?;
      let parent = path.parent().unwrap_or_else(|| path::Path::new("/"));
      if let Some(fname) = path.file_name() {
        let wh_name = fname.to_string_lossy();
        if wh_name == ".wh..wh..opq" {
          // Already handled in pre-pass; just remove the marker.
          let rel_parent = path::PathBuf::from("./".to_string() + &parent.to_string_lossy());
          let abs_wh_path = target_dir.join(&rel_parent).join(fname);
          remove_whiteout(abs_wh_path)?;
        } else if wh_name.starts_with(".wh.") {
          let rel_parent = path::PathBuf::from("./".to_string() + &parent.to_string_lossy());

          // Remove real file behind whiteout
          let real_name = wh_name.trim_start_matches(".wh.");
          let abs_real_path = target_dir.join(&rel_parent).join(real_name);
          remove_whiteout(abs_real_path)?;

          // Remove whiteout place-holder
          let abs_wh_path = target_dir.join(&rel_parent).join(fname);
          remove_whiteout(abs_wh_path)?;
        };
      }
    }
  }
  Ok(())
}

// Whiteout files in archive may not exist on filesystem if they were
// filtered out via filter_unpack.  If not found, that's ok and the
// error is non-fatal.  Otherwise still return error for other
// failures.
fn remove_whiteout(path: path::PathBuf) -> io::Result<()> {
  if path.is_dir() {
    let res = fs::remove_dir_all(&path);
    match res {
      Ok(_) => Ok(()),
      Err(ref e) if e.kind() == io::ErrorKind::NotFound => Ok(()),
      Err(e) => Err(e),
    }
  } else {
    let res = fs::remove_file(&path);
    match res {
      Ok(_) => Ok(()),
      Err(ref e) if e.kind() == io::ErrorKind::NotFound => Ok(()),
      Err(e) => Err(e),
    }
  }
}

#[cfg(test)]
mod tests {
  use std::{io::Write, path::Path};

  use super::*;

  /// Helper: create a gzip-compressed tar archive containing a single file.
  fn make_layer(file_name: &str, content: &[u8]) -> Vec<u8> {
    let mut tar_buf = Vec::new();
    {
      let mut builder = tar::Builder::new(&mut tar_buf);
      let mut header = tar::Header::new_gnu();
      header.set_path(file_name).unwrap();
      header.set_size(content.len() as u64);
      header.set_mode(0o644);
      header.set_cksum();
      builder.append(&header, content).unwrap();
      builder.finish().unwrap();
    }
    let mut gz_buf = Vec::new();
    {
      let mut encoder = gzip::Encoder::new(&mut gz_buf).unwrap();
      encoder.write_all(&tar_buf).unwrap();
      encoder.finish().into_result().unwrap();
    }
    gz_buf
  }

  /// Helper: create a layer with a whiteout marker file.
  fn make_whiteout_layer(whiteout_path: &str) -> Vec<u8> {
    make_layer(whiteout_path, b"")
  }

  #[test]
  fn test_unpack_single_layer() {
    let dir = tempfile::tempdir().unwrap();
    let layer = make_layer("hello.txt", b"hello world");
    unpack(&[layer], dir.path()).unwrap();
    let content = fs::read_to_string(dir.path().join("hello.txt")).unwrap();
    assert_eq!(content, "hello world");
  }

  #[test]
  fn test_unpack_multiple_layers() {
    let dir = tempfile::tempdir().unwrap();
    let layer1 = make_layer("file1.txt", b"content1");
    let layer2 = make_layer("file2.txt", b"content2");
    unpack(&[layer1, layer2], dir.path()).unwrap();
    assert_eq!(fs::read_to_string(dir.path().join("file1.txt")).unwrap(), "content1");
    assert_eq!(fs::read_to_string(dir.path().join("file2.txt")).unwrap(), "content2");
  }

  #[test]
  fn test_unpack_layer_overwrites_previous() {
    let dir = tempfile::tempdir().unwrap();
    let layer1 = make_layer("file.txt", b"old");
    let layer2 = make_layer("file.txt", b"new");
    unpack(&[layer1, layer2], dir.path()).unwrap();
    assert_eq!(fs::read_to_string(dir.path().join("file.txt")).unwrap(), "new");
  }

  #[test]
  fn test_unpack_relative_path_rejected() {
    let layer = make_layer("hello.txt", b"hello");
    let result = unpack(&[layer], Path::new("relative/path"));
    assert!(result.is_err());
  }

  #[test]
  fn test_unpack_nonexistent_path_rejected() {
    let layer = make_layer("hello.txt", b"hello");
    let result = unpack(&[layer], Path::new("/nonexistent/path/that/does/not/exist"));
    assert!(result.is_err());
  }

  #[test]
  fn test_unpack_empty_layers() {
    let dir = tempfile::tempdir().unwrap();
    unpack(&[], dir.path()).unwrap();
    // Should succeed with no files created
  }

  #[test]
  fn test_filter_unpack_includes_matching() {
    let dir = tempfile::tempdir().unwrap();
    let layer = make_layer("include-me.txt", b"included");
    filter_unpack(&[layer], dir.path(), |p| p.to_string_lossy().contains("include")).unwrap();
    assert!(dir.path().join("include-me.txt").exists());
  }

  #[test]
  fn test_filter_unpack_excludes_non_matching() {
    let dir = tempfile::tempdir().unwrap();
    let layer = make_layer("exclude-me.txt", b"excluded");
    filter_unpack(&[layer], dir.path(), |p| p.to_string_lossy().contains("include")).unwrap();
    assert!(!dir.path().join("exclude-me.txt").exists());
  }

  #[test]
  fn test_whiteout_removes_file() {
    let dir = tempfile::tempdir().unwrap();
    let layer1 = make_layer("myfile.txt", b"content");
    let layer2 = make_whiteout_layer(".wh.myfile.txt");
    unpack(&[layer1, layer2], dir.path()).unwrap();
    assert!(!dir.path().join("myfile.txt").exists());
  }

  #[test]
  fn test_unpack_invalid_gzip() {
    let dir = tempfile::tempdir().unwrap();
    let result = unpack(&[b"not gzip data".to_vec()], dir.path());
    assert!(result.is_err());
  }

  /// Helper: create a zstd-compressed tar archive containing a single file.
  fn make_zstd_layer(file_name: &str, content: &[u8]) -> Vec<u8> {
    let mut tar_buf = Vec::new();
    {
      let mut builder = tar::Builder::new(&mut tar_buf);
      let mut header = tar::Header::new_gnu();
      header.set_path(file_name).unwrap();
      header.set_size(content.len() as u64);
      header.set_mode(0o644);
      header.set_cksum();
      builder.append(&header, content).unwrap();
      builder.finish().unwrap();
    }
    zstd::encode_all(tar_buf.as_slice(), 3).unwrap()
  }

  #[test]
  fn test_unpack_zstd_single_layer() {
    let dir = tempfile::tempdir().unwrap();
    let layer = make_zstd_layer("hello.txt", b"hello zstd");
    unpack(&[layer], dir.path()).unwrap();
    let content = fs::read_to_string(dir.path().join("hello.txt")).unwrap();
    assert_eq!(content, "hello zstd");
  }

  #[test]
  fn test_unpack_zstd_multiple_layers() {
    let dir = tempfile::tempdir().unwrap();
    let layer1 = make_zstd_layer("file1.txt", b"content1");
    let layer2 = make_zstd_layer("file2.txt", b"content2");
    unpack(&[layer1, layer2], dir.path()).unwrap();
    assert_eq!(fs::read_to_string(dir.path().join("file1.txt")).unwrap(), "content1");
    assert_eq!(fs::read_to_string(dir.path().join("file2.txt")).unwrap(), "content2");
  }

  #[test]
  fn test_unpack_mixed_gzip_and_zstd() {
    let dir = tempfile::tempdir().unwrap();
    let gz_layer = make_layer("from_gzip.txt", b"gzip content");
    let zstd_layer = make_zstd_layer("from_zstd.txt", b"zstd content");
    unpack(&[gz_layer, zstd_layer], dir.path()).unwrap();
    assert_eq!(
      fs::read_to_string(dir.path().join("from_gzip.txt")).unwrap(),
      "gzip content"
    );
    assert_eq!(
      fs::read_to_string(dir.path().join("from_zstd.txt")).unwrap(),
      "zstd content"
    );
  }

  #[test]
  fn test_filter_unpack_zstd() {
    let dir = tempfile::tempdir().unwrap();
    let layer = make_zstd_layer("include-me.txt", b"included");
    filter_unpack(&[layer], dir.path(), |p| p.to_string_lossy().contains("include")).unwrap();
    assert!(dir.path().join("include-me.txt").exists());
  }

  #[test]
  fn test_whiteout_removes_file_zstd() {
    let dir = tempfile::tempdir().unwrap();
    let layer1 = make_zstd_layer("myfile.txt", b"content");
    let layer2 = make_zstd_layer(".wh.myfile.txt", b"");
    unpack(&[layer1, layer2], dir.path()).unwrap();
    assert!(!dir.path().join("myfile.txt").exists());
  }

  #[test]
  fn test_opaque_whiteout_clears_directory() {
    let dir = tempfile::tempdir().unwrap();

    // Layer 1: create a directory with files
    let mut tar_buf = Vec::new();
    {
      let mut builder = tar::Builder::new(&mut tar_buf);

      // Create dir
      let mut header = tar::Header::new_gnu();
      header.set_path("mydir/").unwrap();
      header.set_size(0);
      header.set_mode(0o755);
      header.set_entry_type(tar::EntryType::Directory);
      header.set_cksum();
      builder.append(&header, &[] as &[u8]).unwrap();

      // Create file inside dir
      let content = b"old content";
      let mut header = tar::Header::new_gnu();
      header.set_path("mydir/old_file.txt").unwrap();
      header.set_size(content.len() as u64);
      header.set_mode(0o644);
      header.set_cksum();
      builder.append(&header, content.as_slice()).unwrap();

      builder.finish().unwrap();
    }
    let mut gz_buf = Vec::new();
    {
      let mut encoder = gzip::Encoder::new(&mut gz_buf).unwrap();
      io::Write::write_all(&mut encoder, &tar_buf).unwrap();
      encoder.finish().into_result().unwrap();
    }
    let layer1 = gz_buf;

    // Layer 2: opaque whiteout marker + new file in same dir
    let mut tar_buf2 = Vec::new();
    {
      let mut builder = tar::Builder::new(&mut tar_buf2);

      // Opaque whiteout marker
      let mut header = tar::Header::new_gnu();
      header.set_path("mydir/.wh..wh..opq").unwrap();
      header.set_size(0);
      header.set_mode(0o644);
      header.set_cksum();
      builder.append(&header, &[] as &[u8]).unwrap();

      // New file in the same directory
      let content = b"new content";
      let mut header = tar::Header::new_gnu();
      header.set_path("mydir/new_file.txt").unwrap();
      header.set_size(content.len() as u64);
      header.set_mode(0o644);
      header.set_cksum();
      builder.append(&header, content.as_slice()).unwrap();

      builder.finish().unwrap();
    }
    let mut gz_buf2 = Vec::new();
    {
      let mut encoder = gzip::Encoder::new(&mut gz_buf2).unwrap();
      io::Write::write_all(&mut encoder, &tar_buf2).unwrap();
      encoder.finish().into_result().unwrap();
    }
    let layer2 = gz_buf2;

    unpack(&[layer1, layer2], dir.path()).unwrap();

    // Old file should be gone (opaque whiteout clears directory)
    assert!(
      !dir.path().join("mydir/old_file.txt").exists(),
      "opaque whiteout should have removed old_file.txt"
    );
    // New file from layer 2 should exist
    assert!(dir.path().join("mydir/new_file.txt").exists());
    assert_eq!(
      fs::read_to_string(dir.path().join("mydir/new_file.txt")).unwrap(),
      "new content"
    );
    // Directory itself should still exist
    assert!(dir.path().join("mydir").is_dir());
  }

  #[test]
  fn test_is_zstd_detection() {
    assert!(is_zstd(&[0x28, 0xB5, 0x2F, 0xFD, 0x00]));
    assert!(!is_zstd(&[0x1F, 0x8B, 0x08, 0x00])); // gzip
    assert!(!is_zstd(&[0x00, 0x01, 0x02])); // too short
    assert!(!is_zstd(&[]));
  }
}