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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
package util
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"runtime"
internalIO "github.com/lxc/incus/v6/internal/io"
"github.com/lxc/incus/v6/shared/util"
)
// FileMove tries to move a file by using os.Rename,
// if that fails it tries to copy the file and remove the source.
func FileMove(oldPath string, newPath string) error {
err := os.Rename(oldPath, newPath)
if err == nil {
return nil
}
err = FileCopy(oldPath, newPath)
if err != nil {
return err
}
_ = os.Remove(oldPath)
return nil
}
// FileCopy copies a file, overwriting the target if it exists.
func FileCopy(source string, dest string) error {
fi, err := os.Lstat(source)
if err != nil {
return err
}
_, uid, gid := internalIO.GetOwnerMode(fi)
if fi.Mode()&os.ModeSymlink != 0 {
target, err := os.Readlink(source)
if err != nil {
return err
}
if util.PathExists(dest) {
err = os.Remove(dest)
if err != nil {
return err
}
}
err = os.Symlink(target, dest)
if err != nil {
return err
}
if runtime.GOOS != "windows" {
return os.Lchown(dest, uid, gid)
}
return nil
}
s, err := os.Open(source)
if err != nil {
return err
}
defer func() { _ = s.Close() }()
d, err := os.Create(dest)
if err != nil {
if !os.IsExist(err) {
return err
}
d, err = os.OpenFile(dest, os.O_WRONLY, fi.Mode())
if err != nil {
return err
}
}
_, err = io.Copy(d, s)
if err != nil {
return err
}
/* chown not supported on windows */
if runtime.GOOS != "windows" {
err = d.Chown(uid, gid)
if err != nil {
return err
}
}
return d.Close()
}
// DirCopy copies a directory recursively, overwriting the target if it exists.
func DirCopy(source string, dest string) error {
// Get info about source.
info, err := os.Stat(source)
if err != nil {
return fmt.Errorf("failed to get source directory info: %w", err)
}
if !info.IsDir() {
return errors.New("source is not a directory")
}
// Remove dest if it already exists.
if util.PathExists(dest) {
err := os.RemoveAll(dest)
if err != nil {
return fmt.Errorf("failed to remove destination directory %s: %w", dest, err)
}
}
// Create dest.
err = os.MkdirAll(dest, info.Mode())
if err != nil {
return fmt.Errorf("failed to create destination directory %s: %w", dest, err)
}
// Copy all files.
entries, err := os.ReadDir(source)
if err != nil {
return fmt.Errorf("failed to read source directory %s: %w", source, err)
}
for _, entry := range entries {
sourcePath := filepath.Join(source, entry.Name())
destPath := filepath.Join(dest, entry.Name())
if entry.IsDir() {
err := DirCopy(sourcePath, destPath)
if err != nil {
return fmt.Errorf("failed to copy sub-directory from %s to %s: %w", sourcePath, destPath, err)
}
} else {
err := FileCopy(sourcePath, destPath)
if err != nil {
return fmt.Errorf("failed to copy file from %s to %s: %w", sourcePath, destPath, err)
}
}
}
return nil
}
// AddSlash adds a slash to the end of paths if they don't already have one.
// This can be useful for rsyncing things, since rsync has behavior present on
// the presence or absence of a trailing slash.
func AddSlash(path string) string {
if path[len(path)-1] != '/' {
return path + "/"
}
return path
}
// PathIsEmpty checks if the given path is empty.
func PathIsEmpty(path string) (bool, error) {
f, err := os.Open(path)
if err != nil {
return false, err
}
defer func() { _ = f.Close() }()
// read in ONLY one file
_, err = f.ReadDir(1)
// and if the file is EOF... well, the dir is empty.
if err == io.EOF {
return true, nil
}
return false, err
}
func MkdirAllOwner(path string, perm os.FileMode, uid int, gid int) error {
// This function is a slightly modified version of MkdirAll from the Go standard library.
// https://golang.org/src/os/path.go?s=488:535#L9
// Fast path: if we can tell whether path is a directory or file, stop with success or error.
dir, err := os.Stat(path)
if err == nil {
if dir.IsDir() {
return nil
}
return errors.New("path exists but isn't a directory")
}
// Slow path: make sure parent exists and then call Mkdir for path.
i := len(path)
for i > 0 && os.IsPathSeparator(path[i-1]) { // Skip trailing path separator.
i--
}
j := i
for j > 0 && !os.IsPathSeparator(path[j-1]) { // Scan backward over element.
j--
}
if j > 1 {
// Create parent
err = MkdirAllOwner(path[0:j-1], perm, uid, gid)
if err != nil {
return err
}
}
// Parent now exists; invoke Mkdir and use its result.
err = os.Mkdir(path, perm)
err_chown := os.Chown(path, uid, gid)
if err_chown != nil {
return err_chown
}
if err != nil {
// Handle arguments like "foo/." by
// double-checking that directory doesn't exist.
dir, err1 := os.Lstat(path)
if err1 == nil && dir.IsDir() {
return nil
}
return err
}
return nil
}
|