-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtfsclient.go
396 lines (316 loc) · 8.84 KB
/
tfsclient.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package tfsclient
// #cgo CFLAGS: -I/usr/local/tfs-2.2/include
// #cgo LDFLAGS: -L/usr/local/tfs-2.2/lib -ltfsclient_c -ltbsys -ltbnet -lz
// #include <stdlib.h>
// #include <stdint.h>
// #include "tfs_client_capi.h"
import "C"
import (
"errors"
"unsafe"
)
var (
ErrInitTfsClient = errors.New("init tfs client error")
ErrTfsError = errors.New("tfs error")
)
type TfsClient struct {
}
func NewTfsClient(nsAddr string, cacheTime int32, cacheItems int32, startBg bool) (*TfsClient, error) {
var ns_addr *C.char
var ret, start_bg C.int
var cache_time, cache_items C.int32_t
if nsAddr != "" {
ns_addr = C.CString(nsAddr)
defer C.free(unsafe.Pointer(ns_addr))
}
if startBg {
start_bg = 1
} else {
start_bg = 0
}
cache_time = C.int32_t(cacheTime)
cache_items = C.int32_t(cacheItems)
ret = C.t_initialize(ns_addr, cache_time, cache_items, start_bg)
if int(ret) != TFS_SUCCESS {
return nil, ErrInitTfsClient
}
return &TfsClient{}, nil
}
func (c *TfsClient) SetDefaultServer(nsAddr string, cacheTime, cacheItems int32) error {
var ns_addr *C.char
var ret C.int
var cache_time, cache_items C.int32_t
if nsAddr != "" {
ns_addr = C.CString(nsAddr)
defer C.free(unsafe.Pointer(ns_addr))
}
cache_time = C.int32_t(cacheTime)
cache_items = C.int32_t(cacheItems)
ret = C.t_set_default_server(ns_addr, cache_time, cache_items)
if int(ret) != TFS_SUCCESS {
return ErrInitTfsClient
}
return nil
}
func (c *TfsClient) Destroy() error {
var ret C.int
ret = C.t_destroy()
if int(ret) != TFS_SUCCESS {
return ErrTfsError
}
return nil
}
func (c *TfsClient) Open(filename, suffix string, flags int, localKey string) (int, error) {
var c_filename *C.char
var c_suffix *C.char
var local_key *C.char
var fd C.int
if filename != "" {
c_filename = C.CString(filename)
defer C.free(unsafe.Pointer(c_filename))
}
if suffix != "" {
c_suffix = C.CString(suffix)
defer C.free(unsafe.Pointer(c_suffix))
}
if localKey != "" {
local_key = C.CString(localKey)
defer C.free(unsafe.Pointer(local_key))
}
fd = C.t_open(c_filename, c_suffix, C.int(flags), local_key)
if fd <= 0 {
return 0, ErrTfsError
}
return int(fd), nil
}
func (c *TfsClient) Open2(filename, suffix, nsAddr string, flags int, localKey string) (int, error) {
var c_filename *C.char
var c_suffix *C.char
var ns_addr *C.char
var local_key *C.char
var fd C.int
if filename != "" {
c_filename = C.CString(filename)
defer C.free(unsafe.Pointer(c_filename))
}
if suffix != "" {
c_suffix = C.CString(suffix)
defer C.free(unsafe.Pointer(c_suffix))
}
if localKey != "" {
local_key = C.CString(localKey)
defer C.free(unsafe.Pointer(local_key))
}
if nsAddr != "" {
ns_addr = C.CString(nsAddr)
defer C.free(unsafe.Pointer(ns_addr))
}
fd = C.t_open2(c_filename, c_suffix, ns_addr, C.int(flags), local_key)
if fd <= 0 {
return 0, ErrTfsError
}
return int(fd), nil
}
func (c *TfsClient) Read(fd int, buf []byte) int64 {
var count C.int64_t
cbuf := unsafe.Pointer(&buf[0])
count = C.int64_t(len(buf))
ret := C.t_read(C.int(fd), cbuf, count)
return int64(ret)
}
func (c *TfsClient) Read2(fd int, buf []byte) (TfsFileStat, int64) {
var stat TfsFileStat
var cstat C.TfsFileStat
var count C.int64_t
cbuf := unsafe.Pointer(&buf[0])
count = C.int64_t(len(buf))
ret := C.t_readv2(C.int(fd), cbuf, count, &cstat)
stat.FileId = uint64(cstat.file_id_)
stat.Offset = int32(cstat.offset_)
stat.Size = int64(cstat.size_)
stat.USize = int64(cstat.usize_)
stat.ModifyTime = int64(cstat.modify_time_)
stat.CreateTime = int64(cstat.create_time_)
stat.Flag = int32(cstat.flag_)
stat.CRC = uint32(cstat.crc_)
return stat, int64(ret)
}
func (c *TfsClient) Write(fd int, buf []byte) int64 {
var count C.int64_t
cbuf := unsafe.Pointer(&buf[0])
count = C.int64_t(len(buf))
ret := C.t_write(C.int(fd), cbuf, count)
return int64(ret)
}
func (c *TfsClient) Seek(fd int, offset int64, whence int) int64 {
ret := C.t_lseek(C.int(fd), C.int64_t(offset), C.int(whence))
return int64(ret)
}
func (c *TfsClient) Pread(fd int, buf []byte, offset int64) int64 {
var count C.int64_t
cbuf := unsafe.Pointer(&buf[0])
count = C.int64_t(len(buf))
ret := C.t_pread(C.int(fd), cbuf, count, C.int64_t(offset))
return int64(ret)
}
func (c *TfsClient) Pwrite(fd int, buf []byte, offset int64) int64 {
var count C.int64_t
cbuf := unsafe.Pointer(&buf[0])
count = C.int64_t(len(buf))
ret := C.t_pwrite(C.int(fd), cbuf, count, C.int64_t(offset))
return int64(ret)
}
func (c *TfsClient) GetFileLength(fd int) int64 {
return int64(C.t_get_file_length(C.int(fd)))
}
func (c *TfsClient) Fstat(fd int, mode byte) (TfsFileStat, error) {
var stat TfsFileStat
var cstat C.TfsFileStat
ret := C.t_fstat(C.int(fd), &cstat, C.TfsStatType(mode))
if int(ret) != TFS_SUCCESS {
return stat, ErrTfsError
}
stat.FileId = uint64(cstat.file_id_)
stat.Offset = int32(cstat.offset_)
stat.Size = int64(cstat.size_)
stat.USize = int64(cstat.usize_)
stat.ModifyTime = int64(cstat.modify_time_)
stat.CreateTime = int64(cstat.create_time_)
stat.Flag = int32(cstat.flag_)
stat.CRC = uint32(cstat.crc_)
return stat, nil
}
func (c *TfsClient) Close(fd int) (string, error) {
filename := make([]byte, TFS_FILE_LEN)
c_filename := (*C.char)(unsafe.Pointer(&filename[0]))
ret := C.t_close(C.int(fd), c_filename, C.int32_t(TFS_FILE_LEN))
if int(ret) != TFS_SUCCESS {
return "", ErrTfsError
}
return string(filename[:FILE_NAME_LEN]), nil
}
func (c *TfsClient) Unlink(filename, suffix string, action int) (int64, error) {
var c_filename, c_suffix *C.char
c_filename = C.CString(filename)
if suffix != "" {
c_suffix = C.CString(suffix)
defer C.free(unsafe.Pointer(c_suffix))
}
var filesize C.int64_t
ret := C.t_unlink(c_filename, c_suffix, &filesize, C.TfsUnlinkType(action))
C.free(unsafe.Pointer(c_filename))
if int(ret) != TFS_SUCCESS {
return 0, ErrTfsError
}
return int64(filesize), nil
}
func (c *TfsClient) Unlink2(filename, suffix string, action byte, nsAddr string) (int64, error) {
var c_filename *C.char
var c_suffix *C.char
var ns_addr *C.char
var filesize C.int64_t
c_filename = C.CString(filename)
if suffix != "" {
c_suffix = C.CString(suffix)
defer C.free(unsafe.Pointer(c_suffix))
}
if nsAddr != "" {
ns_addr = C.CString(nsAddr)
defer C.free(unsafe.Pointer(ns_addr))
}
ret := C.t_unlink2(c_filename, c_suffix, &filesize, C.TfsUnlinkType(action), ns_addr)
C.free(unsafe.Pointer(c_filename))
if int(ret) != TFS_SUCCESS {
return 0, ErrTfsError
}
return int64(filesize), nil
}
func (c *TfsClient) SetOptionFlag(fd int, optionFlag byte) error {
ret := C.t_set_option_flag(C.int(fd), C.OptionFlag(optionFlag))
if int(ret) != TFS_SUCCESS {
return ErrTfsError
}
return nil
}
func (c *TfsClient) SetCacheItems(val int64) {
C.t_set_cache_items(C.int64_t(val))
}
func (c *TfsClient) GetCacheItems() int64 {
return int64(C.t_get_cache_items())
}
func (c *TfsClient) SetCacheTime(val int64) {
C.t_set_cache_time(C.int64_t(val))
}
func (c *TfsClient) GetCacheTime() int64 {
return int64(C.t_get_cache_time())
}
func (c *TfsClient) SetSegmentSize(val int64) {
C.t_set_segment_size(C.int64_t(val))
}
func (c *TfsClient) GetSegmentSize() int64 {
return int64(C.t_get_segment_size())
}
func (c *TfsClient) SetBatchCount(val int64) {
C.t_set_batch_count(C.int64_t(val))
}
func (c *TfsClient) GetBatchCount() int64 {
return int64(C.t_get_batch_count())
}
func (c *TfsClient) SetStatInterVal(val int64) {
C.t_set_stat_interval(C.int64_t(val))
}
func (c *TfsClient) GetStatInterval() int64 {
return int64(C.t_get_stat_interval())
}
func (c *TfsClient) SetGcInterVal(val int64) {
C.t_set_gc_interval(C.int64_t(val))
}
func (c *TfsClient) GetGcInterval() int64 {
return int64(C.t_get_gc_interval())
}
func (c *TfsClient) SetGcExpiredTime(val int64) {
C.t_set_gc_expired_time(C.int64_t(val))
}
func (c *TfsClient) GetGcExpiredTime() int64 {
return int64(C.t_get_gc_expired_time())
}
func (c *TfsClient) SetBatchTimeout(val int64) {
C.t_set_batch_timeout(C.int64_t(val))
}
func (c *TfsClient) GetBatchTimeout() int64 {
return int64(C.t_get_batch_timeout())
}
func (c *TfsClient) SetWaitTimeout(val int64) {
C.t_set_wait_timeout(C.int64_t(val))
}
func (c *TfsClient) GetWaitTimeout() int64 {
return int64(C.t_get_wait_timeout())
}
func (c *TfsClient) SetClientRetryCount(val int64) {
C.t_set_client_retry_count(C.int64_t(val))
}
func (c *TfsClient) GetClientRetryCount() int64 {
return int64(C.t_get_client_retry_count())
}
func (c *TfsClient) SetLogLevel(level string) {
if level == "" {
return
}
clevel := C.CString(level)
C.t_set_log_level(clevel)
C.free(unsafe.Pointer(clevel))
}
func (c *TfsClient) SetLogFile(file string) {
if file == "" {
return
}
cfile := C.CString(file)
C.t_set_log_file(cfile)
C.free(unsafe.Pointer(cfile))
}
func (c *TfsClient) GetServerId() uint64 {
return uint64(C.t_get_server_id())
}
func (c *TfsClient) GetClusterId() int32 {
return int32(C.t_get_cluster_id())
}