]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - cmd/safcm-remote/sync/filetest/filetest.go
sync: tests: use strict perm for os.WriteFile() in CreateFile()
[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,
38                 d fs.DirEntry, err error) error {
39
40                 if err != nil {
41                         return err
42                 }
43                 info, err := d.Info()
44                 if err != nil {
45                         return err
46                 }
47                 rel, err := filepath.Rel(basePath, path)
48                 if err != nil {
49                         return err
50                 }
51
52                 f := File{
53                         Path: rel,
54                         Mode: info.Mode(),
55                 }
56                 if f.Mode.Type() == 0 {
57                         x, err := os.ReadFile(path)
58                         if err != nil {
59                                 return err
60                         }
61                         f.Data = x
62                 } else if f.Mode.Type() == fs.ModeSymlink {
63                         x, err := os.Readlink(path)
64                         if err != nil {
65                                 return err
66                         }
67                         f.Data = []byte(x)
68                         f.Mode |= 0777 // see sync/files.go
69                 }
70                 res = append(res, f)
71                 return nil
72         })
73         if err != nil {
74                 return nil, err
75         }
76         return res, nil
77 }
78
79 func CurrentUserAndGroup() (string, int, string, int) {
80         u, err := user.Current()
81         if err != nil {
82                 panic(err)
83         }
84         g, err := user.LookupGroupId(u.Gid)
85         if err != nil {
86                 panic(err)
87         }
88         uid, err := strconv.Atoi(u.Uid)
89         if err != nil {
90                 panic(err)
91         }
92         gid, err := strconv.Atoi(g.Gid)
93         if err != nil {
94                 panic(err)
95         }
96         return u.Username, uid, g.Name, gid
97 }
98
99 func CreateFile(path string, data string, mode fs.FileMode) {
100         err := os.WriteFile(path, []byte(data), 0600)
101         if err != nil {
102                 panic(err)
103         }
104         err = os.Chmod(path, mode)
105         if err != nil {
106                 panic(err)
107         }
108 }
109
110 func CreateSymlink(path string, data string) {
111         err := os.Symlink(data, path)
112         if err != nil {
113                 panic(err)
114         }
115 }
116
117 func CreateDirectory(path string, mode fs.FileMode) {
118         err := os.Mkdir(path, 0700)
119         if err != nil {
120                 panic(err)
121         }
122         err = os.Chmod(path, mode)
123         if err != nil {
124                 panic(err)
125         }
126 }
127
128 func CreateDirectoryExists(path string, mode fs.FileMode) {
129         err := os.Mkdir(path, 0700)
130         if err != nil && !os.IsExist(err) {
131                 panic(err)
132         }
133         err = os.Chmod(path, mode)
134         if err != nil {
135                 panic(err)
136         }
137 }
138
139 func CreateFifo(path string, mode fs.FileMode) {
140         err := syscall.Mkfifo(path, 0600)
141         if err != nil {
142                 panic(err)
143         }
144         err = os.Chmod(path, mode)
145         if err != nil {
146                 panic(err)
147         }
148 }