strconv.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //go:build windows
  2. package backuptar
  3. import (
  4. "archive/tar"
  5. "fmt"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. // Functions copied from https://github.com/golang/go/blob/master/src/archive/tar/strconv.go
  11. // as we need to manage the LIBARCHIVE.creationtime PAXRecord manually.
  12. // Idea taken from containerd which did the same thing.
  13. // parsePAXTime takes a string of the form %d.%d as described in the PAX
  14. // specification. Note that this implementation allows for negative timestamps,
  15. // which is allowed for by the PAX specification, but not always portable.
  16. func parsePAXTime(s string) (time.Time, error) {
  17. const maxNanoSecondDigits = 9
  18. // Split string into seconds and sub-seconds parts.
  19. ss, sn := s, ""
  20. if pos := strings.IndexByte(s, '.'); pos >= 0 {
  21. ss, sn = s[:pos], s[pos+1:]
  22. }
  23. // Parse the seconds.
  24. secs, err := strconv.ParseInt(ss, 10, 64)
  25. if err != nil {
  26. return time.Time{}, tar.ErrHeader
  27. }
  28. if len(sn) == 0 {
  29. return time.Unix(secs, 0), nil // No sub-second values
  30. }
  31. // Parse the nanoseconds.
  32. if strings.Trim(sn, "0123456789") != "" {
  33. return time.Time{}, tar.ErrHeader
  34. }
  35. if len(sn) < maxNanoSecondDigits {
  36. sn += strings.Repeat("0", maxNanoSecondDigits-len(sn)) // Right pad
  37. } else {
  38. sn = sn[:maxNanoSecondDigits] // Right truncate
  39. }
  40. nsecs, _ := strconv.ParseInt(sn, 10, 64) // Must succeed
  41. if len(ss) > 0 && ss[0] == '-' {
  42. return time.Unix(secs, -1*nsecs), nil // Negative correction
  43. }
  44. return time.Unix(secs, nsecs), nil
  45. }
  46. // formatPAXTime converts ts into a time of the form %d.%d as described in the
  47. // PAX specification. This function is capable of negative timestamps.
  48. func formatPAXTime(ts time.Time) (s string) {
  49. secs, nsecs := ts.Unix(), ts.Nanosecond()
  50. if nsecs == 0 {
  51. return strconv.FormatInt(secs, 10)
  52. }
  53. // If seconds is negative, then perform correction.
  54. sign := ""
  55. if secs < 0 {
  56. sign = "-" // Remember sign
  57. secs = -(secs + 1) // Add a second to secs
  58. nsecs = -(nsecs - 1e9) // Take that second away from nsecs
  59. }
  60. return strings.TrimRight(fmt.Sprintf("%s%d.%09d", sign, secs, nsecs), "0")
  61. }