OSDN Git Service

libgo: Update to weekly.2012-01-15.
[pf3gnuchains/gcc-fork.git] / libgo / go / 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
19         Mode() FileMode     // file mode bits
20         ModTime() time.Time // modification time
21         IsDir() bool        // abbreviation for Mode().IsDir()
22 }
23
24 // A FileMode represents a file's mode and permission bits.
25 // The bits have the same definition on all systems, so that
26 // information about files can be moved from one system
27 // to another portably.  Not all bits apply to all systems.
28 // The only required bit is ModeDir for directories.
29 type FileMode uint32
30
31 // The defined file mode bits are the most significant bits of the FileMode.
32 // The nine least-significant bits are the standard Unix rwxrwxrwx permissions.
33 // The values of these bits should be considered part of the public API and
34 // may be used in wire protocols or disk representations: they must not be
35 // changed, although new bits might be added.
36 const (
37         // The single letters are the abbreviations
38         // used by the String method's formatting.
39         ModeDir        FileMode = 1 << (32 - 1 - iota) // d: is a directory
40         ModeAppend                                     // a: append-only
41         ModeExclusive                                  // l: exclusive use
42         ModeTemporary                                  // t: temporary file (not backed up)
43         ModeSymlink                                    // L: symbolic link
44         ModeDevice                                     // D: device file
45         ModeNamedPipe                                  // p: named pipe (FIFO)
46         ModeSocket                                     // S: Unix domain socket
47         ModeSetuid                                     // u: setuid
48         ModeSetgid                                     // g: setgid
49         ModeCharDevice                                 // c: Unix character device, when ModeDevice is set
50
51         // Mask for the type bits. For regular files, none will be set.
52         ModeType = ModeDir | ModeSymlink | ModeNamedPipe | ModeSocket | ModeDevice
53
54         ModePerm FileMode = 0777 // permission bits
55 )
56
57 func (m FileMode) String() string {
58         const str = "daltLDpSugc"
59         var buf [20]byte
60         w := 0
61         for i, c := range str {
62                 if m&(1<<uint(32-1-i)) != 0 {
63                         buf[w] = byte(c)
64                         w++
65                 }
66         }
67         if w == 0 {
68                 buf[w] = '-'
69                 w++
70         }
71         const rwx = "rwxrwxrwx"
72         for i, c := range rwx {
73                 if m&(1<<uint(9-1-i)) != 0 {
74                         buf[w] = byte(c)
75                 } else {
76                         buf[w] = '-'
77                 }
78                 w++
79         }
80         return string(buf[:w])
81 }
82
83 // IsDir reports whether m describes a directory.
84 // That is, it tests for the ModeDir bit being set in m.
85 func (m FileMode) IsDir() bool {
86         return m&ModeDir != 0
87 }
88
89 // Perm returns the Unix permission bits in m.
90 func (m FileMode) Perm() FileMode {
91         return m & ModePerm
92 }
93
94 // A FileStat is the implementation of FileInfo returned by Stat and Lstat.
95 // Clients that need access to the underlying system-specific stat information
96 // can test for *os.FileStat and then consult the Sys field.
97 type FileStat struct {
98         name    string
99         size    int64
100         mode    FileMode
101         modTime time.Time
102
103         Sys interface{}
104 }
105
106 func (fs *FileStat) Name() string       { return fs.name }
107 func (fs *FileStat) Size() int64        { return fs.size }
108 func (fs *FileStat) Mode() FileMode     { return fs.mode }
109 func (fs *FileStat) ModTime() time.Time { return fs.modTime }
110 func (fs *FileStat) IsDir() bool        { return fs.mode.IsDir() }
111
112 // SameFile reports whether fs and other 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 func (fs *FileStat) SameFile(other *FileStat) bool {
117         return sameFile(fs, other)
118 }