tar.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
  2. package utils
  3. import (
  4. "archive/tar"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "io/fs"
  9. "os"
  10. "path/filepath"
  11. "strings"
  12. )
  13. var _ = fmt.Print
  14. type TarExtractOptions struct {
  15. DontPreservePermissions bool
  16. }
  17. func ExtractAllFromTar(tr *tar.Reader, dest_path string, optss ...TarExtractOptions) (count int, err error) {
  18. opts := TarExtractOptions{}
  19. if len(optss) > 0 {
  20. opts = optss[0]
  21. }
  22. dest_path, err = filepath.Abs(dest_path)
  23. if err != nil {
  24. return
  25. }
  26. mode := func(hdr *tar.Header) fs.FileMode {
  27. return fs.FileMode(hdr.Mode) & (fs.ModePerm | fs.ModeSetgid | fs.ModeSetuid | fs.ModeSticky)
  28. }
  29. set_metadata := func(chmod func(mode fs.FileMode) error, hdr *tar.Header) (err error) {
  30. if !opts.DontPreservePermissions && chmod != nil {
  31. perms := mode(hdr)
  32. if err = chmod(perms); err != nil {
  33. return err
  34. }
  35. }
  36. count++
  37. return
  38. }
  39. for {
  40. var hdr *tar.Header
  41. hdr, err = tr.Next()
  42. if errors.Is(err, io.EOF) {
  43. break
  44. }
  45. if err != nil {
  46. return count, err
  47. }
  48. dest := hdr.Name
  49. dest = strings.TrimLeft(dest, "/")
  50. if !filepath.IsLocal(dest) {
  51. continue
  52. }
  53. dest = filepath.Join(dest_path, dest)
  54. switch hdr.Typeflag {
  55. case tar.TypeDir:
  56. err = os.MkdirAll(dest, 0o700)
  57. if err != nil {
  58. return
  59. }
  60. if err = set_metadata(func(m fs.FileMode) error { return os.Chmod(dest, m) }, hdr); err != nil {
  61. return
  62. }
  63. case tar.TypeReg, tar.TypeRegA:
  64. var d *os.File
  65. if d, err = os.Create(dest); err != nil {
  66. return
  67. }
  68. err = set_metadata(d.Chmod, hdr)
  69. if err == nil {
  70. _, err = io.Copy(d, tr)
  71. }
  72. d.Close()
  73. if err != nil {
  74. return
  75. }
  76. case tar.TypeLink:
  77. if err = os.Link(hdr.Linkname, dest); err != nil {
  78. return
  79. }
  80. if err = set_metadata(func(m fs.FileMode) error { return os.Chmod(dest, m) }, hdr); err != nil {
  81. return
  82. }
  83. case tar.TypeSymlink:
  84. if err = os.Symlink(hdr.Linkname, dest); err != nil {
  85. return
  86. }
  87. if err = set_metadata(nil, hdr); err != nil {
  88. return
  89. }
  90. }
  91. }
  92. return
  93. }