github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/src/pkg/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  	"syscall"
     9  	"time"
    10  )
    11  
    12  // Getpagesize returns the underlying system's memory page size.
    13  func Getpagesize() int { return syscall.Getpagesize() }
    14  
    15  // A FileInfo describes a file and is returned by Stat and Lstat.
    16  type FileInfo interface {
    17  	Name() string       // base name of the file
    18  	Size() int64        // length in bytes for regular files; system-dependent for others
    19  	Mode() FileMode     // file mode bits
    20  	ModTime() time.Time // modification time
    21  	IsDir() bool        // abbreviation for Mode().IsDir()
    22  	Sys() interface{}   // underlying data source (can return nil)
    23  }
    24  
    25  // A FileMode represents a file's mode and permission bits.
    26  // The bits have the same definition on all systems, so that
    27  // information about files can be moved from one system
    28  // to another portably.  Not all bits apply to all systems.
    29  // The only required bit is ModeDir for directories.
    30  type FileMode uint32
    31  
    32  // The defined file mode bits are the most significant bits of the FileMode.
    33  // The nine least-significant bits are the standard Unix rwxrwxrwx permissions.
    34  // The values of these bits should be considered part of the public API and
    35  // may be used in wire protocols or disk representations: they must not be
    36  // changed, although new bits might be added.
    37  const (
    38  	// The single letters are the abbreviations
    39  	// used by the String method's formatting.
    40  	ModeDir        FileMode = 1 << (32 - 1 - iota) // d: is a directory
    41  	ModeAppend                                     // a: append-only
    42  	ModeExclusive                                  // l: exclusive use
    43  	ModeTemporary                                  // T: temporary file (not backed up)
    44  	ModeSymlink                                    // L: symbolic link
    45  	ModeDevice                                     // D: device file
    46  	ModeNamedPipe                                  // p: named pipe (FIFO)
    47  	ModeSocket                                     // S: Unix domain socket
    48  	ModeSetuid                                     // u: setuid
    49  	ModeSetgid                                     // g: setgid
    50  	ModeCharDevice                                 // c: Unix character device, when ModeDevice is set
    51  	ModeSticky                                     // t: sticky
    52  
    53  	// Mask for the type bits. For regular files, none will be set.
    54  	ModeType = ModeDir | ModeSymlink | ModeNamedPipe | ModeSocket | ModeDevice
    55  
    56  	ModePerm FileMode = 0777 // permission bits
    57  )
    58  
    59  func (m FileMode) String() string {
    60  	const str = "dalTLDpSugct"
    61  	var buf [32]byte // Mode is uint32.
    62  	w := 0
    63  	for i, c := range str {
    64  		if m&(1<<uint(32-1-i)) != 0 {
    65  			buf[w] = byte(c)
    66  			w++
    67  		}
    68  	}
    69  	if w == 0 {
    70  		buf[w] = '-'
    71  		w++
    72  	}
    73  	const rwx = "rwxrwxrwx"
    74  	for i, c := range rwx {
    75  		if m&(1<<uint(9-1-i)) != 0 {
    76  			buf[w] = byte(c)
    77  		} else {
    78  			buf[w] = '-'
    79  		}
    80  		w++
    81  	}
    82  	return string(buf[:w])
    83  }
    84  
    85  // IsDir reports whether m describes a directory.
    86  // That is, it tests for the ModeDir bit being set in m.
    87  func (m FileMode) IsDir() bool {
    88  	return m&ModeDir != 0
    89  }
    90  
    91  // IsRegular reports whether m describes a regular file.
    92  // That is, it tests that no mode type bits are set.
    93  func (m FileMode) IsRegular() bool {
    94  	return m&ModeType == 0
    95  }
    96  
    97  // Perm returns the Unix permission bits in m.
    98  func (m FileMode) Perm() FileMode {
    99  	return m & ModePerm
   100  }
   101  
   102  func (fs *fileStat) Name() string { return fs.name }
   103  func (fs *fileStat) IsDir() bool  { return fs.Mode().IsDir() }
   104  
   105  // SameFile reports whether fi1 and fi2 describe the same file.
   106  // For example, on Unix this means that the device and inode fields
   107  // of the two underlying structures are identical; on other systems
   108  // the decision may be based on the path names.
   109  // SameFile only applies to results returned by this package's Stat.
   110  // It returns false in other cases.
   111  func SameFile(fi1, fi2 FileInfo) bool {
   112  	fs1, ok1 := fi1.(*fileStat)
   113  	fs2, ok2 := fi2.(*fileStat)
   114  	if !ok1 || !ok2 {
   115  		return false
   116  	}
   117  	return sameFile(fs1, fs2)
   118  }