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, d fs.DirEntry, err error) error {
45 rel, err := filepath.Rel(basePath, path)
54 if f.Mode.Type() == 0 {
55 x, err := os.ReadFile(path)
60 } else if f.Mode.Type() == fs.ModeSymlink {
61 x, err := os.Readlink(path)
66 f.Mode |= 0777 // see sync/files.go
77 func CurrentUserAndGroup() (string, int, string, int) {
78 u, err := user.Current()
82 g, err := user.LookupGroupId(u.Gid)
86 uid, err := strconv.Atoi(u.Uid)
90 gid, err := strconv.Atoi(g.Gid)
94 return u.Username, uid, g.Name, gid
97 func CreateFile(path string, data string, mode fs.FileMode) {
98 err := os.WriteFile(path, []byte(data), 0644)
102 err = os.Chmod(path, mode)
108 func CreateSymlink(path string, data string) {
109 err := os.Symlink(data, path)
115 func CreateDirectory(path string, mode fs.FileMode) {
116 err := os.Mkdir(path, 0700)
120 err = os.Chmod(path, mode)
126 func CreateDirectoryExists(path string, mode fs.FileMode) {
127 err := os.Mkdir(path, 0700)
128 if err != nil && !os.IsExist(err) {
131 err = os.Chmod(path, mode)
137 func CreateFifo(path string, mode fs.FileMode) {
138 err := syscall.Mkfifo(path, 0600)
142 err = os.Chmod(path, mode)