newStorage returns a new storage in path root with the given maxFileSize, or defaultMaxFileSize (512MB) if <= 0
(root string, maxFileSize int64, indexConf jsonconfig.Obj)
| 161 | // newStorage returns a new storage in path root with the given maxFileSize, |
| 162 | // or defaultMaxFileSize (512MB) if <= 0 |
| 163 | func 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 | |
| 208 | func newFromConfig(_ blobserver.Loader, config jsonconfig.Obj) (storage blobserver.Storage, err error) { |
| 209 | var ( |