浏览文件
request-interfaces.go
159 行 · 6.3 KB历史下载| 1 | package sftp |
| 2 | |
| 3 | import ( |
| 4 | "io" |
| 5 | "os" |
| 6 | ) |
| 7 | |
| 8 | // WriterAtReaderAt defines the interface to return when a file is to |
| 9 | // be opened for reading and writing |
| 10 | type WriterAtReaderAt interface { |
| 11 | io.WriterAt |
| 12 | io.ReaderAt |
| 13 | } |
| 14 | |
| 15 | // Interfaces are differentiated based on required returned values. |
| 16 | // All input arguments are to be pulled from Request (the only arg). |
| 17 | |
| 18 | // The Handler interfaces all take the Request object as its only argument. |
| 19 | // All the data you should need to handle the call are in the Request object. |
| 20 | // The request.Method attribute is initially the most important one as it |
| 21 | // determines which Handler gets called. |
| 22 | |
| 23 | // FileReader should return an io.ReaderAt for the filepath |
| 24 | // Note in cases of an error, the error text will be sent to the client. |
| 25 | // Called for Methods: Get |
| 26 | type FileReader interface { |
| 27 | Fileread(*Request) (io.ReaderAt, error) |
| 28 | } |
| 29 | |
| 30 | // FileWriter should return an io.WriterAt for the filepath. |
| 31 | // |
| 32 | // The request server code will call Close() on the returned io.WriterAt |
| 33 | // object if an io.Closer type assertion succeeds. |
| 34 | // Note in cases of an error, the error text will be sent to the client. |
| 35 | // Note when receiving an Append flag it is important to not open files using |
| 36 | // O_APPEND if you plan to use WriteAt, as they conflict. |
| 37 | // Called for Methods: Put, Open |
| 38 | type FileWriter interface { |
| 39 | Filewrite(*Request) (io.WriterAt, error) |
| 40 | } |
| 41 | |
| 42 | // OpenFileWriter is a FileWriter that implements the generic OpenFile method. |
| 43 | // You need to implement this optional interface if you want to be able |
| 44 | // to read and write from/to the same handle. |
| 45 | // Called for Methods: Open |
| 46 | type OpenFileWriter interface { |
| 47 | FileWriter |
| 48 | OpenFile(*Request) (WriterAtReaderAt, error) |
| 49 | } |
| 50 | |
| 51 | // FileCmder should return an error |
| 52 | // Note in cases of an error, the error text will be sent to the client. |
| 53 | // Called for Methods: Setstat, Rename, Rmdir, Mkdir, Link, Symlink, Remove |
| 54 | type FileCmder interface { |
| 55 | Filecmd(*Request) error |
| 56 | } |
| 57 | |
| 58 | // PosixRenameFileCmder is a FileCmder that implements the PosixRename method. |
| 59 | // If this interface is implemented PosixRename requests will call it |
| 60 | // otherwise they will be handled in the same way as Rename |
| 61 | type PosixRenameFileCmder interface { |
| 62 | FileCmder |
| 63 | PosixRename(*Request) error |
| 64 | } |
| 65 | |
| 66 | // StatVFSFileCmder is a FileCmder that implements the StatVFS method. |
| 67 | // You need to implement this interface if you want to handle statvfs requests. |
| 68 | // Please also be sure that the [email protected] extension is enabled |
| 69 | type StatVFSFileCmder interface { |
| 70 | FileCmder |
| 71 | StatVFS(*Request) (*StatVFS, error) |
| 72 | } |
| 73 | |
| 74 | // FileLister should return an object that fulfils the ListerAt interface |
| 75 | // Note in cases of an error, the error text will be sent to the client. |
| 76 | // Called for Methods: List, Stat, Readlink |
| 77 | // |
| 78 | // Since Filelist returns an os.FileInfo, this can make it non-ideal for implementing Readlink. |
| 79 | // This is because the Name receiver method defined by that interface defines that it should only return the base name. |
| 80 | // However, Readlink is required to be capable of returning essentially any arbitrary valid path relative or absolute. |
| 81 | // In order to implement this more expressive requirement, implement [ReadlinkFileLister] which will then be used instead. |
| 82 | type FileLister interface { |
| 83 | Filelist(*Request) (ListerAt, error) |
| 84 | } |
| 85 | |
| 86 | // LstatFileLister is a FileLister that implements the Lstat method. |
| 87 | // If this interface is implemented Lstat requests will call it |
| 88 | // otherwise they will be handled in the same way as Stat |
| 89 | type LstatFileLister interface { |
| 90 | FileLister |
| 91 | Lstat(*Request) (ListerAt, error) |
| 92 | } |
| 93 | |
| 94 | // RealPathFileLister is a FileLister that implements the Realpath method. |
| 95 | // The built-in RealPath implementation does not resolve symbolic links. |
| 96 | // By implementing this interface you can customize the returned path |
| 97 | // and, for example, resolve symbolinc links if needed for your use case. |
| 98 | // You have to return an absolute POSIX path. |
| 99 | // |
| 100 | // Up to v1.13.5 the signature for the RealPath method was: |
| 101 | // |
| 102 | // # RealPath(string) string |
| 103 | // |
| 104 | // we have added a legacyRealPathFileLister that implements the old method |
| 105 | // to ensure that your code does not break. |
| 106 | // You should use the new method signature to avoid future issues |
| 107 | type RealPathFileLister interface { |
| 108 | FileLister |
| 109 | RealPath(string) (string, error) |
| 110 | } |
| 111 | |
| 112 | // ReadlinkFileLister is a FileLister that implements the Readlink method. |
| 113 | // By implementing the Readlink method, it is possible to return any arbitrary valid path relative or absolute. |
| 114 | // This allows giving a better response than via the default FileLister (which is limited to os.FileInfo, whose Name method should only return the base name of a file) |
| 115 | type ReadlinkFileLister interface { |
| 116 | FileLister |
| 117 | Readlink(string) (string, error) |
| 118 | } |
| 119 | |
| 120 | // This interface is here for backward compatibility only |
| 121 | type legacyRealPathFileLister interface { |
| 122 | FileLister |
| 123 | RealPath(string) string |
| 124 | } |
| 125 | |
| 126 | // NameLookupFileLister is a FileLister that implmeents the LookupUsername and LookupGroupName methods. |
| 127 | // If this interface is implemented, then longname ls formatting will use these to convert usernames and groupnames. |
| 128 | type NameLookupFileLister interface { |
| 129 | FileLister |
| 130 | LookupUserName(string) string |
| 131 | LookupGroupName(string) string |
| 132 | } |
| 133 | |
| 134 | // ListerAt does for file lists what io.ReaderAt does for files, i.e. a []os.FileInfo buffer is passed to the ListAt function |
| 135 | // and the entries that are populated in the buffer will be passed to the client. |
| 136 | // |
| 137 | // ListAt should return the number of entries copied and an io.EOF error if at end of list. |
| 138 | // This is testable by comparing how many you copied to how many could be copied (eg. n < len(ls) below). |
| 139 | // The copy() builtin is best for the copying. |
| 140 | // |
| 141 | // Uid and gid information will on unix systems be retrieved from [os.FileInfo.Sys] |
| 142 | // if this function returns a [syscall.Stat_t] when called on a populated entry. |
| 143 | // Alternatively, if the entry implements [FileInfoUidGid], it will be used for uid and gid information. |
| 144 | // |
| 145 | // If a populated entry implements [FileInfoExtendedData], extended attributes will also be returned to the client. |
| 146 | // |
| 147 | // The request server code will call Close() on ListerAt if an io.Closer type assertion succeeds. |
| 148 | // |
| 149 | // Note in cases of an error, the error text will be sent to the client. |
| 150 | type ListerAt interface { |
| 151 | ListAt([]os.FileInfo, int64) (int, error) |
| 152 | } |
| 153 | |
| 154 | // TransferError is an optional interface that readerAt and writerAt |
| 155 | // can implement to be notified about the error causing Serve() to exit |
| 156 | // with the request still open |
| 157 | type TransferError interface { |
| 158 | TransferError(err error) |
| 159 | } |

