github.com/stevegt/moby@v1.13.1/image/fs.go (about) 1 package image 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "sync" 9 10 "github.com/Sirupsen/logrus" 11 "github.com/docker/distribution/digest" 12 "github.com/docker/docker/pkg/ioutils" 13 ) 14 15 // DigestWalkFunc is function called by StoreBackend.Walk 16 type DigestWalkFunc func(id digest.Digest) error 17 18 // StoreBackend provides interface for image.Store persistence 19 type StoreBackend interface { 20 Walk(f DigestWalkFunc) error 21 Get(id digest.Digest) ([]byte, error) 22 Set(data []byte) (digest.Digest, error) 23 Delete(id digest.Digest) error 24 SetMetadata(id digest.Digest, key string, data []byte) error 25 GetMetadata(id digest.Digest, key string) ([]byte, error) 26 DeleteMetadata(id digest.Digest, key string) error 27 } 28 29 // fs implements StoreBackend using the filesystem. 30 type fs struct { 31 sync.RWMutex 32 root string 33 } 34 35 const ( 36 contentDirName = "content" 37 metadataDirName = "metadata" 38 ) 39 40 // NewFSStoreBackend returns new filesystem based backend for image.Store 41 func NewFSStoreBackend(root string) (StoreBackend, error) { 42 return newFSStore(root) 43 } 44 45 func newFSStore(root string) (*fs, error) { 46 s := &fs{ 47 root: root, 48 } 49 if err := os.MkdirAll(filepath.Join(root, contentDirName, string(digest.Canonical)), 0700); err != nil { 50 return nil, err 51 } 52 if err := os.MkdirAll(filepath.Join(root, metadataDirName, string(digest.Canonical)), 0700); err != nil { 53 return nil, err 54 } 55 return s, nil 56 } 57 58 func (s *fs) contentFile(dgst digest.Digest) string { 59 return filepath.Join(s.root, contentDirName, string(dgst.Algorithm()), dgst.Hex()) 60 } 61 62 func (s *fs) metadataDir(dgst digest.Digest) string { 63 return filepath.Join(s.root, metadataDirName, string(dgst.Algorithm()), dgst.Hex()) 64 } 65 66 // Walk calls the supplied callback for each image ID in the storage backend. 67 func (s *fs) Walk(f DigestWalkFunc) error { 68 // Only Canonical digest (sha256) is currently supported 69 s.RLock() 70 dir, err := ioutil.ReadDir(filepath.Join(s.root, contentDirName, string(digest.Canonical))) 71 s.RUnlock() 72 if err != nil { 73 return err 74 } 75 for _, v := range dir { 76 dgst := digest.NewDigestFromHex(string(digest.Canonical), v.Name()) 77 if err := dgst.Validate(); err != nil { 78 logrus.Debugf("Skipping invalid digest %s: %s", dgst, err) 79 continue 80 } 81 if err := f(dgst); err != nil { 82 return err 83 } 84 } 85 return nil 86 } 87 88 // Get returns the content stored under a given digest. 89 func (s *fs) Get(dgst digest.Digest) ([]byte, error) { 90 s.RLock() 91 defer s.RUnlock() 92 93 return s.get(dgst) 94 } 95 96 func (s *fs) get(dgst digest.Digest) ([]byte, error) { 97 content, err := ioutil.ReadFile(s.contentFile(dgst)) 98 if err != nil { 99 return nil, err 100 } 101 102 // todo: maybe optional 103 if digest.FromBytes(content) != dgst { 104 return nil, fmt.Errorf("failed to verify: %v", dgst) 105 } 106 107 return content, nil 108 } 109 110 // Set stores content by checksum. 111 func (s *fs) Set(data []byte) (digest.Digest, error) { 112 s.Lock() 113 defer s.Unlock() 114 115 if len(data) == 0 { 116 return "", fmt.Errorf("Invalid empty data") 117 } 118 119 dgst := digest.FromBytes(data) 120 if err := ioutils.AtomicWriteFile(s.contentFile(dgst), data, 0600); err != nil { 121 return "", err 122 } 123 124 return dgst, nil 125 } 126 127 // Delete removes content and metadata files associated with the digest. 128 func (s *fs) Delete(dgst digest.Digest) error { 129 s.Lock() 130 defer s.Unlock() 131 132 if err := os.RemoveAll(s.metadataDir(dgst)); err != nil { 133 return err 134 } 135 if err := os.Remove(s.contentFile(dgst)); err != nil { 136 return err 137 } 138 return nil 139 } 140 141 // SetMetadata sets metadata for a given ID. It fails if there's no base file. 142 func (s *fs) SetMetadata(dgst digest.Digest, key string, data []byte) error { 143 s.Lock() 144 defer s.Unlock() 145 if _, err := s.get(dgst); err != nil { 146 return err 147 } 148 149 baseDir := filepath.Join(s.metadataDir(dgst)) 150 if err := os.MkdirAll(baseDir, 0700); err != nil { 151 return err 152 } 153 return ioutils.AtomicWriteFile(filepath.Join(s.metadataDir(dgst), key), data, 0600) 154 } 155 156 // GetMetadata returns metadata for a given digest. 157 func (s *fs) GetMetadata(dgst digest.Digest, key string) ([]byte, error) { 158 s.RLock() 159 defer s.RUnlock() 160 161 if _, err := s.get(dgst); err != nil { 162 return nil, err 163 } 164 return ioutil.ReadFile(filepath.Join(s.metadataDir(dgst), key)) 165 } 166 167 // DeleteMetadata removes the metadata associated with a digest. 168 func (s *fs) DeleteMetadata(dgst digest.Digest, key string) error { 169 s.Lock() 170 defer s.Unlock() 171 172 return os.RemoveAll(filepath.Join(s.metadataDir(dgst), key)) 173 }