remote_readdir.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package procutils
  15. import (
  16. "fmt"
  17. "os"
  18. "regexp"
  19. "runtime"
  20. "strconv"
  21. "strings"
  22. "yunion.io/x/pkg/errors"
  23. "yunion.io/x/pkg/util/timeutils"
  24. )
  25. var (
  26. splitReg = regexp.MustCompile(`\s+`)
  27. )
  28. func split(line string) []string {
  29. return splitReg.Split(line, -1)
  30. }
  31. func parseLsLine(line string) (os.FileInfo, error) {
  32. // drwxr-xr-x. 7 1000 20 4096 2022-02-16 08:10:21.660000000 +0800 .vim
  33. // -rw-------. 1 0 0 23658 2022-04-21 17:31:04.320995359 +0800 .viminfo
  34. // lrwxr-xr-x 1 501 80 31 2022-11-02 21:01:10.000000000 +0800 zstdmt -> ../Cellar/zstd/1.5.2/bin/zstdmt
  35. line = strings.TrimSpace(line)
  36. if len(line) < 10 {
  37. return nil, errors.Error("invalid ls line: too short")
  38. }
  39. parts := split(line)
  40. if len(parts) < 9 {
  41. return nil, errors.Error(fmt.Sprintf("invalid ls line: parts %d", len(parts)))
  42. }
  43. ftype := "file"
  44. switch parts[0][0] {
  45. case 'd':
  46. ftype = "directory"
  47. case 'l':
  48. ftype = "link"
  49. }
  50. size, err := strconv.ParseInt(parts[4], 10, 64)
  51. if err != nil {
  52. return nil, errors.Wrap(err, "Parse size")
  53. }
  54. tmstr := fmt.Sprintf("%sT%s%s:00", parts[5], parts[6], parts[7][:3])
  55. atime, err := timeutils.ParseTimeStr(tmstr)
  56. if err != nil {
  57. return nil, errors.Wrap(err, "parse time")
  58. }
  59. tmstr = strings.Join(parts[5:8], " ")
  60. nameIndex := strings.Index(line, tmstr) + len(tmstr) + 1
  61. name := strings.TrimSpace(line[nameIndex:])
  62. if ftype == "link" {
  63. arrowPos := strings.Index(name, "->")
  64. if arrowPos > 0 {
  65. name = strings.TrimSpace(name[:arrowPos])
  66. }
  67. }
  68. fs := &sFileStat{
  69. FileSize: size,
  70. FileType: ftype,
  71. FileName: name,
  72. LastModAt: atime,
  73. }
  74. return fs, nil
  75. }
  76. func RemoteReadDir(dirname string) ([]os.FileInfo, error) {
  77. args := []string{}
  78. switch runtime.GOOS {
  79. case "darwin":
  80. args = []string{"-la1n", "-D", `%Y-%m-%d %H:%M:%S.000000000 %z`, dirname}
  81. default:
  82. args = []string{"-la1n", "--full-time", dirname}
  83. }
  84. output, err := NewRemoteCommandAsFarAsPossible("ls", args...).Output()
  85. if err != nil {
  86. if strings.Contains(strings.ToLower(string(output)), "no such file or directory") {
  87. return nil, os.ErrNotExist
  88. }
  89. return nil, errors.Wrap(err, "NewRemoteCommandAsFarAsPossible")
  90. }
  91. files := make([]os.FileInfo, 0)
  92. lines := strings.Split(string(output), "\n")
  93. for _, line := range lines {
  94. f, err := parseLsLine(line)
  95. if err != nil {
  96. //log.Errorf("parseLsLine %s fail %s", line, err)
  97. } else {
  98. files = append(files, f)
  99. }
  100. }
  101. return files, nil
  102. }