1 // Utility functions useful for file-related tests
3 // SPDX-License-Identifier: GPL-3.0-or-later
4 // Copyright (C) 2021-2024 Simon Ruderich
23 func WalkDir(basePath string) ([]File, error) {
25 err := filepath.WalkDir(basePath, func(path string,
26 d fs.DirEntry, err error) error {
35 rel, err := filepath.Rel(basePath, path)
44 if f.Mode.Type() == 0 {
45 x, err := os.ReadFile(path)
50 } else if f.Mode.Type() == fs.ModeSymlink {
51 x, err := os.Readlink(path)
56 f.Mode |= 0777 // see sync/files.go
67 func CurrentUserAndGroup() (string, int, string, int) {
68 u, err := user.Current()
72 g, err := user.LookupGroupId(u.Gid)
76 uid, err := strconv.Atoi(u.Uid)
80 gid, err := strconv.Atoi(g.Gid)
84 return u.Username, uid, g.Name, gid
87 func CreateFile(path string, data string, mode fs.FileMode) {
88 err := os.WriteFile(path, []byte(data), 0600)
92 err = os.Chmod(path, mode)
98 func CreateSymlink(path string, data string) {
99 err := os.Symlink(data, path)
105 func CreateDirectory(path string, mode fs.FileMode) {
106 err := os.Mkdir(path, 0700)
110 err = os.Chmod(path, mode)
116 func CreateDirectoryExists(path string, mode fs.FileMode) {
117 err := os.Mkdir(path, 0700)
118 if err != nil && !os.IsExist(err) {
121 err = os.Chmod(path, mode)
127 func CreateFifo(path string, mode fs.FileMode) {
128 err := syscall.Mkfifo(path, 0600)
132 err = os.Chmod(path, mode)