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