如何判断文件夹是否存在且可写?

我希望func FolderExists(path string) bool这会告诉文件夹是否存在并且可写。我已经到了这里:

func FolderExists(path string) bool {
    info, err := os.Stat(path)
    return os.IsExist(err) && info.Mode().IsDir() && info.Mode().???}

如何判断这个文件夹是否可写?我不能简单地检查文件模式权限(例如,用户写权限为 0200),因为那样我就必须检查文件的所有者。在 Go 中有没有一种直接的方法可以做到这一点?

对于那些有 UNIX 背景的人来说,寻找相当于非常简单的:

if [ -d "$n" && -w "$n" ] ; then ... fi


慕神8447489
浏览 330回答 3
3回答

慕哥6287543

没有独立于平台的方法来实现这一点,因为在 Windows 上无法检查用户是否创建了目录。因此,我们将针对 Windows 和非 Windows 系统以不同的方式实现它,然后使用 Golang 的构建约束根据操作系统有条件地编译文件。如图所示,在目录 file-perm 下创建 3 个 .go 文件 -file-perm /&nbsp; &nbsp; file-perm.go&nbsp; &nbsp; is-writable.go&nbsp; &nbsp; is-writable_windows.go文件:file-perm.gopackage mainimport (&nbsp; &nbsp; "fmt")func main() {&nbsp; &nbsp; path := "/tmp/xyz"&nbsp; &nbsp; fmt.Println(IsWritable(path))}文件:is-writable.go这构建在除 Windows 之外的所有平台上,因为 syscall.Stat_t 在 Windows 上不可用。请注意,在构建约束之后需要有一个空行// +build !windows// +build !windowspackage mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "os"&nbsp; &nbsp; "syscall")func IsWritable(path string) (isWritable bool, err error) {&nbsp; &nbsp; isWritable = false&nbsp; &nbsp; info, err := os.Stat(path)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Path doesn't exist")&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; err = nil&nbsp; &nbsp; if !info.IsDir() {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Path isn't a directory")&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; // Check if the user bit is enabled in file permission&nbsp; &nbsp; if info.Mode().Perm() & (1 << (uint(7))) == 0 {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Write permission bit is not set on this file for user")&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; var stat syscall.Stat_t&nbsp; &nbsp; if err = syscall.Stat(path, &stat); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Unable to get stat")&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; err = nil&nbsp; &nbsp; if uint32(os.Geteuid()) != stat.Uid {&nbsp; &nbsp; &nbsp; &nbsp; isWritable = false&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("User doesn't have permission to write to this directory")&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; isWritable = true&nbsp; &nbsp; return}文件:is-writable_windows.go由于文件名中的_windows.go后缀,此文件将仅在 Windows 上构建。更多信息请参考https://golang.org/pkg/go/build/package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "os")func IsWritable(path string) (isWritable bool, err error) {&nbsp; &nbsp; isWritable = false&nbsp; &nbsp; info, err := os.Stat(path)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Path doesn't exist")&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; err = nil&nbsp; &nbsp; if !info.IsDir() {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Path isn't a directory")&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; // Check if the user bit is enabled in file permission&nbsp; &nbsp; if info.Mode().Perm()&(1<<(uint(7))) == 0 {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Write permission bit is not set on this file for user")&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; isWritable = true&nbsp; &nbsp; return}现在,您必须在 file-perm 目录中使用go build,然后运行可执行文件。请注意,它不适用于 go run。

12345678_0001

我的回答不会直接回答你的问题,但仍然......简而言之:不要这样做(除非您正在编写嵌入的东西)。说明:文件系统是完全并发的,因此它们本质上是竞争条件的主题。简单来说,一个序列检查文件是否存在。如果是,请阅读它。不会在现实世界中工作,因为文件完全能够在 (1) 和 (2) 之间消失,因为其他进程删除了它。因此,编写上述序列的唯一真正方法是删除(1)并准备处理(2)中的文件打开错误。回到您的问题,唯一明智的方法是尝试在目标目录中创建一个文件,并准备好处理由于目录不可写而导致的错误。此外,Go 的错误处理方法是专门针对此类(现实世界)情况量身定制的。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go