OSDN Git Service

Update to current Go library.
[pf3gnuchains/gcc-fork.git] / libgo / go / os / file_unix.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         "runtime"
9         "syscall"
10 )
11
12 // Auxiliary information if the File describes a directory
13 type dirInfo struct {
14         buf  []byte // buffer for directory I/O
15         dir *syscall.DIR // from opendir
16 }
17
18 // DevNull is the name of the operating system's ``null device.''
19 // On Unix-like systems, it is "/dev/null"; on Windows, "NUL".
20 const DevNull = "/dev/null"
21
22 // Open opens the named file with specified flag (O_RDONLY etc.) and perm, (0666 etc.)
23 // if applicable.  If successful, methods on the returned File can be used for I/O.
24 // It returns the File and an Error, if any.
25 func Open(name string, flag int, perm uint32) (file *File, err Error) {
26         r, e := syscall.Open(name, flag|syscall.O_CLOEXEC, perm)
27         if e != 0 {
28                 return nil, &PathError{"open", name, Errno(e)}
29         }
30
31         // There's a race here with fork/exec, which we are
32         // content to live with.  See ../syscall/exec.go
33         if syscall.O_CLOEXEC == 0 { // O_CLOEXEC not supported
34                 syscall.CloseOnExec(r)
35         }
36
37         return NewFile(r, name), nil
38 }
39
40 // Close closes the File, rendering it unusable for I/O.
41 // It returns an Error, if any.
42 func (file *File) Close() Error {
43         if file == nil || file.fd < 0 {
44                 return EINVAL
45         }
46         var err Error
47         if e := syscall.Close(file.fd); e != 0 {
48                 err = &PathError{"close", file.name, Errno(e)}
49         }
50
51         if file.dirinfo != nil {
52                 if libc_closedir(file.dirinfo.dir) < 0  && err == nil {
53                         err = &PathError{"closedir", file.name, Errno(syscall.GetErrno())}
54                 }
55         }
56
57         file.fd = -1 // so it can't be closed again
58
59         // no need for a finalizer anymore
60         runtime.SetFinalizer(file, nil)
61         return err
62 }
63
64 // Stat returns the FileInfo structure describing file.
65 // It returns the FileInfo and an error, if any.
66 func (file *File) Stat() (fi *FileInfo, err Error) {
67         var stat syscall.Stat_t
68         e := syscall.Fstat(file.fd, &stat)
69         if e != 0 {
70                 return nil, &PathError{"stat", file.name, Errno(e)}
71         }
72         return fileInfoFromStat(file.name, new(FileInfo), &stat, &stat), nil
73 }
74
75 // Readdir reads the contents of the directory associated with file and
76 // returns an array of up to count FileInfo structures, as would be returned
77 // by Lstat, in directory order.  Subsequent calls on the same file will yield
78 // further FileInfos.
79 // A negative count means to read until EOF.
80 // Readdir returns the array and an Error, if any.
81 func (file *File) Readdir(count int) (fi []FileInfo, err Error) {
82         dirname := file.name
83         if dirname == "" {
84                 dirname = "."
85         }
86         dirname += "/"
87         names, err1 := file.Readdirnames(count)
88         if err1 != nil {
89                 return nil, err1
90         }
91         fi = make([]FileInfo, len(names))
92         for i, filename := range names {
93                 fip, err := Lstat(dirname + filename)
94                 if fip == nil || err != nil {
95                         fi[i].Name = filename // rest is already zeroed out
96                 } else {
97                         fi[i] = *fip
98                 }
99         }
100         return
101 }
102
103 // Truncate changes the size of the named file.
104 // If the file is a symbolic link, it changes the size of the link's target.
105 func Truncate(name string, size int64) Error {
106         if e := syscall.Truncate(name, size); e != 0 {
107                 return &PathError{"truncate", name, Errno(e)}
108         }
109         return nil
110 }
111
112 // basename removes trailing slashes and the leading directory name from path name
113 func basename(name string) string {
114         i := len(name) - 1
115         // Remove trailing slashes
116         for ; i > 0 && name[i] == '/'; i-- {
117                 name = name[:i]
118         }
119         // Remove leading directory name
120         for i--; i >= 0; i-- {
121                 if name[i] == '/' {
122                         name = name[i+1:]
123                         break
124                 }
125         }
126
127         return name
128 }