Table of Contents
Question content
Workaround
Home Backend Development Golang Cross-platform way to check if a directory is writable in Golang?

Cross-platform way to check if a directory is writable in Golang?

Feb 10, 2024 pm 12:51 PM

在 Golang 中查看目录是否可写的跨平台方法?

In Golang, to check whether a directory is writable, you can use `os.FileMode` as a cross-platform method. First, obtain the file information of the directory through the `os.Stat` function. Then, use the `file.Mode().Perm()` method to obtain the file's permissions. Finally, use the `file.Mode().IsDir()` method to determine whether it is a directory. If the permissions of the directory are `0777`, it means it is writable; if the permissions are `0444` or `0555`, it means it is read-only; if the permissions are other values, it means it is not writable. This method is suitable for cross-platform directory writability checking.

Question content

I have a program that is trying to write a file to a directory using golang. I need it to work in macos, linux, and windows (at least).

golang provides the following test - but it seems to be limited to linux (from the so question linked below):

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

func IsWritable(path string) (isWritable bool, err error) {

    isWritable = false

    info, err := os.Stat(path)

    if err != nil {

        fmt.Println("Path doesn't exist")

        return

    }

 

    err = nil

    if !info.IsDir() {

        fmt.Println("Path isn't a directory")

        return

    }

 

    // Check if the user bit is enabled in file permission

    if info.Mode().Perm() & (1 << (uint(7))) == 0 {

        fmt.Println("Write permission bit is not set on this file for user")

        return

    }

 

    var stat syscall.Stat_t

    if err = syscall.Stat(path, &stat); err != nil {

        fmt.Println("Unable to get stat")

        return

    }

 

    err = nil

    if uint32(os.Geteuid()) != stat.Uid {

        isWritable = false

        fmt.Println("User doesn't have permission to write to this directory")

        return

    }

 

    isWritable = true

    return

}

Copy after login

I saw this answer [1], but this question is 10 years old, is there a better way to accomplish this than conditional compilation?

Summary: I just want the go process to know if it can write to a given directory.

[1]How to determine whether a folder exists and is writable?

Workaround

This is how I achieved my goal without conditional compilation since permissions and privileges can differ between operating systems.

  • I tried using os.createtemp to create a temporary file in that directory. If the function returns no errors, there is no problem with the path or permissions and we can create the file in that directory.

This is the code

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

func IsWritable(path string) (bool, error) {

    tmpFile := "tmpfile"

 

    file, err := os.CreateTemp(path, tmpFile)

    if err != nil {

        return false, err

    }

 

    defer os.Remove(file.Name())

    defer file.Close()

 

    return true, nil

}

 

 

func main() {

     path := "absolute-directory-path"

 

    isWritable, err := IsWritable(path)

    if err != nil {

       panic(err)

    }

 

    if isWritable {

      // statements

    }

 

}

Copy after login

The above is the detailed content of Cross-platform way to check if a directory is writable in Golang?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Go language pack import: What is the difference between underscore and without underscore? Go language pack import: What is the difference between underscore and without underscore? Mar 03, 2025 pm 05:17 PM

Go language pack import: What is the difference between underscore and without underscore?

How to implement short-term information transfer between pages in the Beego framework? How to implement short-term information transfer between pages in the Beego framework? Mar 03, 2025 pm 05:22 PM

How to implement short-term information transfer between pages in the Beego framework?

How do I write mock objects and stubs for testing in Go? How do I write mock objects and stubs for testing in Go? Mar 10, 2025 pm 05:38 PM

How do I write mock objects and stubs for testing in Go?

How to convert MySQL query result List into a custom structure slice in Go language? How to convert MySQL query result List into a custom structure slice in Go language? Mar 03, 2025 pm 05:18 PM

How to convert MySQL query result List into a custom structure slice in Go language?

How can I define custom type constraints for generics in Go? How can I define custom type constraints for generics in Go? Mar 10, 2025 pm 03:20 PM

How can I define custom type constraints for generics in Go?

How can I use tracing tools to understand the execution flow of my Go applications? How can I use tracing tools to understand the execution flow of my Go applications? Mar 10, 2025 pm 05:36 PM

How can I use tracing tools to understand the execution flow of my Go applications?

How do you write unit tests in Go? How do you write unit tests in Go? Mar 21, 2025 pm 06:34 PM

How do you write unit tests in Go?

How to write files in Go language conveniently? How to write files in Go language conveniently? Mar 03, 2025 pm 05:15 PM

How to write files in Go language conveniently?

See all articles