summaryrefslogtreecommitdiff
path: root/util/io.go
blob: f8290aec40f17d20c6a653424cdc88bcb358e106 (plain)
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
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package util

import (
	"fmt"
	"io"
	"os"
	"path/filepath"
	"strings"
)

// CopyFile uses io.Copy() to copy a file to another location
// This was copied from https://opensource.com/article/18/6/copying-files-go
func CopyFile(src, dst string) (int64, error) {
	sourceFileStat, err := os.Stat(src)
	if err != nil {
		return 0, err
	}

	if !sourceFileStat.Mode().IsRegular() {
		return 0, fmt.Errorf("%s is not a regular file", src)
	}

	source, err := os.Open(src)
	if err != nil {
		return 0, err
	}
	defer source.Close()

	destination, err := os.Create(dst)
	if err != nil {
		return 0, err
	}
	defer destination.Close()
	nBytes, err := io.Copy(destination, source)
	return nBytes, err
}

// FileExists checks to see if the specified file (or directory) exists
func FileExists(filePath string) bool {
	_, err := os.Stat(filePath)
	fileExists := err == nil
	return fileExists
}

// IsEmpty recursively check path for files and returns true if there are none.
func IsEmpty(path string) bool {
	err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
		if info.IsDir() {
			return nil
		}
		return os.ErrExist
	})
	return err == nil
}

// ExeDir returns the absolute path to the current executing binary (not including the filename)
func ExeDir() (string, error) {
	ex, err := os.Executable()
	if err != nil {
		return "", err
	}
	baseDir := filepath.Dir(ex)
	binDir, err := filepath.Abs(baseDir)
	return binDir, err
}

// GetFirstLineFromFile retrieves the first line of the specified file.
func GetFirstLineFromFile(netFile string) (string, error) {
	addrStr, err := os.ReadFile(netFile)
	if err != nil {
		return "", err
	}
	// We only want the first line, so split at newlines and take the first
	lines := strings.Split(string(addrStr), "\n")
	return lines[0], err
}

// IsDir returns true if the specified directory is valid
func IsDir(path string) bool {
	fi, err := os.Stat(path)
	return err == nil && fi.IsDir()
}

// IncludeFilter is a callback for filtering files and folders encountered while copying with CopyFileWithFilter()
type IncludeFilter func(name string, info os.FileInfo) bool

// CopyFolderWithFilter recursively copies an entire directory to another location (ignoring symlinks)
// with an optional filter function to include/exclude folders or files
func CopyFolderWithFilter(source, dest string, includeFilter IncludeFilter) (err error) {
	if !IsDir(source) {
		return fmt.Errorf("%s is not a folder - unable to copy", source)
	}

	info, err := os.Stat(source)
	if err != nil {
		return fmt.Errorf("error getting status for '%s': %v", source, err)
	}

	if info.Mode()&os.ModeSymlink != 0 {
		return fmt.Errorf("symlink are not supported")
	}

	return copyFolder(source, dest, info, includeFilter)
}

// CopyFolder recursively copies an entire directory to another location (ignoring symlinks)
func CopyFolder(source, dest string) error {
	return CopyFolderWithFilter(source, dest, nil)
}

func copyFolder(source string, dest string, info os.FileInfo, includeFilter IncludeFilter) (err error) {
	if err := os.MkdirAll(dest, info.Mode()); err != nil {
		return fmt.Errorf("error creating destination folder: %v", err)
	}

	contents, err := os.ReadDir(source)
	if err != nil {
		return err
	}

	for _, content := range contents {
		name := content.Name()
		sourceName := filepath.Join(source, name)
		sourceInfo, statErr := os.Stat(sourceName)
		if statErr != nil {
			return statErr
		}

		// Skip symlinks
		if sourceInfo.Mode()&os.ModeSymlink != 0 {
			continue
		}

		// If filter provided, see if it wants to include
		if includeFilter != nil && !includeFilter(sourceName, sourceInfo) {
			continue
		}

		destName := filepath.Join(dest, name)
		if sourceInfo.IsDir() {
			err = copyFolder(sourceName, destName, sourceInfo, includeFilter)
		} else {
			_, err = CopyFile(sourceName, destName)
		}
		if err != nil {
			return
		}
	}

	return
}