aofs.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. package ftp
  2. //arozos virtual path translation handler
  3. //author: tobychui
  4. import (
  5. "errors"
  6. "io/ioutil"
  7. "os"
  8. "path/filepath"
  9. "strings"
  10. "time"
  11. "github.com/spf13/afero"
  12. "imuslab.com/arozos/mod/filesystem"
  13. "imuslab.com/arozos/mod/user"
  14. )
  15. var (
  16. aofsCanRead = 1
  17. aofsCanWrite = 2
  18. )
  19. type aofs struct {
  20. userinfo *user.User
  21. tmpFolder string
  22. }
  23. func (a aofs) Create(name string) (afero.File, error) {
  24. fsh, rewritePath, err := a.pathRewrite(name)
  25. if err != nil {
  26. return nil, err
  27. }
  28. if !a.checkAllowAccess(fsh, rewritePath, aofsCanWrite) {
  29. return nil, errors.New("Permission denied")
  30. }
  31. return fsh.FileSystemAbstraction.Create(rewritePath)
  32. }
  33. func (a aofs) Chown(name string, uid, gid int) error {
  34. fsh, rewritePath, err := a.pathRewrite(name)
  35. if err != nil {
  36. return err
  37. }
  38. if !a.checkAllowAccess(fsh, rewritePath, aofsCanWrite) {
  39. return errors.New("Permission denied")
  40. }
  41. return fsh.FileSystemAbstraction.Chown(rewritePath, uid, gid)
  42. }
  43. func (a aofs) Mkdir(name string, perm os.FileMode) error {
  44. fsh, rewritePath, err := a.pathRewrite(name)
  45. if err != nil {
  46. return err
  47. }
  48. if !a.checkAllowAccess(fsh, rewritePath, aofsCanWrite) {
  49. return errors.New("Permission denied")
  50. }
  51. return fsh.FileSystemAbstraction.Mkdir(rewritePath, perm)
  52. }
  53. func (a aofs) MkdirAll(path string, perm os.FileMode) error {
  54. fsh, rewritePath, err := a.pathRewrite(path)
  55. if err != nil {
  56. return err
  57. }
  58. if !a.checkAllowAccess(fsh, rewritePath, aofsCanWrite) {
  59. return errors.New("Permission denied")
  60. }
  61. return fsh.FileSystemAbstraction.MkdirAll(rewritePath, perm)
  62. }
  63. func (a aofs) Open(name string) (afero.File, error) {
  64. //fmt.Println("FTP OPEN")
  65. fsh, rewritePath, err := a.pathRewrite(name)
  66. if err != nil {
  67. return nil, err
  68. }
  69. if !a.checkAllowAccess(fsh, rewritePath, aofsCanWrite) {
  70. return nil, errors.New("Permission denied")
  71. }
  72. return fsh.FileSystemAbstraction.Open(rewritePath)
  73. }
  74. func (a aofs) Stat(name string) (os.FileInfo, error) {
  75. //fmt.Println("FTP STAT")
  76. fsh, rewritePath, err := a.pathRewrite(name)
  77. if err != nil {
  78. return nil, err
  79. }
  80. if !a.checkAllowAccess(fsh, rewritePath, aofsCanRead) {
  81. return nil, errors.New("Permission denied")
  82. }
  83. return fsh.FileSystemAbstraction.Stat(rewritePath)
  84. }
  85. func (a aofs) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error) {
  86. //fmt.Println("FTP OPEN FILE")
  87. fsh, rewritePath, err := a.pathRewrite(name)
  88. if err != nil {
  89. return nil, err
  90. }
  91. if !a.checkAllowAccess(fsh, rewritePath, aofsCanWrite) {
  92. return nil, errors.New("Permission denied")
  93. }
  94. return fsh.FileSystemAbstraction.OpenFile(rewritePath, flag, perm)
  95. }
  96. func (a aofs) AllocateSpace(size int) error {
  97. if a.userinfo.StorageQuota.HaveSpace(int64(size)) {
  98. return nil
  99. }
  100. return errors.New("Storage Quota Fulled")
  101. }
  102. func (a aofs) Remove(name string) error {
  103. fsh, rewritePath, err := a.pathRewrite(name)
  104. if err != nil {
  105. return err
  106. }
  107. if !a.checkAllowAccess(fsh, rewritePath, aofsCanWrite) {
  108. return errors.New("Permission denied")
  109. }
  110. return fsh.FileSystemAbstraction.Remove(rewritePath)
  111. }
  112. func (a aofs) RemoveAll(path string) error {
  113. fsh, rewritePath, err := a.pathRewrite(path)
  114. if err != nil {
  115. return err
  116. }
  117. if !a.checkAllowAccess(fsh, rewritePath, aofsCanWrite) {
  118. return errors.New("Permission denied")
  119. }
  120. return fsh.FileSystemAbstraction.RemoveAll(rewritePath)
  121. }
  122. func (a aofs) Rename(oldname, newname string) error {
  123. fshsrc, rewritePathsrc, err := a.pathRewrite(oldname)
  124. if err != nil {
  125. return err
  126. }
  127. fshdest, rewritePathdest, err := a.pathRewrite(newname)
  128. if err != nil {
  129. return err
  130. }
  131. if !a.checkAllowAccess(fshsrc, rewritePathsrc, aofsCanWrite) {
  132. return errors.New("Permission denied")
  133. }
  134. if !a.checkAllowAccess(fshdest, rewritePathdest, aofsCanWrite) {
  135. return errors.New("Permission denied")
  136. }
  137. if !fshdest.FileSystemAbstraction.FileExists(filepath.Dir(rewritePathdest)) {
  138. fshdest.FileSystemAbstraction.MkdirAll(filepath.Dir(rewritePathdest), 0775)
  139. }
  140. if fshsrc.UUID == fshdest.UUID {
  141. //Renaming in same fsh
  142. return fshsrc.FileSystemAbstraction.Rename(rewritePathsrc, rewritePathdest)
  143. } else {
  144. //Cross fsh read write.
  145. f, err := fshsrc.FileSystemAbstraction.ReadStream(rewritePathsrc)
  146. if err != nil {
  147. return err
  148. }
  149. err = fshdest.FileSystemAbstraction.WriteStream(rewritePathdest, f, 0775)
  150. if err != nil {
  151. return err
  152. }
  153. f.Close()
  154. err = fshsrc.FileSystemAbstraction.RemoveAll(rewritePathsrc)
  155. if err != nil {
  156. return err
  157. }
  158. }
  159. return nil
  160. }
  161. func (a aofs) Name() string {
  162. return "arozos virtualFS"
  163. }
  164. func (a aofs) Chmod(name string, mode os.FileMode) error {
  165. fsh, rewritePath, err := a.pathRewrite(name)
  166. if err != nil {
  167. return err
  168. }
  169. if !a.checkAllowAccess(fsh, rewritePath, aofsCanWrite) {
  170. return errors.New("Permission denied")
  171. }
  172. return fsh.FileSystemAbstraction.Chmod(rewritePath, mode)
  173. }
  174. func (a aofs) Chtimes(name string, atime time.Time, mtime time.Time) error {
  175. fsh, rewritePath, err := a.pathRewrite(name)
  176. if err != nil {
  177. return err
  178. }
  179. if !a.checkAllowAccess(fsh, rewritePath, aofsCanWrite) {
  180. return errors.New("Permission denied")
  181. }
  182. return fsh.FileSystemAbstraction.Chtimes(rewritePath, atime, mtime)
  183. }
  184. //arozos adaptive functions
  185. //This function rewrite the path from ftp representation to real filepath on disk
  186. func (a aofs) pathRewrite(path string) (*filesystem.FileSystemHandler, string, error) {
  187. path = filepath.ToSlash(filepath.Clean(path))
  188. //log.Println("Original path: ", path)
  189. if path == "/" {
  190. //Roots. Show ftpbuf root
  191. fshs := a.userinfo.GetAllFileSystemHandler()
  192. for _, fsh := range fshs {
  193. //Create a folder representation for this virtual directory
  194. if !fsh.RequireBuffer {
  195. fsh.FileSystemAbstraction.Mkdir(filepath.Join(a.tmpFolder, fsh.UUID), 0755)
  196. }
  197. }
  198. readmeContent, err := ioutil.ReadFile("./system/ftp/README.txt")
  199. if err != nil {
  200. readmeContent = []byte("DO NOT UPLOAD FILES INTO THE ROOT DIRECTORY")
  201. }
  202. ioutil.WriteFile(filepath.Join(a.tmpFolder, "README.txt"), readmeContent, 0755)
  203. //Return the tmpFolder root
  204. tmpfs, _ := a.userinfo.GetFileSystemHandlerFromVirtualPath("tmp:/")
  205. return tmpfs, a.tmpFolder, nil
  206. } else if path == "/README.txt" {
  207. tmpfs, _ := a.userinfo.GetFileSystemHandlerFromVirtualPath("tmp:/")
  208. return tmpfs, a.tmpFolder + "README.txt", nil
  209. } else if len(path) > 0 {
  210. //Rewrite the path for any alternative filepath
  211. //Get the uuid of the filepath
  212. path := path[1:]
  213. subpaths := strings.Split(path, "/")
  214. fsHandlerUUID := subpaths[0]
  215. remainingPaths := subpaths[1:]
  216. fsh, err := a.userinfo.GetFileSystemHandlerFromVirtualPath(fsHandlerUUID + ":")
  217. if err != nil {
  218. return nil, "", errors.New("File System Abstraction not found")
  219. }
  220. if fsh.RequireBuffer {
  221. //Not supported
  222. return nil, "", errors.New("Buffered file system not supported by FTP driver")
  223. }
  224. rpath, err := fsh.FileSystemAbstraction.VirtualPathToRealPath(fsh.UUID+":/"+strings.Join(remainingPaths, "/"), a.userinfo.Username)
  225. if err != nil {
  226. return nil, "", errors.New("File System Handler Hierarchy not supported by FTP driver")
  227. }
  228. return fsh, rpath, nil
  229. } else {
  230. //fsh not found.
  231. return nil, "", errors.New("Invalid path")
  232. }
  233. }
  234. //Check if user has access to the given path, mode can be string {read / write}
  235. func (a aofs) checkAllowAccess(fsh *filesystem.FileSystemHandler, path string, mode int) bool {
  236. vpath, err := fsh.FileSystemAbstraction.RealPathToVirtualPath(path, a.userinfo.Username)
  237. if err != nil {
  238. return false
  239. }
  240. if mode == aofsCanRead {
  241. return a.userinfo.CanRead(vpath)
  242. } else if mode == aofsCanWrite {
  243. return a.userinfo.CanWrite(vpath)
  244. } else {
  245. return false
  246. }
  247. }