-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework accmeth model to live without backing repository (#566)
* method to blobaccess * fix localblob refs to cv * work * disconnect for local acc meth implementations * rework final update on close * cleanup blob interface + prepare commit * fix close error in transfer
- Loading branch information
1 parent
dcaa66f
commit 2f62868
Showing
62 changed files
with
1,008 additions
and
502 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
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,35 @@ | ||
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Open Component Model contributors. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package blobaccess | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/mandelsoft/vfs/pkg/vfs" | ||
|
||
"github.com/open-component-model/ocm/pkg/utils" | ||
) | ||
|
||
func ForCachedBlobAccess(blob BlobAccess, fss ...vfs.FileSystem) (BlobAccess, error) { | ||
fs := utils.FileSystem(fss...) | ||
|
||
r, err := blob.Reader() | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer r.Close() | ||
|
||
file, err := vfs.TempFile(fs, "", "cachedBlob*") | ||
if err != nil { | ||
return nil, err | ||
} | ||
_, err = io.Copy(file, r) | ||
if err != nil { | ||
return nil, err | ||
} | ||
file.Close() | ||
|
||
return ForTemporaryFilePath(blob.MimeType(), file.Name(), fs), nil | ||
} |
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,23 @@ | ||
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Open Component Model contributors. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package blobaccess | ||
|
||
import ( | ||
"github.com/opencontainers/go-digest" | ||
) | ||
|
||
func Digest(access DataAccess) (digest.Digest, error) { | ||
reader, err := access.Reader() | ||
if err != nil { | ||
return "", err | ||
} | ||
defer reader.Close() | ||
|
||
dig, err := digest.FromReader(reader) | ||
if err != nil { | ||
return "", err | ||
} | ||
return dig, nil | ||
} |
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
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 |
---|---|---|
@@ -1,119 +1,48 @@ | ||
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors. | ||
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Open Component Model contributors. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package accessio | ||
|
||
import ( | ||
"crypto" | ||
"hash" | ||
"io" | ||
|
||
"github.com/opencontainers/go-digest" | ||
|
||
"github.com/open-component-model/ocm/pkg/blobaccess" | ||
"github.com/open-component-model/ocm/pkg/errors" | ||
"github.com/open-component-model/ocm/pkg/iotools" | ||
) | ||
|
||
// wow. digest does support a map with supported digesters. Unfortunately this one does not | ||
// contain all the crypto hashes AND this map is private NAD there is no function to add entries, | ||
// so that it cannot be extended from outside the package. I love GO. | ||
// Therefore, we have to fake it a little to support digests with other crypto hashes. | ||
// Deprecated: use iotools.DigestReader. | ||
type DigestReader = iotools.DigestReader | ||
|
||
type DigestReader struct { | ||
reader io.Reader | ||
alg digest.Algorithm | ||
hash hash.Hash | ||
count int64 | ||
// Deprecated: use iotools.NewDefaultDigestReader. | ||
func NewDefaultDigestReader(r io.Reader) *iotools.DigestReader { | ||
return iotools.NewDigestReaderWith(digest.Canonical, r) | ||
} | ||
|
||
func (r *DigestReader) Size() int64 { | ||
return r.count | ||
// Deprecated: use iotools.NewDigestReaderWith. | ||
func NewDigestReaderWith(algorithm digest.Algorithm, r io.Reader) *iotools.DigestReader { | ||
return iotools.NewDigestReaderWith(algorithm, r) | ||
} | ||
|
||
func (r *DigestReader) Digest() digest.Digest { | ||
return digest.NewDigest(r.alg, r.hash) | ||
// Deprecated: use iotools.NewDigestReaderWithHash. | ||
func NewDigestReaderWithHash(hash crypto.Hash, r io.Reader) *iotools.DigestReader { | ||
return iotools.NewDigestReaderWithHash(hash, r) | ||
} | ||
|
||
func (r *DigestReader) Read(buf []byte) (int, error) { | ||
c, err := r.reader.Read(buf) | ||
if c > 0 { | ||
r.count += int64(c) | ||
r.hash.Write(buf[:c]) | ||
} | ||
return c, err | ||
} | ||
|
||
func NewDefaultDigestReader(r io.Reader) *DigestReader { | ||
return NewDigestReaderWith(digest.Canonical, r) | ||
} | ||
|
||
func NewDigestReaderWith(algorithm digest.Algorithm, r io.Reader) *DigestReader { | ||
digester := algorithm.Digester() | ||
return &DigestReader{ | ||
reader: r, | ||
hash: digester.Hash(), | ||
alg: algorithm, | ||
count: 0, | ||
} | ||
// Deprecated: use iotools.VerifyingReader. | ||
func VerifyingReader(r io.ReadCloser, digest digest.Digest) io.ReadCloser { | ||
return iotools.VerifyingReader(r, digest) | ||
} | ||
|
||
func NewDigestReaderWithHash(hash crypto.Hash, r io.Reader) *DigestReader { | ||
return &DigestReader{ | ||
reader: r, | ||
hash: hash.New(), | ||
alg: digest.Algorithm(hash.String()), // fake a non-supported digest algorithm | ||
count: 0, | ||
} | ||
// Deprecated: use iotools.VerifyingReaderWithHash. | ||
func VerifyingReaderWithHash(r io.ReadCloser, hash crypto.Hash, digest string) io.ReadCloser { | ||
return iotools.VerifyingReaderWithHash(r, hash, digest) | ||
} | ||
|
||
// Deprecated: use blobaccess.Digest. | ||
func Digest(access blobaccess.DataAccess) (digest.Digest, error) { | ||
reader, err := access.Reader() | ||
if err != nil { | ||
return "", err | ||
} | ||
defer reader.Close() | ||
|
||
dig, err := digest.FromReader(reader) | ||
if err != nil { | ||
return "", err | ||
} | ||
return dig, nil | ||
} | ||
|
||
type verifiedReader struct { | ||
closer io.Closer | ||
*DigestReader | ||
hash string | ||
digest string | ||
} | ||
|
||
func (v *verifiedReader) Close() error { | ||
err := v.closer.Close() | ||
if err != nil { | ||
return err | ||
} | ||
dig := v.DigestReader.Digest() | ||
if dig.Hex() != v.digest { | ||
return errors.Newf("%s digest mismatch: expected %s, found %s", v.hash, v.digest, dig.Hex()) | ||
} | ||
return nil | ||
} | ||
|
||
func VerifyingReader(r io.ReadCloser, digest digest.Digest) io.ReadCloser { | ||
return &verifiedReader{ | ||
closer: r, | ||
DigestReader: NewDigestReaderWith(digest.Algorithm(), r), | ||
hash: digest.Algorithm().String(), | ||
digest: digest.Hex(), | ||
} | ||
} | ||
|
||
func VerifyingReaderWithHash(r io.ReadCloser, hash crypto.Hash, digest string) io.ReadCloser { | ||
return &verifiedReader{ | ||
closer: r, | ||
DigestReader: NewDigestReaderWithHash(hash, r), | ||
hash: hash.String(), | ||
digest: digest, | ||
} | ||
return blobaccess.Digest(access) | ||
} |
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
Oops, something went wrong.