1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
package internal
import (
"os"
)
// NOTE: taken from amd64 Linux
type Timespec struct {
Sec int64
Nsec int64
}
type Stat_t struct {
Dev uint64
Ino uint64
Nlink uint64
Mode uint32
Uid uint32
Gid uint32
Rdev uint64
Size int64
Blksize int64
Blocks int64
Atim Timespec
Mtim Timespec
Ctim Timespec
}
// InfoToStat takes a platform native FileInfo and converts it into a 9P2000.L compatible Stat_t
func InfoToStat(fi os.FileInfo) *Stat_t {
return &Stat_t{
Size: fi.Size(),
Mode: uint32(modeFromOS(fi.Mode())),
Mtim: Timespec{
Sec: fi.ModTime().Unix(),
Nsec: fi.ModTime().UnixNano(),
},
}
}
// TODO: copied from pkg p9
// we should probably migrate the OS methods from p9 into sys
const (
FileModeMask uint32 = 0170000
ModeSocket = 0140000
ModeSymlink = 0120000
ModeRegular = 0100000
ModeBlockDevice = 060000
ModeDirectory = 040000
ModeCharacterDevice = 020000
ModeNamedPipe = 010000
)
func modeFromOS(mode os.FileMode) uint32 {
m := uint32(mode.Perm())
switch {
case mode.IsDir():
m |= ModeDirectory
case mode&os.ModeSymlink != 0:
m |= ModeSymlink
case mode&os.ModeSocket != 0:
m |= ModeSocket
case mode&os.ModeNamedPipe != 0:
m |= ModeNamedPipe
case mode&os.ModeCharDevice != 0:
m |= ModeCharacterDevice
case mode&os.ModeDevice != 0:
m |= ModeBlockDevice
default:
m |= ModeRegular
}
return m
}
|