MCPcopy
hub / github.com/perkeep/perkeep / newStorage

Function newStorage

pkg/blobserver/diskpacked/diskpacked.go:163–206  ·  view source on GitHub ↗

newStorage returns a new storage in path root with the given maxFileSize, or defaultMaxFileSize (512MB) if <= 0

(root string, maxFileSize int64, indexConf jsonconfig.Obj)

Source from the content-addressed store, hash-verified

161// newStorage returns a new storage in path root with the given maxFileSize,
162// or defaultMaxFileSize (512MB) if <= 0
163func newStorage(root string, maxFileSize int64, indexConf jsonconfig.Obj) (s *storage, err error) {
164 fi, err := os.Stat(root)
165 if os.IsNotExist(err) {
166 return nil, fmt.Errorf("storage root %q doesn't exist", root)
167 }
168 if err != nil {
169 return nil, fmt.Errorf("failed to stat directory %q: %w", root, err)
170 }
171 if !fi.IsDir() {
172 return nil, fmt.Errorf("storage root %q exists but is not a directory", root)
173 }
174 index, err := newIndex(root, indexConf)
175 if err != nil {
176 return nil, err
177 }
178 defer func() {
179 if err != nil {
180 index.Close()
181 }
182 }()
183 if maxFileSize <= 0 {
184 maxFileSize = defaultMaxFileSize
185 }
186 // Be consistent with trailing slashes. Makes expvar stats for total
187 // reads/writes consistent across diskpacked targets, regardless of what
188 // people put in their low level config.
189 root = strings.TrimRight(root, `\/`)
190 s = &storage{
191 root: root,
192 index: index,
193 maxFileSize: maxFileSize,
194 Generationer: local.NewGenerationer(root),
195 }
196 if err := s.openAllPacks(); err != nil {
197 s.Close()
198 return nil, err
199 }
200 s.mu.Lock()
201 defer s.mu.Unlock()
202 if _, _, err := s.StorageGeneration(); err != nil {
203 return nil, fmt.Errorf("error initialization generation for %q: %w", root, err)
204 }
205 return s, nil
206}
207
208func newFromConfig(_ blobserver.Loader, config jsonconfig.Obj) (storage blobserver.Storage, err error) {
209 var (

Callers 6

newTestStorageFunction · 0.85
TestBadDirFunction · 0.85
TestWriteErrorFunction · 0.85
NewFunction · 0.85
newFromConfigFunction · 0.85

Calls 9

NewGenerationerFunction · 0.92
newIndexFunction · 0.85
openAllPacksMethod · 0.80
LockMethod · 0.80
UnlockMethod · 0.80
StatMethod · 0.65
IsDirMethod · 0.65
CloseMethod · 0.65
StorageGenerationMethod · 0.65

Tested by 4

newTestStorageFunction · 0.68
TestBadDirFunction · 0.68
TestWriteErrorFunction · 0.68