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

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

Find filesystem objects in Go (Golang)

问题

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

/path/to/filename.ext
/path/to/dirname
/path/to/*.txt

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

func getPathType(path string) (bool, string, error) {
    cpath := filepath.Clean(path)

    l, err := filepath.Glob(cpath)
    if err != nil {
        return false, "", err
    }

    switch len(l) {
    case 0:
        return false, "", nil
    case 1:
        fsstat, fserr := os.Stat(cpath)
        if fserr != nil {
            return false, "", fserr
        }
        if fsstat.IsDir() {
            return true, "dir", nil
        }
        return true, "file", nil
    default:
        return false, "regex", nil
    }
}

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

英文:

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:

/path/to/filename.ext
/path/to/dirname
/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:

func getPathType(path string) (bool, string, error) {
    cpath := filepath.Clean(path)

    l, err := filepath.Glob(cpath)
    if err != nil {
        return false, "", err
    }

    switch len(l) {
    case 0:
        return false, "", nil
    case 1:
        fsstat, fserr := os.Stat(cpath)
        if fserr != nil {
            return false, "", fserr
        }
        if fsstat.IsDir() {
            return true, "dir", nil
        }
        return true, "file", nil
    default:
        return false, "regex", nil
    }
}

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模式语法。

func getPathType(path string) (bool, string, error) {
    cpath := filepath.Clean(path)

    // 使用Match检查glob语法。
    if _, err := filepath.Match(cpath, ""); err != nil {
        return false, "", err
    }

    // 如果语法正确且路径包含特殊的glob字符,则为glob模式。
    special := `*?[`
    if runtime.GOOS != "windows" {
        special = `*?[\`
    }
    if strings.ContainsAny(cpath, special) {
        return false, "regex", nil
    }

    fsstat, err := os.Stat(cpath)
    if os.IsNotExist(err) {
        return false, "", nil
    } else if err != nil {
        return false, "", err
    }
    if fsstat.IsDir() {
        return true, "dir", nil
    }
    return true, "file", nil
}

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

英文:

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

func getPathType(path string) (bool, string, error) {
	cpath := filepath.Clean(path)

	// Use Match to check glob syntax.
	if _, err := filepath.Match(cpath, ""); err != nil {
		return false, "", err
	}

	// If syntax is good and the path includes special
	// glob characters, then it's a glob pattern.
	special := `*?[`
	if runtime.GOOS != "windows" {
		special = `*?[\`
	}
	if strings.ContainsAny(cpath, special) {
		return false, "regex", nil
	}

	fsstat, err := os.Stat(cpath)
	if os.IsNotExist(err) {
		return false, "", nil
	} else if err != nil {
		return false, "", err
	}
	if fsstat.IsDir() {
		return true, "dir", nil
	}
	return true, "file", nil
}

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:

确定