...
Run Format

Source file src/os/types.go

Documentation: os

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

View as plain text