Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access is Denied when trying to os.rename a file #29106

Closed
LinkyDev opened this issue Dec 5, 2018 · 7 comments
Closed

Access is Denied when trying to os.rename a file #29106

LinkyDev opened this issue Dec 5, 2018 · 7 comments

Comments

@LinkyDev
Copy link

LinkyDev commented Dec 5, 2018

What version of Go are you using (go version)?

go version go1.11.2 windows/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\omerd\AppData\Local\go-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\omerd\go
set GOPROXY=
set GORACE=
set GOROOT=C:\Go
set GOTMPDIR=
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\omerd\AppData\Local\Temp\go-build225487594=/tmp/go-build -gno-record-gcc-switches

What did you do?

main.go https://play.golang.org/p/byFvCdNZ6WA
server-config-handler.go https://play.golang.org/p/NT0YNaDCzfh
server-config.json https://play.golang.org/p/f0MMU9F7t6-

Im trying to create an http website server that has the ability to upload files and save them on the server storage

After troubleshooting for 2hours I believe the problem is somehow connected to the os.Rename permission (Im a new Go programmer so I dont know for sure)

What did you expect to see?

Expected to see the newly created file moved to the "/srv/file-storage/testdir/" folder

What did you see instead?

2018/12/05 02:34:58 test.txt
File created succesffuly
2018/12/05 02:34:58 Wrote 617 bytes.
2018/12/05 02:34:58 rename test.txt /srv/file-storage/testdir/: Access is denied.
2018/12/05 02:34:58 An error occurred while renaming the file directory! ERROR:004
exit status 2

@ianlancetaylor
Copy link
Contributor

This is very unlikely to be a problem with Go. Are you able to run

mv my-new-file /srv/file-storage/testdir/

while running as the same user as the Go program?

@LinkyDev
Copy link
Author

LinkyDev commented Dec 5, 2018

This is very unlikely to be a problem with Go. Are you able to run

mv my-new-file /srv/file-storage/testdir/

while running as the same user as the Go program?

Tried it on Cmder bash and cmd and same result:
"mv: cannot stat 'new-file': No such file or directory"

@ianlancetaylor
Copy link
Contributor

When I wrote my-new-file I meant that it would be the name of some real file.

@LinkyDev
Copy link
Author

LinkyDev commented Dec 5, 2018

When I wrote my-new-file I meant that it would be the name of some real file.

When I try this command:
"mv test.txt /srv/file-storage/testdir/" its gives me an error - "mv: cannot move 'test.txt' to '/srv/file-storage/testdir/': No such file or directory"

But, when I try "mv test.txt /c/srv/file-storage/testdir/" it moves it with no problem

@LinkyDev
Copy link
Author

LinkyDev commented Dec 5, 2018

When I wrote my-new-file I meant that it would be the name of some real file.

When I try this command:
"mv test.txt /srv/file-storage/testdir/" its gives me an error - "mv: cannot move 'test.txt' to '/srv/file-storage/testdir/': No such file or directory"

But, when I try "mv test.txt /c/srv/file-storage/testdir/" it moves it with no problem

I tried changing the "server-directory" to C:/srv/file-storage/testdir or /c/srv/file-storage/testdir but still the same issue

@LinkyDev
Copy link
Author

LinkyDev commented Dec 5, 2018

I found what was my problem!
I added the filename to the end of the new path:
err := os.Rename(filename, path+filename)
instead of
err := os.Rename(filename, path)

I dont know how i havent tought about it before but Im glad I did, finally I can go to sleep :)
thank you ian for the help btw

@agnivade agnivade closed this as completed Dec 5, 2018
@ianlancetaylor
Copy link
Contributor

@LinkyDev Thanks for following up.

@golang golang locked and limited conversation to collaborators Dec 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants