Golang检查字符串是否是有效路径 [英] Golang check if string is valid path

查看:78
本文介绍了Golang检查字符串是否是有效路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Golang文件路径模块( https://golang.org/pkg/path/filepath/)包含一个很少有用于操纵路径的函数和 os.Stat 可以用来检查文件是否存在.有没有办法检查一个字符串是否真的构成了一个有效的路径(不管该路径上是否有文件)?

The Golang filepath module (https://golang.org/pkg/path/filepath/) contains a few functions for manipulating paths and os.Stat can be used to check if a file exists. Is there a way to check if a string actually forms a valid path at all (regardless of whether there's a file at that path or not)?

推荐答案

这个问题听起来很简单,但实际上并非如此.这是我发现的两个可能的解决方案:

This problem sounds very simple, but it is actually not. Here is two possible solutions that I found :

这里的想法是根据规则检查给定的文件路径.

The idea here is to check a given filepath based on rules.

  1. 操作系统(UNIX/Windows)
  2. 文件系统
  3. 保留关键字

操作系统

第一个是最简单的.Go为操作系统特定的文件名/分隔符/...提供了各种工具.

The first one is the easiest. Go provides various tools for OS-specific filenames/separators/...

操作系统包中的示例:

const (
    PathSeparator     = '/' // OS-specific path separator
    PathListSeparator = ':' // OS-specific path list separator
)

文件路径包中的另一个:

// VolumeName returns leading volume name.
// Given "C:\foo\bar" it returns "C:" on Windows.
// Given "\\host\share\foo" it returns "\\host\share".
// On other platforms it returns "".
func VolumeName(path string) string {
    return path[:volumeNameLen(path)]
}

文件系统

文件系统具有不同的限制.最大长度或允许的字符集可能会有所不同.不幸的是,您无法(至少据我所知)无法知道您的路径将遍历哪个文件系统.

Filesystems have different restrictions. The maximum length or the charset allowed may vary. Unfortunately, there is no way you can tell (not as far as I know at least) which filesystem(s) your path will traverse.

保留的关键字

拥有给定操作系统的所有保留关键字的黑名单.

Have a blacklist of all reserved keywords for a given OS.

对于此解决方案,我将构建一个词典器/

For this solution, I would build a lexer/parser.

要权衡的是,它不能保证100%的文件路径有效.

The tradeoff is that it would not guarantee 100% that a filepath is valid.

尝试创建文件,然后立即将其删除.

Attempt to create the file and delete it right after.

func IsValid(fp string) bool {
  // Check if file already exists
  if _, err := os.Stat(fp); err == nil {
    return true
  }

  // Attempt to create it
  var d []byte
  if err := ioutil.WriteFile(fp, d, 0644); err == nil {
    os.Remove(fp) // And delete it
    return true
  }

  return false
}

此解决方案的主要优点是它简单明了且更准确.如果文件已经存在或可以在给定路径下创建,则表示该文件有效.但是,由于访问受限,该解决方案还会使有效路径无效.

The main benefit of this solution is that it is straightforward and more accurate. If a file already exists or can be created at a given path, it means it is valid. However, this solution can also invalidate valid paths because of restricted access.

第一个解决方案将比第二个解决方案更不准确,即使从纯粹的角度来看更正确.您应该选择的解决方案取决于您的需求.您喜欢误报还是误报?第一种解决方案可以给您带来误报,而第二种解决方案可以给您带来误报.

The first solution will be less accurate than the second one, even though more correct from a puristic point of view. The solution you should pick up depends on your need. Do you prefer false positives or false negatives? The first solution can give you false positives, while the second one false negatives.

这篇关于Golang检查字符串是否是有效路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆