Skip to content

Commit f1f0eec

Browse files
author
Robert Read
committed
Replace the file_id extended attribute with uuid
Also adds hash and url EA as well.
1 parent 525c3eb commit f1f0eec

File tree

12 files changed

+266
-262
lines changed

12 files changed

+266
-262
lines changed

cmd/lhsm-plugin-posix/posix/mover.go

Lines changed: 17 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ package posix
66

77
import (
88
"bufio"
9+
"bytes"
910
"compress/gzip"
10-
"encoding/json"
1111
"fmt"
1212
"io"
1313
"os"
@@ -87,12 +87,6 @@ type (
8787
Compression CompressionOption
8888
Checksums ChecksumConfig
8989
}
90-
91-
// FileID is used to identify a file in the backend
92-
FileID struct {
93-
UUID string
94-
Sum string `json:",omitempty"`
95-
}
9690
)
9791

9892
const (
@@ -332,65 +326,31 @@ func (m *Mover) Archive(action dmplugin.Action) error {
332326
m.Destination(fileID),
333327
cw.Sum())
334328

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())
346331
return nil
347332
}
348333

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-
370334
// Restore fulfills an HSM Restore request
371335
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())
373337
rate.Mark(1)
374338
start := time.Now()
375339

376340
// 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")
383343
}
384344

385345
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())
388348
enableUnzip = true
389349
}
390350

391-
src, err := os.Open(m.Destination(id.UUID))
351+
src, err := os.Open(m.Destination(action.UUID()))
392352
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()))
394354
}
395355
defer src.Close()
396356

@@ -426,9 +386,9 @@ func (m *Mover) Restore(action dmplugin.Action) error {
426386
return errors.Wrap(err, "copy failed")
427387
}
428388

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())
432392
return errors.New("Checksum mismatch!")
433393
}
434394
}
@@ -443,15 +403,11 @@ func (m *Mover) Restore(action dmplugin.Action) error {
443403

444404
// Remove fulfills an HSM Remove request
445405
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())
447407
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")
454410
}
455411

456-
return os.Remove(m.Destination(id.UUID))
412+
return os.Remove(m.Destination(action.UUID()))
457413
}

0 commit comments

Comments
 (0)