archive_linux.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. if hdr.Xattrs != nil {
  39. delete(hdr.Xattrs, "trusted.overlay.opaque")
  40. }
  41. // create a header for the whiteout file
  42. // it should inherit some properties from the parent, but be a regular file
  43. wo = &tar.Header{
  44. Typeflag: tar.TypeReg,
  45. Mode: hdr.Mode & int64(os.ModePerm),
  46. Name: filepath.Join(hdr.Name, WhiteoutOpaqueDir),
  47. Size: 0,
  48. Uid: hdr.Uid,
  49. Uname: hdr.Uname,
  50. Gid: hdr.Gid,
  51. Gname: hdr.Gname,
  52. AccessTime: hdr.AccessTime,
  53. ChangeTime: hdr.ChangeTime,
  54. } //#nosec G305 -- An archive is being created, not extracted.
  55. }
  56. }
  57. return
  58. }
  59. func (c overlayWhiteoutConverter) ConvertRead(hdr *tar.Header, path string) (bool, error) {
  60. base := filepath.Base(path)
  61. dir := filepath.Dir(path)
  62. // if a directory is marked as opaque by the AUFS special file, we need to translate that to overlay
  63. if base == WhiteoutOpaqueDir {
  64. err := unix.Setxattr(dir, "trusted.overlay.opaque", []byte{'y'}, 0)
  65. if err != nil {
  66. return false, errors.Wrapf(err, "setxattr(%q, trusted.overlay.opaque=y)", dir)
  67. }
  68. // don't write the file itself
  69. return false, err
  70. }
  71. // if a file was deleted and we are using overlay, we need to create a character device
  72. if strings.HasPrefix(base, WhiteoutPrefix) {
  73. originalBase := base[len(WhiteoutPrefix):]
  74. originalPath := filepath.Join(dir, originalBase)
  75. if err := unix.Mknod(originalPath, unix.S_IFCHR, 0); err != nil {
  76. return false, errors.Wrapf(err, "failed to mknod(%q, S_IFCHR, 0)", originalPath)
  77. }
  78. if err := os.Chown(originalPath, hdr.Uid, hdr.Gid); err != nil {
  79. return false, err
  80. }
  81. // don't write the file itself
  82. return false, nil
  83. }
  84. return true, nil
  85. }