storage.go 8.2 KB

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