2009-02-06 18:54:26 -07:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package os
|
|
|
|
|
2011-11-30 10:01:46 -07:00
|
|
|
import (
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
)
|
2009-02-09 13:50:54 -07:00
|
|
|
|
2009-06-03 04:25:34 -06:00
|
|
|
// Getpagesize returns the underlying system's memory page size.
|
2009-12-15 16:40:16 -07:00
|
|
|
func Getpagesize() int { return syscall.Getpagesize() }
|
2009-06-03 04:25:34 -06:00
|
|
|
|
2016-09-05 03:19:30 -06:00
|
|
|
// File represents an open file descriptor.
|
|
|
|
type File struct {
|
|
|
|
*file // os specific
|
|
|
|
}
|
|
|
|
|
2012-07-15 10:48:31 -06:00
|
|
|
// A FileInfo describes a file and is returned by Stat and Lstat.
|
2011-11-30 10:04:16 -07:00
|
|
|
type FileInfo interface {
|
|
|
|
Name() string // base name of the file
|
2012-03-12 14:17:48 -06:00
|
|
|
Size() int64 // length in bytes for regular files; system-dependent for others
|
2011-11-30 10:04:16 -07:00
|
|
|
Mode() FileMode // file mode bits
|
|
|
|
ModTime() time.Time // modification time
|
|
|
|
IsDir() bool // abbreviation for Mode().IsDir()
|
2012-02-02 19:16:18 -07:00
|
|
|
Sys() interface{} // underlying data source (can return nil)
|
2009-02-06 18:54:26 -07:00
|
|
|
}
|
2009-02-09 13:50:54 -07:00
|
|
|
|
2011-11-30 10:04:16 -07:00
|
|
|
// A FileMode represents a file's mode and permission bits.
|
|
|
|
// The bits have the same definition on all systems, so that
|
|
|
|
// information about files can be moved from one system
|
2016-03-01 16:21:55 -07:00
|
|
|
// to another portably. Not all bits apply to all systems.
|
2011-11-30 10:04:16 -07:00
|
|
|
// The only required bit is ModeDir for directories.
|
|
|
|
type FileMode uint32
|
2009-02-09 13:50:54 -07:00
|
|
|
|
2011-11-30 10:04:16 -07:00
|
|
|
// The defined file mode bits are the most significant bits of the FileMode.
|
|
|
|
// The nine least-significant bits are the standard Unix rwxrwxrwx permissions.
|
2012-01-09 15:22:53 -07:00
|
|
|
// The values of these bits should be considered part of the public API and
|
|
|
|
// may be used in wire protocols or disk representations: they must not be
|
|
|
|
// changed, although new bits might be added.
|
2011-11-30 10:04:16 -07:00
|
|
|
const (
|
|
|
|
// The single letters are the abbreviations
|
|
|
|
// used by the String method's formatting.
|
2012-01-09 15:22:53 -07:00
|
|
|
ModeDir FileMode = 1 << (32 - 1 - iota) // d: is a directory
|
|
|
|
ModeAppend // a: append-only
|
|
|
|
ModeExclusive // l: exclusive use
|
2017-06-29 16:47:29 -06:00
|
|
|
ModeTemporary // T: temporary file; Plan 9 only
|
2012-01-09 15:22:53 -07:00
|
|
|
ModeSymlink // L: symbolic link
|
|
|
|
ModeDevice // D: device file
|
|
|
|
ModeNamedPipe // p: named pipe (FIFO)
|
|
|
|
ModeSocket // S: Unix domain socket
|
|
|
|
ModeSetuid // u: setuid
|
|
|
|
ModeSetgid // g: setgid
|
|
|
|
ModeCharDevice // c: Unix character device, when ModeDevice is set
|
2012-01-19 12:29:24 -07:00
|
|
|
ModeSticky // t: sticky
|
2017-11-29 18:11:45 -07:00
|
|
|
ModeIrregular // ?: non-regular file; nothing else is known about this file
|
2009-02-09 13:50:54 -07:00
|
|
|
|
2011-12-01 12:35:43 -07:00
|
|
|
// Mask for the type bits. For regular files, none will be set.
|
2017-11-29 18:11:45 -07:00
|
|
|
ModeType = ModeDir | ModeSymlink | ModeNamedPipe | ModeSocket | ModeDevice | ModeIrregular
|
2011-12-01 12:35:43 -07:00
|
|
|
|
2015-04-14 16:22:02 -06:00
|
|
|
ModePerm FileMode = 0777 // Unix permission bits
|
2011-11-30 10:04:16 -07:00
|
|
|
)
|
2009-02-09 13:50:54 -07:00
|
|
|
|
2011-11-30 10:04:16 -07:00
|
|
|
func (m FileMode) String() string {
|
2017-11-29 18:11:45 -07:00
|
|
|
const str = "dalTLDpSugct?"
|
2012-03-23 15:16:57 -06:00
|
|
|
var buf [32]byte // Mode is uint32.
|
2011-11-30 10:04:16 -07:00
|
|
|
w := 0
|
|
|
|
for i, c := range str {
|
|
|
|
if m&(1<<uint(32-1-i)) != 0 {
|
|
|
|
buf[w] = byte(c)
|
|
|
|
w++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if w == 0 {
|
|
|
|
buf[w] = '-'
|
|
|
|
w++
|
|
|
|
}
|
|
|
|
const rwx = "rwxrwxrwx"
|
|
|
|
for i, c := range rwx {
|
|
|
|
if m&(1<<uint(9-1-i)) != 0 {
|
|
|
|
buf[w] = byte(c)
|
|
|
|
} else {
|
|
|
|
buf[w] = '-'
|
|
|
|
}
|
|
|
|
w++
|
|
|
|
}
|
|
|
|
return string(buf[:w])
|
|
|
|
}
|
2009-02-09 13:50:54 -07:00
|
|
|
|
2011-11-30 10:04:16 -07:00
|
|
|
// IsDir reports whether m describes a directory.
|
|
|
|
// That is, it tests for the ModeDir bit being set in m.
|
|
|
|
func (m FileMode) IsDir() bool {
|
|
|
|
return m&ModeDir != 0
|
|
|
|
}
|
2009-02-09 13:50:54 -07:00
|
|
|
|
2012-11-15 12:46:00 -07:00
|
|
|
// IsRegular reports whether m describes a regular file.
|
|
|
|
// That is, it tests that no mode type bits are set.
|
|
|
|
func (m FileMode) IsRegular() bool {
|
|
|
|
return m&ModeType == 0
|
|
|
|
}
|
|
|
|
|
2011-11-30 10:04:16 -07:00
|
|
|
// Perm returns the Unix permission bits in m.
|
|
|
|
func (m FileMode) Perm() FileMode {
|
|
|
|
return m & ModePerm
|
|
|
|
}
|
2009-02-09 13:50:54 -07:00
|
|
|
|
2013-01-30 23:17:37 -07:00
|
|
|
func (fs *fileStat) Name() string { return fs.name }
|
|
|
|
func (fs *fileStat) IsDir() bool { return fs.Mode().IsDir() }
|
2011-11-30 10:04:16 -07:00
|
|
|
|
2012-02-02 19:16:18 -07:00
|
|
|
// SameFile reports whether fi1 and fi2 describe the same file.
|
2011-11-30 10:04:16 -07:00
|
|
|
// For example, on Unix this means that the device and inode fields
|
|
|
|
// of the two underlying structures are identical; on other systems
|
|
|
|
// the decision may be based on the path names.
|
2012-02-02 19:16:18 -07:00
|
|
|
// SameFile only applies to results returned by this package's Stat.
|
|
|
|
// It returns false in other cases.
|
|
|
|
func SameFile(fi1, fi2 FileInfo) bool {
|
|
|
|
fs1, ok1 := fi1.(*fileStat)
|
|
|
|
fs2, ok2 := fi2.(*fileStat)
|
|
|
|
if !ok1 || !ok2 {
|
|
|
|
return false
|
|
|
|
}
|
2013-01-30 23:17:37 -07:00
|
|
|
return sameFile(fs1, fs2)
|
2011-11-30 10:04:16 -07:00
|
|
|
}
|