2022-04-01 10:34:57 -06:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
2022-11-27 11:20:29 -07:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-04-01 10:34:57 -06:00
|
|
|
|
|
|
|
package unittest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2024-11-14 12:28:46 -07:00
|
|
|
"path/filepath"
|
2022-04-01 10:34:57 -06:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Copy copies file from source to target path.
|
|
|
|
func Copy(src, dest string) error {
|
|
|
|
// Gather file information to set back later.
|
|
|
|
si, err := os.Lstat(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle symbolic link.
|
|
|
|
if si.Mode()&os.ModeSymlink != 0 {
|
|
|
|
target, err := os.Readlink(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// NOTE: os.Chmod and os.Chtimes don't recognize symbolic link,
|
|
|
|
// which will lead "no such file or directory" error.
|
|
|
|
return os.Symlink(target, dest)
|
|
|
|
}
|
|
|
|
|
2024-11-14 12:28:46 -07:00
|
|
|
return util.CopyFile(src, dest)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sync synchronizes the two files. This is skipped if both files
|
|
|
|
// exist and the size, modtime, and mode match.
|
|
|
|
func Sync(srcPath, destPath string) error {
|
|
|
|
dest, err := os.Stat(destPath)
|
2022-04-01 10:34:57 -06:00
|
|
|
if err != nil {
|
2024-11-14 12:28:46 -07:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return Copy(srcPath, destPath)
|
|
|
|
}
|
2022-04-01 10:34:57 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-11-14 12:28:46 -07:00
|
|
|
src, err := os.Stat(srcPath)
|
2022-04-01 10:34:57 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-11-14 12:28:46 -07:00
|
|
|
if src.Size() == dest.Size() &&
|
|
|
|
src.ModTime() == dest.ModTime() &&
|
|
|
|
src.Mode() == dest.Mode() {
|
|
|
|
return nil
|
2022-04-01 10:34:57 -06:00
|
|
|
}
|
|
|
|
|
2024-11-14 12:28:46 -07:00
|
|
|
return Copy(srcPath, destPath)
|
2022-04-01 10:34:57 -06:00
|
|
|
}
|
|
|
|
|
2024-11-14 12:28:46 -07:00
|
|
|
// SyncDirs synchronizes files recursively from source to target directory.
|
2022-04-01 10:34:57 -06:00
|
|
|
// It returns error when error occurs in underlying functions.
|
2024-11-14 12:28:46 -07:00
|
|
|
func SyncDirs(srcPath, destPath string) error {
|
2022-04-01 10:34:57 -06:00
|
|
|
err := os.MkdirAll(destPath, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-11-14 12:28:46 -07:00
|
|
|
// find and delete all untracked files
|
|
|
|
destFiles, err := util.StatDir(destPath, true)
|
2022-04-01 10:34:57 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-11-14 12:28:46 -07:00
|
|
|
for _, destFile := range destFiles {
|
|
|
|
destFilePath := filepath.Join(destPath, destFile)
|
|
|
|
if _, err = os.Stat(filepath.Join(srcPath, destFile)); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// if src file does not exist, remove dest file
|
|
|
|
if err = os.RemoveAll(destFilePath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-01 10:34:57 -06:00
|
|
|
}
|
2024-11-14 12:28:46 -07:00
|
|
|
}
|
2022-04-01 10:34:57 -06:00
|
|
|
|
2024-11-14 12:28:46 -07:00
|
|
|
// sync src files to dest
|
|
|
|
srcFiles, err := util.StatDir(srcPath, true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, srcFile := range srcFiles {
|
|
|
|
destFilePath := filepath.Join(destPath, srcFile)
|
|
|
|
// util.StatDir appends a slash to the directory name
|
|
|
|
if strings.HasSuffix(srcFile, "/") {
|
|
|
|
err = os.MkdirAll(destFilePath, os.ModePerm)
|
2022-04-01 10:34:57 -06:00
|
|
|
} else {
|
2024-11-14 12:28:46 -07:00
|
|
|
err = Sync(filepath.Join(srcPath, srcFile), destFilePath)
|
2022-04-01 10:34:57 -06:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|