forked from cs3org/reva
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch the ocis driver over to the decomposed base driver
- Loading branch information
Showing
19 changed files
with
403 additions
and
3,858 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright 2018-2021 CERN | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// In applying this license, CERN does not waive the privileges and immunities | ||
// granted to it by virtue of its status as an Intergovernmental Organization | ||
// or submit itself to any jurisdiction. | ||
|
||
package blobstore | ||
|
||
import ( | ||
"bufio" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
"path" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
// Blobstore provides an interface to an filesystem based blobstore | ||
type Blobstore struct { | ||
root string | ||
} | ||
|
||
// New returns a new Blobstore | ||
func New(root string) (*Blobstore, error) { | ||
err := os.MkdirAll(root, 0700) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Blobstore{ | ||
root: root, | ||
}, nil | ||
} | ||
|
||
// Upload stores some data in the blobstore under the given key | ||
func (bs *Blobstore) Upload(key string, data io.Reader) error { | ||
f, err := os.OpenFile(bs.path(key), os.O_CREATE|os.O_WRONLY, 0700) | ||
if err != nil { | ||
return errors.Wrapf(err, "could not open blob '%s' for writing", key) | ||
} | ||
|
||
w := bufio.NewWriter(f) | ||
_, err = w.ReadFrom(data) | ||
if err != nil { | ||
return errors.Wrapf(err, "could not write blob '%s'", key) | ||
} | ||
|
||
w.Flush() | ||
return nil | ||
} | ||
|
||
// Download retrieves a blob from the blobstore for reading | ||
func (bs *Blobstore) Download(key string) (io.ReadCloser, error) { | ||
file, err := os.Open(path.Join(bs.root, key)) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "could not read blob '%s'", key) | ||
} | ||
return ioutil.NopCloser(file), nil | ||
} | ||
|
||
// Delete deletes a blob from the blobstore | ||
func (bs *Blobstore) Delete(key string) error { | ||
err := os.Remove(bs.path(key)) | ||
if err != nil { | ||
return errors.Wrapf(err, "could not delete blod '%s'", key) | ||
} | ||
return nil | ||
} | ||
|
||
func (bs *Blobstore) path(key string) string { | ||
return path.Join(bs.root, key) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright 2018-2021 CERN | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// In applying this license, CERN does not waive the privileges and immunities | ||
// granted to it by virtue of its status as an Intergovernmental Organization | ||
// or submit itself to any jurisdiction. | ||
|
||
package blobstore_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestBlobstore(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Blobstore Suite") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// Copyright 2018-2021 CERN | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// In applying this license, CERN does not waive the privileges and immunities | ||
// granted to it by virtue of its status as an Intergovernmental Organization | ||
// or submit itself to any jurisdiction. | ||
|
||
package blobstore_test | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"os" | ||
"path" | ||
"strings" | ||
|
||
"github.com/cs3org/reva/pkg/storage/fs/ocis/blobstore" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("Blobstore", func() { | ||
var ( | ||
tmpRoot string | ||
key string | ||
blobPath string | ||
data []byte | ||
|
||
bs *blobstore.Blobstore | ||
) | ||
|
||
BeforeEach(func() { | ||
var err error | ||
tmpRoot, err = ioutil.TempDir("", "reva-unit-tests-*-root") | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
data = []byte("1234567890") | ||
key = "foo" | ||
blobPath = path.Join(tmpRoot, "blobs", key) | ||
|
||
bs, err = blobstore.New(path.Join(tmpRoot, "blobs")) | ||
Expect(err).ToNot(HaveOccurred()) | ||
}) | ||
|
||
AfterEach(func() { | ||
if strings.HasPrefix(tmpRoot, os.TempDir()) { | ||
os.RemoveAll(tmpRoot) | ||
} | ||
}) | ||
|
||
It("creates the root directory if it doesn't exist", func() { | ||
_, err := os.Stat(path.Join(tmpRoot, "blobs")) | ||
Expect(err).ToNot(HaveOccurred()) | ||
}) | ||
|
||
Describe("Upload", func() { | ||
It("writes the blob", func() { | ||
err := bs.Upload(key, bytes.NewReader(data)) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
writtenBytes, err := ioutil.ReadFile(blobPath) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(writtenBytes).To(Equal(data)) | ||
}) | ||
}) | ||
|
||
Context("with an existing blob", func() { | ||
BeforeEach(func() { | ||
Expect(ioutil.WriteFile(blobPath, data, 0700)).To(Succeed()) | ||
}) | ||
|
||
Describe("Download", func() { | ||
It("returns a reader to the blob", func() { | ||
reader, err := bs.Download(key) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
readData, err := ioutil.ReadAll(reader) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(readData).To(Equal(data)) | ||
}) | ||
}) | ||
|
||
Describe("Delete", func() { | ||
It("deletes the blob", func() { | ||
_, err := os.Stat(blobPath) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
err = bs.Delete(key) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
_, err = os.Stat(blobPath) | ||
Expect(err).To(HaveOccurred()) | ||
}) | ||
}) | ||
}) | ||
|
||
}) |
Oops, something went wrong.