OLD | NEW |
(Empty) | |
| 1 // Copyright 2011 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 // This file defines abstract file system access. |
| 6 |
| 7 package main |
| 8 |
| 9 import ( |
| 10 "io" |
| 11 "io/ioutil" |
| 12 "os" |
| 13 ) |
| 14 |
| 15 |
| 16 // The FileInfo interface provides access to file information. |
| 17 type FileInfo interface { |
| 18 Name() string |
| 19 Size() int64 |
| 20 IsRegular() bool |
| 21 IsDirectory() bool |
| 22 } |
| 23 |
| 24 |
| 25 // The FileSystem interface specifies the methods godoc is using |
| 26 // to access the file system for which it serves documentation. |
| 27 type FileSystem interface { |
| 28 Open(path string) (io.ReadCloser, os.Error) |
| 29 Lstat(path string) (FileInfo, os.Error) |
| 30 Stat(path string) (FileInfo, os.Error) |
| 31 ReadDir(path string) ([]FileInfo, os.Error) |
| 32 ReadFile(path string) ([]byte, os.Error) |
| 33 } |
| 34 |
| 35 |
| 36 // ---------------------------------------------------------------------------- |
| 37 // OS-specific FileSystem implementation |
| 38 |
| 39 var OS FileSystem = osFS{} |
| 40 |
| 41 |
| 42 // osFI is the OS-specific implementation of FileInfo. |
| 43 type osFI struct { |
| 44 *os.FileInfo |
| 45 } |
| 46 |
| 47 |
| 48 func (fi osFI) Name() string { |
| 49 return fi.FileInfo.Name |
| 50 } |
| 51 |
| 52 |
| 53 func (fi osFI) Size() int64 { |
| 54 if fi.IsDirectory() { |
| 55 return 0 |
| 56 } |
| 57 return fi.FileInfo.Size |
| 58 } |
| 59 |
| 60 |
| 61 // osFS is the OS-specific implementation of FileSystem |
| 62 type osFS struct{} |
| 63 |
| 64 func (osFS) Open(path string) (io.ReadCloser, os.Error) { |
| 65 return os.Open(path) |
| 66 } |
| 67 |
| 68 |
| 69 func (osFS) Lstat(path string) (FileInfo, os.Error) { |
| 70 fi, err := os.Lstat(path) |
| 71 return osFI{fi}, err |
| 72 } |
| 73 |
| 74 |
| 75 func (osFS) Stat(path string) (FileInfo, os.Error) { |
| 76 fi, err := os.Stat(path) |
| 77 return osFI{fi}, err |
| 78 } |
| 79 |
| 80 |
| 81 func (osFS) ReadDir(path string) ([]FileInfo, os.Error) { |
| 82 l0, err := ioutil.ReadDir(path) |
| 83 if err != nil { |
| 84 return nil, err |
| 85 } |
| 86 l1 := make([]FileInfo, len(l0)) |
| 87 for i, e := range l0 { |
| 88 l1[i] = osFI{e} |
| 89 } |
| 90 return l1, nil |
| 91 } |
| 92 |
| 93 |
| 94 func (osFS) ReadFile(path string) ([]byte, os.Error) { |
| 95 return ioutil.ReadFile(path) |
| 96 } |
OLD | NEW |