smbfs.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. package smbfs
  2. import (
  3. "fmt"
  4. "io"
  5. "io/fs"
  6. "log"
  7. "net"
  8. "os"
  9. "path/filepath"
  10. "strings"
  11. "time"
  12. "github.com/hirochachacha/go-smb2"
  13. "imuslab.com/arozos/mod/filesystem/fsdef"
  14. )
  15. /*
  16. Server Message Block.go
  17. This is a file abstraction that mount SMB folders onto ArozOS as virtual drive
  18. */
  19. type ServerMessageBlockFileSystemAbstraction struct {
  20. UUID string
  21. Hierarchy string
  22. ipaddr string
  23. user string
  24. conn *net.Conn
  25. session *smb2.Session
  26. share *smb2.Share
  27. }
  28. func NewServerMessageBlockFileSystemAbstraction(uuid string, hierarchy string, ipaddr string, rootShare string, username string, password string) (ServerMessageBlockFileSystemAbstraction, error) {
  29. conn, err := net.Dial("tcp", ipaddr)
  30. if err != nil {
  31. log.Println("[SMB-FS] Unable to connect to remote: ", err.Error())
  32. return ServerMessageBlockFileSystemAbstraction{}, err
  33. }
  34. d := &smb2.Dialer{
  35. Initiator: &smb2.NTLMInitiator{
  36. User: username,
  37. Password: password,
  38. },
  39. }
  40. s, err := d.Dial(conn)
  41. if err != nil {
  42. log.Println("[SMB-FS] Unable to connect to remote: ", err.Error())
  43. return ServerMessageBlockFileSystemAbstraction{}, err
  44. }
  45. //Mound remote storage
  46. fs, err := s.Mount(rootShare)
  47. if err != nil {
  48. log.Println("[SMB-FS] Unable to connect to remote: ", err.Error())
  49. return ServerMessageBlockFileSystemAbstraction{}, err
  50. }
  51. log.Println("[SMB-FS] Connected to remote: " + ipaddr)
  52. return ServerMessageBlockFileSystemAbstraction{
  53. UUID: uuid,
  54. Hierarchy: hierarchy,
  55. ipaddr: ipaddr,
  56. user: username,
  57. conn: &conn,
  58. session: s,
  59. share: fs,
  60. }, nil
  61. }
  62. func (a ServerMessageBlockFileSystemAbstraction) Chmod(filename string, mode os.FileMode) error {
  63. filename = filterFilepath(filename)
  64. filename = toWinPath(filename)
  65. return a.share.Chmod(filename, mode)
  66. }
  67. func (a ServerMessageBlockFileSystemAbstraction) Chown(filename string, uid int, gid int) error {
  68. return fsdef.ErrOperationNotSupported
  69. }
  70. func (a ServerMessageBlockFileSystemAbstraction) Chtimes(filename string, atime time.Time, mtime time.Time) error {
  71. filename = filterFilepath(filename)
  72. filename = toWinPath(filename)
  73. return a.share.Chtimes(filename, atime, mtime)
  74. }
  75. func (a ServerMessageBlockFileSystemAbstraction) Create(filename string) (*os.File, error) {
  76. return nil, fsdef.ErrOperationNotSupported
  77. }
  78. func (a ServerMessageBlockFileSystemAbstraction) Mkdir(filename string, mode os.FileMode) error {
  79. filename = filterFilepath(filename)
  80. filename = toWinPath(filename)
  81. return a.share.Mkdir(filename, mode)
  82. }
  83. func (a ServerMessageBlockFileSystemAbstraction) MkdirAll(filename string, mode os.FileMode) error {
  84. filename = filterFilepath(filename)
  85. filename = toWinPath(filename)
  86. return a.share.MkdirAll(filename, mode)
  87. }
  88. func (a ServerMessageBlockFileSystemAbstraction) Name() string {
  89. return ""
  90. }
  91. func (a ServerMessageBlockFileSystemAbstraction) Open(filename string) (*os.File, error) {
  92. return nil, fsdef.ErrOperationNotSupported
  93. }
  94. func (a ServerMessageBlockFileSystemAbstraction) OpenFile(filename string, flag int, perm os.FileMode) (*os.File, error) {
  95. return nil, fsdef.ErrOperationNotSupported
  96. }
  97. func (a ServerMessageBlockFileSystemAbstraction) Remove(filename string) error {
  98. filename = filterFilepath(filename)
  99. filename = toWinPath(filename)
  100. return a.share.Remove(filename)
  101. }
  102. func (a ServerMessageBlockFileSystemAbstraction) RemoveAll(filename string) error {
  103. filename = filterFilepath(filename)
  104. filename = toWinPath(filename)
  105. return a.share.RemoveAll(filename)
  106. }
  107. func (a ServerMessageBlockFileSystemAbstraction) Rename(oldname, newname string) error {
  108. oldname = toWinPath(filterFilepath(oldname))
  109. newname = toWinPath(filterFilepath(newname))
  110. return a.share.Rename(oldname, newname)
  111. }
  112. func (a ServerMessageBlockFileSystemAbstraction) Stat(filename string) (os.FileInfo, error) {
  113. filename = toWinPath(filterFilepath(filename))
  114. return a.share.Stat(filename)
  115. }
  116. func (a ServerMessageBlockFileSystemAbstraction) Close() error {
  117. a.share.Umount()
  118. a.session.Logoff()
  119. conn := *(a.conn)
  120. conn.Close()
  121. return nil
  122. }
  123. /*
  124. Abstraction Utilities
  125. */
  126. func (a ServerMessageBlockFileSystemAbstraction) VirtualPathToRealPath(subpath string, username string) (string, error) {
  127. if strings.HasPrefix(subpath, a.UUID+":") {
  128. //This is full virtual path. Trim the uuid and correct the subpath
  129. subpath = strings.TrimPrefix(subpath, a.UUID+":")
  130. }
  131. subpath = filterFilepath(subpath)
  132. if a.Hierarchy == "user" {
  133. return toWinPath(filepath.ToSlash(filepath.Clean(filepath.Join("users", username, subpath)))), nil
  134. } else if a.Hierarchy == "public" {
  135. return toWinPath(filepath.ToSlash(filepath.Clean(subpath))), nil
  136. }
  137. return "", fsdef.ErrVpathResolveFailed
  138. }
  139. func (a ServerMessageBlockFileSystemAbstraction) RealPathToVirtualPath(fullpath string, username string) (string, error) {
  140. fullpath = strings.TrimPrefix(fullpath, "\\")
  141. vpath := a.UUID + ":/" + filterFilepath(fullpath)
  142. return vpath, nil
  143. }
  144. func (a ServerMessageBlockFileSystemAbstraction) FileExists(realpath string) bool {
  145. realpath = toWinPath(filterFilepath(realpath))
  146. _, err := a.share.Open(realpath)
  147. return err == nil
  148. }
  149. func (a ServerMessageBlockFileSystemAbstraction) IsDir(realpath string) bool {
  150. realpath = filterFilepath(realpath)
  151. realpath = toWinPath(realpath)
  152. stx, err := a.share.Stat(realpath)
  153. if err != nil {
  154. return false
  155. }
  156. return stx.IsDir()
  157. }
  158. func (a ServerMessageBlockFileSystemAbstraction) Glob(realpathWildcard string) ([]string, error) {
  159. realpathWildcard = toWinPath(filterFilepath(realpathWildcard))
  160. return a.share.Glob(realpathWildcard)
  161. }
  162. func (a ServerMessageBlockFileSystemAbstraction) GetFileSize(realpath string) int64 {
  163. realpath = toWinPath(filterFilepath(realpath))
  164. stat, err := a.share.Stat(realpath)
  165. if err != nil {
  166. return 0
  167. }
  168. return stat.Size()
  169. }
  170. func (a ServerMessageBlockFileSystemAbstraction) GetModTime(realpath string) (int64, error) {
  171. realpath = toWinPath(filterFilepath(realpath))
  172. stat, err := a.share.Stat(realpath)
  173. if err != nil {
  174. return 0, nil
  175. }
  176. return stat.ModTime().Unix(), nil
  177. }
  178. func (a ServerMessageBlockFileSystemAbstraction) WriteFile(filename string, content []byte, mode os.FileMode) error {
  179. filename = toWinPath(filterFilepath(filename))
  180. return a.share.WriteFile(filename, content, mode)
  181. }
  182. func (a ServerMessageBlockFileSystemAbstraction) ReadFile(filename string) ([]byte, error) {
  183. filename = toWinPath(filterFilepath(filename))
  184. return a.share.ReadFile(filename)
  185. }
  186. func (a ServerMessageBlockFileSystemAbstraction) WriteStream(filename string, stream io.Reader, mode os.FileMode) error {
  187. filename = toWinPath(filterFilepath(filename))
  188. f, err := a.share.OpenFile(filename, os.O_CREATE|os.O_WRONLY, mode)
  189. if err != nil {
  190. return err
  191. }
  192. p := make([]byte, 1024)
  193. for {
  194. _, err := stream.Read(p)
  195. if err != nil {
  196. if err == io.EOF {
  197. break
  198. } else {
  199. return err
  200. }
  201. }
  202. _, err = f.Write(p)
  203. if err != nil {
  204. return err
  205. }
  206. }
  207. return nil
  208. }
  209. func (a ServerMessageBlockFileSystemAbstraction) ReadStream(filename string) (io.ReadCloser, error) {
  210. filename = toWinPath(filterFilepath(filename))
  211. f, err := a.share.OpenFile(filename, os.O_RDONLY, 0755)
  212. if err != nil {
  213. return nil, err
  214. }
  215. return f, nil
  216. }
  217. func (a ServerMessageBlockFileSystemAbstraction) Walk(root string, walkFn filepath.WalkFunc) error {
  218. root = toWinPath(filterFilepath(root))
  219. err := fs.WalkDir(a.share.DirFS(root), ".", func(path string, d fs.DirEntry, err error) error {
  220. statInfo, _ := d.Info()
  221. walkFn(path, statInfo, err)
  222. return nil
  223. })
  224. return err
  225. }
  226. /*
  227. Optional Functions
  228. */
  229. func (a *ServerMessageBlockFileSystemAbstraction) CapacityInfo() {
  230. fsinfo, err := a.share.Statfs(".")
  231. if err != nil {
  232. return
  233. }
  234. fmt.Println(fsinfo)
  235. }
  236. /*
  237. Helper Functions
  238. */
  239. func toWinPath(filename string) string {
  240. backslashed := strings.ReplaceAll(filename, "/", "\\")
  241. return strings.TrimPrefix(backslashed, "\\")
  242. }
  243. func filterFilepath(rawpath string) string {
  244. rawpath = filepath.ToSlash(filepath.Clean(rawpath))
  245. rawpath = strings.TrimSpace(rawpath)
  246. if strings.HasPrefix(rawpath, "./") {
  247. return rawpath[1:]
  248. } else if rawpath == "." || rawpath == "" {
  249. return "/"
  250. }
  251. return rawpath
  252. }