github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/swarm/fuse/swarmfs_util.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 12:09:47</date> 10 //</624342671556612096> 11 12 // 13 // 14 // 15 // 16 // 17 // 18 // 19 // 20 // 21 // 22 // 23 // 24 // 25 // 26 // 27 28 // 29 30 package fuse 31 32 import ( 33 "context" 34 "fmt" 35 "os/exec" 36 "runtime" 37 38 "github.com/ethereum/go-ethereum/swarm/log" 39 ) 40 41 func externalUnmount(mountPoint string) error { 42 ctx, cancel := context.WithTimeout(context.Background(), unmountTimeout) 43 defer cancel() 44 45 // 46 if err := exec.CommandContext(ctx, "umount", mountPoint).Run(); err == nil { 47 return nil 48 } 49 // 50 switch runtime.GOOS { 51 case "darwin": 52 return exec.CommandContext(ctx, "diskutil", "umount", mountPoint).Run() 53 case "linux": 54 return exec.CommandContext(ctx, "fusermount", "-u", mountPoint).Run() 55 default: 56 return fmt.Errorf("swarmfs unmount: unimplemented") 57 } 58 } 59 60 func addFileToSwarm(sf *SwarmFile, content []byte, size int) error { 61 fkey, mhash, err := sf.mountInfo.swarmApi.AddFile(context.TODO(), sf.mountInfo.LatestManifest, sf.path, sf.name, content, true) 62 if err != nil { 63 return err 64 } 65 66 sf.lock.Lock() 67 defer sf.lock.Unlock() 68 sf.addr = fkey 69 sf.fileSize = int64(size) 70 71 sf.mountInfo.lock.Lock() 72 defer sf.mountInfo.lock.Unlock() 73 sf.mountInfo.LatestManifest = mhash 74 75 log.Info("swarmfs added new file:", "fname", sf.name, "new Manifest hash", mhash) 76 return nil 77 } 78 79 func removeFileFromSwarm(sf *SwarmFile) error { 80 mkey, err := sf.mountInfo.swarmApi.RemoveFile(context.TODO(), sf.mountInfo.LatestManifest, sf.path, sf.name, true) 81 if err != nil { 82 return err 83 } 84 85 sf.mountInfo.lock.Lock() 86 defer sf.mountInfo.lock.Unlock() 87 sf.mountInfo.LatestManifest = mkey 88 89 log.Info("swarmfs removed file:", "fname", sf.name, "new Manifest hash", mkey) 90 return nil 91 } 92 93 func removeDirectoryFromSwarm(sd *SwarmDir) error { 94 if len(sd.directories) == 0 && len(sd.files) == 0 { 95 return nil 96 } 97 98 for _, d := range sd.directories { 99 err := removeDirectoryFromSwarm(d) 100 if err != nil { 101 return err 102 } 103 } 104 105 for _, f := range sd.files { 106 err := removeFileFromSwarm(f) 107 if err != nil { 108 return err 109 } 110 } 111 112 return nil 113 } 114 115 func appendToExistingFileInSwarm(sf *SwarmFile, content []byte, offset int64, length int64) error { 116 fkey, mhash, err := sf.mountInfo.swarmApi.AppendFile(context.TODO(), sf.mountInfo.LatestManifest, sf.path, sf.name, sf.fileSize, content, sf.addr, offset, length, true) 117 if err != nil { 118 return err 119 } 120 121 sf.lock.Lock() 122 defer sf.lock.Unlock() 123 sf.addr = fkey 124 sf.fileSize = sf.fileSize + int64(len(content)) 125 126 sf.mountInfo.lock.Lock() 127 defer sf.mountInfo.lock.Unlock() 128 sf.mountInfo.LatestManifest = mhash 129 130 log.Info("swarmfs appended file:", "fname", sf.name, "new Manifest hash", mhash) 131 return nil 132 } 133