]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - remote/sync/filetest/filetest.go
Use SPDX license identifiers
[safcm/safcm.git] / remote / sync / filetest / filetest.go
1 // Utility functions useful for file-related tests
2
3 // SPDX-License-Identifier: GPL-3.0-or-later
4 // Copyright (C) 2021-2024  Simon Ruderich
5
6 package filetest
7
8 import (
9         "io/fs"
10         "os"
11         "os/user"
12         "path/filepath"
13         "strconv"
14         "syscall"
15 )
16
17 type File struct {
18         Path string
19         Mode fs.FileMode
20         Data []byte
21 }
22
23 func WalkDir(basePath string) ([]File, error) {
24         var res []File
25         err := filepath.WalkDir(basePath, func(path string,
26                 d fs.DirEntry, err error) error {
27
28                 if err != nil {
29                         return err
30                 }
31                 info, err := d.Info()
32                 if err != nil {
33                         return err
34                 }
35                 rel, err := filepath.Rel(basePath, path)
36                 if err != nil {
37                         return err
38                 }
39
40                 f := File{
41                         Path: rel,
42                         Mode: info.Mode(),
43                 }
44                 if f.Mode.Type() == 0 {
45                         x, err := os.ReadFile(path)
46                         if err != nil {
47                                 return err
48                         }
49                         f.Data = x
50                 } else if f.Mode.Type() == fs.ModeSymlink {
51                         x, err := os.Readlink(path)
52                         if err != nil {
53                                 return err
54                         }
55                         f.Data = []byte(x)
56                         f.Mode |= 0777 // see sync/files.go
57                 }
58                 res = append(res, f)
59                 return nil
60         })
61         if err != nil {
62                 return nil, err
63         }
64         return res, nil
65 }
66
67 func CurrentUserAndGroup() (string, int, string, int) {
68         u, err := user.Current()
69         if err != nil {
70                 panic(err)
71         }
72         g, err := user.LookupGroupId(u.Gid)
73         if err != nil {
74                 panic(err)
75         }
76         uid, err := strconv.Atoi(u.Uid)
77         if err != nil {
78                 panic(err)
79         }
80         gid, err := strconv.Atoi(g.Gid)
81         if err != nil {
82                 panic(err)
83         }
84         return u.Username, uid, g.Name, gid
85 }
86
87 func CreateFile(path string, data string, mode fs.FileMode) {
88         err := os.WriteFile(path, []byte(data), 0600)
89         if err != nil {
90                 panic(err)
91         }
92         err = os.Chmod(path, mode)
93         if err != nil {
94                 panic(err)
95         }
96 }
97
98 func CreateSymlink(path string, data string) {
99         err := os.Symlink(data, path)
100         if err != nil {
101                 panic(err)
102         }
103 }
104
105 func CreateDirectory(path string, mode fs.FileMode) {
106         err := os.Mkdir(path, 0700)
107         if err != nil {
108                 panic(err)
109         }
110         err = os.Chmod(path, mode)
111         if err != nil {
112                 panic(err)
113         }
114 }
115
116 func CreateDirectoryExists(path string, mode fs.FileMode) {
117         err := os.Mkdir(path, 0700)
118         if err != nil && !os.IsExist(err) {
119                 panic(err)
120         }
121         err = os.Chmod(path, mode)
122         if err != nil {
123                 panic(err)
124         }
125 }
126
127 func CreateFifo(path string, mode fs.FileMode) {
128         err := syscall.Mkfifo(path, 0600)
129         if err != nil {
130                 panic(err)
131         }
132         err = os.Chmod(path, mode)
133         if err != nil {
134                 panic(err)
135         }
136 }