如何以与操作系统无关的方式在Go中获取文件系统的根目录

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

How to get root of filesystem in Go in an OS-agnostic way

问题

我想以一种既适用于Windows又适用于Unix的方式获取文件系统的根目录。

我原以为可以这样做:

func fsRootDir() string {
    s := os.Getenv("SystemDrive")
    if s != "" {
        return s
    }
    return "/"
}

然而,这种方法存在一些问题,所以我放弃了这种方法:

  • 用户可以在Unix上创建一个名为SystemDrive的环境变量,并设置为一些虚假的路径。
  • 用户可以更改Windows上SystemDrive环境变量的值为一些虚假的路径。

我查看了这个相关问题的答案,但这个答案也存在一些问题:

  • 第一种选项依赖于SystemDrive环境变量,但由于上述原因,无法保证在Unix或Windows上它将保持预期的值。
  • 此答案中的所有其他选项都依赖于os.TempDir。在Windows上,os.TempDir使用GetTempPath,返回%TMP%%TEMP%%USERPROFILE%或Windows目录中的第一个非空值。我不相信这些环境变量没有被修改过。

我还考虑了以下方法:

func fsRootDir() string {
    if runtime.GOOS == "windows" {
        return "C:"
    }
    return "/"
}

但我记得在某个地方读到过,在Windows上可以将文件系统根目录更改为除了C:之外的其他内容。

我应该如何以一种既适用于Windows又适用于Unix的方式获取文件系统的根目录?

英文:

I want to get the root directory of the filesystem in Go in such a way that it will work on both Windows and Unix.

I thought I could do something like

func fsRootDir() string {
    s := os.Getenv("SystemDrive")
    if s != "" {
        return s
    }
    return "/"
}

However, there are a couple problems with this, so I rejected this approach:

  • A user could create a SystemDrive environment variable on Unix with some bogus path.
  • A user could change the value of the SystemDrive environment variable on Windows to some bogus path.

I looked at this answer to a related question, but this also has some problems:

  • The first option relies on the SystemDrive environment variable, which for the reasons above is not guaranteed to hold the expected value on either Unix or Windows.
  • All other options in this answer rely on os.TempDir. On Windows, os.TempDir uses GetTempPath, returning the first non-empty value from %TMP%, %TEMP%, %USERPROFILE%, or the Windows directory. I do not believe I can trust that these environment variables have not been modified.

I also considered

func fsRootDir() string {
    if runtime.GOOS == "windows" {
        return "C:"
    }
    return "/"
}

but I thought I read somewhere that it is possible to change the filesystem root on Windows to something other than C:.

How can I get the root directory of a filesystem in such a way that it will work on both Windows and Unix?

答案1

得分: 2

为什么不将两者结合起来?

func fsRootDir() string {
    if runtime.GOOS == "windows" {
        return os.Getenv("SystemDrive")
    }
    return "/"
}

用户可以将Windows上的SystemDrive环境变量的值更改为一些虚假的路径。

不,他们不能,SystemDrive是一个只读变量。

英文:

Why not combine the two?

func fsRootDir() string {
    if runtime.GOOS == "windows" {
        return os.Getenv("SystemDrive")
    }
    return "/"
}

> A user could change the value of the SystemDrive environment variable on Windows to some bogus path.

No they can't, SystemDrive is a read-only variable.

huangapple
  • 本文由 发表于 2022年11月23日 05:07:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/74538952.html
匿名

发表评论

匿名网友

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

确定