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?
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.
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.
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.
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.
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.
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.
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⏎
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With