12216: Add webdav handler.
[arvados.git] / sdk / go / arvados / collection_fs.go
index 89b296e3b527facfe8a14f81e8d055dbb3de5ef0..8ecbe9c197de49da13a6a8d4ded90d1cba83e198 100644 (file)
@@ -1,3 +1,7 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: Apache-2.0
+
 package arvados
 
 import (
@@ -6,6 +10,7 @@ import (
        "os"
        "path"
        "strings"
+       "sync"
        "time"
 
        "git.curoverse.com/arvados.git/sdk/go/manifest"
@@ -146,6 +151,8 @@ type collectionFS struct {
        collection *Collection
        client     *Client
        kc         keepClient
+       sizes      map[string]int64
+       sizesOnce  sync.Once
 }
 
 // FileSystem returns an http.FileSystem for the collection.
@@ -188,8 +195,6 @@ func (c *collectionFS) Open(name string) (http.File, error) {
        // entries below it.
        children := map[string]collectionDirent{}
        for fnm, size := range filesizes {
-               if fnm == name {
-               }
                if !strings.HasPrefix(fnm, name+"/") {
                        continue
                }
@@ -223,15 +228,14 @@ func (c *collectionFS) Open(name string) (http.File, error) {
 // fileSizes returns a map of files that can be opened. Each key
 // starts with "./".
 func (c *collectionFS) fileSizes() map[string]int64 {
-       var sizes map[string]int64
-       m := manifest.Manifest{Text: c.collection.ManifestText}
-       for ms := range m.StreamIter() {
-               for _, fss := range ms.FileStreamSegments {
-                       if sizes == nil {
-                               sizes = map[string]int64{}
+       c.sizesOnce.Do(func() {
+               c.sizes = map[string]int64{}
+               m := manifest.Manifest{Text: c.collection.ManifestText}
+               for ms := range m.StreamIter() {
+                       for _, fss := range ms.FileStreamSegments {
+                               c.sizes[ms.StreamName+"/"+fss.Name] += int64(fss.SegLen)
                        }
-                       sizes[ms.StreamName+"/"+fss.Name] += int64(fss.SegLen)
                }
-       }
-       return sizes
+       })
+       return c.sizes
 }