afero.go (3355B)
1 // Copyright © 2014 Steve Francia <spf@spf13.com>. 2 // Copyright 2013 tsuru authors. All rights reserved. 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package afero provides types and methods for interacting with the filesystem, 16 // as an abstraction layer. 17 18 // Afero also provides a few implementations that are mostly interoperable. One that 19 // uses the operating system filesystem, one that uses memory to store files 20 // (cross platform) and an interface that should be implemented if you want to 21 // provide your own filesystem. 22 23 package afero 24 25 import ( 26 "errors" 27 "io" 28 "os" 29 "time" 30 ) 31 32 type Afero struct { 33 Fs 34 } 35 36 // File represents a file in the filesystem. 37 type File interface { 38 io.Closer 39 io.Reader 40 io.ReaderAt 41 io.Seeker 42 io.Writer 43 io.WriterAt 44 45 Name() string 46 Readdir(count int) ([]os.FileInfo, error) 47 Readdirnames(n int) ([]string, error) 48 Stat() (os.FileInfo, error) 49 Sync() error 50 Truncate(size int64) error 51 WriteString(s string) (ret int, err error) 52 } 53 54 // Fs is the filesystem interface. 55 // 56 // Any simulated or real filesystem should implement this interface. 57 type Fs interface { 58 // Create creates a file in the filesystem, returning the file and an 59 // error, if any happens. 60 Create(name string) (File, error) 61 62 // Mkdir creates a directory in the filesystem, return an error if any 63 // happens. 64 Mkdir(name string, perm os.FileMode) error 65 66 // MkdirAll creates a directory path and all parents that does not exist 67 // yet. 68 MkdirAll(path string, perm os.FileMode) error 69 70 // Open opens a file, returning it or an error, if any happens. 71 Open(name string) (File, error) 72 73 // OpenFile opens a file using the given flags and the given mode. 74 OpenFile(name string, flag int, perm os.FileMode) (File, error) 75 76 // Remove removes a file identified by name, returning an error, if any 77 // happens. 78 Remove(name string) error 79 80 // RemoveAll removes a directory path and any children it contains. It 81 // does not fail if the path does not exist (return nil). 82 RemoveAll(path string) error 83 84 // Rename renames a file. 85 Rename(oldname, newname string) error 86 87 // Stat returns a FileInfo describing the named file, or an error, if any 88 // happens. 89 Stat(name string) (os.FileInfo, error) 90 91 // The name of this FileSystem 92 Name() string 93 94 // Chmod changes the mode of the named file to mode. 95 Chmod(name string, mode os.FileMode) error 96 97 // Chown changes the uid and gid of the named file. 98 Chown(name string, uid, gid int) error 99 100 // Chtimes changes the access and modification times of the named file 101 Chtimes(name string, atime time.Time, mtime time.Time) error 102 } 103 104 var ( 105 ErrFileClosed = errors.New("File is closed") 106 ErrOutOfRange = errors.New("out of range") 107 ErrTooLarge = errors.New("too large") 108 ErrFileNotFound = os.ErrNotExist 109 ErrFileExists = os.ErrExist 110 ErrDestinationExists = os.ErrExist 111 )