github.com/dara-project/godist@v0.0.0-20200823115410-e0c80c8f0c78/src/os/types.go (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package os
     6  
     7  import (
     8  	"dara"
     9  	"runtime"
    10  	"syscall"
    11  	"time"
    12  )
    13  
    14  // Getpagesize returns the underlying system's memory page size.
    15  func Getpagesize() int {
    16  	size := syscall.Getpagesize()
    17  	// DARA Instrumentation
    18  	// This doesn't actually trap into the OS. Provided by the runtime.
    19  	if runtime.Is_dara_profiling_on() {
    20  		runtime.Dara_Debug_Print(func() { println("[GETPAGESIZE]") })
    21  		retInfo := dara.GeneralType{Type: dara.INTEGER, Integer: size}
    22  		syscallInfo := dara.GeneralSyscall{dara.DSYS_GETPAGESIZE, 0, 1, [10]dara.GeneralType{}, [10]dara.GeneralType{retInfo}}
    23  		runtime.Report_Syscall_To_Scheduler(dara.DSYS_GETPAGESIZE, syscallInfo)
    24  	}
    25  	return size
    26  }
    27  
    28  // File represents an open file descriptor.
    29  type File struct {
    30  	*file // os specific
    31  }
    32  
    33  // A FileInfo describes a file and is returned by Stat and Lstat.
    34  type FileInfo interface {
    35  	Name() string       // base name of the file
    36  	Size() int64        // length in bytes for regular files; system-dependent for others
    37  	Mode() FileMode     // file mode bits
    38  	ModTime() time.Time // modification time
    39  	IsDir() bool        // abbreviation for Mode().IsDir()
    40  	Sys() interface{}   // underlying data source (can return nil)
    41  }
    42  
    43  // A FileMode represents a file's mode and permission bits.
    44  // The bits have the same definition on all systems, so that
    45  // information about files can be moved from one system
    46  // to another portably. Not all bits apply to all systems.
    47  // The only required bit is ModeDir for directories.
    48  type FileMode uint32
    49  
    50  // The defined file mode bits are the most significant bits of the FileMode.
    51  // The nine least-significant bits are the standard Unix rwxrwxrwx permissions.
    52  // The values of these bits should be considered part of the public API and
    53  // may be used in wire protocols or disk representations: they must not be
    54  // changed, although new bits might be added.
    55  const (
    56  	// The single letters are the abbreviations
    57  	// used by the String method's formatting.
    58  	ModeDir        FileMode = 1 << (32 - 1 - iota) // d: is a directory
    59  	ModeAppend                                     // a: append-only
    60  	ModeExclusive                                  // l: exclusive use
    61  	ModeTemporary                                  // T: temporary file; Plan 9 only
    62  	ModeSymlink                                    // L: symbolic link
    63  	ModeDevice                                     // D: device file
    64  	ModeNamedPipe                                  // p: named pipe (FIFO)
    65  	ModeSocket                                     // S: Unix domain socket
    66  	ModeSetuid                                     // u: setuid
    67  	ModeSetgid                                     // g: setgid
    68  	ModeCharDevice                                 // c: Unix character device, when ModeDevice is set
    69  	ModeSticky                                     // t: sticky
    70  
    71  	// Mask for the type bits. For regular files, none will be set.
    72  	ModeType = ModeDir | ModeSymlink | ModeNamedPipe | ModeSocket | ModeDevice
    73  
    74  	ModePerm FileMode = 0777 // Unix permission bits
    75  )
    76  
    77  func (m FileMode) String() string {
    78  	const str = "dalTLDpSugct"
    79  	var buf [32]byte // Mode is uint32.
    80  	w := 0
    81  	for i, c := range str {
    82  		if m&(1<<uint(32-1-i)) != 0 {
    83  			buf[w] = byte(c)
    84  			w++
    85  		}
    86  	}
    87  	if w == 0 {
    88  		buf[w] = '-'
    89  		w++
    90  	}
    91  	const rwx = "rwxrwxrwx"
    92  	for i, c := range rwx {
    93  		if m&(1<<uint(9-1-i)) != 0 {
    94  			buf[w] = byte(c)
    95  		} else {
    96  			buf[w] = '-'
    97  		}
    98  		w++
    99  	}
   100  	return string(buf[:w])
   101  }
   102  
   103  // IsDir reports whether m describes a directory.
   104  // That is, it tests for the ModeDir bit being set in m.
   105  func (m FileMode) IsDir() bool {
   106  	return m&ModeDir != 0
   107  }
   108  
   109  // IsRegular reports whether m describes a regular file.
   110  // That is, it tests that no mode type bits are set.
   111  func (m FileMode) IsRegular() bool {
   112  	return m&ModeType == 0
   113  }
   114  
   115  // Perm returns the Unix permission bits in m.
   116  func (m FileMode) Perm() FileMode {
   117  	return m & ModePerm
   118  }
   119  
   120  func (fs *fileStat) Name() string { return fs.name }
   121  func (fs *fileStat) IsDir() bool  { return fs.Mode().IsDir() }
   122  
   123  // SameFile reports whether fi1 and fi2 describe the same file.
   124  // For example, on Unix this means that the device and inode fields
   125  // of the two underlying structures are identical; on other systems
   126  // the decision may be based on the path names.
   127  // SameFile only applies to results returned by this package's Stat.
   128  // It returns false in other cases.
   129  func SameFile(fi1, fi2 FileInfo) bool {
   130  	fs1, ok1 := fi1.(*fileStat)
   131  	fs2, ok2 := fi2.(*fileStat)
   132  	if !ok1 || !ok2 {
   133  		return false
   134  	}
   135  	return sameFile(fs1, fs2)
   136  }