ALR

Форк
1
/
move.go 
110 строк · 2.6 Кб
1
/*
2
 * ALR - Any Linux Repository
3
 * Copyright (C) 2024 Евгений Храмов
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18

19
package osutils
20

21
import (
22
	"io"
23
	"os"
24
	"path/filepath"
25
)
26

27
// Move attempts to use os.Rename and if that fails (such as for a cross-device move),
28
// it instead copies the source to the destination and then removes the source.
29
func Move(sourcePath, destPath string) error {
30
	// Try to rename the source to the destination
31
	err := os.Rename(sourcePath, destPath)
32
	if err == nil {
33
		return nil // Successful move
34
	}
35

36
	// Rename failed, so copy the source to the destination
37
	err = copyDirOrFile(sourcePath, destPath)
38
	if err != nil {
39
		return err
40
	}
41

42
	// Copy successful, remove the original source
43
	err = os.RemoveAll(sourcePath)
44
	if err != nil {
45
		return err
46
	}
47

48
	return nil
49
}
50

51
func copyDirOrFile(sourcePath, destPath string) error {
52
	sourceInfo, err := os.Stat(sourcePath)
53
	if err != nil {
54
		return err
55
	}
56

57
	if sourceInfo.IsDir() {
58
		return copyDir(sourcePath, destPath, sourceInfo)
59
	} else if sourceInfo.Mode().IsRegular() {
60
		return copyFile(sourcePath, destPath, sourceInfo)
61
	} else {
62
		// ignore non-regular files
63
		return nil
64
	}
65
}
66

67
func copyDir(sourcePath, destPath string, sourceInfo os.FileInfo) error {
68
	err := os.MkdirAll(destPath, sourceInfo.Mode())
69
	if err != nil {
70
		return err
71
	}
72

73
	entries, err := os.ReadDir(sourcePath)
74
	if err != nil {
75
		return err
76
	}
77

78
	for _, entry := range entries {
79
		sourceEntry := filepath.Join(sourcePath, entry.Name())
80
		destEntry := filepath.Join(destPath, entry.Name())
81

82
		err = copyDirOrFile(sourceEntry, destEntry)
83
		if err != nil {
84
			return err
85
		}
86
	}
87

88
	return nil
89
}
90

91
func copyFile(sourcePath, destPath string, sourceInfo os.FileInfo) error {
92
	sourceFile, err := os.Open(sourcePath)
93
	if err != nil {
94
		return err
95
	}
96
	defer sourceFile.Close()
97

98
	destFile, err := os.OpenFile(destPath, os.O_CREATE|os.O_TRUNC|os.O_RDWR, sourceInfo.Mode())
99
	if err != nil {
100
		return err
101
	}
102
	defer destFile.Close()
103

104
	_, err = io.Copy(destFile, sourceFile)
105
	if err != nil {
106
		return err
107
	}
108

109
	return nil
110
}
111

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.