github.com/fawick/restic@v0.1.1-0.20171126184616-c02923fbfc79/internal/fs/file.go (about) 1 package fs 2 3 import ( 4 "io" 5 "os" 6 "path/filepath" 7 ) 8 9 // File is an open file on a file system. 10 type File interface { 11 io.Reader 12 io.Writer 13 io.Closer 14 15 Fd() uintptr 16 Readdirnames(n int) ([]string, error) 17 Readdir(int) ([]os.FileInfo, error) 18 Seek(int64, int) (int64, error) 19 Stat() (os.FileInfo, error) 20 } 21 22 // Mkdir creates a new directory with the specified name and permission bits. 23 // If there is an error, it will be of type *PathError. 24 func Mkdir(name string, perm os.FileMode) error { 25 return os.Mkdir(fixpath(name), perm) 26 } 27 28 // Readlink returns the destination of the named symbolic link. 29 // If there is an error, it will be of type *PathError. 30 func Readlink(name string) (string, error) { 31 return os.Readlink(fixpath(name)) 32 } 33 34 // Remove removes the named file or directory. 35 // If there is an error, it will be of type *PathError. 36 func Remove(name string) error { 37 return os.Remove(fixpath(name)) 38 } 39 40 // RemoveAll removes path and any children it contains. 41 // It removes everything it can but returns the first error 42 // it encounters. If the path does not exist, RemoveAll 43 // returns nil (no error). 44 func RemoveAll(path string) error { 45 return os.RemoveAll(fixpath(path)) 46 } 47 48 // Rename renames (moves) oldpath to newpath. 49 // If newpath already exists, Rename replaces it. 50 // OS-specific restrictions may apply when oldpath and newpath are in different directories. 51 // If there is an error, it will be of type *LinkError. 52 func Rename(oldpath, newpath string) error { 53 return os.Rename(fixpath(oldpath), fixpath(newpath)) 54 } 55 56 // Symlink creates newname as a symbolic link to oldname. 57 // If there is an error, it will be of type *LinkError. 58 func Symlink(oldname, newname string) error { 59 return os.Symlink(fixpath(oldname), fixpath(newname)) 60 } 61 62 // Link creates newname as a hard link to oldname. 63 // If there is an error, it will be of type *LinkError. 64 func Link(oldname, newname string) error { 65 return os.Link(fixpath(oldname), fixpath(newname)) 66 } 67 68 // Stat returns a FileInfo structure describing the named file. 69 // If there is an error, it will be of type *PathError. 70 func Stat(name string) (os.FileInfo, error) { 71 return os.Stat(fixpath(name)) 72 } 73 74 // Lstat returns the FileInfo structure describing the named file. 75 // If the file is a symbolic link, the returned FileInfo 76 // describes the symbolic link. Lstat makes no attempt to follow the link. 77 // If there is an error, it will be of type *PathError. 78 func Lstat(name string) (os.FileInfo, error) { 79 return os.Lstat(fixpath(name)) 80 } 81 82 // Create creates the named file with mode 0666 (before umask), truncating 83 // it if it already exists. If successful, methods on the returned 84 // File can be used for I/O; the associated file descriptor has mode 85 // O_RDWR. 86 // If there is an error, it will be of type *PathError. 87 func Create(name string) (*os.File, error) { 88 return os.Create(fixpath(name)) 89 } 90 91 // Open opens a file for reading. 92 func Open(name string) (File, error) { 93 return os.Open(fixpath(name)) 94 } 95 96 // OpenFile is the generalized open call; most users will use Open 97 // or Create instead. It opens the named file with specified flag 98 // (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful, 99 // methods on the returned File can be used for I/O. 100 // If there is an error, it will be of type *PathError. 101 func OpenFile(name string, flag int, perm os.FileMode) (*os.File, error) { 102 return os.OpenFile(fixpath(name), flag, perm) 103 } 104 105 // Walk walks the file tree rooted at root, calling walkFn for each file or 106 // directory in the tree, including root. All errors that arise visiting files 107 // and directories are filtered by walkFn. The files are walked in lexical 108 // order, which makes the output deterministic but means that for very 109 // large directories Walk can be inefficient. 110 // Walk does not follow symbolic links. 111 func Walk(root string, walkFn filepath.WalkFunc) error { 112 return filepath.Walk(fixpath(root), walkFn) 113 } 114 115 // RemoveIfExists removes a file, returning no error if it does not exist. 116 func RemoveIfExists(filename string) error { 117 err := os.Remove(filename) 118 if err != nil && os.IsNotExist(err) { 119 err = nil 120 } 121 return err 122 }