github.com/cs3org/reva/v2@v2.27.7/pkg/storage/utils/walker/mock/walker_mock.go (about) 1 // Copyright 2018-2021 CERN 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // In applying this license, CERN does not waive the privileges and immunities 16 // granted to it by virtue of its status as an Intergovernmental Organization 17 // or submit itself to any jurisdiction. 18 19 package mock 20 21 import ( 22 "context" 23 "io/fs" 24 "path/filepath" 25 26 provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" 27 typesv1beta1 "github.com/cs3org/go-cs3apis/cs3/types/v1beta1" 28 "github.com/cs3org/reva/v2/pkg/storage/utils/walker" 29 ) 30 31 type mockWalker struct { 32 tmpDir string 33 } 34 35 // NewWalker creates a mock walker that implements the Walk interface 36 // supposed to be used for testing 37 func NewWalker(tmpDir string) walker.Walker { 38 return &mockWalker{ 39 tmpDir: tmpDir, 40 } 41 } 42 43 // converts a FileInfo to a reva ResourceInfo 44 func convertFileInfoToResourceInfo(path string, f fs.FileInfo) *provider.ResourceInfo { 45 if f == nil { 46 return nil 47 } 48 // resource type conversion 49 t := provider.ResourceType_RESOURCE_TYPE_FILE 50 if f.IsDir() { 51 t = provider.ResourceType_RESOURCE_TYPE_CONTAINER 52 } 53 return &provider.ResourceInfo{ 54 Type: t, 55 Path: f.Name(), 56 Id: &provider.ResourceId{OpaqueId: path}, 57 Size: uint64(f.Size()), 58 Mtime: &typesv1beta1.Timestamp{ 59 Seconds: uint64(f.ModTime().Second()), 60 }, 61 } 62 } 63 64 func mockWalkFunc(fn walker.WalkFunc, tmpDir string) filepath.WalkFunc { 65 return func(path string, info fs.FileInfo, err error) error { 66 relativePath, relErr := filepath.Rel(tmpDir, path) 67 if relErr != nil { 68 return relErr 69 } 70 relativePath = filepath.Join("/", relativePath) 71 return fn(filepath.Dir(relativePath), convertFileInfoToResourceInfo(path, info), err) 72 } 73 } 74 75 // Walk walks into the local file system using the built-in filepath.Walk go function 76 func (m *mockWalker) Walk(_ context.Context, root *provider.ResourceId, fn walker.WalkFunc) error { 77 return filepath.Walk(root.OpaqueId, mockWalkFunc(fn, m.tmpDir)) 78 }