This repository was archived by the owner on May 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfs.go
More file actions
103 lines (87 loc) · 2.55 KB
/
Copy pathfs.go
File metadata and controls
103 lines (87 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
)
func copyFiles(currentPath string, targetPath string) error {
// When the currentPath folder does not exist, return
if _, err := os.Stat(currentPath); os.IsNotExist(err) {
return nil
}
// Create target directory if it doesn't exist
if err := os.MkdirAll(targetPath, 0755); err != nil {
return fmt.Errorf("failed to create target directory: %w", err)
}
// Walk through the current directory
return filepath.Walk(currentPath, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return fmt.Errorf("failed to access path %q: %w", path, err)
}
// Get the relative path
relPath, err := filepath.Rel(currentPath, path)
if err != nil {
return fmt.Errorf("failed to get relative path for %q: %w", path, err)
}
// Skip development environment and VCS metadata folders
// (e.g., .devenv, .direnv, .git)
if info.IsDir() && (relPath == ".devenv" || relPath == ".direnv" || relPath == ".git") {
return filepath.SkipDir
}
// Construct target path
targetFilePath := filepath.Join(targetPath, relPath)
// If it's a directory, create it in target
if info.IsDir() {
return os.MkdirAll(targetFilePath, 0755)
}
// Copy the file
return copyFile(path, targetFilePath)
})
}
func copyFile(src, dst string) error {
// Check if it's a symlink
info, err := os.Lstat(src)
if err != nil {
return fmt.Errorf("failed to get file info: %w", err)
}
// If it's a symlink, create a new symlink
if info.Mode()&os.ModeSymlink != 0 {
linkTarget, err := os.Readlink(src)
if err != nil {
return fmt.Errorf("failed to read symlink: %w", err)
}
return os.Symlink(linkTarget, dst)
}
// Open source file
sourceFile, err := os.Open(src)
if err != nil {
return fmt.Errorf("failed to open source file: %w", err)
}
defer func() {
if closeErr := sourceFile.Close(); closeErr != nil {
err = fmt.Errorf("failed to close source file: %w", closeErr)
}
}()
// Create target file
targetFile, err := os.Create(dst)
if err != nil {
return fmt.Errorf("failed to create target file: %w", err)
}
defer func() {
if closeErr := targetFile.Close(); closeErr != nil {
err = fmt.Errorf("failed to close target file: %w", closeErr)
}
}()
// Copy the contents
if _, err := io.Copy(targetFile, sourceFile); err != nil {
return fmt.Errorf("failed to copy file contents: %w", err)
}
// Copy file permissions
sourceInfo, err := os.Stat(src)
if err != nil {
return fmt.Errorf("failed to get source file info: %w", err)
}
return os.Chmod(dst, sourceInfo.Mode())
}