github.com/m3shine/gochain@v2.2.26+incompatible/swarm/fuse/swarmfs_util.go (about) 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 17 // +build linux darwin freebsd 18 19 package fuse 20 21 import ( 22 "context" 23 "fmt" 24 "os/exec" 25 "runtime" 26 27 "github.com/gochain-io/gochain/log" 28 ) 29 30 func externalUnmount(mountPoint string) error { 31 ctx, cancel := context.WithTimeout(context.Background(), unmountTimeout) 32 defer cancel() 33 34 // Try generic umount. 35 if err := exec.CommandContext(ctx, "umount", mountPoint).Run(); err == nil { 36 return nil 37 } 38 // Try FUSE-specific commands if umount didn't work. 39 switch runtime.GOOS { 40 case "darwin": 41 return exec.CommandContext(ctx, "diskutil", "umount", "force", mountPoint).Run() 42 case "linux": 43 return exec.CommandContext(ctx, "fusermount", "-u", mountPoint).Run() 44 default: 45 return fmt.Errorf("unmount: unimplemented") 46 } 47 } 48 49 func addFileToSwarm(sf *SwarmFile, content []byte, size int) error { 50 sf.mountInfo.LatestManifestMu.RLock() 51 lm := sf.mountInfo.LatestManifest 52 sf.mountInfo.LatestManifestMu.RUnlock() 53 fkey, mhash, err := sf.mountInfo.swarmApi.AddFile(lm, sf.path, sf.name, content, true) 54 if err != nil { 55 return err 56 } 57 58 sf.lock.Lock() 59 defer sf.lock.Unlock() 60 sf.key = fkey 61 sf.fileSize = int64(size) 62 63 sf.mountInfo.LatestManifestMu.Lock() 64 defer sf.mountInfo.LatestManifestMu.Unlock() 65 sf.mountInfo.LatestManifest = mhash 66 67 log.Info("Added new file:", "fname", sf.name, "New Manifest hash", mhash) 68 return nil 69 70 } 71 72 func removeFileFromSwarm(sf *SwarmFile) error { 73 sf.mountInfo.LatestManifestMu.RLock() 74 lm := sf.mountInfo.LatestManifest 75 sf.mountInfo.LatestManifestMu.RUnlock() 76 mkey, err := sf.mountInfo.swarmApi.RemoveFile(lm, sf.path, sf.name, true) 77 if err != nil { 78 return err 79 } 80 81 sf.mountInfo.LatestManifestMu.Lock() 82 defer sf.mountInfo.LatestManifestMu.Unlock() 83 sf.mountInfo.LatestManifest = mkey 84 85 log.Info("Removed file:", "fname", sf.name, "New Manifest hash", mkey) 86 return nil 87 } 88 89 func removeDirectoryFromSwarm(sd *SwarmDir) error { 90 91 if len(sd.directories) == 0 && len(sd.files) == 0 { 92 return nil 93 } 94 95 for _, d := range sd.directories { 96 err := removeDirectoryFromSwarm(d) 97 if err != nil { 98 return err 99 } 100 } 101 102 for _, f := range sd.files { 103 err := removeFileFromSwarm(f) 104 if err != nil { 105 return err 106 } 107 } 108 109 return nil 110 111 } 112 113 func appendToExistingFileInSwarm(sf *SwarmFile, content []byte, offset int64, length int64) error { 114 sf.mountInfo.LatestManifestMu.RLock() 115 lm := sf.mountInfo.LatestManifest 116 sf.mountInfo.LatestManifestMu.RUnlock() 117 fkey, mhash, err := sf.mountInfo.swarmApi.AppendFile(lm, sf.path, sf.name, sf.fileSize, content, sf.key, offset, length, true) 118 if err != nil { 119 return err 120 } 121 122 sf.lock.Lock() 123 defer sf.lock.Unlock() 124 sf.key = fkey 125 sf.fileSize = sf.fileSize + int64(len(content)) 126 127 sf.mountInfo.LatestManifestMu.Lock() 128 defer sf.mountInfo.LatestManifestMu.Unlock() 129 sf.mountInfo.LatestManifest = mhash 130 131 log.Info("Appended file:", "fname", sf.name, "New Manifest hash", mhash) 132 return nil 133 134 }