handler.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. // Copyright (C) 2019-2022 Nicola Murino
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as published
  5. // by the Free Software Foundation, version 3.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU Affero General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU Affero General Public License
  13. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. package sftpd
  15. import (
  16. "io"
  17. "net"
  18. "os"
  19. "path"
  20. "time"
  21. "github.com/pkg/sftp"
  22. "github.com/sftpgo/sdk"
  23. "github.com/drakkan/sftpgo/v2/internal/common"
  24. "github.com/drakkan/sftpgo/v2/internal/dataprovider"
  25. "github.com/drakkan/sftpgo/v2/internal/logger"
  26. "github.com/drakkan/sftpgo/v2/internal/util"
  27. "github.com/drakkan/sftpgo/v2/internal/vfs"
  28. )
  29. // Connection details for an authenticated user
  30. type Connection struct {
  31. *common.BaseConnection
  32. // client's version string
  33. ClientVersion string
  34. // Remote address for this connection
  35. RemoteAddr net.Addr
  36. LocalAddr net.Addr
  37. channel io.ReadWriteCloser
  38. command string
  39. folderPrefix string
  40. }
  41. // GetClientVersion returns the connected client's version
  42. func (c *Connection) GetClientVersion() string {
  43. return c.ClientVersion
  44. }
  45. // GetLocalAddress returns local connection address
  46. func (c *Connection) GetLocalAddress() string {
  47. if c.LocalAddr == nil {
  48. return ""
  49. }
  50. return c.LocalAddr.String()
  51. }
  52. // GetRemoteAddress returns the connected client's address
  53. func (c *Connection) GetRemoteAddress() string {
  54. if c.RemoteAddr == nil {
  55. return ""
  56. }
  57. return c.RemoteAddr.String()
  58. }
  59. // GetCommand returns the SSH command, if any
  60. func (c *Connection) GetCommand() string {
  61. return c.command
  62. }
  63. // Fileread creates a reader for a file on the system and returns the reader back.
  64. func (c *Connection) Fileread(request *sftp.Request) (io.ReaderAt, error) {
  65. c.UpdateLastActivity()
  66. if !c.User.HasPerm(dataprovider.PermDownload, path.Dir(request.Filepath)) {
  67. return nil, sftp.ErrSSHFxPermissionDenied
  68. }
  69. transferQuota := c.GetTransferQuota()
  70. if !transferQuota.HasDownloadSpace() {
  71. c.Log(logger.LevelInfo, "denying file read due to quota limits")
  72. return nil, c.GetReadQuotaExceededError()
  73. }
  74. if ok, policy := c.User.IsFileAllowed(request.Filepath); !ok {
  75. c.Log(logger.LevelWarn, "reading file %#v is not allowed", request.Filepath)
  76. return nil, c.GetErrorForDeniedFile(policy)
  77. }
  78. fs, p, err := c.GetFsAndResolvedPath(request.Filepath)
  79. if err != nil {
  80. return nil, err
  81. }
  82. if err := common.ExecutePreAction(c.BaseConnection, common.OperationPreDownload, p, request.Filepath, 0, 0); err != nil {
  83. c.Log(logger.LevelDebug, "download for file %#v denied by pre action: %v", request.Filepath, err)
  84. return nil, c.GetPermissionDeniedError()
  85. }
  86. file, r, cancelFn, err := fs.Open(p, 0)
  87. if err != nil {
  88. c.Log(logger.LevelError, "could not open file %#v for reading: %+v", p, err)
  89. return nil, c.GetFsError(fs, err)
  90. }
  91. baseTransfer := common.NewBaseTransfer(file, c.BaseConnection, cancelFn, p, p, request.Filepath, common.TransferDownload,
  92. 0, 0, 0, 0, false, fs, transferQuota)
  93. t := newTransfer(baseTransfer, nil, r, nil)
  94. return t, nil
  95. }
  96. // OpenFile implements OpenFileWriter interface
  97. func (c *Connection) OpenFile(request *sftp.Request) (sftp.WriterAtReaderAt, error) {
  98. return c.handleFilewrite(request)
  99. }
  100. // Filewrite handles the write actions for a file on the system.
  101. func (c *Connection) Filewrite(request *sftp.Request) (io.WriterAt, error) {
  102. return c.handleFilewrite(request)
  103. }
  104. func (c *Connection) handleFilewrite(request *sftp.Request) (sftp.WriterAtReaderAt, error) {
  105. c.UpdateLastActivity()
  106. if ok, _ := c.User.IsFileAllowed(request.Filepath); !ok {
  107. c.Log(logger.LevelWarn, "writing file %#v is not allowed", request.Filepath)
  108. return nil, c.GetPermissionDeniedError()
  109. }
  110. fs, p, err := c.GetFsAndResolvedPath(request.Filepath)
  111. if err != nil {
  112. return nil, err
  113. }
  114. filePath := p
  115. if common.Config.IsAtomicUploadEnabled() && fs.IsAtomicUploadSupported() {
  116. filePath = fs.GetAtomicUploadPath(p)
  117. }
  118. var errForRead error
  119. if !vfs.HasOpenRWSupport(fs) && request.Pflags().Read {
  120. // read and write mode is only supported for local filesystem
  121. errForRead = sftp.ErrSSHFxOpUnsupported
  122. }
  123. if !c.User.HasPerm(dataprovider.PermDownload, path.Dir(request.Filepath)) {
  124. // we can try to read only for local fs here, see above.
  125. // os.ErrPermission will become sftp.ErrSSHFxPermissionDenied when sent to
  126. // the client
  127. errForRead = os.ErrPermission
  128. }
  129. stat, statErr := fs.Lstat(p)
  130. if (statErr == nil && stat.Mode()&os.ModeSymlink != 0) || fs.IsNotExist(statErr) {
  131. if !c.User.HasPerm(dataprovider.PermUpload, path.Dir(request.Filepath)) {
  132. return nil, sftp.ErrSSHFxPermissionDenied
  133. }
  134. return c.handleSFTPUploadToNewFile(fs, request.Pflags(), p, filePath, request.Filepath, errForRead)
  135. }
  136. if statErr != nil {
  137. c.Log(logger.LevelError, "error performing file stat %#v: %+v", p, statErr)
  138. return nil, c.GetFsError(fs, statErr)
  139. }
  140. // This happen if we upload a file that has the same name of an existing directory
  141. if stat.IsDir() {
  142. c.Log(logger.LevelError, "attempted to open a directory for writing to: %#v", p)
  143. return nil, sftp.ErrSSHFxOpUnsupported
  144. }
  145. if !c.User.HasPerm(dataprovider.PermOverwrite, path.Dir(request.Filepath)) {
  146. return nil, sftp.ErrSSHFxPermissionDenied
  147. }
  148. return c.handleSFTPUploadToExistingFile(fs, request.Pflags(), p, filePath, stat.Size(), request.Filepath, errForRead)
  149. }
  150. // Filecmd hander for basic SFTP system calls related to files, but not anything to do with reading
  151. // or writing to those files.
  152. func (c *Connection) Filecmd(request *sftp.Request) error {
  153. c.UpdateLastActivity()
  154. switch request.Method {
  155. case "Setstat":
  156. return c.handleSFTPSetstat(request)
  157. case "Rename":
  158. if err := c.Rename(request.Filepath, request.Target); err != nil {
  159. return err
  160. }
  161. case "Rmdir":
  162. return c.RemoveDir(request.Filepath)
  163. case "Mkdir":
  164. err := c.CreateDir(request.Filepath, true)
  165. if err != nil {
  166. return err
  167. }
  168. case "Symlink":
  169. if err := c.CreateSymlink(request.Filepath, request.Target); err != nil {
  170. return err
  171. }
  172. case "Remove":
  173. return c.handleSFTPRemove(request)
  174. default:
  175. return sftp.ErrSSHFxOpUnsupported
  176. }
  177. return sftp.ErrSSHFxOk
  178. }
  179. // Filelist is the handler for SFTP filesystem list calls. This will handle calls to list the contents of
  180. // a directory as well as perform file/folder stat calls.
  181. func (c *Connection) Filelist(request *sftp.Request) (sftp.ListerAt, error) {
  182. c.UpdateLastActivity()
  183. switch request.Method {
  184. case "List":
  185. files, err := c.ListDir(request.Filepath)
  186. if err != nil {
  187. return nil, err
  188. }
  189. modTime := time.Unix(0, 0)
  190. if request.Filepath != "/" || c.folderPrefix != "" {
  191. files = util.PrependFileInfo(files, vfs.NewFileInfo("..", true, 0, modTime, false))
  192. }
  193. files = util.PrependFileInfo(files, vfs.NewFileInfo(".", true, 0, modTime, false))
  194. return listerAt(files), nil
  195. case "Stat":
  196. if !c.User.HasPerm(dataprovider.PermListItems, path.Dir(request.Filepath)) {
  197. return nil, sftp.ErrSSHFxPermissionDenied
  198. }
  199. s, err := c.DoStat(request.Filepath, 0, true)
  200. if err != nil {
  201. return nil, err
  202. }
  203. return listerAt([]os.FileInfo{s}), nil
  204. default:
  205. return nil, sftp.ErrSSHFxOpUnsupported
  206. }
  207. }
  208. // Readlink implements the ReadlinkFileLister interface
  209. func (c *Connection) Readlink(filePath string) (string, error) {
  210. if err := c.canReadLink(filePath); err != nil {
  211. return "", err
  212. }
  213. fs, p, err := c.GetFsAndResolvedPath(filePath)
  214. if err != nil {
  215. return "", err
  216. }
  217. s, err := fs.Readlink(p)
  218. if err != nil {
  219. c.Log(logger.LevelDebug, "error running readlink on path %#v: %+v", p, err)
  220. return "", c.GetFsError(fs, err)
  221. }
  222. if err := c.canReadLink(s); err != nil {
  223. return "", err
  224. }
  225. return s, nil
  226. }
  227. // Lstat implements LstatFileLister interface
  228. func (c *Connection) Lstat(request *sftp.Request) (sftp.ListerAt, error) {
  229. c.UpdateLastActivity()
  230. if !c.User.HasPerm(dataprovider.PermListItems, path.Dir(request.Filepath)) {
  231. return nil, sftp.ErrSSHFxPermissionDenied
  232. }
  233. s, err := c.DoStat(request.Filepath, 1, true)
  234. if err != nil {
  235. return nil, err
  236. }
  237. return listerAt([]os.FileInfo{s}), nil
  238. }
  239. // RealPath implements the RealPathFileLister interface
  240. func (c *Connection) RealPath(p string) (string, error) {
  241. if !c.User.HasPerm(dataprovider.PermListItems, path.Dir(p)) {
  242. return "", sftp.ErrSSHFxPermissionDenied
  243. }
  244. if c.User.Filters.StartDirectory == "" {
  245. p = util.CleanPath(p)
  246. } else {
  247. p = util.CleanPathWithBase(c.User.Filters.StartDirectory, p)
  248. }
  249. fs, fsPath, err := c.GetFsAndResolvedPath(p)
  250. if err != nil {
  251. return "", err
  252. }
  253. if realPather, ok := fs.(vfs.FsRealPather); ok {
  254. realPath, err := realPather.RealPath(fsPath)
  255. if err != nil {
  256. return "", c.GetFsError(fs, err)
  257. }
  258. return realPath, nil
  259. }
  260. return p, nil
  261. }
  262. // StatVFS implements StatVFSFileCmder interface
  263. func (c *Connection) StatVFS(r *sftp.Request) (*sftp.StatVFS, error) {
  264. c.UpdateLastActivity()
  265. // we are assuming that r.Filepath is a dir, this could be wrong but should
  266. // not produce any side effect here.
  267. // we don't consider c.User.Filters.MaxUploadFileSize, we return disk stats here
  268. // not the limit for a single file upload
  269. quotaResult, _ := c.HasSpace(true, true, path.Join(r.Filepath, "fakefile.txt"))
  270. fs, p, err := c.GetFsAndResolvedPath(r.Filepath)
  271. if err != nil {
  272. return nil, err
  273. }
  274. if !quotaResult.HasSpace {
  275. return c.getStatVFSFromQuotaResult(fs, p, quotaResult)
  276. }
  277. if quotaResult.QuotaSize == 0 && quotaResult.QuotaFiles == 0 {
  278. // no quota restrictions
  279. statvfs, err := fs.GetAvailableDiskSize(p)
  280. if err == vfs.ErrStorageSizeUnavailable {
  281. return c.getStatVFSFromQuotaResult(fs, p, quotaResult)
  282. }
  283. return statvfs, err
  284. }
  285. // there is free space but some limits are configured
  286. return c.getStatVFSFromQuotaResult(fs, p, quotaResult)
  287. }
  288. func (c *Connection) canReadLink(name string) error {
  289. if !c.User.HasPerm(dataprovider.PermListItems, path.Dir(name)) {
  290. return sftp.ErrSSHFxPermissionDenied
  291. }
  292. ok, policy := c.User.IsFileAllowed(name)
  293. if !ok && policy == sdk.DenyPolicyHide {
  294. return sftp.ErrSSHFxNoSuchFile
  295. }
  296. return nil
  297. }
  298. func (c *Connection) handleSFTPSetstat(request *sftp.Request) error {
  299. attrs := common.StatAttributes{
  300. Flags: 0,
  301. }
  302. if request.AttrFlags().Permissions {
  303. attrs.Flags |= common.StatAttrPerms
  304. attrs.Mode = request.Attributes().FileMode()
  305. }
  306. if request.AttrFlags().UidGid {
  307. attrs.Flags |= common.StatAttrUIDGID
  308. attrs.UID = int(request.Attributes().UID)
  309. attrs.GID = int(request.Attributes().GID)
  310. }
  311. if request.AttrFlags().Acmodtime {
  312. attrs.Flags |= common.StatAttrTimes
  313. attrs.Atime = time.Unix(int64(request.Attributes().Atime), 0)
  314. attrs.Mtime = time.Unix(int64(request.Attributes().Mtime), 0)
  315. }
  316. if request.AttrFlags().Size {
  317. attrs.Flags |= common.StatAttrSize
  318. attrs.Size = int64(request.Attributes().Size)
  319. }
  320. return c.SetStat(request.Filepath, &attrs)
  321. }
  322. func (c *Connection) handleSFTPRemove(request *sftp.Request) error {
  323. fs, fsPath, err := c.GetFsAndResolvedPath(request.Filepath)
  324. if err != nil {
  325. return err
  326. }
  327. var fi os.FileInfo
  328. if fi, err = fs.Lstat(fsPath); err != nil {
  329. c.Log(logger.LevelDebug, "failed to remove file %#v: stat error: %+v", fsPath, err)
  330. return c.GetFsError(fs, err)
  331. }
  332. if fi.IsDir() && fi.Mode()&os.ModeSymlink == 0 {
  333. c.Log(logger.LevelDebug, "cannot remove %#v is not a file/symlink", fsPath)
  334. return sftp.ErrSSHFxFailure
  335. }
  336. return c.RemoveFile(fs, fsPath, request.Filepath, fi)
  337. }
  338. func (c *Connection) handleSFTPUploadToNewFile(fs vfs.Fs, pflags sftp.FileOpenFlags, resolvedPath, filePath, requestPath string, errForRead error) (sftp.WriterAtReaderAt, error) {
  339. diskQuota, transferQuota := c.HasSpace(true, false, requestPath)
  340. if !diskQuota.HasSpace || !transferQuota.HasUploadSpace() {
  341. c.Log(logger.LevelInfo, "denying file write due to quota limits")
  342. return nil, c.GetQuotaExceededError()
  343. }
  344. if err := common.ExecutePreAction(c.BaseConnection, common.OperationPreUpload, resolvedPath, requestPath, 0, 0); err != nil {
  345. c.Log(logger.LevelDebug, "upload for file %#v denied by pre action: %v", requestPath, err)
  346. return nil, c.GetPermissionDeniedError()
  347. }
  348. osFlags := getOSOpenFlags(pflags)
  349. file, w, cancelFn, err := fs.Create(filePath, osFlags, c.GetCreateChecks(requestPath, true))
  350. if err != nil {
  351. c.Log(logger.LevelError, "error creating file %#vm os flags %v, pflags %+v: %+v", resolvedPath, osFlags, pflags, err)
  352. return nil, c.GetFsError(fs, err)
  353. }
  354. vfs.SetPathPermissions(fs, filePath, c.User.GetUID(), c.User.GetGID())
  355. // we can get an error only for resume
  356. maxWriteSize, _ := c.GetMaxWriteSize(diskQuota, false, 0, fs.IsUploadResumeSupported())
  357. baseTransfer := common.NewBaseTransfer(file, c.BaseConnection, cancelFn, resolvedPath, filePath, requestPath,
  358. common.TransferUpload, 0, 0, maxWriteSize, 0, true, fs, transferQuota)
  359. t := newTransfer(baseTransfer, w, nil, errForRead)
  360. return t, nil
  361. }
  362. func (c *Connection) handleSFTPUploadToExistingFile(fs vfs.Fs, pflags sftp.FileOpenFlags, resolvedPath, filePath string,
  363. fileSize int64, requestPath string, errForRead error) (sftp.WriterAtReaderAt, error) {
  364. var err error
  365. diskQuota, transferQuota := c.HasSpace(false, false, requestPath)
  366. if !diskQuota.HasSpace || !transferQuota.HasUploadSpace() {
  367. c.Log(logger.LevelInfo, "denying file write due to quota limits")
  368. return nil, c.GetQuotaExceededError()
  369. }
  370. osFlags := getOSOpenFlags(pflags)
  371. minWriteOffset := int64(0)
  372. isTruncate := osFlags&os.O_TRUNC != 0
  373. // for upload resumes OpenSSH sets the APPEND flag while WinSCP does not set it,
  374. // so we suppose this is an upload resume if the TRUNCATE flag is not set
  375. isResume := !isTruncate
  376. // if there is a size limit the remaining size cannot be 0 here, since quotaResult.HasSpace
  377. // will return false in this case and we deny the upload before.
  378. // For Cloud FS GetMaxWriteSize will return unsupported operation
  379. maxWriteSize, err := c.GetMaxWriteSize(diskQuota, isResume, fileSize, fs.IsUploadResumeSupported())
  380. if err != nil {
  381. c.Log(logger.LevelDebug, "unable to get max write size: %v", err)
  382. return nil, err
  383. }
  384. if err := common.ExecutePreAction(c.BaseConnection, common.OperationPreUpload, resolvedPath, requestPath, fileSize, osFlags); err != nil {
  385. c.Log(logger.LevelDebug, "upload for file %#v denied by pre action: %v", requestPath, err)
  386. return nil, c.GetPermissionDeniedError()
  387. }
  388. if common.Config.IsAtomicUploadEnabled() && fs.IsAtomicUploadSupported() {
  389. err = fs.Rename(resolvedPath, filePath)
  390. if err != nil {
  391. c.Log(logger.LevelError, "error renaming existing file for atomic upload, source: %#v, dest: %#v, err: %+v",
  392. resolvedPath, filePath, err)
  393. return nil, c.GetFsError(fs, err)
  394. }
  395. }
  396. file, w, cancelFn, err := fs.Create(filePath, osFlags, c.GetCreateChecks(requestPath, false))
  397. if err != nil {
  398. c.Log(logger.LevelError, "error opening existing file, os flags %v, pflags: %+v, source: %#v, err: %+v",
  399. osFlags, pflags, filePath, err)
  400. return nil, c.GetFsError(fs, err)
  401. }
  402. initialSize := int64(0)
  403. truncatedSize := int64(0) // bytes truncated and not included in quota
  404. if isResume {
  405. c.Log(logger.LevelDebug, "resuming upload requested, file path %#v initial size: %v has append flag %v",
  406. filePath, fileSize, pflags.Append)
  407. // enforce min write offset only if the client passed the APPEND flag
  408. if pflags.Append {
  409. minWriteOffset = fileSize
  410. }
  411. initialSize = fileSize
  412. } else {
  413. if isTruncate && vfs.HasTruncateSupport(fs) {
  414. vfolder, err := c.User.GetVirtualFolderForPath(path.Dir(requestPath))
  415. if err == nil {
  416. dataprovider.UpdateVirtualFolderQuota(&vfolder.BaseVirtualFolder, 0, -fileSize, false) //nolint:errcheck
  417. if vfolder.IsIncludedInUserQuota() {
  418. dataprovider.UpdateUserQuota(&c.User, 0, -fileSize, false) //nolint:errcheck
  419. }
  420. } else {
  421. dataprovider.UpdateUserQuota(&c.User, 0, -fileSize, false) //nolint:errcheck
  422. }
  423. } else {
  424. initialSize = fileSize
  425. truncatedSize = fileSize
  426. }
  427. }
  428. vfs.SetPathPermissions(fs, filePath, c.User.GetUID(), c.User.GetGID())
  429. baseTransfer := common.NewBaseTransfer(file, c.BaseConnection, cancelFn, resolvedPath, filePath, requestPath,
  430. common.TransferUpload, minWriteOffset, initialSize, maxWriteSize, truncatedSize, false, fs, transferQuota)
  431. t := newTransfer(baseTransfer, w, nil, errForRead)
  432. return t, nil
  433. }
  434. // Disconnect disconnects the client by closing the channel
  435. func (c *Connection) Disconnect() error {
  436. if c.channel == nil {
  437. c.Log(logger.LevelWarn, "cannot disconnect a nil channel")
  438. return nil
  439. }
  440. return c.channel.Close()
  441. }
  442. func (c *Connection) getStatVFSFromQuotaResult(fs vfs.Fs, name string, quotaResult vfs.QuotaCheckResult) (*sftp.StatVFS, error) {
  443. s, err := fs.GetAvailableDiskSize(name)
  444. if err == nil {
  445. if quotaResult.QuotaSize == 0 || quotaResult.QuotaSize > int64(s.TotalSpace()) {
  446. quotaResult.QuotaSize = int64(s.TotalSpace())
  447. }
  448. if quotaResult.QuotaFiles == 0 || quotaResult.QuotaFiles > int(s.Files) {
  449. quotaResult.QuotaFiles = int(s.Files)
  450. }
  451. } else if err != vfs.ErrStorageSizeUnavailable {
  452. return nil, err
  453. }
  454. // if we are unable to get quota size or quota files we add some arbitrary values
  455. if quotaResult.QuotaSize == 0 {
  456. quotaResult.QuotaSize = quotaResult.UsedSize + 8*1024*1024*1024*1024 // 8TB
  457. }
  458. if quotaResult.QuotaFiles == 0 {
  459. quotaResult.QuotaFiles = quotaResult.UsedFiles + 1000000 // 1 million
  460. }
  461. bsize := uint64(4096)
  462. for bsize > uint64(quotaResult.QuotaSize) {
  463. bsize /= 4
  464. }
  465. blocks := uint64(quotaResult.QuotaSize) / bsize
  466. bfree := uint64(quotaResult.QuotaSize-quotaResult.UsedSize) / bsize
  467. files := uint64(quotaResult.QuotaFiles)
  468. ffree := uint64(quotaResult.QuotaFiles - quotaResult.UsedFiles)
  469. if !quotaResult.HasSpace {
  470. bfree = 0
  471. ffree = 0
  472. }
  473. return &sftp.StatVFS{
  474. Bsize: bsize,
  475. Frsize: bsize,
  476. Blocks: blocks,
  477. Bfree: bfree,
  478. Bavail: bfree,
  479. Files: files,
  480. Ffree: ffree,
  481. Favail: ffree,
  482. Namemax: 255,
  483. }, nil
  484. }
  485. func getOSOpenFlags(requestFlags sftp.FileOpenFlags) (flags int) {
  486. var osFlags int
  487. if requestFlags.Read && requestFlags.Write {
  488. osFlags |= os.O_RDWR
  489. } else if requestFlags.Write {
  490. osFlags |= os.O_WRONLY
  491. }
  492. // we ignore Append flag since pkg/sftp use WriteAt that cannot work with os.O_APPEND
  493. /*if requestFlags.Append {
  494. osFlags |= os.O_APPEND
  495. }*/
  496. if requestFlags.Creat {
  497. osFlags |= os.O_CREATE
  498. }
  499. if requestFlags.Trunc {
  500. osFlags |= os.O_TRUNC
  501. }
  502. if requestFlags.Excl {
  503. osFlags |= os.O_EXCL
  504. }
  505. return osFlags
  506. }