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)
76 func CurrentUserAndGroup() (string, int, string, int) {
77 u, err := user.Current()
81 g, err := user.LookupGroupId(u.Gid)
85 uid, err := strconv.Atoi(u.Uid)
89 gid, err := strconv.Atoi(g.Gid)
93 return u.Username, uid, g.Name, gid
96 func CreateFile(path string, data string, mode fs.FileMode) {
97 err := os.WriteFile(path, []byte(data), 0644)
101 err = os.Chmod(path, mode)
107 func CreateSymlink(path string, data string) {
108 err := os.Symlink(data, path)
114 func CreateDirectory(path string, mode fs.FileMode) {
115 err := os.Mkdir(path, 0700)
119 err = os.Chmod(path, mode)
125 func CreateFifo(path string, mode fs.FileMode) {
126 err := syscall.Mkfifo(path, 0600)
130 err = os.Chmod(path, mode)