1 // Utility functions useful for file-related tests
3 // Copyright (C) 2021 Simon Ruderich
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.
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.
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/>.
35 func WalkDir(basePath string) ([]File, error) {
37 err := filepath.WalkDir(basePath, func(path string,
38 d fs.DirEntry, err error) error {
47 rel, err := filepath.Rel(basePath, path)
56 if f.Mode.Type() == 0 {
57 x, err := os.ReadFile(path)
62 } else if f.Mode.Type() == fs.ModeSymlink {
63 x, err := os.Readlink(path)
68 f.Mode |= 0777 // see sync/files.go
79 func CurrentUserAndGroup() (string, int, string, int) {
80 u, err := user.Current()
84 g, err := user.LookupGroupId(u.Gid)
88 uid, err := strconv.Atoi(u.Uid)
92 gid, err := strconv.Atoi(g.Gid)
96 return u.Username, uid, g.Name, gid
99 func CreateFile(path string, data string, mode fs.FileMode) {
100 err := os.WriteFile(path, []byte(data), 0600)
104 err = os.Chmod(path, mode)
110 func CreateSymlink(path string, data string) {
111 err := os.Symlink(data, path)
117 func CreateDirectory(path string, mode fs.FileMode) {
118 err := os.Mkdir(path, 0700)
122 err = os.Chmod(path, mode)
128 func CreateDirectoryExists(path string, mode fs.FileMode) {
129 err := os.Mkdir(path, 0700)
130 if err != nil && !os.IsExist(err) {
133 err = os.Chmod(path, mode)
139 func CreateFifo(path string, mode fs.FileMode) {
140 err := syscall.Mkfifo(path, 0600)
144 err = os.Chmod(path, mode)