linker.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package hybridBackup
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "path/filepath"
  6. )
  7. /*
  8. Linker.go
  9. This script handle the linking file operations
  10. */
  11. type LinkFileMap struct {
  12. UnchangedFile map[string]string
  13. DeletedFiles map[string]string
  14. }
  15. //Generate and write link file to disk
  16. func generateLinkFile(snapshotFolder string, lf LinkFileMap) error {
  17. js, err := json.MarshalIndent(lf, "", "\t")
  18. if err != nil {
  19. return err
  20. }
  21. return ioutil.WriteFile(filepath.Join(snapshotFolder, "snapshot.datalink"), js, 0755)
  22. }
  23. //Read link file and parse it into link file map
  24. func readLinkFile(snapshotFolder string) (*LinkFileMap, error) {
  25. result := LinkFileMap{
  26. UnchangedFile: map[string]string{},
  27. DeletedFiles: map[string]string{},
  28. }
  29. //Check if the link file exists
  30. expectedLinkFilePath := filepath.Join(snapshotFolder, "snapshot.datalink")
  31. if fileExists(expectedLinkFilePath) {
  32. //Read the content of the link file
  33. content, err := ioutil.ReadFile(expectedLinkFilePath)
  34. if err == nil {
  35. //No error. Read and parse the content
  36. lfContent := LinkFileMap{}
  37. err := json.Unmarshal(content, &lfContent)
  38. if err == nil {
  39. return &lfContent, nil
  40. }
  41. }
  42. }
  43. return &result, nil
  44. }
  45. //Check if a file exists in a linkFileMap. return boolean and its linked to snapshot name
  46. func (lfm *LinkFileMap) fileExists(fileRelPath string) (bool, string) {
  47. val, ok := lfm.UnchangedFile[filepath.ToSlash(fileRelPath)]
  48. if !ok {
  49. return false, ""
  50. } else {
  51. return true, val
  52. }
  53. }