Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
Address review feedback
Browse files Browse the repository at this point in the history
Signed-off-by: Dennis Marttinen <dennis@weave.works>
  • Loading branch information
twelho committed Aug 21, 2020
1 parent ef8bafd commit 0b21bbc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
18 changes: 7 additions & 11 deletions pkg/storage/mappedrawstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strconv"
"sync"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -60,6 +59,7 @@ func (r *GenericMappedRawStorage) realPath(key ObjectKey) (string, error) {
return path, nil
}

// If the file doesn't exist, returns ErrNotFound + ErrNotTracked.
func (r *GenericMappedRawStorage) Read(key ObjectKey) ([]byte, error) {
file, err := r.realPath(key)
if err != nil {
Expand Down Expand Up @@ -89,6 +89,7 @@ func (r *GenericMappedRawStorage) Write(key ObjectKey, content []byte) error {
return ioutil.WriteFile(file, content, 0644)
}

// If the file doesn't exist, returns ErrNotFound + ErrNotTracked.
func (r *GenericMappedRawStorage) Delete(key ObjectKey) (err error) {
file, err := r.realPath(key)
if err != nil {
Expand Down Expand Up @@ -121,20 +122,15 @@ func (r *GenericMappedRawStorage) List(kind KindKey) ([]ObjectKey, error) {
return result, nil
}

// This returns the modification time as a UnixNano string
// If the file doesn't exist, return ErrNotFound
func (r *GenericMappedRawStorage) Checksum(key ObjectKey) (s string, err error) {
file, err := r.realPath(key)
if err != nil {
return
}

fi, err := os.Stat(file)
// This returns the modification time as a UnixNano string.
// If the file doesn't exist, returns ErrNotFound + ErrNotTracked.
func (r *GenericMappedRawStorage) Checksum(key ObjectKey) (string, error) {
path, err := r.realPath(key)
if err != nil {
return "", err
}

return strconv.FormatInt(fi.ModTime().UnixNano(), 10), nil
return checksumFromModTime(path)
}

func (r *GenericMappedRawStorage) ContentType(key ObjectKey) (ct serializer.ContentType) {
Expand Down
18 changes: 11 additions & 7 deletions pkg/storage/rawstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (r *GenericRawStorage) List(kind KindKey) ([]ObjectKey, error) {

// This returns the modification time as a UnixNano string
// If the file doesn't exist, return ErrNotFound
func (r *GenericRawStorage) Checksum(key ObjectKey) (s string, err error) {
func (r *GenericRawStorage) Checksum(key ObjectKey) (string, error) {
// Validate GroupVersion first
if err := r.validateGroupVersion(key); err != nil {
return "", err
Expand All @@ -172,12 +172,7 @@ func (r *GenericRawStorage) Checksum(key ObjectKey) (s string, err error) {
return "", ErrNotFound
}

fi, err := os.Stat(r.keyPath(key))
if err != nil {
return "", err
}

return strconv.FormatInt(fi.ModTime().UnixNano(), 10), nil
return checksumFromModTime(r.keyPath(key))
}

func (r *GenericRawStorage) ContentType(_ ObjectKey) serializer.ContentType {
Expand Down Expand Up @@ -211,3 +206,12 @@ func (r *GenericRawStorage) GetKey(p string) (ObjectKey, error) {

return NewObjectKey(NewKindKey(gvk), runtime.NewIdentifier(uid)), nil
}

func checksumFromModTime(path string) (string, error) {
fi, err := os.Stat(path)
if err != nil {
return "", err
}

return strconv.FormatInt(fi.ModTime().UnixNano(), 10), nil
}
4 changes: 2 additions & 2 deletions pkg/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ type ReadStorage interface {

type WriteStorage interface {
// Create creates an entry for and stores the given Object in the storage. The Object must be new to the storage.
// The ObjectMeta.Created field is set automatically to the current time if it is unset.
// The ObjectMeta.CreationTimestamp field is set automatically to the current time if it is unset.
Create(obj runtime.Object) error
// Update updates the state of the given Object in the storage. The Object must exist in the storage.
// The ObjectMeta.Created field is set automatically to the current time if it is unset.
// The ObjectMeta.CreationTimestamp field is set automatically to the current time if it is unset.
Update(obj runtime.Object) error

// Patch performs a strategic merge patch on the Object with the given UID, using the byte-encoded patch given
Expand Down

0 comments on commit 0b21bbc

Please sign in to comment.