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: ModeType is missing os.ModeCharDevice #27640

Closed
somersf opened this issue Sep 12, 2018 · 2 comments
Closed

os: ModeType is missing os.ModeCharDevice #27640

somersf opened this issue Sep 12, 2018 · 2 comments

Comments

@somersf
Copy link
Contributor

@somersf somersf commented Sep 12, 2018

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

go1.11

Does this issue reproduce with the latest release?

Yes

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

GOARCH="amd64"
GOOS="darwin"
and
GOARCH="amd64"
GOOS="linux"

What did you do?

I stat-ed a character device and analyzed the returned mode.

The os/stat_linux.go and os/stat_darwin.go implementations map a system stat value of S_IFCHR to ModeDevice | ModeCharDevice. I did not check other platforms.

However, the ModeType mask defined in os/type.go (https://golang.org/pkg/os/#FileMode) doesn't include the ModeCharDevice bit.
Masking a mode with ModeType in order to recover the type from the mode doesn't allow ModeDevice | ModeCharDevice to be recovered.

package main

import (
    "fmt"
    "os"
)

func main() {
    path := "/dev/zero"
    info, err := os.Lstat(path)
    if err != nil {
        fmt.Printf("stat failed; %v\n", err)
        return
    }
    mode := info.Mode()

    const charDev = os.ModeDevice | os.ModeCharDevice
    if mode & charDev == charDev {
        fmt.Printf("%s has character device bits set\n", path)
    }

    if mode & os.ModeType == charDev {
        fmt.Printf("%s type is character device\n", path)
    } else if mode & os.ModeType == os.ModeDevice {
        fmt.Printf("%s type is other device\n", path)
    }
}

https://play.golang.org/p/lcciw1JFCtj

What did you expect to see?

/dev/zero has character device bits set
/dev/zero type is character device

What did you see instead?

/dev/zero has character device bits set
/dev/zero type is other device
@ianlancetaylor ianlancetaylor changed the title os.ModeType is missing os.ModeCharDevice os: ModeType is missing os.ModeCharDevice Sep 12, 2018
@ianlancetaylor ianlancetaylor added this to the Go1.12 milestone Sep 12, 2018
@tklauser

This comment has been minimized.

Copy link
Member

@tklauser tklauser commented Sep 12, 2018

FWIW, in archive/zip.fileModeToUnixMode this also leads to the character device bit not represented correctly.

os.ModeCharDevice was added by @rsc in https://golang.org/cl/5531052, but nothing indicates why it was omitted from os.ModeType. Maybe just an oversight?

@gopherbot

This comment has been minimized.

Copy link

@gopherbot gopherbot commented Sep 12, 2018

Change https://golang.org/cl/135075 mentions this issue: os: add ModeCharDevice to ModeType

@gopherbot gopherbot closed this in a2a3dd0 Sep 13, 2018
rillig added a commit to rillig/pkglint that referenced this issue Jun 19, 2019
See golang/go#27640, which was fixed in
Go 1.12.
@golang golang locked and limited conversation to collaborators Sep 13, 2019
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
4 participants
You can’t perform that action at this time.