config.go 3.5 KB

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