storage.go 8.2 KB

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