MCPcopy
hub / github.com/kopia/kopia / handleSourcesCreate

Function handleSourcesCreate

internal/server/api_sources.go:41–93  ·  view source on GitHub ↗
(ctx context.Context, rc requestContext)

Source from the content-addressed store, hash-verified

39}
40
41func handleSourcesCreate(ctx context.Context, rc requestContext) (any, *apiError) {
42 var req serverapi.CreateSnapshotSourceRequest
43
44 if err := json.Unmarshal(rc.body, &req); err != nil {
45 return nil, requestError(serverapi.ErrorMalformedRequest, "malformed request body")
46 }
47
48 if req.Path == "" {
49 return nil, requestError(serverapi.ErrorMalformedRequest, "missing path")
50 }
51
52 if req.Policy == nil {
53 return nil, requestError(serverapi.ErrorMalformedRequest, "missing policy")
54 }
55
56 req.Path = ospath.ResolveUserFriendlyPath(req.Path, true)
57
58 _, err := os.Stat(req.Path)
59 if os.IsNotExist(err) {
60 return nil, requestError(serverapi.ErrorPathNotFound, "path does not exist")
61 }
62
63 if err != nil {
64 return nil, internalServerError(err)
65 }
66
67 sourceInfo := snapshot.SourceInfo{
68 UserName: rc.rep.ClientOptions().Username,
69 Host: rc.rep.ClientOptions().Hostname,
70 Path: req.Path,
71 }
72
73 resp := &serverapi.CreateSnapshotSourceResponse{}
74
75 if err = repo.WriteSession(ctx, rc.rep, repo.WriteSessionOptions{
76 Purpose: "handleSourcesCreate",
77 }, func(ctx context.Context, w repo.RepositoryWriter) error {
78 return policy.SetPolicy(ctx, w, sourceInfo, req.Policy)
79 }); err != nil {
80 return nil, internalServerError(errors.Wrap(err, "unable to set initial policy"))
81 }
82
83 manager := rc.srv.getOrCreateSourceManager(ctx, sourceInfo)
84
85 if req.CreateSnapshot {
86 resp.SnapshotStarted = true
87
88 userLog(ctx).Debugf("scheduling snapshot of %v immediately...", sourceInfo)
89 manager.scheduleSnapshotNow()
90 }
91
92 return resp, nil
93}

Callers

nothing calls this directly

Calls 10

ResolveUserFriendlyPathFunction · 0.92
WriteSessionFunction · 0.92
SetPolicyFunction · 0.92
requestErrorFunction · 0.85
internalServerErrorFunction · 0.85
scheduleSnapshotNowMethod · 0.80
StatMethod · 0.65
IsNotExistMethod · 0.65
ClientOptionsMethod · 0.65

Tested by

no test coverage detected