MCPcopy Index your code
hub / github.com/wavetermdev/waveterm / prepareDestForCopy

Function prepareDestForCopy

pkg/wshrpc/wshremote/wshremote_file.go:40–71  ·  view source on GitHub ↗

prepareDestForCopy resolves the final destination path and handles overwrite logic. destPath is the raw destination path (may be a directory or file path). srcBaseName is the basename of the source file (used when dest is a directory or ends with slash). destHasSlash indicates if the original URI en

(destPath string, srcBaseName string, destHasSlash bool, overwrite bool)

Source from the content-addressed store, hash-verified

38// destHasSlash indicates if the original URI ended with a slash (forcing directory interpretation).
39// Returns the resolved path ready for writing.
40func prepareDestForCopy(destPath string, srcBaseName string, destHasSlash bool, overwrite bool) (string, error) {
41 destInfo, err := os.Stat(destPath)
42 if err != nil && !errors.Is(err, fs.ErrNotExist) {
43 return "", fmt.Errorf("cannot stat destination %q: %w", destPath, err)
44 }
45
46 destExists := destInfo != nil
47 destIsDir := destExists && destInfo.IsDir()
48
49 var finalPath string
50 if destHasSlash || destIsDir {
51 finalPath = filepath.Join(destPath, srcBaseName)
52 } else {
53 finalPath = destPath
54 }
55
56 finalInfo, err := os.Stat(finalPath)
57 if err != nil && !errors.Is(err, fs.ErrNotExist) {
58 return "", fmt.Errorf("cannot stat file %q: %w", finalPath, err)
59 }
60
61 if finalInfo != nil {
62 if !overwrite {
63 return "", fmt.Errorf(wshfs.OverwriteRequiredError, finalPath)
64 }
65 if err := os.Remove(finalPath); err != nil {
66 return "", fmt.Errorf("cannot remove file %q: %w", finalPath, err)
67 }
68 }
69
70 return finalPath, nil
71}
72
73// remoteCopyFileInternal copies FROM local (this host) TO local (this host)
74// Only supports copying files, not directories

Callers 2

remoteCopyFileInternalFunction · 0.85
RemoteFileCopyCommandMethod · 0.85

Calls 2

StatMethod · 0.80
IsDirMethod · 0.45

Tested by

no test coverage detected