This repository has been archived by the owner on Mar 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
helpers.go
203 lines (181 loc) · 4.2 KB
/
helpers.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*** Copyright (c) 2016, University of Florida Research Foundation, Inc. and The BioTeam, Inc. ***
*** For more information please refer to the LICENSE.md file ***/
package gorods
// #include "wrapper.h"
import "C"
import (
"fmt"
"strconv"
"time"
"unsafe"
)
func cTimeToTime(cTime *C.char) time.Time {
unixStamp, _ := strconv.ParseInt(C.GoString(cTime), 10, 64)
return time.Unix(unixStamp, 0)
}
func timeStringToTime(ts string) time.Time {
unixStamp, _ := strconv.ParseInt(ts, 10, 64)
return time.Unix(unixStamp, 0)
}
func GetShortTypeString(typ int) string {
switch typ {
case DataObjType:
return "d"
case CollectionType:
return "C"
case ZoneType:
return "Z"
case ResourceType:
return "R"
case UserType:
return "u"
case AdminType:
return "u"
case GroupAdminType:
return "u"
case GroupType:
return "u"
case UnknownType:
return "?"
// case Null:
// return "null"
// case Read:
// return "read"
// case Write:
// return "write"
// case Own:
// return "own"
// case Inherit:
// return "inherit"
// case NoInherit:
// return "noinherit"
// case Local:
// return "local"
// case Remote:
// return "remote"
// case Cache:
// return "cache"
// case Archive:
// return "archive"
default:
panic(newError(Fatal, -1, "unrecognized type constant"))
}
}
func GetTypeString(typ int) string {
return getTypeString(typ)
}
func getTypeString(typ int) string {
switch typ {
case DataObjType:
return "d"
case CollectionType:
return "C"
case ZoneType:
return "Z"
case ResourceType:
return "R"
case UserType:
return "rodsuser"
case AdminType:
return "rodsadmin"
case GroupAdminType:
return "rodsgroupadmin"
case GroupType:
return "rodsgroup"
case UnknownType:
return "?"
case Null:
return "null"
case Read:
return "read"
case Write:
return "write"
case Own:
return "own"
case Inherit:
return "inherit"
case NoInherit:
return "noinherit"
case Local:
return "local"
case Remote:
return "remote"
case Cache:
return "cache"
case Archive:
return "archive"
default:
panic(newError(Fatal, -1, "unrecognized type constant"))
}
}
func aclSliceToResponse(result *C.goRodsACLResult_t, con *Connection) (ACLs, error) {
defer C.gorods_free_acl_result(result)
unsafeArr := unsafe.Pointer(result.aclArr)
arrLen := int(result.size)
// Convert C array to slice, backed by arr *C.goRodsACL_t
slice := (*[1 << 30]C.goRodsACL_t)(unsafeArr)[:arrLen:arrLen]
response := make(ACLs, 0)
for _, acl := range slice {
typeString := C.GoString(acl.acltype)
typeMap := map[string]int{
"rodsgroup": GroupType,
"rodsuser": UserType,
"rodsadmin": AdminType,
"groupadmin": GroupAdminType,
}
var aclType int = typeMap[typeString]
if aclType == 0 {
aclType = UnknownType
}
accessString := C.GoString(acl.dataAccess)
var accessLevel int
switch accessString {
case "own":
accessLevel = Own
case "modify object":
accessLevel = Write
case "read object":
accessLevel = Read
default:
accessLevel = Null
}
var accessObject AccessObject
if aclType == UserType || aclType == AdminType || aclType == GroupAdminType {
if usrs, err := con.Users(); err == nil {
if existingUsr := usrs.FindByName(C.GoString(acl.name), con); existingUsr != nil {
accessObject = existingUsr
} else {
return nil, newError(Fatal, -1, fmt.Sprintf("iRODS GetACL Failed: can't find iRODS user by string"))
}
} else {
return nil, err
}
} else if aclType == GroupType {
if grps, err := con.Groups(); err == nil {
if existingGrp := grps.FindByName(C.GoString(acl.name), con); existingGrp != nil {
accessObject = existingGrp
} else {
return nil, newError(Fatal, -1, fmt.Sprintf("iRODS GetACL Failed: can't find iRODS group by string"))
}
} else {
return nil, err
}
} else if aclType == UnknownType {
return nil, newError(Fatal, -1, fmt.Sprintf("iRODS GetACL Failed: Unknown Type"))
}
response = append(response, &ACL{
AccessObject: accessObject,
AccessLevel: accessLevel,
Type: aclType,
})
}
return response, nil
}
func isString(obj interface{}) bool {
switch obj.(type) {
case string:
return true
default:
}
return false
}