config.go 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package filesystem
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "strings"
  6. )
  7. //FileSystem configuration. Append more lines if required.
  8. type FileSystemOption struct {
  9. Name string `json:"name"` //Display name of this device
  10. Uuid string `json:"uuid"` //UUID of this device, e.g. S1
  11. Path string `json:"path"` //Path for the storage root
  12. Access string `json:"access,omitempty"` //Access right, allow {readonly, readwrite}
  13. Hierarchy string `json:"hierarchy"` //Folder hierarchy, allow {public, user}
  14. Automount bool `json:"automount"` //Automount this device if exists
  15. Filesystem string `json:"filesystem,omitempty"` //Support {"ext4","ext2", "ext3", "fat", "vfat", "ntfs"}
  16. Mountdev string `json:"mountdev,omitempty"` //Device file (e.g. /dev/sda1)
  17. Mountpt string `json:"mountpt,omitempty"` //Device mount point (e.g. /media/storage1)
  18. //Backup Hierarchy Options
  19. Parentuid string `json:"parentuid,omitempty"` //The parent mount point for backup source, backup disk only
  20. BackupMode string `json:"backupmode,omitempty"` //Backup mode of the virtual disk
  21. Username string `json:"username,omitempty"` //Username if the storage require auth
  22. Password string `json:"password,omitempty"` //Password if the storage require auth
  23. }
  24. //Parse a list of StorageConfig from the given json content
  25. func loadConfigFromJSON(jsonContent []byte) ([]FileSystemOption, error) {
  26. storageInConfig := []FileSystemOption{}
  27. //Try to parse the JSON content
  28. err := json.Unmarshal(jsonContent, &storageInConfig)
  29. return storageInConfig, err
  30. }
  31. //Validate if the given options are correct
  32. func ValidateOption(options *FileSystemOption) error {
  33. //Check if path exists
  34. if options.Name == "" {
  35. return errors.New("File System Handler name cannot be empty")
  36. }
  37. if options.Uuid == "" {
  38. return errors.New("File System Handler uuid cannot be empty")
  39. }
  40. //Check if uuid is reserved by system
  41. if inSlice([]string{"user", "tmp", "share", "network"}, options.Uuid) {
  42. return errors.New("This File System Handler UUID is reserved by the system")
  43. }
  44. if !fileExists(options.Path) {
  45. return errors.New("Path not exists, given: " + options.Path)
  46. }
  47. //Check if access mode is supported
  48. if !inSlice([]string{"readonly", "readwrite"}, options.Access) {
  49. return errors.New("Not supported access mode: " + options.Access)
  50. }
  51. //Check if hierarchy is supported
  52. if !inSlice([]string{"user", "public", "backup"}, options.Hierarchy) {
  53. return errors.New("Not supported hierarchy: " + options.Hierarchy)
  54. }
  55. //Check disk format is supported
  56. if !inSlice([]string{"ext4", "ext2", "ext3", "fat", "vfat", "ntfs"}, options.Filesystem) {
  57. return errors.New("Not supported file system type: " + options.Filesystem)
  58. }
  59. //Check if mount point exists
  60. if options.Mountpt != "" && !fileExists(options.Mountpt) {
  61. return errors.New("Mount point not exists: " + options.Mountpt)
  62. }
  63. //This drive is backup drive
  64. if options.Hierarchy == "backup" {
  65. //Check if parent uid is not empty
  66. if strings.TrimSpace(options.Parentuid) == "" {
  67. return errors.New("Invalid backup source ID given")
  68. }
  69. //Check if the backup drive source and target are not the same drive
  70. if options.Parentuid == options.Uuid {
  71. return errors.New("Recursive backup detected. You cannot backup the backup drive itself.")
  72. }
  73. //Check if the backup mode exists
  74. if !inSlice([]string{"basic", "nightly", "version"}, options.BackupMode) {
  75. return errors.New("Invalid backup mode given")
  76. }
  77. }
  78. return nil
  79. }