在Go(Golang)中查找文件系统对象

huangapple go评论119阅读模式
英文:

Find filesystem objects in Go (Golang)

问题

我正在尝试使用Go语言找到匹配的文件系统对象,并确定输入的路径类型。具体而言,如果对象与提供的路径匹配,我需要执行相应的操作。路径的示例输入可能如下所示:

  1. /path/to/filename.ext
  2. /path/to/dirname
  3. /path/to/*.txt

我需要知道路径是否存在,是文件还是目录或正则表达式,以便相应地处理输入。以下是我目前设计的解决方案:

  1. func getPathType(path string) (bool, string, error) {
  2. cpath := filepath.Clean(path)
  3. l, err := filepath.Glob(cpath)
  4. if err != nil {
  5. return false, "", err
  6. }
  7. switch len(l) {
  8. case 0:
  9. return false, "", nil
  10. case 1:
  11. fsstat, fserr := os.Stat(cpath)
  12. if fserr != nil {
  13. return false, "", fserr
  14. }
  15. if fsstat.IsDir() {
  16. return true, "dir", nil
  17. }
  18. return true, "file", nil
  19. default:
  20. return false, "regex", nil
  21. }
  22. }

我意识到上述代码允许将返回单个值的正则表达式解释为目录或文件,而不是正则表达式。对于我的目的来说,我可以忽略这一点,但我只是好奇是否有人开发出更好的方法来接受可能包含正则表达式的路径作为输入,并确定最后一个元素是否是正则表达式。

英文:

I am trying to find matching filesystem objects using Go and determine the type of path I received as input. Specifically, I need to perform actions on the object(s) if they match the path provided. Example input for the path could look like this:

  1. /path/to/filename.ext
  2. /path/to/dirname
  3. /path/to/*.txt

I need to know if the path exists, is a file or a directory or a regex so I can process the input accordingly. Here's the solution I've devised so far:

  1. func getPathType(path string) (bool, string, error) {
  2. cpath := filepath.Clean(path)
  3. l, err := filepath.Glob(cpath)
  4. if err != nil {
  5. return false, "", err
  6. }
  7. switch len(l) {
  8. case 0:
  9. return false, "", nil
  10. case 1:
  11. fsstat, fserr := os.Stat(cpath)
  12. if fserr != nil {
  13. return false, "", fserr
  14. }
  15. if fsstat.IsDir() {
  16. return true, "dir", nil
  17. }
  18. return true, "file", nil
  19. default:
  20. return false, "regex", nil
  21. }
  22. }

I realize that the above code would allow a regex that returned a single value to be interpreted as a dir or file and not as a regex. For my purposes, I can let that slide but just curious if anyone has developed a better way of taking a path potentially containing regex as input and determining whether or not the last element is a regex or not.

答案1

得分: 1

测试glob特殊字符以确定路径是否为glob模式。使用filepath.Match检查有效的glob模式语法。

  1. func getPathType(path string) (bool, string, error) {
  2. cpath := filepath.Clean(path)
  3. // 使用Match检查glob语法。
  4. if _, err := filepath.Match(cpath, ""); err != nil {
  5. return false, "", err
  6. }
  7. // 如果语法正确且路径包含特殊的glob字符,则为glob模式。
  8. special := `*?[`
  9. if runtime.GOOS != "windows" {
  10. special = `*?[\`
  11. }
  12. if strings.ContainsAny(cpath, special) {
  13. return false, "regex", nil
  14. }
  15. fsstat, err := os.Stat(cpath)
  16. if os.IsNotExist(err) {
  17. return false, "", nil
  18. } else if err != nil {
  19. return false, "", err
  20. }
  21. if fsstat.IsDir() {
  22. return true, "dir", nil
  23. }
  24. return true, "file", nil
  25. }

以上是给定的代码的翻译。

英文:

Test for glob special characters to determine if the path is a glob pattern. Use filepath.Match to check for valid glob pattern syntax.

  1. func getPathType(path string) (bool, string, error) {
  2. cpath := filepath.Clean(path)
  3. // Use Match to check glob syntax.
  4. if _, err := filepath.Match(cpath, ""); err != nil {
  5. return false, "", err
  6. }
  7. // If syntax is good and the path includes special
  8. // glob characters, then it's a glob pattern.
  9. special := `*?[`
  10. if runtime.GOOS != "windows" {
  11. special = `*?[\`
  12. }
  13. if strings.ContainsAny(cpath, special) {
  14. return false, "regex", nil
  15. }
  16. fsstat, err := os.Stat(cpath)
  17. if os.IsNotExist(err) {
  18. return false, "", nil
  19. } else if err != nil {
  20. return false, "", err
  21. }
  22. if fsstat.IsDir() {
  23. return true, "dir", nil
  24. }
  25. return true, "file", nil
  26. }

huangapple
  • 本文由 发表于 2021年10月22日 06:03:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/69669251.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定