archive_linux.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. }
  22. func (overlayWhiteoutConverter) ConvertWrite(hdr *tar.Header, path string, fi os.FileInfo) (wo *tar.Header, err error) {
  23. // convert whiteouts to AUFS format
  24. if fi.Mode()&os.ModeCharDevice != 0 && hdr.Devmajor == 0 && hdr.Devminor == 0 {
  25. // we just rename the file and make it normal
  26. dir, filename := filepath.Split(hdr.Name)
  27. hdr.Name = filepath.Join(dir, WhiteoutPrefix+filename)
  28. hdr.Mode = 0600
  29. hdr.Typeflag = tar.TypeReg
  30. hdr.Size = 0
  31. }
  32. if fi.Mode()&os.ModeDir != 0 {
  33. // convert opaque dirs to AUFS format by writing an empty file with the prefix
  34. opaque, err := system.Lgetxattr(path, "trusted.overlay.opaque")
  35. if err != nil {
  36. return nil, err
  37. }
  38. if len(opaque) == 1 && opaque[0] == 'y' {
  39. if hdr.Xattrs != nil {
  40. delete(hdr.Xattrs, "trusted.overlay.opaque")
  41. }
  42. // create a header for the whiteout file
  43. // it should inherit some properties from the parent, but be a regular file
  44. wo = &tar.Header{
  45. Typeflag: tar.TypeReg,
  46. Mode: hdr.Mode & int64(os.ModePerm),
  47. Name: filepath.Join(hdr.Name, WhiteoutOpaqueDir),
  48. Size: 0,
  49. Uid: hdr.Uid,
  50. Uname: hdr.Uname,
  51. Gid: hdr.Gid,
  52. Gname: hdr.Gname,
  53. AccessTime: hdr.AccessTime,
  54. ChangeTime: hdr.ChangeTime,
  55. } //#nosec G305 -- An archive is being created, not extracted.
  56. }
  57. }
  58. return
  59. }
  60. func (c overlayWhiteoutConverter) ConvertRead(hdr *tar.Header, path string) (bool, error) {
  61. base := filepath.Base(path)
  62. dir := filepath.Dir(path)
  63. // if a directory is marked as opaque by the AUFS special file, we need to translate that to overlay
  64. if base == WhiteoutOpaqueDir {
  65. err := unix.Setxattr(dir, "trusted.overlay.opaque", []byte{'y'}, 0)
  66. if err != nil {
  67. return false, errors.Wrapf(err, "setxattr(%q, trusted.overlay.opaque=y)", dir)
  68. }
  69. // don't write the file itself
  70. return false, err
  71. }
  72. // if a file was deleted and we are using overlay, we need to create a character device
  73. if strings.HasPrefix(base, WhiteoutPrefix) {
  74. originalBase := base[len(WhiteoutPrefix):]
  75. originalPath := filepath.Join(dir, originalBase)
  76. if err := unix.Mknod(originalPath, unix.S_IFCHR, 0); err != nil {
  77. return false, errors.Wrapf(err, "failed to mknod(%q, S_IFCHR, 0)", originalPath)
  78. }
  79. if err := os.Chown(originalPath, hdr.Uid, hdr.Gid); err != nil {
  80. return false, err
  81. }
  82. // don't write the file itself
  83. return false, nil
  84. }
  85. return true, nil
  86. }