storage.go 8.2 KB

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