Files
Atay-Makhzan/models/unittest/fscopy.go
T

108 lines
2.9 KiB
Go
Raw Normal View History

2022-04-02 00:34:57 +08:00
// Copyright 2022 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
2022-04-02 00:34:57 +08:00
package unittest
import (
"errors"
2022-04-02 00:34:57 +08:00
"os"
2024-11-14 13:28:46 -06:00
"path/filepath"
2022-04-02 00:34:57 +08:00
"strings"
"code.gitea.io/gitea/modules/setting"
2022-04-02 00:34:57 +08:00
"code.gitea.io/gitea/modules/util"
)
2025-01-13 23:35:34 -08:00
// SyncFile synchronizes the two files. This is skipped if both files
2024-11-14 13:28:46 -06:00
// exist and the size, modtime, and mode match.
2025-01-13 23:35:34 -08:00
func SyncFile(srcPath, destPath string) error {
2024-11-14 13:28:46 -06:00
dest, err := os.Stat(destPath)
2022-04-02 00:34:57 +08:00
if err != nil {
2024-11-14 13:28:46 -06:00
if os.IsNotExist(err) {
2025-01-13 23:35:34 -08:00
return util.CopyFile(srcPath, destPath)
2024-11-14 13:28:46 -06:00
}
2022-04-02 00:34:57 +08:00
return err
}
2024-11-14 13:28:46 -06:00
src, err := os.Stat(srcPath)
2022-04-02 00:34:57 +08:00
if err != nil {
return err
}
2024-11-14 13:28:46 -06:00
if src.Size() == dest.Size() &&
2025-03-29 22:32:28 +01:00
src.ModTime().Equal(dest.ModTime()) &&
2024-11-14 13:28:46 -06:00
src.Mode() == dest.Mode() {
return nil
2022-04-02 00:34:57 +08:00
}
2025-01-13 23:35:34 -08:00
return util.CopyFile(srcPath, destPath)
2022-04-02 00:34:57 +08:00
}
2024-11-14 13:28:46 -06:00
// SyncDirs synchronizes files recursively from source to target directory.
2022-04-02 00:34:57 +08:00
// It returns error when error occurs in underlying functions.
2024-11-14 13:28:46 -06:00
func SyncDirs(srcPath, destPath string) error {
destPath = filepath.Clean(destPath)
destPathAbs, err := filepath.Abs(destPath)
if err != nil {
return err
}
devDataPathAbs, err := filepath.Abs(filepath.Join(setting.GetGiteaTestSourceRoot(), "data"))
if err != nil {
return err
}
if strings.HasPrefix(destPathAbs+string(filepath.Separator), devDataPathAbs+string(filepath.Separator)) {
return errors.New("destination path should not be inside Gitea data directory, otherwise your data for dev mode will be removed")
}
err = os.MkdirAll(destPath, os.ModePerm)
2022-04-02 00:34:57 +08:00
if err != nil {
return err
}
2025-01-13 23:35:34 -08:00
// the keep file is used to keep the directory in a git repository, it doesn't need to be synced
// and go-git doesn't work with the ".keep" file (it would report errors like "ref is empty")
const keepFile = ".keep"
2024-11-14 13:28:46 -06:00
// find and delete all untracked files
2024-12-31 18:45:05 +08:00
destFiles, err := util.ListDirRecursively(destPath, &util.ListDirOptions{IncludeDir: true})
2022-04-02 00:34:57 +08:00
if err != nil {
return err
}
2024-11-14 13:28:46 -06:00
for _, destFile := range destFiles {
destFilePath := filepath.Join(destPath, destFile)
2025-01-13 23:35:34 -08:00
shouldRemove := filepath.Base(destFilePath) == keepFile
2024-11-14 13:28:46 -06:00
if _, err = os.Stat(filepath.Join(srcPath, destFile)); err != nil {
if os.IsNotExist(err) {
2025-01-13 23:35:34 -08:00
shouldRemove = true
2024-11-14 13:28:46 -06:00
} else {
return err
}
2022-04-02 00:34:57 +08:00
}
2025-01-13 23:35:34 -08:00
// if src file does not exist, remove dest file
if shouldRemove {
if err = os.RemoveAll(destFilePath); err != nil {
return err
}
}
2024-11-14 13:28:46 -06:00
}
2022-04-02 00:34:57 +08:00
2024-11-14 13:28:46 -06:00
// sync src files to dest
2024-12-31 18:45:05 +08:00
srcFiles, err := util.ListDirRecursively(srcPath, &util.ListDirOptions{IncludeDir: true})
2024-11-14 13:28:46 -06:00
if err != nil {
return err
}
for _, srcFile := range srcFiles {
destFilePath := filepath.Join(destPath, srcFile)
2024-12-31 18:45:05 +08:00
// util.ListDirRecursively appends a slash to the directory name
2024-11-14 13:28:46 -06:00
if strings.HasSuffix(srcFile, "/") {
err = os.MkdirAll(destFilePath, os.ModePerm)
2025-01-13 23:35:34 -08:00
} else if filepath.Base(destFilePath) != keepFile {
err = SyncFile(filepath.Join(srcPath, srcFile), destFilePath)
2022-04-02 00:34:57 +08:00
}
if err != nil {
return err
}
}
return nil
}