swarmfs_util.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2017 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. // +build linux darwin freebsd
  17. package fuse
  18. import (
  19. "context"
  20. "fmt"
  21. "os/exec"
  22. "runtime"
  23. "github.com/ethereum/go-ethereum/log"
  24. )
  25. func externalUnmount(mountPoint string) error {
  26. ctx, cancel := context.WithTimeout(context.Background(), unmountTimeout)
  27. defer cancel()
  28. // Try generic umount.
  29. if err := exec.CommandContext(ctx, "umount", mountPoint).Run(); err == nil {
  30. return nil
  31. }
  32. // Try FUSE-specific commands if umount didn't work.
  33. switch runtime.GOOS {
  34. case "darwin":
  35. return exec.CommandContext(ctx, "diskutil", "umount", "force", mountPoint).Run()
  36. case "linux":
  37. return exec.CommandContext(ctx, "fusermount", "-u", mountPoint).Run()
  38. default:
  39. return fmt.Errorf("unmount: unimplemented")
  40. }
  41. }
  42. func addFileToSwarm(sf *SwarmFile, content []byte, size int) error {
  43. fkey, mhash, err := sf.mountInfo.swarmApi.AddFile(sf.mountInfo.LatestManifest, sf.path, sf.name, content, true)
  44. if err != nil {
  45. return err
  46. }
  47. sf.lock.Lock()
  48. defer sf.lock.Unlock()
  49. sf.key = fkey
  50. sf.fileSize = int64(size)
  51. sf.mountInfo.lock.Lock()
  52. defer sf.mountInfo.lock.Unlock()
  53. sf.mountInfo.LatestManifest = mhash
  54. log.Info("Added new file:", "fname", sf.name, "New Manifest hash", mhash)
  55. return nil
  56. }
  57. func removeFileFromSwarm(sf *SwarmFile) error {
  58. mkey, err := sf.mountInfo.swarmApi.RemoveFile(sf.mountInfo.LatestManifest, sf.path, sf.name, true)
  59. if err != nil {
  60. return err
  61. }
  62. sf.mountInfo.lock.Lock()
  63. defer sf.mountInfo.lock.Unlock()
  64. sf.mountInfo.LatestManifest = mkey
  65. log.Info("Removed file:", "fname", sf.name, "New Manifest hash", mkey)
  66. return nil
  67. }
  68. func removeDirectoryFromSwarm(sd *SwarmDir) error {
  69. if len(sd.directories) == 0 && len(sd.files) == 0 {
  70. return nil
  71. }
  72. for _, d := range sd.directories {
  73. err := removeDirectoryFromSwarm(d)
  74. if err != nil {
  75. return err
  76. }
  77. }
  78. for _, f := range sd.files {
  79. err := removeFileFromSwarm(f)
  80. if err != nil {
  81. return err
  82. }
  83. }
  84. return nil
  85. }
  86. func appendToExistingFileInSwarm(sf *SwarmFile, content []byte, offset int64, length int64) error {
  87. fkey, mhash, err := sf.mountInfo.swarmApi.AppendFile(sf.mountInfo.LatestManifest, sf.path, sf.name, sf.fileSize, content, sf.key, offset, length, true)
  88. if err != nil {
  89. return err
  90. }
  91. sf.lock.Lock()
  92. defer sf.lock.Unlock()
  93. sf.key = fkey
  94. sf.fileSize = sf.fileSize + int64(len(content))
  95. sf.mountInfo.lock.Lock()
  96. defer sf.mountInfo.lock.Unlock()
  97. sf.mountInfo.LatestManifest = mhash
  98. log.Info("Appended file:", "fname", sf.name, "New Manifest hash", mhash)
  99. return nil
  100. }