github.com/cs3org/reva/v2@v2.27.7/pkg/sdk/action/enumfiles.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 action 20 21 import ( 22 "fmt" 23 24 storage "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" 25 26 "github.com/cs3org/reva/v2/pkg/sdk" 27 "github.com/cs3org/reva/v2/pkg/sdk/common/net" 28 ) 29 30 // EnumFilesAction offers functions to enumerate files and directories. 31 type EnumFilesAction struct { 32 action 33 } 34 35 // ListAll retrieves all files and directories contained in the provided path. 36 func (action *EnumFilesAction) ListAll(path string, includeSubdirectories bool) ([]*storage.ResourceInfo, error) { 37 ref := &storage.Reference{Path: path} 38 req := &storage.ListContainerRequest{Ref: ref} 39 res, err := action.session.Client().ListContainer(action.session.Context(), req) 40 if err := net.CheckRPCInvocation("listing container", res, err); err != nil { 41 return nil, err 42 } 43 44 fileList := make([]*storage.ResourceInfo, 0, len(res.Infos)*64) 45 for _, fi := range res.Infos { 46 // Ignore resources that are neither files nor directories 47 if fi.Type <= storage.ResourceType_RESOURCE_TYPE_INVALID || fi.Type >= storage.ResourceType_RESOURCE_TYPE_INTERNAL { 48 continue 49 } 50 51 fileList = append(fileList, fi) 52 53 if fi.Type == storage.ResourceType_RESOURCE_TYPE_CONTAINER && includeSubdirectories { 54 subFileList, err := action.ListAll(fi.Path, includeSubdirectories) 55 if err != nil { 56 return nil, err 57 } 58 59 fileList = append(fileList, subFileList...) 60 } 61 } 62 63 return fileList, nil 64 } 65 66 // ListAllWithFilter retrieves all files and directories that fulfill the provided predicate. 67 func (action *EnumFilesAction) ListAllWithFilter(path string, includeSubdirectories bool, filter func(*storage.ResourceInfo) bool) ([]*storage.ResourceInfo, error) { 68 all, err := action.ListAll(path, includeSubdirectories) 69 if err != nil { 70 return nil, err 71 } 72 73 fileList := make([]*storage.ResourceInfo, 0, len(all)) 74 75 for _, fi := range all { 76 // Add only those entries that fulfill the predicate 77 if filter(fi) { 78 fileList = append(fileList, fi) 79 } 80 } 81 82 return fileList, nil 83 } 84 85 // ListFiles retrieves all files contained in the provided path. 86 func (action *EnumFilesAction) ListFiles(path string, includeSubdirectories bool) ([]*storage.ResourceInfo, error) { 87 return action.ListAllWithFilter(path, includeSubdirectories, func(fi *storage.ResourceInfo) bool { 88 return fi.Type == storage.ResourceType_RESOURCE_TYPE_FILE || fi.Type == storage.ResourceType_RESOURCE_TYPE_SYMLINK 89 }) 90 } 91 92 // ListDirs retrieves all directories contained in the provided path. 93 func (action *EnumFilesAction) ListDirs(path string, includeSubdirectories bool) ([]*storage.ResourceInfo, error) { 94 return action.ListAllWithFilter(path, includeSubdirectories, func(fi *storage.ResourceInfo) bool { 95 return fi.Type == storage.ResourceType_RESOURCE_TYPE_CONTAINER 96 }) 97 } 98 99 // NewEnumFilesAction creates a new enum files action. 100 func NewEnumFilesAction(session *sdk.Session) (*EnumFilesAction, error) { 101 action := &EnumFilesAction{} 102 if err := action.initAction(session); err != nil { 103 return nil, fmt.Errorf("unable to create the EnumFilesAction: %v", err) 104 } 105 return action, nil 106 } 107 108 // MustNewEnumFilesAction creates a new enum files action and panics on failure. 109 func MustNewEnumFilesAction(session *sdk.Session) *EnumFilesAction { 110 action, err := NewEnumFilesAction(session) 111 if err != nil { 112 panic(err) 113 } 114 return action 115 }