-
Notifications
You must be signed in to change notification settings - Fork 8
/
metadata.go
74 lines (60 loc) · 1.85 KB
/
metadata.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2014 Gyepi Sam. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package redux
import "os"
// File Metadata.
type Metadata struct {
Path string //not used for comparison
ContentHash Hash
DoFile string
}
// Equal compares metadata instances for equality.
func (m *Metadata) Equal(other *Metadata) bool {
return other != nil && m.ContentHash == other.ContentHash
}
// IsCreated compares m to other to determine m represents a newly created file.
func (m Metadata) IsCreated(other Metadata) bool {
return len(other.ContentHash) == 0 && len(m.ContentHash) > 0
}
// NewMetadata returns a metadata instance for the given path.
// If the file is not found, nil is returned.
func NewMetadata(path string, storedPath string) (*Metadata, error) {
hash, err := ContentHash(path)
if os.IsNotExist(err) {
return nil, nil
}
if err != nil {
return nil, err
}
return &Metadata{Path: storedPath, ContentHash: hash}, nil
}
//HasDoFile returns true if the metadata has a non-empty DoField field.
func (m Metadata) HasDoFile() bool {
return len(m.DoFile) > 0
}
// PutMetadata stores the file's metadata in the database.
func (f *File) PutMetadata(m *Metadata) error {
if m != nil {
return f.Put(f.metadataKey(), *m)
}
m, err := NewMetadata(f.Fullpath(), f.Path)
if err != nil {
return err
}
if m == nil {
return f.ErrNotFound("PutMetadata")
}
return f.Put(f.metadataKey(), m)
}
// GetMetadata returns a record as a metadata structure
// found denotes whether the record was found.
func (f *File) GetMetadata() (Metadata, bool, error) {
m := Metadata{}
found, err := f.Get(f.metadataKey(), &m)
return m, found, err
}
// DeleteMetadata removes the metadata record, if any, from the database.
func (f *File) DeleteMetadata() error {
return f.Delete(f.metadataKey())
}