NewFs constructs an Fs from the path, container:path
(ctx context.Context, name, rootPath string, m configmap.Mapper)
| 348 | |
| 349 | // NewFs constructs an Fs from the path, container:path |
| 350 | func NewFs(ctx context.Context, name, rootPath string, m configmap.Mapper) (fs.Fs, error) { |
| 351 | warnDeprecated.Do(func() { |
| 352 | fs.Logf(nil, "WARNING: Cache backend is deprecated and may be removed in future. Please use VFS instead.") |
| 353 | }) |
| 354 | |
| 355 | // Parse config into Options struct |
| 356 | opt := new(Options) |
| 357 | err := configstruct.Set(m, opt) |
| 358 | if err != nil { |
| 359 | return nil, err |
| 360 | } |
| 361 | if opt.ChunkTotalSize < opt.ChunkSize*fs.SizeSuffix(opt.TotalWorkers) { |
| 362 | return nil, fmt.Errorf("don't set cache-chunk-total-size(%v) less than cache-chunk-size(%v) * cache-workers(%v)", |
| 363 | opt.ChunkTotalSize, opt.ChunkSize, opt.TotalWorkers) |
| 364 | } |
| 365 | |
| 366 | if strings.HasPrefix(opt.Remote, name+":") { |
| 367 | return nil, errors.New("can't point cache remote at itself - check the value of the remote setting") |
| 368 | } |
| 369 | |
| 370 | rpath, err := parseRootPath(rootPath) |
| 371 | if err != nil { |
| 372 | return nil, fmt.Errorf("failed to clean root path %q: %w", rootPath, err) |
| 373 | } |
| 374 | |
| 375 | remotePath := fspath.JoinRootPath(opt.Remote, rootPath) |
| 376 | wrappedFs, wrapErr := cache.Get(ctx, remotePath) |
| 377 | if wrapErr != nil && wrapErr != fs.ErrorIsFile { |
| 378 | return nil, fmt.Errorf("failed to make remote %q to wrap: %w", remotePath, wrapErr) |
| 379 | } |
| 380 | var fsErr error |
| 381 | fs.Debugf(name, "wrapped %v:%v at root %v", wrappedFs.Name(), wrappedFs.Root(), rpath) |
| 382 | if wrapErr == fs.ErrorIsFile { |
| 383 | fsErr = fs.ErrorIsFile |
| 384 | rpath = cleanPath(path.Dir(rpath)) |
| 385 | } |
| 386 | // configure cache backend |
| 387 | if opt.DbPurge { |
| 388 | fs.Debugf(name, "Purging the DB") |
| 389 | } |
| 390 | f := &Fs{ |
| 391 | Fs: wrappedFs, |
| 392 | name: name, |
| 393 | root: rpath, |
| 394 | opt: *opt, |
| 395 | lastChunkCleanup: time.Now().Truncate(time.Hour * 24 * 30), |
| 396 | cleanupChan: make(chan bool, 1), |
| 397 | notifiedRemotes: make(map[string]bool), |
| 398 | } |
| 399 | cache.PinUntilFinalized(f.Fs, f) |
| 400 | rps := rate.Inf |
| 401 | if opt.Rps > 0 { |
| 402 | rps = rate.Limit(float64(opt.Rps)) |
| 403 | } |
| 404 | f.rateLimiter = rate.NewLimiter(rps, opt.TotalWorkers) |
| 405 | |
| 406 | f.plexConnector = &plexConnector{} |
| 407 | if opt.PlexURL != "" { |
searching dependent graphs…