src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/mods/os/stat.go (about)

     1  package os
     2  
     3  import (
     4  	"fmt"
     5  	"io/fs"
     6  
     7  	"src.elv.sh/pkg/eval/vals"
     8  )
     9  
    10  var typeNames = map[fs.FileMode]string{
    11  	0:                                 "regular",
    12  	fs.ModeDir:                        "dir",
    13  	fs.ModeSymlink:                    "symlink",
    14  	fs.ModeNamedPipe:                  "named-pipe",
    15  	fs.ModeSocket:                     "socket",
    16  	fs.ModeDevice:                     "device",
    17  	fs.ModeDevice | fs.ModeCharDevice: "char-device",
    18  	fs.ModeIrregular:                  "irregular",
    19  }
    20  
    21  // Implementation of the stat function itself is in os.go.
    22  
    23  func statMap(fi fs.FileInfo) vals.Map {
    24  	mode := fi.Mode()
    25  	typeName, ok := typeNames[mode.Type()]
    26  	if !ok {
    27  		// This shouldn't happen, but if there is a bug this gives us a bit of
    28  		// information.
    29  		typeName = fmt.Sprintf("unknown %d", mode.Type())
    30  	}
    31  	return vals.MakeMap(
    32  		"name", fi.Name(),
    33  		"size", vals.Int64ToNum(fi.Size()),
    34  		"type", typeName,
    35  		"perm", int(mode&fs.ModePerm),
    36  		"special-modes", specialModesToList(mode),
    37  		"sys", statSysMap(fi.Sys()))
    38  	// TODO: ModTime
    39  }