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

os: File.Truncate reports unhelpful error in Linux. #9640

Closed
Zilog8 opened this issue Jan 20, 2015 · 5 comments
Closed

os: File.Truncate reports unhelpful error in Linux. #9640

Zilog8 opened this issue Jan 20, 2015 · 5 comments
Assignees

Comments

@Zilog8
Copy link

@Zilog8 Zilog8 commented Jan 20, 2015

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

go version go1.4 linux/386
go version go1.4 windows/amd64

What operating system and processor architecture are you using?

Debian Wheezy, x86
Windows 7, x64

What did you do?

    filepath := "/play/p2/test2.txt"
    file, err := os.Open(filepath)
    if err != nil {
        fmt.Println("Error 1", err)
    }
    err = file.Truncate(0)
    if err != nil {
        fmt.Println("Error 2", err)
    }
    err = file.Close()
    if err != nil {
        fmt.Println("Error 3", err)
    }

What did you expect to see?

Error 2 truncate /play/p2/test2.txt: Access is denied

What did you see instead?

Error 2 truncate /play/p2/test2.txt: invalid argument

Executing Truncate(int) rightfully results in an error because os.Open() yields a read-only file handle.
However, in windows this error is helpful (the "expect to see" above), while in linux it is not helpful (the "see instead" above)

@bradfitz

This comment has been minimized.

Copy link
Contributor

@bradfitz bradfitz commented Jan 20, 2015

Assigning to @ianlancetaylor to decide if this is something we should care about. Where does this end? There are surely tons of different error messages between operating systems.

@Zilog8

This comment has been minimized.

Copy link
Author

@Zilog8 Zilog8 commented Jan 20, 2015

I agree it's not exactly a critical issue :-). However, precise and accurate error messages are very useful. I just spent a few hours trying to figure out why my code wasn't working (my project involves joining various filesystems through an abstraction layer, which is then shared over NFS, so it could've been a couple of different things going wrong), until I decided to try various snippets of it on a windows machine. Once I had the windows error message it was just a few moments before I knew exactly what was wrong and why.

My issue isn't that the messages are different. It's that the message in Linux is so un-useful.

@minux

This comment has been minimized.

Copy link
Member

@minux minux commented Jan 20, 2015

That's how Linux reports error when you've provided a read only file
descriptor to an operation that requires writing permission. (See man 2
ftruncate)

Actually, if you look at the man page, EBADF / EINVAL is only used for the
"fd is not open for writing error", so the error is not ambiguous.

It's not feasible for the os package to try to provide better error message
than what the underlying syscall provides in every case.

PS: when debugging this kind of issue, I suggest using strace.

@Zilog8

This comment has been minimized.

Copy link
Author

@Zilog8 Zilog8 commented Jan 20, 2015

Hmm.. the documentation that I read showed:

 EINVAL
    The argument length is negative or larger than the maximum file size.

However, further down on the page it does say, which I hadn't noticed earlier:

    EBADF or EINVAL
    fd is not open for writing.
    EINVAL
    fd does not reference a regular file.

Well, if Linux itself is what provides the ambiguous error, I guess there's not much to be done. Adding extra logic just for this is probably not worth it.

@minux

This comment has been minimized.

Copy link
Member

@minux minux commented Jan 20, 2015

The first part of the man page is for truncate(2), and the 2nd part is
for ftruncate(2).

Close as nothing to do.

@minux minux closed this Jan 20, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.