config.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package filesystem
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "imuslab.com/arozos/mod/filesystem/fsdef"
  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. Username string `json:"username,omitempty"` //Username if the storage require auth
  19. Password string `json:"password,omitempty"` //Password if the storage require auth
  20. }
  21. //Parse a list of StorageConfig from the given json content
  22. func loadConfigFromJSON(jsonContent []byte) ([]FileSystemOption, error) {
  23. storageInConfig := []FileSystemOption{}
  24. //Try to parse the JSON content
  25. err := json.Unmarshal(jsonContent, &storageInConfig)
  26. return storageInConfig, err
  27. }
  28. //Validate if the given options are correct
  29. func ValidateOption(options *FileSystemOption) error {
  30. //Check if path exists
  31. if options.Name == "" {
  32. return errors.New("File System Handler name cannot be empty")
  33. }
  34. if options.Uuid == "" {
  35. return errors.New("File System Handler uuid cannot be empty")
  36. }
  37. //Check if uuid is reserved by system
  38. if inSlice([]string{"user", "tmp", "network"}, options.Uuid) {
  39. return errors.New("This File System Handler UUID is reserved by the system")
  40. }
  41. if !FileExists(options.Path) && !fsdef.IsNetworkDrive(options.Filesystem) {
  42. return errors.New("Path not exists, given: " + options.Path)
  43. }
  44. //Check if access mode is supported
  45. if !inSlice([]string{fsdef.FsReadOnly, fsdef.FsReadWrite}, options.Access) {
  46. return errors.New("Not supported access mode: " + options.Access)
  47. }
  48. //Check if hierarchy is supported
  49. if !inSlice([]string{"user", "public"}, options.Hierarchy) {
  50. return errors.New("Not supported hierarchy: " + options.Hierarchy)
  51. }
  52. //Check disk format is supported
  53. if !inSlice([]string{"ext4", "ext2", "ext3", "fat", "vfat", "ntfs", "webdav", "ftp"}, options.Filesystem) {
  54. return errors.New("Not supported file system type: " + options.Filesystem)
  55. }
  56. //Check if mount point exists
  57. if options.Mountpt != "" {
  58. return errors.New("Mount point cannot be empty")
  59. }
  60. return nil
  61. }