Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
92 lines (73 loc) · 2.2 KB
/
Copy pathmain.go
File metadata and controls
92 lines (73 loc) · 2.2 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
package main
import (
"fmt"
"io"
"os"
"path/filepath"
)
func flattenDir(srcDir, dstDir string) error {
// 检查源目录是否存在
if _, err := os.Stat(srcDir); os.IsNotExist(err) {
return fmt.Errorf("source directory does not exist")
}
// 创建目标目录(如果不存在)
if err := os.MkdirAll(dstDir, 0755); err != nil {
return fmt.Errorf("failed to create destination directory: %v", err)
}
// 遍历源目录
err := filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// 跳过目录
if info.IsDir() {
return nil
}
fmt.Printf("Processing file: %s\n", path)
// 构建目标文件路径
fileName := filepath.Base(path)
dstPath := filepath.Join(dstDir, fileName)
// 复制文件
srcFile, err := os.Open(path)
if err != nil {
return fmt.Errorf("failed to open source file: %v", err)
}
defer srcFile.Close()
dstFile, err := os.Create(dstPath)
if err != nil {
return fmt.Errorf("failed to create destination file: %v", err)
}
defer dstFile.Close()
_, err = io.Copy(dstFile, srcFile)
if err != nil {
return fmt.Errorf("failed to copy file: %v", err)
}
// 复制文件权限和时间戳
srcInfo, err := os.Stat(path)
if err != nil {
return fmt.Errorf("failed to get source file info: %v", err)
}
err = os.Chmod(dstPath, srcInfo.Mode())
if err != nil {
return fmt.Errorf("failed to set file permissions: %v", err)
}
return nil
})
if err != nil {
return fmt.Errorf("error walking through directory: %v", err)
}
return nil
}
func main() {
if len(os.Args) < 3 {
fmt.Println("Usage: flattendir src_dir dst_dir")
os.Exit(1)
}
srcDir := os.Args[1]
dstDir := os.Args[2]
if err := flattenDir(srcDir, dstDir); err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
fmt.Println("Flatten directory successfully.")
}