-
Notifications
You must be signed in to change notification settings - Fork 1
/
file.go
executable file
·154 lines (126 loc) · 4.28 KB
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package checkers
import (
"fmt"
"os"
"reflect"
gc "launchpad.net/gocheck"
)
// IsNonEmptyFile checker
type isNonEmptyFileChecker struct {
*gc.CheckerInfo
}
var IsNonEmptyFile gc.Checker = &isNonEmptyFileChecker{
&gc.CheckerInfo{Name: "IsNonEmptyFile", Params: []string{"obtained"}},
}
func (checker *isNonEmptyFileChecker) Check(params []interface{}, names []string) (result bool, error string) {
filename, isString := stringOrStringer(params[0])
if isString {
fileInfo, err := os.Stat(filename)
if os.IsNotExist(err) {
return false, fmt.Sprintf("%s does not exist", filename)
} else if err != nil {
return false, fmt.Sprintf("other stat error: %v", err)
}
if fileInfo.Size() > 0 {
return true, ""
} else {
return false, fmt.Sprintf("%s is empty", filename)
}
}
value := reflect.ValueOf(params[0])
return false, fmt.Sprintf("obtained value is not a string and has no .String(), %s:%#v", value.Kind(), params[0])
}
// IsDirectory checker
type isDirectoryChecker struct {
*gc.CheckerInfo
}
var IsDirectory gc.Checker = &isDirectoryChecker{
&gc.CheckerInfo{Name: "IsDirectory", Params: []string{"obtained"}},
}
func (checker *isDirectoryChecker) Check(params []interface{}, names []string) (result bool, error string) {
path, isString := stringOrStringer(params[0])
if isString {
fileInfo, err := os.Stat(path)
if os.IsNotExist(err) {
return false, fmt.Sprintf("%s does not exist", path)
} else if err != nil {
return false, fmt.Sprintf("other stat error: %v", err)
}
if fileInfo.IsDir() {
return true, ""
} else {
return false, fmt.Sprintf("%s is not a directory", path)
}
}
value := reflect.ValueOf(params[0])
return false, fmt.Sprintf("obtained value is not a string and has no .String(), %s:%#v", value.Kind(), params[0])
}
// IsSymlink checker
type isSymlinkChecker struct {
*gc.CheckerInfo
}
var IsSymlink gc.Checker = &isSymlinkChecker{
&gc.CheckerInfo{Name: "IsSymlink", Params: []string{"obtained"}},
}
func (checker *isSymlinkChecker) Check(params []interface{}, names []string) (result bool, error string) {
path, isString := stringOrStringer(params[0])
if isString {
fileInfo, err := os.Lstat(path)
if os.IsNotExist(err) {
return false, fmt.Sprintf("%s does not exist", path)
} else if err != nil {
return false, fmt.Sprintf("other stat error: %v", err)
}
if fileInfo.Mode()&os.ModeSymlink != 0 {
return true, ""
} else {
return false, fmt.Sprintf("%s is not a symlink: %+v", path, fileInfo)
}
}
value := reflect.ValueOf(params[0])
return false, fmt.Sprintf("obtained value is not a string and has no .String(), %s:%#v", value.Kind(), params[0])
}
// DoesNotExist checker makes sure the path specified doesn't exist.
type doesNotExistChecker struct {
*gc.CheckerInfo
}
var DoesNotExist gc.Checker = &doesNotExistChecker{
&gc.CheckerInfo{Name: "DoesNotExist", Params: []string{"obtained"}},
}
func (checker *doesNotExistChecker) Check(params []interface{}, names []string) (result bool, error string) {
path, isString := stringOrStringer(params[0])
if isString {
_, err := os.Stat(path)
if os.IsNotExist(err) {
return true, ""
} else if err != nil {
return false, fmt.Sprintf("other stat error: %v", err)
}
return false, fmt.Sprintf("%s exists", path)
}
value := reflect.ValueOf(params[0])
return false, fmt.Sprintf("obtained value is not a string and has no .String(), %s:%#v", value.Kind(), params[0])
}
// SymlinkDoesNotExist checker makes sure the path specified doesn't exist.
type symlinkDoesNotExistChecker struct {
*gc.CheckerInfo
}
var SymlinkDoesNotExist gc.Checker = &symlinkDoesNotExistChecker{
&gc.CheckerInfo{Name: "SymlinkDoesNotExist", Params: []string{"obtained"}},
}
func (checker *symlinkDoesNotExistChecker) Check(params []interface{}, names []string) (result bool, error string) {
path, isString := stringOrStringer(params[0])
if isString {
_, err := os.Lstat(path)
if os.IsNotExist(err) {
return true, ""
} else if err != nil {
return false, fmt.Sprintf("other stat error: %v", err)
}
return false, fmt.Sprintf("%s exists", path)
}
value := reflect.ValueOf(params[0])
return false, fmt.Sprintf("obtained value is not a string and has no .String(), %s:%#v", value.Kind(), params[0])
}