azblobfs.go 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244
  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. //go:build !noazblob
  15. // +build !noazblob
  16. package vfs
  17. import (
  18. "bytes"
  19. "context"
  20. "encoding/base64"
  21. "errors"
  22. "fmt"
  23. "io"
  24. "mime"
  25. "net/http"
  26. "os"
  27. "path"
  28. "path/filepath"
  29. "strings"
  30. "sync"
  31. "sync/atomic"
  32. "time"
  33. "github.com/Azure/azure-sdk-for-go/sdk/azcore"
  34. "github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
  35. "github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
  36. "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blob"
  37. "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blockblob"
  38. "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/container"
  39. "github.com/eikenb/pipeat"
  40. "github.com/google/uuid"
  41. "github.com/pkg/sftp"
  42. "github.com/drakkan/sftpgo/v2/internal/logger"
  43. "github.com/drakkan/sftpgo/v2/internal/metric"
  44. "github.com/drakkan/sftpgo/v2/internal/util"
  45. "github.com/drakkan/sftpgo/v2/internal/version"
  46. )
  47. const (
  48. azureDefaultEndpoint = "blob.core.windows.net"
  49. azFolderKey = "hdi_isfolder"
  50. )
  51. var (
  52. azureBlobDefaultPageSize = int32(5000)
  53. )
  54. // AzureBlobFs is a Fs implementation for Azure Blob storage.
  55. type AzureBlobFs struct {
  56. connectionID string
  57. localTempDir string
  58. // if not empty this fs is mouted as virtual folder in the specified path
  59. mountPath string
  60. config *AzBlobFsConfig
  61. containerClient *container.Client
  62. ctxTimeout time.Duration
  63. ctxLongTimeout time.Duration
  64. }
  65. func init() {
  66. version.AddFeature("+azblob")
  67. }
  68. // NewAzBlobFs returns an AzBlobFs object that allows to interact with Azure Blob storage
  69. func NewAzBlobFs(connectionID, localTempDir, mountPath string, config AzBlobFsConfig) (Fs, error) {
  70. if localTempDir == "" {
  71. localTempDir = getLocalTempDir()
  72. }
  73. fs := &AzureBlobFs{
  74. connectionID: connectionID,
  75. localTempDir: localTempDir,
  76. mountPath: getMountPath(mountPath),
  77. config: &config,
  78. ctxTimeout: 30 * time.Second,
  79. ctxLongTimeout: 90 * time.Second,
  80. }
  81. if err := fs.config.validate(); err != nil {
  82. return fs, err
  83. }
  84. if err := fs.config.tryDecrypt(); err != nil {
  85. return fs, err
  86. }
  87. fs.setConfigDefaults()
  88. if fs.config.SASURL.GetPayload() != "" {
  89. return fs.initFromSASURL()
  90. }
  91. credential, err := blob.NewSharedKeyCredential(fs.config.AccountName, fs.config.AccountKey.GetPayload())
  92. if err != nil {
  93. return fs, fmt.Errorf("invalid credentials: %v", err)
  94. }
  95. var endpoint string
  96. if fs.config.UseEmulator {
  97. endpoint = fmt.Sprintf("%s/%s", fs.config.Endpoint, fs.config.AccountName)
  98. } else {
  99. endpoint = fmt.Sprintf("https://%s.%s/", fs.config.AccountName, fs.config.Endpoint)
  100. }
  101. containerURL := runtime.JoinPaths(endpoint, fs.config.Container)
  102. svc, err := container.NewClientWithSharedKeyCredential(containerURL, credential, getAzContainerClientOptions())
  103. if err != nil {
  104. return fs, fmt.Errorf("invalid credentials: %v", err)
  105. }
  106. fs.containerClient = svc
  107. return fs, err
  108. }
  109. func (fs *AzureBlobFs) initFromSASURL() (Fs, error) {
  110. parts, err := blob.ParseURL(fs.config.SASURL.GetPayload())
  111. if err != nil {
  112. return fs, fmt.Errorf("invalid SAS URL: %w", err)
  113. }
  114. if parts.BlobName != "" {
  115. return fs, fmt.Errorf("SAS URL with blob name not supported")
  116. }
  117. if parts.ContainerName != "" {
  118. if fs.config.Container != "" && fs.config.Container != parts.ContainerName {
  119. return fs, fmt.Errorf("container name in SAS URL %q and container provided %q do not match",
  120. parts.ContainerName, fs.config.Container)
  121. }
  122. svc, err := container.NewClientWithNoCredential(fs.config.SASURL.GetPayload(), getAzContainerClientOptions())
  123. if err != nil {
  124. return fs, fmt.Errorf("invalid credentials: %v", err)
  125. }
  126. fs.config.Container = parts.ContainerName
  127. fs.containerClient = svc
  128. return fs, nil
  129. }
  130. if fs.config.Container == "" {
  131. return fs, errors.New("container is required with this SAS URL")
  132. }
  133. sasURL := runtime.JoinPaths(fs.config.SASURL.GetPayload(), fs.config.Container)
  134. svc, err := container.NewClientWithNoCredential(sasURL, getAzContainerClientOptions())
  135. if err != nil {
  136. return fs, fmt.Errorf("invalid credentials: %v", err)
  137. }
  138. fs.containerClient = svc
  139. return fs, nil
  140. }
  141. // Name returns the name for the Fs implementation
  142. func (fs *AzureBlobFs) Name() string {
  143. if !fs.config.SASURL.IsEmpty() {
  144. return fmt.Sprintf("%s with SAS URL, container %q", azBlobFsName, fs.config.Container)
  145. }
  146. return fmt.Sprintf("%s container %q", azBlobFsName, fs.config.Container)
  147. }
  148. // ConnectionID returns the connection ID associated to this Fs implementation
  149. func (fs *AzureBlobFs) ConnectionID() string {
  150. return fs.connectionID
  151. }
  152. // Stat returns a FileInfo describing the named file
  153. func (fs *AzureBlobFs) Stat(name string) (os.FileInfo, error) {
  154. if name == "" || name == "/" || name == "." {
  155. return NewFileInfo(name, true, 0, time.Unix(0, 0), false), nil
  156. }
  157. if fs.config.KeyPrefix == name+"/" {
  158. return NewFileInfo(name, true, 0, time.Unix(0, 0), false), nil
  159. }
  160. attrs, err := fs.headObject(name)
  161. if err == nil {
  162. contentType := util.GetStringFromPointer(attrs.ContentType)
  163. isDir := checkDirectoryMarkers(contentType, attrs.Metadata)
  164. metric.AZListObjectsCompleted(nil)
  165. return NewFileInfo(name, isDir, util.GetIntFromPointer(attrs.ContentLength), util.GetTimeFromPointer(attrs.LastModified), false), nil
  166. }
  167. if !fs.IsNotExist(err) {
  168. return nil, err
  169. }
  170. // now check if this is a prefix (virtual directory)
  171. hasContents, err := fs.hasContents(name)
  172. if err != nil {
  173. return nil, err
  174. }
  175. if hasContents {
  176. return NewFileInfo(name, true, 0, time.Unix(0, 0), false), nil
  177. }
  178. return nil, os.ErrNotExist
  179. }
  180. // Lstat returns a FileInfo describing the named file
  181. func (fs *AzureBlobFs) Lstat(name string) (os.FileInfo, error) {
  182. return fs.Stat(name)
  183. }
  184. // Open opens the named file for reading
  185. func (fs *AzureBlobFs) Open(name string, offset int64) (File, PipeReader, func(), error) {
  186. r, w, err := pipeat.PipeInDir(fs.localTempDir)
  187. if err != nil {
  188. return nil, nil, nil, err
  189. }
  190. p := NewPipeReader(r)
  191. ctx, cancelFn := context.WithCancel(context.Background())
  192. go func() {
  193. defer cancelFn()
  194. blockBlob := fs.containerClient.NewBlockBlobClient(name)
  195. err := fs.handleMultipartDownload(ctx, blockBlob, offset, w, p)
  196. w.CloseWithError(err) //nolint:errcheck
  197. fsLog(fs, logger.LevelDebug, "download completed, path: %q size: %v, err: %+v", name, w.GetWrittenBytes(), err)
  198. metric.AZTransferCompleted(w.GetWrittenBytes(), 1, err)
  199. }()
  200. return nil, p, cancelFn, nil
  201. }
  202. // Create creates or opens the named file for writing
  203. func (fs *AzureBlobFs) Create(name string, flag, checks int) (File, PipeWriter, func(), error) {
  204. if checks&CheckParentDir != 0 {
  205. _, err := fs.Stat(path.Dir(name))
  206. if err != nil {
  207. return nil, nil, nil, err
  208. }
  209. }
  210. r, w, err := pipeat.PipeInDir(fs.localTempDir)
  211. if err != nil {
  212. return nil, nil, nil, err
  213. }
  214. ctx, cancelFn := context.WithCancel(context.Background())
  215. var p PipeWriter
  216. if checks&CheckResume != 0 {
  217. p = newPipeWriterAtOffset(w, 0)
  218. } else {
  219. p = NewPipeWriter(w)
  220. }
  221. headers := blob.HTTPHeaders{}
  222. var contentType string
  223. var metadata map[string]*string
  224. if flag == -1 {
  225. contentType = dirMimeType
  226. metadata = map[string]*string{
  227. azFolderKey: util.NilIfEmpty("true"),
  228. }
  229. } else {
  230. contentType = mime.TypeByExtension(path.Ext(name))
  231. }
  232. if contentType != "" {
  233. headers.BlobContentType = &contentType
  234. }
  235. go func() {
  236. defer cancelFn()
  237. blockBlob := fs.containerClient.NewBlockBlobClient(name)
  238. err := fs.handleMultipartUpload(ctx, r, blockBlob, &headers, metadata)
  239. r.CloseWithError(err) //nolint:errcheck
  240. p.Done(err)
  241. fsLog(fs, logger.LevelDebug, "upload completed, path: %q, readed bytes: %v, err: %+v", name, r.GetReadedBytes(), err)
  242. metric.AZTransferCompleted(r.GetReadedBytes(), 0, err)
  243. }()
  244. if checks&CheckResume != 0 {
  245. readCh := make(chan error, 1)
  246. go func() {
  247. n, err := fs.downloadToWriter(name, p)
  248. pw := p.(*pipeWriterAtOffset)
  249. pw.offset = 0
  250. pw.writeOffset = n
  251. readCh <- err
  252. }()
  253. err = <-readCh
  254. if err != nil {
  255. cancelFn()
  256. p.Close()
  257. fsLog(fs, logger.LevelDebug, "download before resume failed, writer closed and read cancelled")
  258. return nil, nil, nil, err
  259. }
  260. }
  261. if uploadMode&16 != 0 {
  262. return nil, p, nil, nil
  263. }
  264. return nil, p, cancelFn, nil
  265. }
  266. // Rename renames (moves) source to target.
  267. func (fs *AzureBlobFs) Rename(source, target string) (int, int64, error) {
  268. if source == target {
  269. return -1, -1, nil
  270. }
  271. _, err := fs.Stat(path.Dir(target))
  272. if err != nil {
  273. return -1, -1, err
  274. }
  275. fi, err := fs.Stat(source)
  276. if err != nil {
  277. return -1, -1, err
  278. }
  279. return fs.renameInternal(source, target, fi, 0)
  280. }
  281. // Remove removes the named file or (empty) directory.
  282. func (fs *AzureBlobFs) Remove(name string, isDir bool) error {
  283. if isDir {
  284. hasContents, err := fs.hasContents(name)
  285. if err != nil {
  286. return err
  287. }
  288. if hasContents {
  289. return fmt.Errorf("cannot remove non empty directory: %q", name)
  290. }
  291. }
  292. ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(fs.ctxTimeout))
  293. defer cancelFn()
  294. blobBlock := fs.containerClient.NewBlockBlobClient(name)
  295. var deletSnapshots blob.DeleteSnapshotsOptionType
  296. if !isDir {
  297. deletSnapshots = blob.DeleteSnapshotsOptionTypeInclude
  298. }
  299. _, err := blobBlock.Delete(ctx, &blob.DeleteOptions{
  300. DeleteSnapshots: &deletSnapshots,
  301. })
  302. if err != nil && isDir {
  303. if fs.isBadRequestError(err) {
  304. deletSnapshots = blob.DeleteSnapshotsOptionTypeInclude
  305. _, err = blobBlock.Delete(ctx, &blob.DeleteOptions{
  306. DeleteSnapshots: &deletSnapshots,
  307. })
  308. }
  309. }
  310. metric.AZDeleteObjectCompleted(err)
  311. return err
  312. }
  313. // Mkdir creates a new directory with the specified name and default permissions
  314. func (fs *AzureBlobFs) Mkdir(name string) error {
  315. _, err := fs.Stat(name)
  316. if !fs.IsNotExist(err) {
  317. return err
  318. }
  319. return fs.mkdirInternal(name)
  320. }
  321. // Symlink creates source as a symbolic link to target.
  322. func (*AzureBlobFs) Symlink(_, _ string) error {
  323. return ErrVfsUnsupported
  324. }
  325. // Readlink returns the destination of the named symbolic link
  326. func (*AzureBlobFs) Readlink(_ string) (string, error) {
  327. return "", ErrVfsUnsupported
  328. }
  329. // Chown changes the numeric uid and gid of the named file.
  330. func (*AzureBlobFs) Chown(_ string, _ int, _ int) error {
  331. return ErrVfsUnsupported
  332. }
  333. // Chmod changes the mode of the named file to mode.
  334. func (*AzureBlobFs) Chmod(_ string, _ os.FileMode) error {
  335. return ErrVfsUnsupported
  336. }
  337. // Chtimes changes the access and modification times of the named file.
  338. func (fs *AzureBlobFs) Chtimes(_ string, _, _ time.Time, _ bool) error {
  339. return ErrVfsUnsupported
  340. }
  341. // Truncate changes the size of the named file.
  342. // Truncate by path is not supported, while truncating an opened
  343. // file is handled inside base transfer
  344. func (*AzureBlobFs) Truncate(_ string, _ int64) error {
  345. return ErrVfsUnsupported
  346. }
  347. // ReadDir reads the directory named by dirname and returns
  348. // a list of directory entries.
  349. func (fs *AzureBlobFs) ReadDir(dirname string) (DirLister, error) {
  350. // dirname must be already cleaned
  351. prefix := fs.getPrefix(dirname)
  352. pager := fs.containerClient.NewListBlobsHierarchyPager("/", &container.ListBlobsHierarchyOptions{
  353. Include: container.ListBlobsInclude{
  354. //Metadata: true,
  355. },
  356. Prefix: &prefix,
  357. MaxResults: &azureBlobDefaultPageSize,
  358. })
  359. return &azureBlobDirLister{
  360. paginator: pager,
  361. timeout: fs.ctxTimeout,
  362. prefix: prefix,
  363. prefixes: make(map[string]bool),
  364. }, nil
  365. }
  366. // IsUploadResumeSupported returns true if resuming uploads is supported.
  367. // Resuming uploads is not supported on Azure Blob
  368. func (*AzureBlobFs) IsUploadResumeSupported() bool {
  369. return false
  370. }
  371. // IsConditionalUploadResumeSupported returns if resuming uploads is supported
  372. // for the specified size
  373. func (*AzureBlobFs) IsConditionalUploadResumeSupported(size int64) bool {
  374. return size <= resumeMaxSize
  375. }
  376. // IsAtomicUploadSupported returns true if atomic upload is supported.
  377. // Azure Blob uploads are already atomic, we don't need to upload to a temporary
  378. // file
  379. func (*AzureBlobFs) IsAtomicUploadSupported() bool {
  380. return false
  381. }
  382. // IsNotExist returns a boolean indicating whether the error is known to
  383. // report that a file or directory does not exist
  384. func (*AzureBlobFs) IsNotExist(err error) bool {
  385. if err == nil {
  386. return false
  387. }
  388. var respErr *azcore.ResponseError
  389. if errors.As(err, &respErr) {
  390. return respErr.StatusCode == http.StatusNotFound
  391. }
  392. // os.ErrNotExist can be returned internally by fs.Stat
  393. return errors.Is(err, os.ErrNotExist)
  394. }
  395. // IsPermission returns a boolean indicating whether the error is known to
  396. // report that permission is denied.
  397. func (*AzureBlobFs) IsPermission(err error) bool {
  398. if err == nil {
  399. return false
  400. }
  401. var respErr *azcore.ResponseError
  402. if errors.As(err, &respErr) {
  403. return respErr.StatusCode == http.StatusForbidden || respErr.StatusCode == http.StatusUnauthorized
  404. }
  405. return false
  406. }
  407. // IsNotSupported returns true if the error indicate an unsupported operation
  408. func (*AzureBlobFs) IsNotSupported(err error) bool {
  409. if err == nil {
  410. return false
  411. }
  412. return err == ErrVfsUnsupported
  413. }
  414. func (*AzureBlobFs) isBadRequestError(err error) bool {
  415. if err == nil {
  416. return false
  417. }
  418. var respErr *azcore.ResponseError
  419. if errors.As(err, &respErr) {
  420. return respErr.StatusCode == http.StatusBadRequest
  421. }
  422. return false
  423. }
  424. // CheckRootPath creates the specified local root directory if it does not exists
  425. func (fs *AzureBlobFs) CheckRootPath(username string, uid int, gid int) bool {
  426. // we need a local directory for temporary files
  427. osFs := NewOsFs(fs.ConnectionID(), fs.localTempDir, "", nil)
  428. return osFs.CheckRootPath(username, uid, gid)
  429. }
  430. // ScanRootDirContents returns the number of files contained in the bucket,
  431. // and their size
  432. func (fs *AzureBlobFs) ScanRootDirContents() (int, int64, error) {
  433. return fs.GetDirSize(fs.config.KeyPrefix)
  434. }
  435. // GetDirSize returns the number of files and the size for a folder
  436. // including any subfolders
  437. func (fs *AzureBlobFs) GetDirSize(dirname string) (int, int64, error) {
  438. numFiles := 0
  439. size := int64(0)
  440. prefix := fs.getPrefix(dirname)
  441. pager := fs.containerClient.NewListBlobsFlatPager(&container.ListBlobsFlatOptions{
  442. Include: container.ListBlobsInclude{
  443. Metadata: true,
  444. },
  445. Prefix: &prefix,
  446. MaxResults: &azureBlobDefaultPageSize,
  447. })
  448. for pager.More() {
  449. ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(fs.ctxTimeout))
  450. defer cancelFn()
  451. resp, err := pager.NextPage(ctx)
  452. if err != nil {
  453. metric.AZListObjectsCompleted(err)
  454. return numFiles, size, err
  455. }
  456. for _, blobItem := range resp.ListBlobsFlatSegmentResponse.Segment.BlobItems {
  457. if blobItem.Properties != nil {
  458. contentType := util.GetStringFromPointer(blobItem.Properties.ContentType)
  459. isDir := checkDirectoryMarkers(contentType, blobItem.Metadata)
  460. blobSize := util.GetIntFromPointer(blobItem.Properties.ContentLength)
  461. if isDir && blobSize == 0 {
  462. continue
  463. }
  464. numFiles++
  465. size += blobSize
  466. if numFiles%1000 == 0 {
  467. fsLog(fs, logger.LevelDebug, "dirname %q scan in progress, files: %d, size: %d", dirname, numFiles, size)
  468. }
  469. }
  470. }
  471. }
  472. metric.AZListObjectsCompleted(nil)
  473. return numFiles, size, nil
  474. }
  475. // GetAtomicUploadPath returns the path to use for an atomic upload.
  476. // Azure Blob Storage uploads are already atomic, we never call this method
  477. func (*AzureBlobFs) GetAtomicUploadPath(_ string) string {
  478. return ""
  479. }
  480. // GetRelativePath returns the path for a file relative to the user's home dir.
  481. // This is the path as seen by SFTPGo users
  482. func (fs *AzureBlobFs) GetRelativePath(name string) string {
  483. rel := path.Clean(name)
  484. if rel == "." {
  485. rel = ""
  486. }
  487. if !path.IsAbs(rel) {
  488. rel = "/" + rel
  489. }
  490. if fs.config.KeyPrefix != "" {
  491. if !strings.HasPrefix(rel, "/"+fs.config.KeyPrefix) {
  492. rel = "/"
  493. }
  494. rel = path.Clean("/" + strings.TrimPrefix(rel, "/"+fs.config.KeyPrefix))
  495. }
  496. if fs.mountPath != "" {
  497. rel = path.Join(fs.mountPath, rel)
  498. }
  499. return rel
  500. }
  501. // Walk walks the file tree rooted at root, calling walkFn for each file or
  502. // directory in the tree, including root
  503. func (fs *AzureBlobFs) Walk(root string, walkFn filepath.WalkFunc) error {
  504. prefix := fs.getPrefix(root)
  505. pager := fs.containerClient.NewListBlobsFlatPager(&container.ListBlobsFlatOptions{
  506. Include: container.ListBlobsInclude{
  507. Metadata: true,
  508. },
  509. Prefix: &prefix,
  510. MaxResults: &azureBlobDefaultPageSize,
  511. })
  512. for pager.More() {
  513. ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(fs.ctxTimeout))
  514. defer cancelFn()
  515. resp, err := pager.NextPage(ctx)
  516. if err != nil {
  517. metric.AZListObjectsCompleted(err)
  518. return err
  519. }
  520. for _, blobItem := range resp.ListBlobsFlatSegmentResponse.Segment.BlobItems {
  521. name := util.GetStringFromPointer(blobItem.Name)
  522. if fs.isEqual(name, prefix) {
  523. continue
  524. }
  525. blobSize := int64(0)
  526. lastModified := time.Unix(0, 0)
  527. isDir := false
  528. if blobItem.Properties != nil {
  529. contentType := util.GetStringFromPointer(blobItem.Properties.ContentType)
  530. isDir = checkDirectoryMarkers(contentType, blobItem.Metadata)
  531. blobSize = util.GetIntFromPointer(blobItem.Properties.ContentLength)
  532. lastModified = util.GetTimeFromPointer(blobItem.Properties.LastModified)
  533. }
  534. err := walkFn(name, NewFileInfo(name, isDir, blobSize, lastModified, false), nil)
  535. if err != nil {
  536. return err
  537. }
  538. }
  539. }
  540. metric.AZListObjectsCompleted(nil)
  541. return walkFn(root, NewFileInfo(root, true, 0, time.Unix(0, 0), false), nil)
  542. }
  543. // Join joins any number of path elements into a single path
  544. func (*AzureBlobFs) Join(elem ...string) string {
  545. return strings.TrimPrefix(path.Join(elem...), "/")
  546. }
  547. // HasVirtualFolders returns true if folders are emulated
  548. func (*AzureBlobFs) HasVirtualFolders() bool {
  549. return true
  550. }
  551. // ResolvePath returns the matching filesystem path for the specified sftp path
  552. func (fs *AzureBlobFs) ResolvePath(virtualPath string) (string, error) {
  553. if fs.mountPath != "" {
  554. virtualPath = strings.TrimPrefix(virtualPath, fs.mountPath)
  555. }
  556. if !path.IsAbs(virtualPath) {
  557. virtualPath = path.Clean("/" + virtualPath)
  558. }
  559. return fs.Join(fs.config.KeyPrefix, strings.TrimPrefix(virtualPath, "/")), nil
  560. }
  561. // CopyFile implements the FsFileCopier interface
  562. func (fs *AzureBlobFs) CopyFile(source, target string, _ int64) error {
  563. return fs.copyFileInternal(source, target)
  564. }
  565. func (fs *AzureBlobFs) headObject(name string) (blob.GetPropertiesResponse, error) {
  566. ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(fs.ctxTimeout))
  567. defer cancelFn()
  568. resp, err := fs.containerClient.NewBlockBlobClient(name).GetProperties(ctx, &blob.GetPropertiesOptions{})
  569. metric.AZHeadObjectCompleted(err)
  570. return resp, err
  571. }
  572. // GetMimeType returns the content type
  573. func (fs *AzureBlobFs) GetMimeType(name string) (string, error) {
  574. response, err := fs.headObject(name)
  575. if err != nil {
  576. return "", err
  577. }
  578. return util.GetStringFromPointer(response.ContentType), nil
  579. }
  580. // Close closes the fs
  581. func (*AzureBlobFs) Close() error {
  582. return nil
  583. }
  584. // GetAvailableDiskSize returns the available size for the specified path
  585. func (*AzureBlobFs) GetAvailableDiskSize(_ string) (*sftp.StatVFS, error) {
  586. return nil, ErrStorageSizeUnavailable
  587. }
  588. func (*AzureBlobFs) getPrefix(name string) string {
  589. prefix := ""
  590. if name != "" && name != "." {
  591. prefix = strings.TrimPrefix(name, "/")
  592. if !strings.HasSuffix(prefix, "/") {
  593. prefix += "/"
  594. }
  595. }
  596. return prefix
  597. }
  598. func (fs *AzureBlobFs) isEqual(key string, virtualName string) bool {
  599. if key == virtualName {
  600. return true
  601. }
  602. if key == virtualName+"/" {
  603. return true
  604. }
  605. if key+"/" == virtualName {
  606. return true
  607. }
  608. return false
  609. }
  610. func (fs *AzureBlobFs) setConfigDefaults() {
  611. if fs.config.Endpoint == "" {
  612. fs.config.Endpoint = azureDefaultEndpoint
  613. }
  614. if fs.config.UploadPartSize == 0 {
  615. fs.config.UploadPartSize = 5
  616. }
  617. if fs.config.UploadPartSize < 1024*1024 {
  618. fs.config.UploadPartSize *= 1024 * 1024
  619. }
  620. if fs.config.UploadConcurrency == 0 {
  621. fs.config.UploadConcurrency = 5
  622. }
  623. if fs.config.DownloadPartSize == 0 {
  624. fs.config.DownloadPartSize = 5
  625. }
  626. if fs.config.DownloadPartSize < 1024*1024 {
  627. fs.config.DownloadPartSize *= 1024 * 1024
  628. }
  629. if fs.config.DownloadConcurrency == 0 {
  630. fs.config.DownloadConcurrency = 5
  631. }
  632. }
  633. func (fs *AzureBlobFs) copyFileInternal(source, target string) error {
  634. ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(fs.ctxLongTimeout))
  635. defer cancelFn()
  636. srcBlob := fs.containerClient.NewBlockBlobClient(source)
  637. dstBlob := fs.containerClient.NewBlockBlobClient(target)
  638. resp, err := dstBlob.StartCopyFromURL(ctx, srcBlob.URL(), fs.getCopyOptions())
  639. if err != nil {
  640. metric.AZCopyObjectCompleted(err)
  641. return err
  642. }
  643. copyStatus := blob.CopyStatusType(util.GetStringFromPointer((*string)(resp.CopyStatus)))
  644. nErrors := 0
  645. for copyStatus == blob.CopyStatusTypePending {
  646. // Poll until the copy is complete.
  647. time.Sleep(500 * time.Millisecond)
  648. resp, err := dstBlob.GetProperties(ctx, &blob.GetPropertiesOptions{})
  649. if err != nil {
  650. // A GetProperties failure may be transient, so allow a couple
  651. // of them before giving up.
  652. nErrors++
  653. if ctx.Err() != nil || nErrors == 3 {
  654. metric.AZCopyObjectCompleted(err)
  655. return err
  656. }
  657. } else {
  658. copyStatus = blob.CopyStatusType(util.GetStringFromPointer((*string)(resp.CopyStatus)))
  659. }
  660. }
  661. if copyStatus != blob.CopyStatusTypeSuccess {
  662. err := fmt.Errorf("copy failed with status: %s", copyStatus)
  663. metric.AZCopyObjectCompleted(err)
  664. return err
  665. }
  666. metric.AZCopyObjectCompleted(nil)
  667. return nil
  668. }
  669. func (fs *AzureBlobFs) renameInternal(source, target string, fi os.FileInfo, recursion int) (int, int64, error) {
  670. var numFiles int
  671. var filesSize int64
  672. if fi.IsDir() {
  673. if renameMode == 0 {
  674. hasContents, err := fs.hasContents(source)
  675. if err != nil {
  676. return numFiles, filesSize, err
  677. }
  678. if hasContents {
  679. return numFiles, filesSize, fmt.Errorf("cannot rename non empty directory: %q", source)
  680. }
  681. }
  682. if err := fs.mkdirInternal(target); err != nil {
  683. return numFiles, filesSize, err
  684. }
  685. if renameMode == 1 {
  686. files, size, err := doRecursiveRename(fs, source, target, fs.renameInternal, recursion)
  687. numFiles += files
  688. filesSize += size
  689. if err != nil {
  690. return numFiles, filesSize, err
  691. }
  692. }
  693. } else {
  694. if err := fs.copyFileInternal(source, target); err != nil {
  695. return numFiles, filesSize, err
  696. }
  697. numFiles++
  698. filesSize += fi.Size()
  699. }
  700. err := fs.skipNotExistErr(fs.Remove(source, fi.IsDir()))
  701. return numFiles, filesSize, err
  702. }
  703. func (fs *AzureBlobFs) skipNotExistErr(err error) error {
  704. if fs.IsNotExist(err) {
  705. return nil
  706. }
  707. return err
  708. }
  709. func (fs *AzureBlobFs) mkdirInternal(name string) error {
  710. _, w, _, err := fs.Create(name, -1, 0)
  711. if err != nil {
  712. return err
  713. }
  714. return w.Close()
  715. }
  716. func (fs *AzureBlobFs) hasContents(name string) (bool, error) {
  717. result := false
  718. prefix := fs.getPrefix(name)
  719. maxResults := int32(1)
  720. pager := fs.containerClient.NewListBlobsFlatPager(&container.ListBlobsFlatOptions{
  721. MaxResults: &maxResults,
  722. Prefix: &prefix,
  723. })
  724. if pager.More() {
  725. ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(fs.ctxTimeout))
  726. defer cancelFn()
  727. resp, err := pager.NextPage(ctx)
  728. if err != nil {
  729. metric.AZListObjectsCompleted(err)
  730. return result, err
  731. }
  732. result = len(resp.ListBlobsFlatSegmentResponse.Segment.BlobItems) > 0
  733. }
  734. metric.AZListObjectsCompleted(nil)
  735. return result, nil
  736. }
  737. func (fs *AzureBlobFs) downloadPart(ctx context.Context, blockBlob *blockblob.Client, buf []byte,
  738. w io.WriterAt, offset, count, writeOffset int64,
  739. ) error {
  740. if count == 0 {
  741. return nil
  742. }
  743. resp, err := blockBlob.DownloadStream(ctx, &blob.DownloadStreamOptions{
  744. Range: blob.HTTPRange{
  745. Offset: offset,
  746. Count: count,
  747. },
  748. })
  749. if err != nil {
  750. return err
  751. }
  752. defer resp.DownloadResponse.Body.Close()
  753. _, err = io.ReadAtLeast(resp.DownloadResponse.Body, buf, int(count))
  754. if err != nil {
  755. return err
  756. }
  757. _, err = fs.writeAtFull(w, buf, writeOffset, int(count))
  758. return err
  759. }
  760. func (fs *AzureBlobFs) handleMultipartDownload(ctx context.Context, blockBlob *blockblob.Client,
  761. offset int64, writer io.WriterAt, pipeReader PipeReader,
  762. ) error {
  763. props, err := blockBlob.GetProperties(ctx, &blob.GetPropertiesOptions{})
  764. metric.AZHeadObjectCompleted(err)
  765. if err != nil {
  766. fsLog(fs, logger.LevelError, "unable to get blob properties, download aborted: %+v", err)
  767. return err
  768. }
  769. if readMetadata > 0 && pipeReader != nil {
  770. pipeReader.setMetadataFromPointerVal(props.Metadata)
  771. }
  772. contentLength := util.GetIntFromPointer(props.ContentLength)
  773. sizeToDownload := contentLength - offset
  774. if sizeToDownload < 0 {
  775. fsLog(fs, logger.LevelError, "invalid multipart download size or offset, size: %v, offset: %v, size to download: %v",
  776. contentLength, offset, sizeToDownload)
  777. return errors.New("the requested offset exceeds the file size")
  778. }
  779. if sizeToDownload == 0 {
  780. fsLog(fs, logger.LevelDebug, "nothing to download, offset %v, content length %v", offset, contentLength)
  781. return nil
  782. }
  783. partSize := fs.config.DownloadPartSize
  784. guard := make(chan struct{}, fs.config.DownloadConcurrency)
  785. blockCtxTimeout := time.Duration(fs.config.DownloadPartSize/(1024*1024)) * time.Minute
  786. pool := newBufferAllocator(int(partSize))
  787. finished := false
  788. var wg sync.WaitGroup
  789. var errOnce sync.Once
  790. var hasError atomic.Bool
  791. var poolError error
  792. poolCtx, poolCancel := context.WithCancel(ctx)
  793. defer poolCancel()
  794. for part := 0; !finished; part++ {
  795. start := offset
  796. end := offset + partSize
  797. if end >= contentLength {
  798. end = contentLength
  799. finished = true
  800. }
  801. writeOffset := int64(part) * partSize
  802. offset = end
  803. guard <- struct{}{}
  804. if hasError.Load() {
  805. fsLog(fs, logger.LevelDebug, "pool error, download for part %v not started", part)
  806. break
  807. }
  808. buf := pool.getBuffer()
  809. wg.Add(1)
  810. go func(start, end, writeOffset int64, buf []byte) {
  811. defer func() {
  812. pool.releaseBuffer(buf)
  813. <-guard
  814. wg.Done()
  815. }()
  816. innerCtx, cancelFn := context.WithDeadline(poolCtx, time.Now().Add(blockCtxTimeout))
  817. defer cancelFn()
  818. count := end - start
  819. err := fs.downloadPart(innerCtx, blockBlob, buf, writer, start, count, writeOffset)
  820. if err != nil {
  821. errOnce.Do(func() {
  822. fsLog(fs, logger.LevelError, "multipart download error: %+v", err)
  823. hasError.Store(true)
  824. poolError = fmt.Errorf("multipart download error: %w", err)
  825. poolCancel()
  826. })
  827. }
  828. }(start, end, writeOffset, buf)
  829. }
  830. wg.Wait()
  831. close(guard)
  832. pool.free()
  833. return poolError
  834. }
  835. func (fs *AzureBlobFs) handleMultipartUpload(ctx context.Context, reader io.Reader,
  836. blockBlob *blockblob.Client, httpHeaders *blob.HTTPHeaders, metadata map[string]*string,
  837. ) error {
  838. partSize := fs.config.UploadPartSize
  839. guard := make(chan struct{}, fs.config.UploadConcurrency)
  840. blockCtxTimeout := time.Duration(fs.config.UploadPartSize/(1024*1024)) * time.Minute
  841. // sync.Pool seems to use a lot of memory so prefer our own, very simple, allocator
  842. // we only need to recycle few byte slices
  843. pool := newBufferAllocator(int(partSize))
  844. finished := false
  845. var blocks []string
  846. var wg sync.WaitGroup
  847. var errOnce sync.Once
  848. var hasError atomic.Bool
  849. var poolError error
  850. poolCtx, poolCancel := context.WithCancel(ctx)
  851. defer poolCancel()
  852. for part := 0; !finished; part++ {
  853. buf := pool.getBuffer()
  854. n, err := fs.readFill(reader, buf)
  855. if err == io.EOF {
  856. // read finished, if n > 0 we need to process the last data chunck
  857. if n == 0 {
  858. pool.releaseBuffer(buf)
  859. break
  860. }
  861. finished = true
  862. } else if err != nil {
  863. pool.releaseBuffer(buf)
  864. pool.free()
  865. return err
  866. }
  867. // Block IDs are unique values to avoid issue if 2+ clients are uploading blocks
  868. // at the same time causing CommitBlockList to get a mix of blocks from all the clients.
  869. generatedUUID, err := uuid.NewRandom()
  870. if err != nil {
  871. pool.releaseBuffer(buf)
  872. pool.free()
  873. return fmt.Errorf("unable to generate block ID: %w", err)
  874. }
  875. blockID := base64.StdEncoding.EncodeToString([]byte(generatedUUID.String()))
  876. blocks = append(blocks, blockID)
  877. guard <- struct{}{}
  878. if hasError.Load() {
  879. fsLog(fs, logger.LevelError, "pool error, upload for part %d not started", part)
  880. pool.releaseBuffer(buf)
  881. break
  882. }
  883. wg.Add(1)
  884. go func(blockID string, buf []byte, bufSize int) {
  885. defer func() {
  886. pool.releaseBuffer(buf)
  887. <-guard
  888. wg.Done()
  889. }()
  890. bufferReader := &bytesReaderWrapper{
  891. Reader: bytes.NewReader(buf[:bufSize]),
  892. }
  893. innerCtx, cancelFn := context.WithDeadline(poolCtx, time.Now().Add(blockCtxTimeout))
  894. defer cancelFn()
  895. _, err := blockBlob.StageBlock(innerCtx, blockID, bufferReader, &blockblob.StageBlockOptions{})
  896. if err != nil {
  897. errOnce.Do(func() {
  898. fsLog(fs, logger.LevelDebug, "multipart upload error: %+v", err)
  899. hasError.Store(true)
  900. poolError = fmt.Errorf("multipart upload error: %w", err)
  901. poolCancel()
  902. })
  903. }
  904. }(blockID, buf, n)
  905. }
  906. wg.Wait()
  907. close(guard)
  908. pool.free()
  909. if poolError != nil {
  910. return poolError
  911. }
  912. commitOptions := blockblob.CommitBlockListOptions{
  913. HTTPHeaders: httpHeaders,
  914. Metadata: metadata,
  915. }
  916. if fs.config.AccessTier != "" {
  917. commitOptions.Tier = (*blob.AccessTier)(&fs.config.AccessTier)
  918. }
  919. _, err := blockBlob.CommitBlockList(ctx, blocks, &commitOptions)
  920. return err
  921. }
  922. func (*AzureBlobFs) writeAtFull(w io.WriterAt, buf []byte, offset int64, count int) (int, error) {
  923. written := 0
  924. for written < count {
  925. n, err := w.WriteAt(buf[written:count], offset+int64(written))
  926. written += n
  927. if err != nil {
  928. return written, err
  929. }
  930. }
  931. return written, nil
  932. }
  933. // copied from rclone
  934. func (*AzureBlobFs) readFill(r io.Reader, buf []byte) (n int, err error) {
  935. var nn int
  936. for n < len(buf) && err == nil {
  937. nn, err = r.Read(buf[n:])
  938. n += nn
  939. }
  940. return n, err
  941. }
  942. func (fs *AzureBlobFs) getCopyOptions() *blob.StartCopyFromURLOptions {
  943. copyOptions := &blob.StartCopyFromURLOptions{}
  944. if fs.config.AccessTier != "" {
  945. copyOptions.Tier = (*blob.AccessTier)(&fs.config.AccessTier)
  946. }
  947. return copyOptions
  948. }
  949. func (fs *AzureBlobFs) downloadToWriter(name string, w PipeWriter) (int64, error) {
  950. fsLog(fs, logger.LevelDebug, "starting download before resuming upload, path %q", name)
  951. ctx, cancelFn := context.WithTimeout(context.Background(), preResumeTimeout)
  952. defer cancelFn()
  953. blockBlob := fs.containerClient.NewBlockBlobClient(name)
  954. err := fs.handleMultipartDownload(ctx, blockBlob, 0, w, nil)
  955. n := w.GetWrittenBytes()
  956. fsLog(fs, logger.LevelDebug, "download before resuming upload completed, path %q size: %d, err: %+v",
  957. name, n, err)
  958. metric.AZTransferCompleted(n, 1, err)
  959. return n, err
  960. }
  961. func checkDirectoryMarkers(contentType string, metadata map[string]*string) bool {
  962. if contentType == dirMimeType {
  963. return true
  964. }
  965. for k, v := range metadata {
  966. if strings.ToLower(k) == azFolderKey {
  967. return util.GetStringFromPointer(v) == "true"
  968. }
  969. }
  970. return false
  971. }
  972. func getAzContainerClientOptions() *container.ClientOptions {
  973. version := version.Get()
  974. return &container.ClientOptions{
  975. ClientOptions: azcore.ClientOptions{
  976. Telemetry: policy.TelemetryOptions{
  977. ApplicationID: fmt.Sprintf("SFTPGo-%s", version.CommitHash),
  978. },
  979. },
  980. }
  981. }
  982. type bytesReaderWrapper struct {
  983. *bytes.Reader
  984. }
  985. func (b *bytesReaderWrapper) Close() error {
  986. return nil
  987. }
  988. type bufferAllocator struct {
  989. sync.Mutex
  990. available [][]byte
  991. bufferSize int
  992. finalized bool
  993. }
  994. func newBufferAllocator(size int) *bufferAllocator {
  995. return &bufferAllocator{
  996. bufferSize: size,
  997. finalized: false,
  998. }
  999. }
  1000. func (b *bufferAllocator) getBuffer() []byte {
  1001. b.Lock()
  1002. defer b.Unlock()
  1003. if len(b.available) > 0 {
  1004. var result []byte
  1005. truncLength := len(b.available) - 1
  1006. result = b.available[truncLength]
  1007. b.available[truncLength] = nil
  1008. b.available = b.available[:truncLength]
  1009. return result
  1010. }
  1011. return make([]byte, b.bufferSize)
  1012. }
  1013. func (b *bufferAllocator) releaseBuffer(buf []byte) {
  1014. b.Lock()
  1015. defer b.Unlock()
  1016. if b.finalized || len(buf) != b.bufferSize {
  1017. return
  1018. }
  1019. b.available = append(b.available, buf)
  1020. }
  1021. func (b *bufferAllocator) free() {
  1022. b.Lock()
  1023. defer b.Unlock()
  1024. b.available = nil
  1025. b.finalized = true
  1026. }
  1027. type azureBlobDirLister struct {
  1028. baseDirLister
  1029. paginator *runtime.Pager[container.ListBlobsHierarchyResponse]
  1030. timeout time.Duration
  1031. prefix string
  1032. prefixes map[string]bool
  1033. metricUpdated bool
  1034. }
  1035. func (l *azureBlobDirLister) Next(limit int) ([]os.FileInfo, error) {
  1036. if limit <= 0 {
  1037. return nil, errInvalidDirListerLimit
  1038. }
  1039. if len(l.cache) >= limit {
  1040. return l.returnFromCache(limit), nil
  1041. }
  1042. if !l.paginator.More() {
  1043. if !l.metricUpdated {
  1044. l.metricUpdated = true
  1045. metric.AZListObjectsCompleted(nil)
  1046. }
  1047. return l.returnFromCache(limit), io.EOF
  1048. }
  1049. ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(l.timeout))
  1050. defer cancelFn()
  1051. page, err := l.paginator.NextPage(ctx)
  1052. if err != nil {
  1053. metric.AZListObjectsCompleted(err)
  1054. return l.cache, err
  1055. }
  1056. for _, blobPrefix := range page.ListBlobsHierarchySegmentResponse.Segment.BlobPrefixes {
  1057. name := util.GetStringFromPointer(blobPrefix.Name)
  1058. // we don't support prefixes == "/" this will be sent if a key starts with "/"
  1059. if name == "" || name == "/" {
  1060. continue
  1061. }
  1062. // sometime we have duplicate prefixes, maybe an Azurite bug
  1063. name = strings.TrimPrefix(name, l.prefix)
  1064. if _, ok := l.prefixes[strings.TrimSuffix(name, "/")]; ok {
  1065. continue
  1066. }
  1067. l.cache = append(l.cache, NewFileInfo(name, true, 0, time.Unix(0, 0), false))
  1068. l.prefixes[strings.TrimSuffix(name, "/")] = true
  1069. }
  1070. for _, blobItem := range page.ListBlobsHierarchySegmentResponse.Segment.BlobItems {
  1071. name := util.GetStringFromPointer(blobItem.Name)
  1072. name = strings.TrimPrefix(name, l.prefix)
  1073. size := int64(0)
  1074. isDir := false
  1075. modTime := time.Unix(0, 0)
  1076. if blobItem.Properties != nil {
  1077. size = util.GetIntFromPointer(blobItem.Properties.ContentLength)
  1078. modTime = util.GetTimeFromPointer(blobItem.Properties.LastModified)
  1079. contentType := util.GetStringFromPointer(blobItem.Properties.ContentType)
  1080. isDir = checkDirectoryMarkers(contentType, blobItem.Metadata)
  1081. if isDir {
  1082. // check if the dir is already included, it will be sent as blob prefix if it contains at least one item
  1083. if _, ok := l.prefixes[name]; ok {
  1084. continue
  1085. }
  1086. l.prefixes[name] = true
  1087. }
  1088. }
  1089. l.cache = append(l.cache, NewFileInfo(name, isDir, size, modTime, false))
  1090. }
  1091. return l.returnFromCache(limit), nil
  1092. }
  1093. func (l *azureBlobDirLister) Close() error {
  1094. clear(l.prefixes)
  1095. return l.baseDirLister.Close()
  1096. }