Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go "permission denied" when trying to create a file in a newly created directory?

I'm trying to create a directory using os.Mkdir() and then create files in it, similar to this script:

package main

import (
    "log"
    "os"
    "path"
)

func main() {
    dir := "test_dir"

    os.Mkdir(dir, os.ModeDir)

    fileName := path.Join(dir, "file.txt")

    _, err := os.Create(fileName)
    if err != nil {
        log.Fatalf("create file: %v", err)
    }
}

However, if I run this, I get

> go run fileperms.go
2019/10/15 14:44:02 create file: open test_dir/file.txt: permission denied
exit status 1

It is not immediately clear to me from https://golang.org/pkg/os/#FileMode how to specify the FileMode in order to allow the same script to create files in the newly-created directory. How can I achieve this?

like image 768
Kurt Peek Avatar asked Oct 15 '19 21:10

Kurt Peek


People also ask

Why can't I create a directory permission denied?

Overview. If you receive an error telling you that you do not have permissions to create a directory or to write a file to a directory, this is likely an indication that your script is attempting to write to a directory that you do not own.

How do I fix permissions denied?

To fix the permission denied error in Linux, one needs to change the file permission of the script. Use the “chmod” (change mode) command for this purpose. But before that, check the file permission.

Why do I get permission denied when creating a directory?

Permission Denied When Creating Directory or Writing a File. If you receive an error telling you that you do not have permissions to create a directory or to write a file to a directory then this is likely an indication that your script is attempting to write to a directory that the user running the build does not own.

Why can't I create a file in the Desktop directory?

The error occurs because of a permissions issue. You wish to create a file in your users Desktop directory. Your script is not being run by your user it is being run by apahce's user, which is ussually www-data. This user most likely does not have permission to create files in your users home directory.

How to grant permission to the my Documents folder?

Note: Later, turn on the security software. You may also try to grant permission to the My Documents folder. Thereafter, try to create the folder. You may follow these steps. a. Right click on drive and select “Properties” from Context Menu. b. Click on Edit button in Properties windows Click ok to confirm UAC elevation request.

Why am I unable to write to a directory?

If you receive an error telling you that you do not have permissions to create a directory or to write a file to a directory then this is likely an indication that your script is attempting to write to a directory that the user running the build does not own.


2 Answers

I found that I am able to create files in the directory if I set the permission to 0777:

package main

import (
    "io/ioutil"
    "os"
    "path"
)

func main() {
    dir := "test_dir"

    os.Mkdir(dir, 0777)

    fileName := path.Join(dir, "file.txt")

    ioutil.WriteFile(fileName, []byte("foobar"), 0666)
}

Now the file is created with the expected content:

> cat test_dir/file.txt 
foobar⏎ 
like image 146
Kurt Peek Avatar answered Sep 20 '22 07:09

Kurt Peek


Here, Go was trying to create inside /tmp directory during an AUR package installation.

So I've changed the permissions in /tmp:

chmod 0777 -R /tmp

But that was not enough, so I had to change /tmp ownership (it was root's):

sudo chown -R "$USER":wheel /tmp
like image 21
Caleb Santos Avatar answered Sep 22 '22 07:09

Caleb Santos