storage.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package main
  2. import (
  3. "io/ioutil"
  4. "log"
  5. "os"
  6. "path/filepath"
  7. fs "imuslab.com/aroz_online/mod/filesystem"
  8. storage "imuslab.com/aroz_online/mod/storage"
  9. )
  10. var (
  11. baseStoragePool *storage.StoragePool
  12. fsHandlers []*fs.FileSystemHandler
  13. //grpHandlers []*fs.FileSystemHandler
  14. )
  15. func StorageInit(){
  16. //Load the default handler for the user storage root
  17. if !fileExists(filepath.Clean(*root_directory) + "/"){
  18. os.MkdirAll(filepath.Clean(*root_directory) + "/", 0755)
  19. }
  20. baseHandler, err := fs.NewFileSystemHandler(fs.FileSystemOption{
  21. Name: "User",
  22. Uuid: "user",
  23. Path: filepath.ToSlash(filepath.Clean(*root_directory)) + "/",
  24. Hierarchy: "user",
  25. Automount: false,
  26. Filesystem: "ext4",
  27. })
  28. if err != nil{
  29. log.Println("Failed to initiate user root storage directory: " + *root_directory)
  30. panic(err)
  31. }
  32. fsHandlers = append(fsHandlers, baseHandler);
  33. //Load the tmp folder as storage unit
  34. tmpHandler, err := fs.NewFileSystemHandler(fs.FileSystemOption{
  35. Name: "tmp",
  36. Uuid: "tmp",
  37. Path: filepath.ToSlash(filepath.Clean(*tmp_directory)) + "/",
  38. Hierarchy: "user",
  39. Automount: false,
  40. Filesystem: "ext4",
  41. })
  42. if err != nil{
  43. log.Println("Failed to initiate tmp storage directory: " + *tmp_directory)
  44. panic(err)
  45. }
  46. fsHandlers = append(fsHandlers, tmpHandler);
  47. //Load all the storage config from file
  48. rawConfig, err := ioutil.ReadFile(*storage_config_file)
  49. if (err != nil){
  50. //File not found. Use internal storage only
  51. log.Println("Storage configuration file not found. Using internal storage only.")
  52. }else{
  53. //Configuration loaded. Initializing handler
  54. externalHandlers, err := fs.NewFileSystemHandlersFromJSON(rawConfig);
  55. if err != nil{
  56. log.Println("Failed to load storage configuration: " + err.Error() + " -- Skipping")
  57. }else{
  58. for _, thisHandler := range externalHandlers{
  59. fsHandlers = append(fsHandlers, thisHandler);
  60. log.Println(thisHandler.Name + " Mounted as " + thisHandler.UUID + ":/")
  61. }
  62. }
  63. }
  64. //Create a base storage pool for all users
  65. sp, err := storage.NewStoragePool(fsHandlers, "system");
  66. if err != nil{
  67. log.Println("Failed to create base Storaeg Pool")
  68. panic(err.Error())
  69. return
  70. }
  71. //Update the storage pool permission to readwrite
  72. sp.OtherPermission = "readwrite"
  73. baseStoragePool = sp
  74. //Mount permission group's storage pool
  75. //WIP
  76. }
  77. func CloseAllStorages(){
  78. for _, fsh := range fsHandlers{
  79. fsh.FilesystemDatabase.Close();
  80. }
  81. }