archive_linux.go 2.4 KB

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