github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/os/stat_linuxlike.go (about)

     1  //go:build (linux && !baremetal) || wasip1
     2  
     3  // Copyright 2009 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  // Note: this file is used for both Linux and WASI.
     8  // Eventually it might be better to spit it up, and make the syscall constants
     9  // match the typical WASI constants instead of the Linux-equivalents used here.
    10  
    11  package os
    12  
    13  import (
    14  	"syscall"
    15  	"time"
    16  )
    17  
    18  func fillFileStatFromSys(fs *fileStat, name string) {
    19  	fs.name = basename(name)
    20  	fs.size = fs.sys.Size
    21  	fs.modTime = timespecToTime(fs.sys.Mtim)
    22  	fs.mode = FileMode(fs.sys.Mode & 0777)
    23  	switch fs.sys.Mode & syscall.S_IFMT {
    24  	case syscall.S_IFBLK:
    25  		fs.mode |= ModeDevice
    26  	case syscall.S_IFCHR:
    27  		fs.mode |= ModeDevice | ModeCharDevice
    28  	case syscall.S_IFDIR:
    29  		fs.mode |= ModeDir
    30  	case syscall.S_IFIFO:
    31  		fs.mode |= ModeNamedPipe
    32  	case syscall.S_IFLNK:
    33  		fs.mode |= ModeSymlink
    34  	case syscall.S_IFREG:
    35  		// nothing to do
    36  	case syscall.S_IFSOCK:
    37  		fs.mode |= ModeSocket
    38  	}
    39  	if fs.sys.Mode&syscall.S_ISGID != 0 {
    40  		fs.mode |= ModeSetgid
    41  	}
    42  	if fs.sys.Mode&syscall.S_ISUID != 0 {
    43  		fs.mode |= ModeSetuid
    44  	}
    45  	if fs.sys.Mode&syscall.S_ISVTX != 0 {
    46  		fs.mode |= ModeSticky
    47  	}
    48  }
    49  
    50  func timespecToTime(ts syscall.Timespec) time.Time {
    51  	return time.Unix(int64(ts.Sec), int64(ts.Nsec))
    52  }
    53  
    54  // For testing.
    55  func atime(fi FileInfo) time.Time {
    56  	return timespecToTime(fi.Sys().(*syscall.Stat_t).Atim)
    57  }