filesystem.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. package filesystem
  2. /*
  3. ArOZ Online File System Handler Wrappers
  4. author: tobychui
  5. This is a module design to do the followings
  6. 1. Mount / Create a fs when open
  7. 2. Provide the basic function and operations of a file system type
  8. 3. THIS MODULE **SHOULD NOT CONTAIN** CROSS FILE SYSTEM TYPE OPERATIONS
  9. */
  10. import (
  11. "errors"
  12. "log"
  13. "os"
  14. "path/filepath"
  15. "strings"
  16. "time"
  17. db "imuslab.com/arozos/mod/database"
  18. "imuslab.com/arozos/mod/filesystem/hybridBackup"
  19. )
  20. //Options for creating new file system handler
  21. /*
  22. type FileSystemOpeningOptions struct{
  23. Name string `json:"name"` //Display name of this device
  24. Uuid string `json:"uuid"` //UUID of this device, e.g. S1
  25. Path string `json:"path"` //Path for the storage root
  26. Access string `json:"access,omitempty"` //Access right, allow {readonly, readwrite}
  27. Hierarchy string `json:"hierarchy"` //Folder hierarchy, allow {public, user}
  28. Automount bool `json:"automount"` //Automount this device if exists
  29. Filesystem string `json:"filesystem,omitempty"` //Support {"ext4","ext2", "ext3", "fat", "vfat", "ntfs"}
  30. Mountdev string `json:"mountdev,omitempty"` //Device file (e.g. /dev/sda1)
  31. Mountpt string `json:"mountpt,omitempty"` //Device mount point (e.g. /media/storage1)
  32. }
  33. */
  34. /*
  35. An interface for storing data related to a specific hierarchy settings.
  36. Example like the account information of network drive,
  37. backup mode of backup drive etc
  38. */
  39. type HierarchySpecificConfig interface{}
  40. //System Handler for returing
  41. type FileSystemHandler struct {
  42. Name string
  43. UUID string
  44. Path string
  45. Hierarchy string
  46. HierarchyConfig HierarchySpecificConfig
  47. ReadOnly bool
  48. Parentuid string
  49. InitiationTime int64
  50. FilesystemDatabase *db.Database
  51. Filesystem string
  52. Closed bool
  53. }
  54. //Create a list of file system handler from the given json content
  55. func NewFileSystemHandlersFromJSON(jsonContent []byte) ([]*FileSystemHandler, error) {
  56. //Generate a list of handler option from json file
  57. options, err := loadConfigFromJSON(jsonContent)
  58. if err != nil {
  59. return []*FileSystemHandler{}, err
  60. }
  61. resultingHandlers := []*FileSystemHandler{}
  62. for _, option := range options {
  63. thisHandler, err := NewFileSystemHandler(option)
  64. if err != nil {
  65. log.Println("Failed to create system handler for " + option.Name)
  66. log.Println(err.Error())
  67. continue
  68. }
  69. resultingHandlers = append(resultingHandlers, thisHandler)
  70. }
  71. return resultingHandlers, nil
  72. }
  73. //Create a new file system handler with the given config
  74. func NewFileSystemHandler(option FileSystemOption) (*FileSystemHandler, error) {
  75. fstype := strings.ToLower(option.Filesystem)
  76. if inSlice([]string{"ext4", "ext2", "ext3", "fat", "vfat", "ntfs"}, fstype) || fstype == "" {
  77. //Check if the target fs require mounting
  78. if option.Automount == true {
  79. err := MountDevice(option.Mountpt, option.Mountdev, option.Filesystem)
  80. if err != nil {
  81. return &FileSystemHandler{}, err
  82. }
  83. }
  84. //Check if the path exists
  85. if !fileExists(option.Path) {
  86. return &FileSystemHandler{}, errors.New("Mount point not exists!")
  87. }
  88. //Handle Hierarchy branching
  89. var hierarchySpecificConfig interface{} = nil
  90. if option.Hierarchy == "user" {
  91. //Create user hierarchy for this virtual device
  92. os.MkdirAll(filepath.ToSlash(filepath.Clean(option.Path))+"/users", 0755)
  93. }
  94. if option.Hierarchy == "backup" {
  95. //Backup disk. Create an Hierarchy Config for this drive
  96. hierarchySpecificConfig = hybridBackup.BackupConfig{
  97. DiskUID: option.Uuid,
  98. ParentUID: option.Parentuid,
  99. ParentMountPoint: option.Mountpt,
  100. Mode: option.BackupMode,
  101. }
  102. }
  103. //Create the fsdb for this handler
  104. fsdb, err := db.NewDatabase(filepath.ToSlash(filepath.Join(filepath.Clean(option.Path), "aofs.db")), false)
  105. if err != nil {
  106. return &FileSystemHandler{}, errors.New("Unable to create fsdb inside the target path. Is the directory read only?")
  107. }
  108. return &FileSystemHandler{
  109. Name: option.Name,
  110. UUID: option.Uuid,
  111. Path: filepath.ToSlash(filepath.Clean(option.Path)) + "/",
  112. ReadOnly: option.Access == "readonly",
  113. Parentuid: option.Parentuid,
  114. Hierarchy: option.Hierarchy,
  115. HierarchyConfig: hierarchySpecificConfig,
  116. InitiationTime: time.Now().Unix(),
  117. FilesystemDatabase: fsdb,
  118. Filesystem: fstype,
  119. Closed: false,
  120. }, nil
  121. }
  122. return nil, errors.New("Not supported file system: " + fstype)
  123. }
  124. //Create a file ownership record
  125. func (fsh *FileSystemHandler) CreateFileRecord(realpath string, owner string) error {
  126. rpabs, _ := filepath.Abs(realpath)
  127. fsrabs, _ := filepath.Abs(fsh.Path)
  128. reldir, err := filepath.Rel(fsrabs, rpabs)
  129. if err != nil {
  130. return err
  131. }
  132. fsh.FilesystemDatabase.NewTable("owner")
  133. fsh.FilesystemDatabase.Write("owner", "owner/"+reldir, owner)
  134. return nil
  135. }
  136. //Read the owner of a file
  137. func (fsh *FileSystemHandler) GetFileRecord(realpath string) (string, error) {
  138. rpabs, _ := filepath.Abs(realpath)
  139. fsrabs, _ := filepath.Abs(fsh.Path)
  140. reldir, err := filepath.Rel(fsrabs, rpabs)
  141. if err != nil {
  142. return "", err
  143. }
  144. fsh.FilesystemDatabase.NewTable("owner")
  145. if fsh.FilesystemDatabase.KeyExists("owner", "owner/"+reldir) {
  146. owner := ""
  147. fsh.FilesystemDatabase.Read("owner", "owner/"+reldir, &owner)
  148. return owner, nil
  149. } else {
  150. return "", errors.New("Owner not exists")
  151. }
  152. }
  153. //Delete a file ownership record
  154. func (fsh *FileSystemHandler) DeleteFileRecord(realpath string) error {
  155. rpabs, _ := filepath.Abs(realpath)
  156. fsrabs, _ := filepath.Abs(fsh.Path)
  157. reldir, err := filepath.Rel(fsrabs, rpabs)
  158. if err != nil {
  159. return err
  160. }
  161. fsh.FilesystemDatabase.NewTable("owner")
  162. if fsh.FilesystemDatabase.KeyExists("owner", "owner/"+reldir) {
  163. fsh.FilesystemDatabase.Delete("owner", "owner/"+reldir)
  164. }
  165. return nil
  166. }
  167. func (fsh *FileSystemHandler) Close() {
  168. fsh.FilesystemDatabase.Close()
  169. }
  170. //Helper function
  171. func inSlice(slice []string, val string) bool {
  172. for _, item := range slice {
  173. if item == val {
  174. return true
  175. }
  176. }
  177. return false
  178. }
  179. func FileExists(filename string) bool {
  180. return fileExists(filename)
  181. }
  182. //Check if file exists
  183. func fileExists(filename string) bool {
  184. _, err := os.Stat(filename)
  185. if os.IsNotExist(err) {
  186. return false
  187. }
  188. return true
  189. }