|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + |
| 10 | + "github.com/gogf/gf/v2/util/gconv" |
| 11 | +) |
| 12 | + |
| 13 | +func CopyFile(src, dst string) (written int64, err error) { |
| 14 | + srcFile, err := os.Open(src) |
| 15 | + |
| 16 | + if err != nil { |
| 17 | + return 0, err |
| 18 | + } |
| 19 | + defer srcFile.Close() |
| 20 | + |
| 21 | + dstFile, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE, os.ModePerm) |
| 22 | + if err != nil { |
| 23 | + return 0, fmt.Errorf("open dst file failed: %s", err.Error()) |
| 24 | + } |
| 25 | + defer dstFile.Close() |
| 26 | + |
| 27 | + return io.Copy(dstFile, srcFile) |
| 28 | +} |
| 29 | + |
| 30 | +// CopyAFile copies the file at the source path to the provided destination. |
| 31 | +func CopyAFile(source, destination string) error { |
| 32 | + //Validate the source and destination paths |
| 33 | + if len(source) == 0 { |
| 34 | + return errors.New("no source file path provided") |
| 35 | + } |
| 36 | + |
| 37 | + if len(destination) == 0 { |
| 38 | + return errors.New("no destination file path provided") |
| 39 | + } |
| 40 | + |
| 41 | + //Verify the source path refers to a regular file |
| 42 | + sourceFileInfo, err := os.Lstat(source) |
| 43 | + if err != nil { |
| 44 | + return err |
| 45 | + } |
| 46 | + |
| 47 | + //Handle regular files differently than symbolic links and other non-regular files. |
| 48 | + if sourceFileInfo.Mode().IsRegular() { |
| 49 | + //open the source file |
| 50 | + sourceFile, err := os.Open(source) |
| 51 | + if err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + defer sourceFile.Close() |
| 55 | + |
| 56 | + //create the destinatin file |
| 57 | + destinationFile, err := os.Create(destination) |
| 58 | + if err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + defer destinationFile.Close() |
| 62 | + |
| 63 | + //copy the source file contents to the destination file |
| 64 | + if _, err = io.Copy(destinationFile, sourceFile); err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + |
| 68 | + //replicate the source file mode for the destination file |
| 69 | + if err := os.Chmod(destination, sourceFileInfo.Mode()); err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + } else if sourceFileInfo.Mode()&os.ModeSymlink != 0 { |
| 73 | + linkDestinaton, err := os.Readlink(source) |
| 74 | + if err != nil { |
| 75 | + return errors.New("Unable to read symlink. " + err.Error()) |
| 76 | + } |
| 77 | + |
| 78 | + if err := os.Symlink(linkDestinaton, destination); err != nil { |
| 79 | + return errors.New("Unable to replicate symlink. " + err.Error()) |
| 80 | + } |
| 81 | + } else { |
| 82 | + return errors.New("Unable to use io.Copy on file with mode " + gconv.String(sourceFileInfo.Mode())) |
| 83 | + } |
| 84 | + |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +// CopyDirectory copies the directory at the source path to the provided destination, recursively copying subdirectories. |
| 89 | +func CopyDirectory(source string, destination string) error { |
| 90 | + if len(source) == 0 || len(destination) == 0 { |
| 91 | + return errors.New("file paths must not be empty") |
| 92 | + } |
| 93 | + |
| 94 | + //get properties of the source directory |
| 95 | + sourceInfo, err := os.Stat(source) |
| 96 | + if err != nil { |
| 97 | + return err |
| 98 | + } |
| 99 | + |
| 100 | + //create the destination directory |
| 101 | + err = os.MkdirAll(destination, sourceInfo.Mode()) |
| 102 | + if err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + |
| 106 | + sourceDirectory, err := os.Open(source) |
| 107 | + if err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + defer sourceDirectory.Close() |
| 111 | + |
| 112 | + objects, err := sourceDirectory.Readdir(-1) |
| 113 | + if err != nil { |
| 114 | + return err |
| 115 | + } |
| 116 | + |
| 117 | + for _, object := range objects { |
| 118 | + if object.Name() == ".Trashes" || object.Name() == ".DS_Store" { |
| 119 | + continue |
| 120 | + } |
| 121 | + |
| 122 | + sourceObjectName := source + string(filepath.Separator) + object.Name() |
| 123 | + destObjectName := destination + string(filepath.Separator) + object.Name() |
| 124 | + |
| 125 | + if object.IsDir() { |
| 126 | + //create sub-directories |
| 127 | + err = CopyDirectory(sourceObjectName, destObjectName) |
| 128 | + if err != nil { |
| 129 | + return err |
| 130 | + } |
| 131 | + } else { |
| 132 | + err = CopyAFile(sourceObjectName, destObjectName) |
| 133 | + if err != nil { |
| 134 | + return err |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + return nil |
| 140 | +} |
0 commit comments