...
Run Format

Source file src/os/types.go

     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 (not backed up)
    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	
    58		// Mask for the type bits. For regular files, none will be set.
    59		ModeType = ModeDir | ModeSymlink | ModeNamedPipe | ModeSocket | ModeDevice
    60	
    61		ModePerm FileMode = 0777 // Unix permission bits
    62	)
    63	
    64	func (m FileMode) String() string {
    65		const str = "dalTLDpSugct"
    66		var buf [32]byte // Mode is uint32.
    67		w := 0
    68		for i, c := range str {
    69			if m&(1<<uint(32-1-i)) != 0 {
    70				buf[w] = byte(c)
    71				w++
    72			}
    73		}
    74		if w == 0 {
    75			buf[w] = '-'
    76			w++
    77		}
    78		const rwx = "rwxrwxrwx"
    79		for i, c := range rwx {
    80			if m&(1<<uint(9-1-i)) != 0 {
    81				buf[w] = byte(c)
    82			} else {
    83				buf[w] = '-'
    84			}
    85			w++
    86		}
    87		return string(buf[:w])
    88	}
    89	
    90	// IsDir reports whether m describes a directory.
    91	// That is, it tests for the ModeDir bit being set in m.
    92	func (m FileMode) IsDir() bool {
    93		return m&ModeDir != 0
    94	}
    95	
    96	// IsRegular reports whether m describes a regular file.
    97	// That is, it tests that no mode type bits are set.
    98	func (m FileMode) IsRegular() bool {
    99		return m&ModeType == 0
   100	}
   101	
   102	// Perm returns the Unix permission bits in m.
   103	func (m FileMode) Perm() FileMode {
   104		return m & ModePerm
   105	}
   106	
   107	func (fs *fileStat) Name() string { return fs.name }
   108	func (fs *fileStat) IsDir() bool  { return fs.Mode().IsDir() }
   109	
   110	// SameFile reports whether fi1 and fi2 describe the same file.
   111	// For example, on Unix this means that the device and inode fields
   112	// of the two underlying structures are identical; on other systems
   113	// the decision may be based on the path names.
   114	// SameFile only applies to results returned by this package's Stat.
   115	// It returns false in other cases.
   116	func SameFile(fi1, fi2 FileInfo) bool {
   117		fs1, ok1 := fi1.(*fileStat)
   118		fs2, ok2 := fi2.(*fileStat)
   119		if !ok1 || !ok2 {
   120			return false
   121		}
   122		return sameFile(fs1, fs2)
   123	}
   124	

View as plain text