handler.go 18 KB

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