|
| 1 | +package fileutils |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + "os/user" |
| 9 | + "path/filepath" |
| 10 | + "strings" |
| 11 | +) |
| 12 | + |
| 13 | +// This implementation is based on https://github.com/nmrshll/go-cp/blob/master/cp.go |
| 14 | + |
| 15 | +func replaceHomeFolder(path string) (string, error) { |
| 16 | + if !strings.HasPrefix(path, "~") { |
| 17 | + return path, nil |
| 18 | + } |
| 19 | + var buffer bytes.Buffer |
| 20 | + usr, err := user.Current() |
| 21 | + if err != nil { |
| 22 | + return "", err |
| 23 | + } |
| 24 | + _, err = buffer.WriteString(usr.HomeDir) |
| 25 | + if err != nil { |
| 26 | + return "", err |
| 27 | + } |
| 28 | + _, err = buffer.WriteString(strings.TrimPrefix(path, "~")) |
| 29 | + if err != nil { |
| 30 | + return "", err |
| 31 | + } |
| 32 | + |
| 33 | + return buffer.String(), nil |
| 34 | +} |
| 35 | + |
| 36 | +// AbsolutePath converts a path (relative or absolute) into an absolute one. |
| 37 | +// Supports '~' notation for $HOME directory of the current user. |
| 38 | +func AbsolutePath(path string) (string, error) { |
| 39 | + homeReplaced, err := replaceHomeFolder(path) |
| 40 | + if err != nil { |
| 41 | + return "", err |
| 42 | + } |
| 43 | + return filepath.Abs(homeReplaced) |
| 44 | +} |
| 45 | + |
| 46 | +// CopyFile copies a file from src to dst. If src and dst files exist, and are |
| 47 | +// the same, then return success. Otherwise, attempt to create a hard link |
| 48 | +// between the two files. If that fails, copy the file contents from src to dst. |
| 49 | +// Creates any missing directories. Supports '~' notation for $HOME directory of the current user. |
| 50 | +func CopyFile(src, dst string) error { |
| 51 | + srcAbs, err := AbsolutePath(src) |
| 52 | + if err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + dstAbs, err := AbsolutePath(dst) |
| 56 | + if err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + |
| 60 | + // open source file |
| 61 | + sfi, err := os.Stat(srcAbs) |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + if !sfi.Mode().IsRegular() { |
| 66 | + // cannot copy non-regular files (e.g., directories, |
| 67 | + // symlinks, devices, etc.) |
| 68 | + return fmt.Errorf("CopyFile: non-regular source file %s (%q)", sfi.Name(), sfi.Mode().String()) |
| 69 | + } |
| 70 | + |
| 71 | + // open dest file |
| 72 | + dfi, err := os.Stat(dstAbs) |
| 73 | + if err != nil && !os.IsNotExist(err) { |
| 74 | + return err |
| 75 | + } |
| 76 | + |
| 77 | + if err != nil { |
| 78 | + // file doesn't exist |
| 79 | + err := os.MkdirAll(filepath.Dir(dst), 0o750) |
| 80 | + if err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + } else { |
| 84 | + if !(dfi.Mode().IsRegular()) { |
| 85 | + return fmt.Errorf("CopyFile: non-regular destination file %s (%q)", dfi.Name(), dfi.Mode().String()) |
| 86 | + } |
| 87 | + if os.SameFile(sfi, dfi) { |
| 88 | + return err |
| 89 | + } |
| 90 | + } |
| 91 | + if err = os.Link(src, dst); err == nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + return copyFileContents(src, dst) |
| 95 | +} |
| 96 | + |
| 97 | +// copyFileContents copies the contents of the file named src to the file named |
| 98 | +// by dst. The file will be created if it does not already exist. If the |
| 99 | +// destination file exists, all it's contents will be replaced by the contents |
| 100 | +// of the source file. |
| 101 | +func copyFileContents(src, dst string) error { |
| 102 | + // Open the source file for reading |
| 103 | + srcFile, err := os.Open(src) // nolint:gosec |
| 104 | + if err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + defer func() { |
| 108 | + _ = srcFile.Close() |
| 109 | + }() |
| 110 | + |
| 111 | + // Open the destination file for writing |
| 112 | + dstFile, err := os.Create(dst) // nolint:gosec |
| 113 | + if err != nil { |
| 114 | + return err |
| 115 | + } |
| 116 | + // Return any errors that result from closing the destination file |
| 117 | + // Will return nil if no errors occurred |
| 118 | + defer func() { |
| 119 | + cerr := dstFile.Close() |
| 120 | + if err == nil { |
| 121 | + err = cerr |
| 122 | + } |
| 123 | + }() |
| 124 | + |
| 125 | + // Copy the contents of the source file into the destination files |
| 126 | + const size = 1024 * 1024 |
| 127 | + buf := make([]byte, size) |
| 128 | + if _, err = io.CopyBuffer(dstFile, srcFile, buf); err != nil { |
| 129 | + return err |
| 130 | + } |
| 131 | + err = dstFile.Sync() |
| 132 | + return err |
| 133 | +} |
0 commit comments