archive_linux.go 2.4 KB

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