Repository: rclone/rclone
File: backend/b2/b2.go (Go)

Code before:
		ReadMimeType:          true,
		WriteMimeType:         true,
		BucketBased:           true,
		BucketBasedRootOK:     true,
		ChunkWriterDoesntSeek: true,
	}).Fill(ctx, f)
	// Set the test flag if required
	if opt.TestMode != "" {
		testMode := strings.TrimSpace(opt.TestMode)
		f.srv.SetHeader(testModeHeader, testMode)
		fs.Debugf(f, "Setting test header \"%s: %s\"", testModeHeader, testMode)
	}
	err = f.authorizeAccount(ctx)
	if err != nil {
		return nil, fmt.Errorf("failed to authorize account: %w", err)
	}
	// If this is a key limited to number of bucket, all must exist already,
	// and one of them must be ours.
	if f.rootBucket != "" && len(f.info.APIs.Storage.Allowed.Buckets) != 0 {
		buckets := f.info.APIs.Storage.Allowed.Buckets
		var rootFound = false
		var rootID string
		for _, b := range buckets {
			allowedBucket := f.opt.Enc.ToStandardName(b.Name)
			if allowedBucket == "" {
				return nil, errors.New("bucket that application key is restricted to no longer exists")
			}

			if allowedBucket == f.rootBucket {
				rootFound = true
				rootID = b.ID
			}
		}
		if !rootFound {
			return nil, fmt.Errorf("you must use bucket(s) %q with this application key", buckets)
		}
		f.cache.MarkOK(f.rootBucket)
		f.setBucketID(f.rootBucket, rootID)
	}
	if f.rootBucket != "" && f.rootDirectory != "" {
		// Check to see if the (bucket,directory) is actually an existing file
		oldRoot := f.root
		newRoot, leaf := path.Split(oldRoot)
		f.setRoot(newRoot)
		_, err := f.NewObject(ctx, leaf)
		if err != nil {
			// File doesn't exist so return old f
			f.setRoot(oldRoot)
			return f, nil
		}
		// return an error with an fs which points to the parent

Review comment (bug):
This probably doesn't want to be a fatal error any more - there might be lots of buckets and if our bucket is still around then that is OK. Probably I'd turn that into a `fs.Debugf` and `continue` the loop

Code after:
		ReadMimeType:          true,
		WriteMimeType:         true,
		BucketBased:           true,
		BucketBasedRootOK:     true,
		ChunkWriterDoesntSeek: true,
	}).Fill(ctx, f)
	// Set the test flag if required
	if opt.TestMode != "" {
		testMode := strings.TrimSpace(opt.TestMode)
		f.srv.SetHeader(testModeHeader, testMode)
		fs.Debugf(f, "Setting test header \"%s: %s\"", testModeHeader, testMode)
	}
	err = f.authorizeAccount(ctx)
	if err != nil {
		return nil, fmt.Errorf("failed to authorize account: %w", err)
	}
	// If this is a key limited to one or more buckets, one of them must exist
	// and be ours.
	if f.rootBucket != "" && len(f.info.APIs.Storage.Allowed.Buckets) != 0 {
		buckets := f.info.APIs.Storage.Allowed.Buckets
		var rootFound = false
		var rootID string
		for _, b := range buckets {
			allowedBucket := f.opt.Enc.ToStandardName(b.Name)
			if allowedBucket == "" {
				fs.Debugf(f, "bucket %q that application key is restricted to no longer exists", b.ID)
				continue
			}

			if allowedBucket == f.rootBucket {
				rootFound = true
				rootID = b.ID
			}
		}
		if !rootFound {
			return nil, fmt.Errorf("you must use bucket(s) %q with this application key", buckets)
		}
		f.cache.MarkOK(f.rootBucket)
		f.setBucketID(f.rootBucket, rootID)
	}
	if f.rootBucket != "" && f.rootDirectory != "" {
		// Check to see if the (bucket,directory) is actually an existing file
		oldRoot := f.root
		newRoot, leaf := path.Split(oldRoot)
		f.setRoot(newRoot)
		_, err := f.NewObject(ctx, leaf)
		if err != nil {
			// File doesn't exist so return old f
			f.setRoot(oldRoot)
			return f, nil
		}