@@ -6,8 +6,8 @@ package posix
6
6
7
7
import (
8
8
"bufio"
9
+ "bytes"
9
10
"compress/gzip"
10
- "encoding/json"
11
11
"fmt"
12
12
"io"
13
13
"os"
@@ -87,12 +87,6 @@ type (
87
87
Compression CompressionOption
88
88
Checksums ChecksumConfig
89
89
}
90
-
91
- // FileID is used to identify a file in the backend
92
- FileID struct {
93
- UUID string
94
- Sum string `json:",omitempty"`
95
- }
96
90
)
97
91
98
92
const (
@@ -332,65 +326,31 @@ func (m *Mover) Archive(action dmplugin.Action) error {
332
326
m .Destination (fileID ),
333
327
cw .Sum ())
334
328
335
- id := & FileID {
336
- UUID : fileID ,
337
- Sum : fmt .Sprintf ("%x" , cw .Sum ()),
338
- }
339
-
340
- buf , err := EncodeFileID (id )
341
- if err != nil {
342
- return errors .Wrap (err , "encode file id failed" )
343
- }
344
- action .SetFileID (buf )
345
- action .SetActualLength (n )
329
+ action .SetUUID (fileID )
330
+ action .SetHash (cw .Sum ())
346
331
return nil
347
332
}
348
333
349
- // EncodeFileID is converts FileID to a json buffer.
350
- func EncodeFileID (id * FileID ) ([]byte , error ) {
351
- buf , err := json .Marshal (id )
352
- if err != nil {
353
- return nil , errors .Wrap (err , "marshal failed" )
354
- }
355
- return buf , nil
356
-
357
- }
358
-
359
- // ParseFileID unmarshalls the FileID struct from
360
- // json encoded data received from the agent.
361
- func ParseFileID (buf []byte ) (* FileID , error ) {
362
- var id FileID
363
- err := json .Unmarshal (buf , & id )
364
- if err != nil {
365
- return nil , errors .Wrap (err , "unmarshal failed" )
366
- }
367
- return & id , nil
368
- }
369
-
370
334
// Restore fulfills an HSM Restore request
371
335
func (m * Mover ) Restore (action dmplugin.Action ) error {
372
- debug .Printf ("%s id:%d RESTORE %s %s" , m .Name , action .ID (), action .PrimaryPath (), action .FileID ())
336
+ debug .Printf ("%s id:%d RESTORE %s %s" , m .Name , action .ID (), action .PrimaryPath (), action .UUID ())
373
337
rate .Mark (1 )
374
338
start := time .Now ()
375
339
376
340
// Initialize Reader for backing file
377
- if action .FileID () == nil {
378
- return errors .New ("Missing file_id" )
379
- }
380
- id , err := ParseFileID (action .FileID ())
381
- if err != nil {
382
- return errors .Wrap (err , "parse file id" )
341
+ if action .UUID () == "" {
342
+ return errors .New ("Missing UUID" )
383
343
}
384
344
385
345
enableUnzip := false
386
- if filepath .Ext (id .UUID ) == ".gz" {
387
- debug .Printf ("%s: id:%d decompressing %s" , m .Name , action .ID (), id .UUID )
346
+ if filepath .Ext (action .UUID () ) == ".gz" {
347
+ debug .Printf ("%s: id:%d decompressing %s" , m .Name , action .ID (), action .UUID () )
388
348
enableUnzip = true
389
349
}
390
350
391
- src , err := os .Open (m .Destination (id .UUID ))
351
+ src , err := os .Open (m .Destination (action .UUID () ))
392
352
if err != nil {
393
- return errors .Wrapf (err , "%s: open failed" , m .Destination (id .UUID ))
353
+ return errors .Wrapf (err , "%s: open failed" , m .Destination (action .UUID () ))
394
354
}
395
355
defer src .Close ()
396
356
@@ -426,9 +386,9 @@ func (m *Mover) Restore(action dmplugin.Action) error {
426
386
return errors .Wrap (err , "copy failed" )
427
387
}
428
388
429
- if id . Sum != "" && ! m .Checksums .DisableCompareOnRestore {
430
- if id . Sum != fmt . Sprintf ( "%x" , cw .Sum ()) {
431
- alert .Warnf ("original checksum doesn't match new: %s != %x" , id . Sum , cw .Sum ())
389
+ if action . Hash () != nil && ! m .Checksums .DisableCompareOnRestore {
390
+ if bytes . Compare ( action . Hash () , cw .Sum ()) != 0 {
391
+ alert .Warnf ("original checksum doesn't match new: %x != %x" , action . Hash () , cw .Sum ())
432
392
return errors .New ("Checksum mismatch!" )
433
393
}
434
394
}
@@ -443,15 +403,11 @@ func (m *Mover) Restore(action dmplugin.Action) error {
443
403
444
404
// Remove fulfills an HSM Remove request
445
405
func (m * Mover ) Remove (action dmplugin.Action ) error {
446
- debug .Printf ("%s id:%d REMOVE %s %s" , m .Name , action .ID (), action .PrimaryPath (), action .FileID ())
406
+ debug .Printf ("%s id:%d REMOVE %s %s" , m .Name , action .ID (), action .PrimaryPath (), action .UUID ())
447
407
rate .Mark (1 )
448
- if action .FileID () == nil {
449
- return errors .New ("Missing file_id" )
450
- }
451
- id , err := ParseFileID (action .FileID ())
452
- if err != nil {
453
- return errors .Wrap (err , "parse file id failed" )
408
+ if action .UUID () == "" {
409
+ return errors .New ("Missing uuid" )
454
410
}
455
411
456
- return os .Remove (m .Destination (id .UUID ))
412
+ return os .Remove (m .Destination (action .UUID () ))
457
413
}
0 commit comments