Skip to content

Commit

Permalink
Validate snapshot ID in SnapshotInfo (#1009)
Browse files Browse the repository at this point in the history
* Validate snapshot ID in SnapshotInfo

Signed-off-by: Prasad Ghangal <prasad.ghangal@gmail.com>

* Add new Validate method

Signed-off-by: Prasad Ghangal <prasad.ghangal@gmail.com>

* Refactor snapshot unmarshal method

Signed-off-by: Prasad Ghangal <prasad.ghangal@gmail.com>

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
PrasadG193 and mergify[bot] committed Jun 2, 2021
1 parent e991690 commit 8065055
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
11 changes: 11 additions & 0 deletions pkg/kopia/stream.go
Expand Up @@ -50,6 +50,17 @@ type SnapshotInfo struct {
PhysicalSize int64 `json:"physicalSize"`
}

// Validate validates SnapshotInfo field values
func (si *SnapshotInfo) Validate() error {
if si == nil {
return errors.New("kopia snapshotInfo cannot be nil")
}
if si.ID == "" {
return errors.New("kopia snapshot ID cannot be empty")
}
return nil
}

// Write creates a kopia snapshot from the given reader
// A virtual directory tree rooted at filepath.Dir(path) is created with
// a kopia streaming file with filepath.Base(path) as name
Expand Down
9 changes: 7 additions & 2 deletions pkg/kopia/utils.go
Expand Up @@ -194,6 +194,9 @@ func GetDataStoreGeneralMetadataCacheSize(opt map[string]int) int {

// MarshalKopiaSnapshot encodes kopia SnapshotInfo struct into a string
func MarshalKopiaSnapshot(snapInfo *SnapshotInfo) (string, error) {
if err := snapInfo.Validate(); err != nil {
return "", err
}
snap, err := json.Marshal(snapInfo)
if err != nil {
return "", errors.Wrap(err, "failed to marshal kopia snapshot information")
Expand All @@ -205,6 +208,8 @@ func MarshalKopiaSnapshot(snapInfo *SnapshotInfo) (string, error) {
// UnmarshalKopiaSnapshot decodes a kopia snapshot JSON string into SnapshotInfo struct
func UnmarshalKopiaSnapshot(snapInfoJSON string) (SnapshotInfo, error) {
snap := SnapshotInfo{}
err := json.Unmarshal([]byte(snapInfoJSON), &snap)
return snap, errors.Wrap(err, "failed to unmarshal kopia snapshot information")
if err := json.Unmarshal([]byte(snapInfoJSON), &snap); err != nil {
return snap, errors.Wrap(err, "failed to unmarshal kopia snapshot information")
}
return snap, snap.Validate()
}

0 comments on commit 8065055

Please sign in to comment.