MCPcopy
hub / github.com/kopia/kopia / tryToConvertPathToID

Method tryToConvertPathToID

cli/command_restore.go:466–523  ·  view source on GitHub ↗

tryToConvertPathToID checks if the source is a path and in this case returns the ID of the snapshot containing the latest version available.

(ctx context.Context, rep repo.Repository, source string)

Source from the content-addressed store, hash-verified

464// tryToConvertPathToID checks if the source is a path and in this case returns the ID of the snapshot
465// containing the latest version available.
466func (c *commandRestore) tryToConvertPathToID(ctx context.Context, rep repo.Repository, source string) (string, error) {
467 pathElements := strings.Split(filepath.ToSlash(source), "/")
468
469 if pathElements[0] != "" {
470 _, err := object.ParseID(pathElements[0])
471 if err == nil {
472 // source is an ID
473 return source, nil
474 }
475 }
476
477 // Consider source as a path
478
479 if c.snapshotTime == "" {
480 return "", errors.New("a snapshot time is needed to use a path as source")
481 }
482
483 filter, err := createSnapshotTimeFilter(c.snapshotTime)
484 if err != nil {
485 return "", err
486 }
487
488 si, err := snapshot.ParseSourceInfo(source, rep.ClientOptions().Hostname, rep.ClientOptions().Username)
489 if err != nil {
490 return "", errors.Errorf("invalid directory: '%s': %s", source, err)
491 }
492
493 if si.Path == "" {
494 return "", errors.New("the source must contain a path element")
495 }
496
497 manifestIDs, err := findSnapshotsForSource(ctx, rep, si, map[string]string{})
498 if err != nil {
499 return "", err
500 }
501
502 if len(manifestIDs) == 0 {
503 return "", errors.Errorf("no snapshots contain data for %v", source)
504 }
505
506 ms, err := snapshot.LoadSnapshots(ctx, rep, manifestIDs)
507 if err != nil {
508 return "", errors.Wrap(err, "unable to load snapshots")
509 }
510
511 m, relPath, ohid := findLastManifestWithPath(ctx, rep, ms, si.Path, filter)
512 if m == nil {
513 return "", errors.Errorf("no snapshots contain data for %v", source)
514 }
515
516 log(ctx).Infof("Restoring from\n"+
517 " Snapshot source: %v\n"+
518 " Snapshot time: %v\n"+
519 " Relative path: %v\n"+
520 " Object ID: %v", m.Source, formatTimestamp(m.StartTime.ToTime()), relPath, ohid)
521
522 return ohid.String(), nil
523}

Callers 1

runMethod · 0.95

Calls 11

ParseIDFunction · 0.92
ParseSourceInfoFunction · 0.92
LoadSnapshotsFunction · 0.92
createSnapshotTimeFilterFunction · 0.85
findSnapshotsForSourceFunction · 0.85
findLastManifestWithPathFunction · 0.85
formatTimestampFunction · 0.85
ErrorfMethod · 0.80
ToTimeMethod · 0.80
ClientOptionsMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected