|
1 |
| -// Copyright 2013 com authors |
2 |
| -// |
3 |
| -// Licensed under the Apache License, Version 2.0 (the "License"): you may |
4 |
| -// not use this file except in compliance with the License. You may obtain |
5 |
| -// a copy of the License at |
6 |
| -// |
7 |
| -// http://www.apache.org/licenses/LICENSE-2.0 |
8 |
| -// |
9 |
| -// Unless required by applicable law or agreed to in writing, software |
10 |
| -// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
11 |
| -// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
12 |
| -// License for the specific language governing permissions and limitations |
13 |
| -// under the License. |
14 |
| -// |
15 | 1 | // Copyright 2020 The Gitea Authors. All rights reserved.
|
16 | 2 | // Use of this source code is governed by a MIT-style
|
17 | 3 | // license that can be found in the LICENSE file.
|
18 | 4 |
|
19 | 5 | package util
|
20 | 6 |
|
21 | 7 | import (
|
22 |
| - "fmt" |
23 |
| - "io" |
24 |
| - "os" |
25 |
| - "path" |
26 |
| - "strings" |
| 8 | + "github.com/unknwon/com" |
27 | 9 | )
|
28 | 10 |
|
29 | 11 | // CopyFile copies file from source to target path.
|
30 | 12 | func CopyFile(src, dest string) error {
|
31 |
| - // Gather file information to set back later. |
32 |
| - si, err := os.Lstat(src) |
33 |
| - if err != nil { |
34 |
| - return err |
35 |
| - } |
36 |
| - |
37 |
| - // Handle symbolic link. |
38 |
| - if si.Mode()&os.ModeSymlink != 0 { |
39 |
| - target, err := os.Readlink(src) |
40 |
| - if err != nil { |
41 |
| - return err |
42 |
| - } |
43 |
| - // NOTE: os.Chmod and os.Chtimes don't recoganize symbolic link, |
44 |
| - // which will lead "no such file or directory" error. |
45 |
| - return os.Symlink(target, dest) |
46 |
| - } |
47 |
| - |
48 |
| - sr, err := os.Open(src) |
49 |
| - if err != nil { |
50 |
| - return err |
51 |
| - } |
52 |
| - defer sr.Close() |
53 |
| - |
54 |
| - dw, err := os.Create(dest) |
55 |
| - if err != nil { |
56 |
| - return err |
57 |
| - } |
58 |
| - defer dw.Close() |
59 |
| - |
60 |
| - if _, err = io.Copy(dw, sr); err != nil { |
61 |
| - return err |
62 |
| - } |
63 |
| - |
64 |
| - // Set back file information. |
65 |
| - if err = os.Chtimes(dest, si.ModTime(), si.ModTime()); err != nil { |
66 |
| - return err |
67 |
| - } |
68 |
| - return os.Chmod(dest, si.Mode()) |
| 13 | + return com.Copy(src, dest) |
69 | 14 | }
|
70 | 15 |
|
71 | 16 | // CopyDir copy files recursively from source to target directory.
|
72 |
| -// |
73 |
| -// The filter accepts a function that process the path info. |
74 |
| -// and should return true for need to filter. |
75 |
| -// |
76 | 17 | // It returns error when error occurs in underlying functions.
|
77 |
| -func CopyDir(srcPath, destPath string, filters ...func(filePath string) bool) error { |
78 |
| - // Check if target directory exists. |
79 |
| - targetExist, err := IsExist(destPath) |
80 |
| - if err != nil { |
81 |
| - return err |
82 |
| - } |
83 |
| - if targetExist { |
84 |
| - return fmt.Errorf("file or directory alreay exists: '%s'", destPath) |
85 |
| - } |
86 |
| - |
87 |
| - err = os.MkdirAll(destPath, os.ModePerm) |
88 |
| - if err != nil { |
89 |
| - return err |
90 |
| - } |
91 |
| - |
92 |
| - // Gather directory info. |
93 |
| - infos, err := StatDir(srcPath, true) |
94 |
| - if err != nil { |
95 |
| - return err |
96 |
| - } |
97 |
| - |
98 |
| - var filter func(filePath string) bool |
99 |
| - if len(filters) > 0 { |
100 |
| - filter = filters[0] |
101 |
| - } |
102 |
| - |
103 |
| - for _, info := range infos { |
104 |
| - if filter != nil && filter(info) { |
105 |
| - continue |
106 |
| - } |
107 |
| - |
108 |
| - curPath := path.Join(destPath, info) |
109 |
| - if strings.HasSuffix(info, "/") { |
110 |
| - err = os.MkdirAll(curPath, os.ModePerm) |
111 |
| - } else { |
112 |
| - err = CopyFile(path.Join(srcPath, info), curPath) |
113 |
| - } |
114 |
| - if err != nil { |
115 |
| - return err |
116 |
| - } |
117 |
| - } |
118 |
| - return nil |
| 18 | +func CopyDir(srcPath, destPath string) error { |
| 19 | + return com.CopyDir(srcPath, destPath) |
119 | 20 | }
|
0 commit comments