archive_linux.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package archive // import "github.com/docker/docker/pkg/archive"
  2. import (
  3. "archive/tar"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "github.com/docker/docker/pkg/system"
  8. "github.com/pkg/errors"
  9. "golang.org/x/sys/unix"
  10. )
  11. func getWhiteoutConverter(format WhiteoutFormat, inUserNS bool) (tarWhiteoutConverter, error) {
  12. if format == OverlayWhiteoutFormat {
  13. if inUserNS {
  14. return nil, errors.New("specifying OverlayWhiteoutFormat is not allowed in userns")
  15. }
  16. return overlayWhiteoutConverter{}, nil
  17. }
  18. return nil, nil
  19. }
  20. type overlayWhiteoutConverter struct{}
  21. func (overlayWhiteoutConverter) ConvertWrite(hdr *tar.Header, path string, fi os.FileInfo) (wo *tar.Header, err error) {
  22. // convert whiteouts to AUFS format
  23. if fi.Mode()&os.ModeCharDevice != 0 && hdr.Devmajor == 0 && hdr.Devminor == 0 {
  24. // we just rename the file and make it normal
  25. dir, filename := filepath.Split(hdr.Name)
  26. hdr.Name = filepath.Join(dir, WhiteoutPrefix+filename)
  27. hdr.Mode = 0o600
  28. hdr.Typeflag = tar.TypeReg
  29. hdr.Size = 0
  30. }
  31. if fi.Mode()&os.ModeDir != 0 {
  32. // convert opaque dirs to AUFS format by writing an empty file with the prefix
  33. opaque, err := system.Lgetxattr(path, "trusted.overlay.opaque")
  34. if err != nil {
  35. return nil, err
  36. }
  37. if len(opaque) == 1 && opaque[0] == 'y' {
  38. delete(hdr.PAXRecords, paxSchilyXattr+"trusted.overlay.opaque")
  39. // create a header for the whiteout file
  40. // it should inherit some properties from the parent, but be a regular file
  41. wo = &tar.Header{
  42. Typeflag: tar.TypeReg,
  43. Mode: hdr.Mode & int64(os.ModePerm),
  44. Name: filepath.Join(hdr.Name, WhiteoutOpaqueDir),
  45. Size: 0,
  46. Uid: hdr.Uid,
  47. Uname: hdr.Uname,
  48. Gid: hdr.Gid,
  49. Gname: hdr.Gname,
  50. AccessTime: hdr.AccessTime,
  51. ChangeTime: hdr.ChangeTime,
  52. } //#nosec G305 -- An archive is being created, not extracted.
  53. }
  54. }
  55. return
  56. }
  57. func (c overlayWhiteoutConverter) ConvertRead(hdr *tar.Header, path string) (bool, error) {
  58. base := filepath.Base(path)
  59. dir := filepath.Dir(path)
  60. // if a directory is marked as opaque by the AUFS special file, we need to translate that to overlay
  61. if base == WhiteoutOpaqueDir {
  62. err := unix.Setxattr(dir, "trusted.overlay.opaque", []byte{'y'}, 0)
  63. if err != nil {
  64. return false, errors.Wrapf(err, "setxattr(%q, trusted.overlay.opaque=y)", dir)
  65. }
  66. // don't write the file itself
  67. return false, err
  68. }
  69. // if a file was deleted and we are using overlay, we need to create a character device
  70. if strings.HasPrefix(base, WhiteoutPrefix) {
  71. originalBase := base[len(WhiteoutPrefix):]
  72. originalPath := filepath.Join(dir, originalBase)
  73. if err := unix.Mknod(originalPath, unix.S_IFCHR, 0); err != nil {
  74. return false, errors.Wrapf(err, "failed to mknod(%q, S_IFCHR, 0)", originalPath)
  75. }
  76. if err := os.Chown(originalPath, hdr.Uid, hdr.Gid); err != nil {
  77. return false, err
  78. }
  79. // don't write the file itself
  80. return false, nil
  81. }
  82. return true, nil
  83. }