internal.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package user
  2. import (
  3. "errors"
  4. "path/filepath"
  5. "strings"
  6. fs "imuslab.com/arozos/mod/filesystem"
  7. )
  8. /*
  9. Private functions
  10. */
  11. //Get a fs handler from a virtual path, quick function for getIDFromHandler + GetHandlerFromID
  12. func getHandlerFromVirtualPath(storages []*fs.FileSystemHandler, vpath string) (*fs.FileSystemHandler, error) {
  13. vid, _, err := getIDFromVirtualPath(vpath)
  14. if err != nil {
  15. return &fs.FileSystemHandler{}, err
  16. }
  17. return getHandlerFromID(storages, vid)
  18. }
  19. //Get a fs handler from the given virtial device id
  20. func getHandlerFromID(storages []*fs.FileSystemHandler, vid string) (*fs.FileSystemHandler, error) {
  21. for _, storage := range storages {
  22. if storage.UUID == vid {
  23. //This storage is the one we are looking at
  24. return storage, nil
  25. }
  26. }
  27. return &fs.FileSystemHandler{}, errors.New("Handler Not Found")
  28. }
  29. //Get the ID part of a virtual path, return ID, subpath and error
  30. func getIDFromVirtualPath(vpath string) (string, string, error) {
  31. if strings.Contains(vpath, ":") == false {
  32. return "", "", errors.New("Path missing Virtual Device ID. Given: " + vpath)
  33. }
  34. //Clean up the virutal path
  35. vpath = filepath.ToSlash(filepath.Clean(vpath))
  36. tmp := strings.Split(vpath, ":")
  37. vdID := tmp[0]
  38. pathSlice := tmp[1:]
  39. path := strings.Join(pathSlice, ":")
  40. return vdID, path, nil
  41. }