Grief
Grief

Reputation: 2040

Why os.Open returns nil error when opening a directory?

I've spend some time looking for an error in my code and it appeared that at one place I was trying to read contents of a directory like it was the file. Consider the following code:

import (
    "fmt"
    "os"
)

func init() {
    file, err := os.Open("/tmp/")
    fmt.Println(file, err) //err == nil here
    var b []byte
    n, err := file.Read(b)
    fmt.Println(n, err) //err == "read /tmp/: is a directory"
}

I am wondering, why os.Open allows to 'open' a directory without an error if I cannot 'read' it anyway? The documentation says

Open opens the named file for reading. If successful, methods on the returned file can be used for reading; the associated file descriptor has mode O_RDONLY. If there is an error, it will be of type *PathError. [reference]

If the 'directory' is a 'file' is disputable but for me it looks a bit misleading. Is there any usage for that behavior?

Upvotes: 2

Views: 813

Answers (1)

icza
icza

Reputation: 418435

"Reading" has more meanings, one of which is reading the contents of a file.

Another meaning is if the file denotes a directory, you may read its content, which is the list of the files / subfolders in it, using File.Readdir() or File.Readdirnames(). This is perfectly valid for an opened file whose name denotes a directory.

Also you may do a lot more with an opened os.File even if it denotes a folder, e.g. call its File.Chdir() (exclusive for directories) or File.Chmod() methods, or get statistics about it using File.Stat(). I don't see why "opening" a folder should be disallowed. The wording in the doc may not be perfect (or it could be extended to mention this though).

Upvotes: 4

Related Questions