Repository navigation
Expand file tree
/
Copy pathfastdfs.lua
More file actions
651 lines (620 loc) · 18.3 KB
/
Copy pathfastdfs.lua
File metadata and controls
651 lines (620 loc) · 18.3 KB
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
-- Copyright (C) 2012 Azure Wang
local string = string
local table = table
local bit = bit
local ngx = ngx
local tonumber = tonumber
local setmetatable = setmetatable
local error = error
module(...)
local VERSION = '0.1'
local FDFS_PROTO_PKG_LEN_SIZE = 8
local TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITHOUT_GROUP_ONE = 101
local TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITH_GROUP_ONE = 104
local TRACKER_PROTO_CMD_SERVICE_QUERY_UPDATE = 103query_update
local TRACKER_PROTO_CMD_SERVICE_QUERY_FETCH_ONE = 102
local STORAGE_PROTO_CMD_UPLOAD_FILE = 11
local STORAGE_PROTO_CMD_DELETE_FILE = 12
local STORAGE_PROTO_CMD_DOWNLOAD_FILE = 14
local STORAGE_PROTO_CMD_UPLOAD_SLAVE_FILE = 21
local STORAGE_PROTO_CMD_QUERY_FILE_INFO = 22
local STORAGE_PROTO_CMD_UPLOAD_APPENDER_FILE = 23
local STORAGE_PROTO_CMD_APPEND_FILE = 24
local FDFS_FILE_EXT_NAME_MAX_LEN = 6
local FDFS_PROTO_CMD_QUIT = 82
local TRACKER_PROTO_CMD_RESP = 100
local mt = { __index = _M }
function new(self)
return setmetatable({}, mt)
end
function set_tracker(self, host, port)
local tracker = {host = host, port = port}
self.tracker = tracker
end
function set_timeout(self, timeout)
if timeout then
self.timeout = timeout
end
end
function set_tracker_keepalive(self, timeout, size)
local keepalive = {timeout = timeout, size = size}
self.tracker_keepalive = keepalive
end
function set_storage_keepalive(self, timeout, size)
local keepalive = {timeout = timeout, size = size}
self.storage_keepalive = keepalive
end
function int2buf(n)
-- only trans 32bit full is 64bit
return string.rep("\00", 4) .. string.char(bit.band(bit.rshift(n, 24), 0xff), bit.band(bit.rshift(n, 16), 0xff), bit.band(bit.rshift(n, 8), 0xff), bit.band(n, 0xff))
end
function buf2int(buf)
-- only trans 32bit full is 64bit
local c1, c2, c3, c4 = string.byte(buf, 5, 8)
return bit.bor(bit.lshift(c1, 24), bit.lshift(c2, 16),bit.lshift(c3, 8), c4)
end
function read_fdfs_header(sock)
local header = {}
local buf, err = sock:receive(10)
if not buf then
ngx.log(ngx.ERR, "fdfs: read header error")
sock:close()
ngx.exit(500)
end
header.len = buf2int(string.sub(buf, 1, 8))
header.cmd = string.byte(buf, 9)
header.status = string.byte(buf, 10)
return header
end
function fix_string(str, fix_length)
local len = string.len(str)
if len > fix_length then
len = fix_length
end
local fix_str = string.sub(str, 1, len)
if len < fix_length then
fix_str = fix_str .. string.rep("\00", fix_length - len )
end
return fix_str
end
function strip_string(str)
local pos = string.find(str, "\00")
if pos then
return string.sub(str, 1, pos - 1)
else
return str
end
end
function get_ext_name(filename)
local extname = filename:match("%.(%w+)$")
if extname then
return fix_string(extname, FDFS_FILE_EXT_NAME_MAX_LEN)
else
return nil
end
end
function read_tracket_result(sock, header)
if header.len > 0 then
local res = {}
local buf = sock:receive(header.len)
res.group_name = strip_string(string.sub(buf, 1, 16))
res.host = strip_string(string.sub(buf, 17, 31))
res.port = buf2int(string.sub(buf, 32, 39))
res.store_path_index = string.byte(string.sub(buf, 40, 40))
return res
else
return nil
end
end
function read_storage_result(sock, header)
if header.len > 0 then
local res = {}
local buf = sock:receive(header.len)
res.group_name = strip_string(string.sub(buf, 1, 16))
res.file_name = strip_string(string.sub(buf, 17, header.len))
return res
else
return nil
end
end
function query_upload_storage(self, group_name)
local tracker = self.tracker
if not tracker then
return nil
end
local out = {}
if group_name then
-- query upload with group_name
-- package length
table.insert(out, int2buf(16))
-- cmd
table.insert(out, string.char(TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITH_GROUP_ONE))
-- status
table.insert(out, "\00")
-- group name
table.insert(out, fix_string(group_name, 16))
else
-- query upload without group_name
-- package length
table.insert(out, string.rep("\00", FDFS_PROTO_PKG_LEN_SIZE))
-- cmd
table.insert(out, string.char(TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITHOUT_GROUP_ONE))
-- status
table.insert(out, "\00")
end
-- init socket
local sock, err = ngx.socket.tcp()
if not sock then
return nil, err
end
if self.timeout then
sock:settimeout(self.timeout)
end
-- connect tracker
local ok, err = sock:connect(tracker.host, tracker.port)
if not ok then
return nil, err
end
-- send request
local bytes, err = sock:send(out)
-- read request header
local hdr = read_fdfs_header(sock)
-- read body
local res = read_tracket_result(sock, hdr)
-- keepalive
local keepalive = self.tracker_keepalive
if keepalive then
sock:setkeepalive(keepalive.timeout, keepalive.size)
end
return res
end
function do_upload_appender(self, ext_name)
local storage = self:query_upload_storage()
if not storage then
return nil
end
-- ext_name
if ext_name then
ext_name = fix_string(ext_name, FDFS_FILE_EXT_NAME_MAX_LEN)
end
-- get file size
local file_size = tonumber(ngx.var.content_length)
if not file_size or file_size <= 0 then
return nil
end
local sock, err = ngx.socket.tcp()
if not sock then
return nil, err
end
if self.timeout then
sock:settimeout(self.timeout)
end
local ok, err = sock:connect(storage.host, storage.port)
if not ok then
return nil, err
end
-- send header
local out = {}
table.insert(out, int2buf(file_size + 15))
table.insert(out, string.char(STORAGE_PROTO_CMD_UPLOAD_APPENDER_FILE))
-- status
table.insert(out, "\00")
-- store_path_index
table.insert(out, string.char(storage.store_path_index))
-- filesize
table.insert(out, int2buf(file_size))
-- exitname
table.insert(out, ext_name)
local bytes, err = sock:send(out)
-- send file data
local send_count = 0
local req_sock, err = ngx.req.socket()
if not req_sock then
ngx.log(ngx.ERR, err)
ngx.exit(500)
end
while true do
local chunk, _, part = req_sock:receive(1024 * 32)
if not part then
local bytes, err = sock:send(chunk)
if not bytes then
ngx.log(ngx.ngx.ERR, "fdfs: send body error")
sock:close()
ngx.exit(500)
end
send_count = send_count + bytes
else
-- part have data, not read full end
local bytes, err = sock:send(part)
if not bytes then
ngx.log(ngx.ngx.ERR, "fdfs: send body error")
sock:close()
ngx.exit(500)
end
send_count = send_count + bytes
break
end
end
if send_count ~= file_size then
-- send file not full
ngx.log(ngx.ngx.ERR, "fdfs: read file body not full")
sock:close()
ngx.exit(500)
end
-- read response
local res_hdr = read_fdfs_header(sock)
local res = read_storage_result(sock, res_hdr)
local keepalive = self.storage_keepalive
if keepalive then
sock:setkeepalive(keepalive.timeout, keepalive.size)
end
return res
end
function do_upload(self, ext_name)
local storage = self:query_upload_storage()
if not storage then
return nil
end
-- ext_name
if ext_name then
ext_name = fix_string(ext_name, FDFS_FILE_EXT_NAME_MAX_LEN)
end
-- get file size
local file_size = tonumber(ngx.var.content_length)
if not file_size or file_size <= 0 then
return nil
end
local sock, err = ngx.socket.tcp()
if not sock then
return nil, err
end
if self.timeout then
sock:settimeout(self.timeout)
end
local ok, err = sock:connect(storage.host, storage.port)
if not ok then
return nil, err
end
-- send header
local out = {}
table.insert(out, int2buf(file_size + 15))
table.insert(out, string.char(STORAGE_PROTO_CMD_UPLOAD_FILE))
-- status
table.insert(out, "\00")
-- store_path_index
table.insert(out, string.char(storage.store_path_index))
-- filesize
table.insert(out, int2buf(file_size))
-- exitname
table.insert(out, ext_name)
local bytes, err = sock:send(out)
-- send file data
local send_count = 0
local req_sock, err = ngx.req.socket()
if not req_sock then
ngx.log(ngx.ERR, err)
ngx.exit(500)
end
while true do
local chunk, _, part = req_sock:receive(1024 * 32)
if not part then
local bytes, err = sock:send(chunk)
if not bytes then
ngx.log(ngx.ngx.ERR, "fdfs: send body error")
sock:close()
ngx.exit(500)
end
send_count = send_count + bytes
else
-- part have data, not read full end
local bytes, err = sock:send(part)
if not bytes then
ngx.log(ngx.ngx.ERR, "fdfs: send body error")
sock:close()
ngx.exit(500)
end
send_count = send_count + bytes
break
end
end
if send_count ~= file_size then
-- send file not full
ngx.log(ngx.ngx.ERR, "fdfs: read file body not full")
sock:close()
ngx.exit(500)
end
-- read response
local res_hdr = read_fdfs_header(sock)
local res = read_storage_result(sock, res_hdr)
local keepalive = self.storage_keepalive
if keepalive then
sock:setkeepalive(keepalive.timeout, keepalive.size)
end
return res
end
function query_update_storage_ex(self, group_name, file_name)
local out = {}
-- package length
table.insert(out, int2buf(16 + string.len(file_name)))
-- cmd
table.insert(out, string.char(TRACKER_PROTO_CMD_SERVICE_QUERY_UPDATE))
-- status
table.insert(out, "\00")
-- group_name
table.insert(out, fix_string(group_name, 16))
-- file name
table.insert(out, file_name)
-- get tracker
local tracker = self.tracker
if not tracker then
return nil
end
-- init socket
local sock, err = ngx.socket.tcp()
if not sock then
return nil, err
end
if self.timeout then
sock:settimeout(self.timeout)
end
-- connect tracker
local ok, err = sock:connect(tracker.host, tracker.port)
if not ok then
return nil, err
end
-- send request
local bytes, err = sock:send(out)
-- read request header
local hdr = read_fdfs_header(sock)
-- read body
local res = read_tracket_result(sock, hdr)
-- keepalive
local keepalive = self.tracker_keepalive
if keepalive then
sock:setkeepalive(keepalive.timeout, keepalive.size)
end
return res
end
function query_update_storage(self, fileid)
local pos = fileid:find('/')
if not pos then
return nil
else
local group_name = fileid:sub(1, pos-1)
local file_name = fileid:sub(pos + 1)
local res = self:query_update_storage_ex(group_name, file_name)
if res then
res.file_name = file_name
end
return res
end
end
function do_delete(self, fileid)
local storage = self:query_update_storage(fileid)
if not storage then
return nil
end
local out = {}
table.insert(out, int2buf(16 + string.len(storage.file_name)))
table.insert(out, string.char(STORAGE_PROTO_CMD_DELETE_FILE))
table.insert(out, "\00")
-- group name
table.insert(out, fix_string(storage.group_name, 16))
-- file name
table.insert(out, storage.file_name)
-- init socket
local sock, err = ngx.socket.tcp()
if not sock then
return nil, err
end
sock:settimeout(self.timeout)
local ok, err = sock:connect(storage.host, storage.port)
if not ok then
return nil, err
end
local bytes, err = sock:send(out)
if not bytes then
ngx.log(ngx.ngx.ERR, "fdfs: send body error")
sock:close()
ngx.exit(500)
end
-- read request header
local hdr = read_fdfs_header(sock)
local keepalive = self.storage_keepalive
if keepalive then
sock:setkeepalive(keepalive.timeout, keepalive.size)
end
return hdr
end
function query_download_storage(self, fileid)
local pos = fileid:find('/')
if not pos then
return nil
else
local group_name = fileid:sub(1, pos-1)
local file_name = fileid:sub(pos + 1)
local res = self:query_download_storage_ex(group_name, file_name)
res.file_name = file_name
return res
end
end
function query_download_storage_ex(self, group_name, file_name)
local out = {}
-- package length
table.insert(out, int2buf(16 + string.len(file_name)))
-- cmd
table.insert(out, string.char(TRACKER_PROTO_CMD_SERVICE_QUERY_FETCH_ONE))
-- status
table.insert(out, "\00")
-- group_name
table.insert(out, fix_string(group_name, 16))
-- file name
table.insert(out, file_name)
-- get tracker
local tracker = self.tracker
if not tracker then
return nil
end
-- init socket
local sock, err = ngx.socket.tcp()
if not sock then
return nil, err
end
if self.timeout then
sock:settimeout(self.timeout)
end
-- connect tracker
local ok, err = sock:connect(tracker.host, tracker.port)
if not ok then
return nil, err
end
-- send request
local bytes, err = sock:send(out)
-- read request header
local hdr = read_fdfs_header(sock)
-- read body
local res = read_tracket_result(sock, hdr)
-- keepalive
local keepalive = self.tracker_keepalive
if keepalive then
sock:setkeepalive(keepalive.timeout, keepalive.size)
end
return res
end
function do_download(self, fileid)
local storage = self:query_download_storage(fileid)
if not storage then
return nil
end
local out = {}
-- file_offset(8) download_bytes(8) group_name(16) file_name(n)
table.insert(out, int2buf(32 + string.len(storage.file_name)))
table.insert(out, string.char(STORAGE_PROTO_CMD_DOWNLOAD_FILE))
table.insert(out, "\00")
-- file_offset download_bytes 8 + 8
table.insert(out, string.rep("\00", 16))
-- group name
table.insert(out, fix_string(storage.group_name, 16))
-- file name
table.insert(out, storage.file_name)
-- init socket
local sock, err = ngx.socket.tcp()
if not sock then
return nil, err
end
sock:settimeout(self.timeout)
local ok, err = sock:connect(storage.host, storage.port)
if not ok then
return nil, err
end
local bytes, err = sock:send(out)
if not bytes then
ngx.log(ngx.ERR, "fdfs: send request error" .. err)
sock:close()
ngx.exit(500)
end
-- read request header
local hdr = read_fdfs_header(sock)
-- read request bodya
local data, partial
if hdr.len > 0 then
data, err, partial = sock:receive(hdr.len)
if not data then
ngx.log(ngx.ERR, "read file body error:" .. err)
sock:close()
ngx.exit(500)
end
end
local keepalive = self.storage_keepalive
if keepalive then
sock:setkeepalive(keepalive.timeout, keepalive.size)
end
return data
end
function do_append(self, fileid)
local storage = self:query_update_storage(fileid)
if not storage then
return nil
end
local file_name = storage.file_name
local file_name_len = string.len(file_name)
-- get file size
local file_size = tonumber(ngx.var.content_length)
if not file_size or file_size <= 0 then
return nil
end
local sock, err = ngx.socket.tcp()
if not sock then
return nil, err
end
if self.timeout then
sock:settimeout(self.timeout)
end
local ok, err = sock:connect(storage.host, storage.port)
if not ok then
return nil, err
end
-- send request
local out = {}
table.insert(out, int2buf(file_size + file_name_len + 16))
table.insert(out, string.char(STORAGE_PROTO_CMD_APPEND_FILE))
-- status
table.insert(out, "\00")
table.insert(out, int2buf(file_name_len))
table.insert(out, int2buf(file_size))
table.insert(out, file_name)
local bytes, err = sock:send(out)
-- send file data
local send_count = 0
local req_sock, err = ngx.req.socket()
if not req_sock then
ngx.log(ngx.ERR, err)
ngx.exit(500)
end
while true do
local chunk, _, part = req_sock:receive(1024 * 32)
if not part then
local bytes, err = sock:send(chunk)
if not bytes then
ngx.log(ngx.ngx.ERR, "fdfs: send body error")
sock:close()
ngx.exit(500)
end
send_count = send_count + bytes
else
-- part have data, not read full end
local bytes, err = sock:send(part)
if not bytes then
ngx.log(ngx.ngx.ERR, "fdfs: send body error")
sock:close()
ngx.exit(500)
end
send_count = send_count + bytes
break
end
end
if send_count ~= file_size then
-- send file not full
ngx.log(ngx.ngx.ERR, "fdfs: read file body not full")
sock:close()
ngx.exit(500)
end
-- read response
local res_hdr = read_fdfs_header(sock)
local res = read_storage_result(sock, res_hdr)
local keepalive = self.storage_keepalive
if keepalive then
sock:setkeepalive(keepalive.timeout, keepalive.size)
end
return res_hdr
end
-- _M.query_upload_storage = query_upload_storage
-- _M.do_upload_storage = do_upload_storage
-- _M.do_delete_storage = do_delete_storage
local class_mt = {
-- to prevent use of casual module global variables
__newindex = function (table, key, val)
error('attempt to write to undeclared variable "' .. key .. '"')
end
}
setmetatable(_M, class_mt)