github.com/cs3org/reva/v2@v2.27.7/pkg/storage/fs/posix/blobstore/blobstore.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 blobstore 20 21 import ( 22 "bufio" 23 "io" 24 "os" 25 26 "github.com/cs3org/reva/v2/pkg/storage/utils/decomposedfs/node" 27 "github.com/pkg/errors" 28 ) 29 30 // Blobstore provides an interface to an filesystem based blobstore 31 type Blobstore struct { 32 root string 33 } 34 35 // New returns a new Blobstore 36 func New(root string) (*Blobstore, error) { 37 return &Blobstore{ 38 root: root, 39 }, nil 40 } 41 42 // Upload stores some data in the blobstore under the given key 43 func (bs *Blobstore) Upload(node *node.Node, source string) error { 44 path := node.InternalPath() 45 46 // preserve the mtime of the file 47 fi, _ := os.Stat(path) 48 49 file, err := os.Open(source) 50 if err != nil { 51 return errors.Wrap(err, "Decomposedfs: oCIS blobstore: Can not open source file to upload") 52 } 53 defer file.Close() 54 55 f, err := os.OpenFile(node.InternalPath(), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0700) 56 if err != nil { 57 return errors.Wrapf(err, "could not open blob '%s' for writing", node.InternalPath()) 58 } 59 defer f.Close() 60 61 w := bufio.NewWriter(f) 62 _, err = w.ReadFrom(file) 63 if err != nil { 64 return errors.Wrapf(err, "could not write blob '%s'", node.InternalPath()) 65 } 66 67 err = w.Flush() 68 if err != nil { 69 return err 70 } 71 72 if fi != nil { 73 return os.Chtimes(path, fi.ModTime(), fi.ModTime()) 74 } 75 return nil 76 } 77 78 // Download retrieves a blob from the blobstore for reading 79 func (bs *Blobstore) Download(node *node.Node) (io.ReadCloser, error) { 80 file, err := os.Open(node.InternalPath()) 81 if err != nil { 82 return nil, errors.Wrapf(err, "could not read blob '%s'", node.InternalPath()) 83 } 84 return file, nil 85 } 86 87 // Delete deletes a blob from the blobstore 88 func (bs *Blobstore) Delete(node *node.Node) error { 89 return nil 90 }