GetDirEntries will return a CachedDirectory, its list of dir entries and/or an error if it encountered issues
(cachedDir *Directory)
| 237 | |
| 238 | // GetDirEntries will return a CachedDirectory, its list of dir entries and/or an error if it encountered issues |
| 239 | func (b *Persistent) GetDirEntries(cachedDir *Directory) (fs.DirEntries, error) { |
| 240 | var dirEntries fs.DirEntries |
| 241 | |
| 242 | err := b.db.View(func(tx *bolt.Tx) error { |
| 243 | bucket := b.getBucket(cachedDir.abs(), false, tx) |
| 244 | if bucket == nil { |
| 245 | return fmt.Errorf("couldn't open bucket (%v)", cachedDir.abs()) |
| 246 | } |
| 247 | |
| 248 | val := bucket.Get([]byte(".")) |
| 249 | if val != nil { |
| 250 | err := json.Unmarshal(val, cachedDir) |
| 251 | if err != nil { |
| 252 | return fmt.Errorf("error during unmarshalling obj: %w", err) |
| 253 | } |
| 254 | } else { |
| 255 | return fmt.Errorf("missing cached dir: %v", cachedDir) |
| 256 | } |
| 257 | |
| 258 | c := bucket.Cursor() |
| 259 | for k, v := c.First(); k != nil; k, v = c.Next() { |
| 260 | // ignore metadata key: . |
| 261 | if bytes.Equal(k, []byte(".")) { |
| 262 | continue |
| 263 | } |
| 264 | entryPath := path.Join(cachedDir.Remote(), string(k)) |
| 265 | |
| 266 | if v == nil { // directory |
| 267 | // we try to find a cached meta for the dir |
| 268 | currentBucket := c.Bucket().Bucket(k) |
| 269 | if currentBucket == nil { |
| 270 | return fmt.Errorf("couldn't open bucket (%v)", string(k)) |
| 271 | } |
| 272 | |
| 273 | metaKey := currentBucket.Get([]byte(".")) |
| 274 | d := NewDirectory(cachedDir.CacheFs, entryPath) |
| 275 | if metaKey != nil { //if we don't find it, we create an empty dir |
| 276 | err := json.Unmarshal(metaKey, d) |
| 277 | if err != nil { // if even this fails, we fallback to an empty dir |
| 278 | fs.Debugf(string(k), "error during unmarshalling obj: %v", err) |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | dirEntries = append(dirEntries, d) |
| 283 | } else { // object |
| 284 | o := NewObject(cachedDir.CacheFs, entryPath) |
| 285 | err := json.Unmarshal(v, o) |
| 286 | if err != nil { |
| 287 | fs.Debugf(string(k), "error during unmarshalling obj: %v", err) |
| 288 | continue |
| 289 | } |
| 290 | |
| 291 | dirEntries = append(dirEntries, o) |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | return nil |
| 296 | }) |
no test coverage detected