src/pkg/os/types.go - The Go Programming Language

Golang

Source file src/pkg/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	// 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	// Perm returns the Unix permission bits in m.
    92	func (m FileMode) Perm() FileMode {
    93		return m & ModePerm
    94	}
    95	
    96	// A fileStat is the implementation of FileInfo returned by Stat and Lstat.
    97	type fileStat struct {
    98		name    string
    99		size    int64
   100		mode    FileMode
   101		modTime time.Time
   102		sys     interface{}
   103	}
   104	
   105	func (fs *fileStat) Name() string       { return fs.name }
   106	func (fs *fileStat) Size() int64        { return fs.size }
   107	func (fs *fileStat) Mode() FileMode     { return fs.mode }
   108	func (fs *fileStat) ModTime() time.Time { return fs.modTime }
   109	func (fs *fileStat) IsDir() bool        { return fs.mode.IsDir() }
   110	func (fs *fileStat) Sys() interface{}   { return fs.sys }
   111	
   112	// SameFile reports whether fi1 and fi2 describe the same file.
   113	// For example, on Unix this means that the device and inode fields
   114	// of the two underlying structures are identical; on other systems
   115	// the decision may be based on the path names.
   116	// SameFile only applies to results returned by this package's Stat.
   117	// It returns false in other cases.
   118	func SameFile(fi1, fi2 FileInfo) bool {
   119		fs1, ok1 := fi1.(*fileStat)
   120		fs2, ok2 := fi2.(*fileStat)
   121		if !ok1 || !ok2 {
   122			return false
   123		}
   124		return sameFile(fs1.sys, fs2.sys)
   125	}