storage.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. package main
  2. import (
  3. "errors"
  4. "io/ioutil"
  5. "log"
  6. "os"
  7. "path/filepath"
  8. "runtime"
  9. "strings"
  10. "imuslab.com/arozos/mod/filesystem/fsdef"
  11. "imuslab.com/arozos/mod/permission"
  12. "imuslab.com/arozos/mod/storage/bridge"
  13. fs "imuslab.com/arozos/mod/filesystem"
  14. storage "imuslab.com/arozos/mod/storage"
  15. )
  16. var (
  17. baseStoragePool *storage.StoragePool //base storage pool, all user can access these virtual roots
  18. fsHandlers []*fs.FileSystemHandler //All File system handlers. All opened handles must be registered in here
  19. //storagePools []*storage.StoragePool //All Storage pool opened
  20. bridgeManager *bridge.Record //Manager to handle bridged FSH
  21. )
  22. func StorageInit() {
  23. //Load the default handler for the user storage root
  24. if !fs.FileExists(filepath.Clean(*root_directory) + "/") {
  25. os.MkdirAll(filepath.Clean(*root_directory)+"/", 0755)
  26. }
  27. //Start loading the base storage pool
  28. err := LoadBaseStoragePool()
  29. if err != nil {
  30. panic(err)
  31. }
  32. //Create a brdige record manager
  33. bm := bridge.NewBridgeRecord("system/bridge.json")
  34. bridgeManager = bm
  35. }
  36. func LoadBaseStoragePool() error {
  37. //Use for Debian buster local file system
  38. localFileSystem := "ext4"
  39. if runtime.GOOS == "windows" {
  40. localFileSystem = "ntfs"
  41. }
  42. baseHandler, err := fs.NewFileSystemHandler(fs.FileSystemOption{
  43. Name: "User",
  44. Uuid: "user",
  45. Path: filepath.ToSlash(filepath.Clean(*root_directory)) + "/",
  46. Hierarchy: "user",
  47. Automount: false,
  48. Filesystem: localFileSystem,
  49. })
  50. if err != nil {
  51. log.Println("Failed to initiate user root storage directory: "+*root_directory, err.Error())
  52. return err
  53. }
  54. fsHandlers = append(fsHandlers, baseHandler)
  55. //Load the tmp folder as storage unit
  56. tmpHandler, err := fs.NewFileSystemHandler(fs.FileSystemOption{
  57. Name: "tmp",
  58. Uuid: "tmp",
  59. Path: filepath.ToSlash(filepath.Clean(*tmp_directory)) + "/",
  60. Hierarchy: "user",
  61. Automount: false,
  62. Filesystem: localFileSystem,
  63. })
  64. if err != nil {
  65. log.Println("Failed to initiate tmp storage directory: "+*tmp_directory, err.Error())
  66. return err
  67. }
  68. fsHandlers = append(fsHandlers, tmpHandler)
  69. //Load all the storage config from file
  70. rawConfig, err := ioutil.ReadFile(*storage_config_file)
  71. if err != nil {
  72. //File not found. Use internal storage only
  73. log.Println("Storage configuration file not found. Using internal storage only.")
  74. } else {
  75. //Configuration loaded. Initializing handler
  76. externalHandlers, err := fs.NewFileSystemHandlersFromJSON(rawConfig)
  77. if err != nil {
  78. log.Println("Failed to load storage configuration: " + err.Error() + " -- Skipping")
  79. } else {
  80. for _, thisHandler := range externalHandlers {
  81. fsHandlers = append(fsHandlers, thisHandler)
  82. log.Println(thisHandler.Name + " Mounted as " + thisHandler.UUID + ":/")
  83. }
  84. }
  85. }
  86. //Create a base storage pool for all users
  87. sp, err := storage.NewStoragePool(fsHandlers, "system")
  88. if err != nil {
  89. log.Println("Failed to create base Storaeg Pool")
  90. return err
  91. }
  92. //Update the storage pool permission to readwrite
  93. sp.OtherPermission = fsdef.FsReadWrite
  94. baseStoragePool = sp
  95. return nil
  96. }
  97. //Initialize group storage pool
  98. func GroupStoragePoolInit() {
  99. //Mount permission groups
  100. for _, pg := range permissionHandler.PermissionGroups {
  101. //For each group, check does this group has a config file
  102. err := LoadStoragePoolForGroup(pg)
  103. if err != nil {
  104. continue
  105. }
  106. //Do something else, WIP
  107. }
  108. //Start editing interface for Storage Pool Editor
  109. StoragePoolEditorInit()
  110. }
  111. func LoadStoragePoolForGroup(pg *permission.PermissionGroup) error {
  112. expectedConfigPath := "./system/storage/" + pg.Name + ".json"
  113. if fs.FileExists(expectedConfigPath) {
  114. //Read the config file
  115. pgStorageConfig, err := ioutil.ReadFile(expectedConfigPath)
  116. if err != nil {
  117. log.Println("Failed to read config for " + pg.Name + ": " + err.Error())
  118. return errors.New("Failed to read config for " + pg.Name + ": " + err.Error())
  119. }
  120. //Generate fsHandler form json
  121. thisGroupFsHandlers, err := fs.NewFileSystemHandlersFromJSON(pgStorageConfig)
  122. if err != nil {
  123. log.Println("Failed to load storage configuration: " + err.Error())
  124. return errors.New("Failed to load storage configuration: " + err.Error())
  125. }
  126. //Add these to mounted handlers
  127. for _, thisHandler := range thisGroupFsHandlers {
  128. fsHandlers = append(fsHandlers, thisHandler)
  129. log.Println(thisHandler.Name + " Mounted as " + thisHandler.UUID + ":/ for group " + pg.Name)
  130. }
  131. //Create a storage pool from these handlers
  132. sp, err := storage.NewStoragePool(thisGroupFsHandlers, pg.Name)
  133. if err != nil {
  134. log.Println("Failed to create storage pool for " + pg.Name)
  135. return errors.New("Failed to create storage pool for " + pg.Name)
  136. }
  137. //Set other permission to denied by default
  138. sp.OtherPermission = fsdef.FsDenied
  139. //Assign storage pool to group
  140. pg.StoragePool = sp
  141. } else {
  142. //Storage configuration not exists. Fill in the basic information and move to next storage pool
  143. //Create a new empty storage pool for this group
  144. sp, err := storage.NewStoragePool([]*fs.FileSystemHandler{}, pg.Name)
  145. if err != nil {
  146. log.Println("Failed to create empty storage pool for group: ", pg.Name)
  147. }
  148. pg.StoragePool = sp
  149. pg.StoragePool.OtherPermission = fsdef.FsDenied
  150. }
  151. return nil
  152. }
  153. //Check if a storage pool exists by its group owner name
  154. func StoragePoolExists(poolOwner string) bool {
  155. _, err := GetStoragePoolByOwner(poolOwner)
  156. return err == nil
  157. }
  158. func GetAllStoragePools() []*storage.StoragePool {
  159. //Append the base pool
  160. results := []*storage.StoragePool{baseStoragePool}
  161. //Add each permissionGroup's pool
  162. for _, pg := range permissionHandler.PermissionGroups {
  163. results = append(results, pg.StoragePool)
  164. }
  165. return results
  166. }
  167. func GetStoragePoolByOwner(owner string) (*storage.StoragePool, error) {
  168. sps := GetAllStoragePools()
  169. for _, pool := range sps {
  170. if pool.Owner == owner {
  171. return pool, nil
  172. }
  173. }
  174. return nil, errors.New("Storage pool owned by " + owner + " not found")
  175. }
  176. func GetFSHandlerSubpathFromVpath(vpath string) (*fs.FileSystemHandler, string, error) {
  177. VirtualRootID, subpath, err := fs.GetIDFromVirtualPath(vpath)
  178. if err != nil {
  179. return nil, "", errors.New("Unable to resolve requested path: " + err.Error())
  180. }
  181. fsh, err := GetFsHandlerByUUID(VirtualRootID)
  182. if err != nil {
  183. return nil, "", errors.New("Unable to resolve requested path: " + err.Error())
  184. }
  185. if fsh == nil || fsh.FileSystemAbstraction == nil {
  186. return nil, "", errors.New("Unable to resolve requested path: " + err.Error())
  187. }
  188. return fsh, subpath, nil
  189. }
  190. func GetFsHandlerByUUID(uuid string) (*fs.FileSystemHandler, error) {
  191. //Filter out the :/ fropm uuid if exists
  192. if strings.Contains(uuid, ":") {
  193. uuid = strings.Split(uuid, ":")[0]
  194. }
  195. for _, fsh := range fsHandlers {
  196. if fsh.UUID == uuid && !fsh.Closed {
  197. return fsh, nil
  198. }
  199. }
  200. return nil, errors.New("Filesystem handler with given UUID not found")
  201. }
  202. func RegisterStorageSettings() {
  203. //Storage Pool Configuration
  204. registerSetting(settingModule{
  205. Name: "Storage Pools",
  206. Desc: "Storage Pool Mounting Configuration",
  207. IconPath: "SystemAO/disk/smart/img/small_icon.png",
  208. Group: "Disk",
  209. StartDir: "SystemAO/storage/poolList.html",
  210. RequireAdmin: true,
  211. })
  212. }
  213. //CloseAllStorages Close all storage database
  214. func CloseAllStorages() {
  215. for _, fsh := range fsHandlers {
  216. fsh.FilesystemDatabase.Close()
  217. }
  218. }
  219. func closeAllStoragePools() {
  220. for _, sp := range GetAllStoragePools() {
  221. sp.Close()
  222. }
  223. }