azblobfs.go 35 KB

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