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