github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/swarm/fuse/swarmfs_util.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //
    10  //
    11  //
    12  //
    13  //
    14  //
    15  //
    16  //
    17  //
    18  //
    19  //
    20  //
    21  //
    22  //
    23  //
    24  
    25  //
    26  
    27  package fuse
    28  
    29  import (
    30  	"context"
    31  	"fmt"
    32  	"os/exec"
    33  	"runtime"
    34  
    35  	"github.com/ethereum/go-ethereum/swarm/log"
    36  )
    37  
    38  func externalUnmount(mountPoint string) error {
    39  	ctx, cancel := context.WithTimeout(context.Background(), unmountTimeout)
    40  	defer cancel()
    41  
    42  //
    43  	if err := exec.CommandContext(ctx, "umount", mountPoint).Run(); err == nil {
    44  		return nil
    45  	}
    46  //
    47  	switch runtime.GOOS {
    48  	case "darwin":
    49  		return exec.CommandContext(ctx, "diskutil", "umount", mountPoint).Run()
    50  	case "linux":
    51  		return exec.CommandContext(ctx, "fusermount", "-u", mountPoint).Run()
    52  	default:
    53  		return fmt.Errorf("swarmfs unmount: unimplemented")
    54  	}
    55  }
    56  
    57  func addFileToSwarm(sf *SwarmFile, content []byte, size int) error {
    58  	fkey, mhash, err := sf.mountInfo.swarmApi.AddFile(context.TODO(), sf.mountInfo.LatestManifest, sf.path, sf.name, content, true)
    59  	if err != nil {
    60  		return err
    61  	}
    62  
    63  	sf.lock.Lock()
    64  	defer sf.lock.Unlock()
    65  	sf.addr = fkey
    66  	sf.fileSize = int64(size)
    67  
    68  	sf.mountInfo.lock.Lock()
    69  	defer sf.mountInfo.lock.Unlock()
    70  	sf.mountInfo.LatestManifest = mhash
    71  
    72  	log.Info("swarmfs added new file:", "fname", sf.name, "new Manifest hash", mhash)
    73  	return nil
    74  }
    75  
    76  func removeFileFromSwarm(sf *SwarmFile) error {
    77  	mkey, err := sf.mountInfo.swarmApi.RemoveFile(context.TODO(), sf.mountInfo.LatestManifest, sf.path, sf.name, true)
    78  	if err != nil {
    79  		return err
    80  	}
    81  
    82  	sf.mountInfo.lock.Lock()
    83  	defer sf.mountInfo.lock.Unlock()
    84  	sf.mountInfo.LatestManifest = mkey
    85  
    86  	log.Info("swarmfs removed file:", "fname", sf.name, "new Manifest hash", mkey)
    87  	return nil
    88  }
    89  
    90  func removeDirectoryFromSwarm(sd *SwarmDir) error {
    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  func appendToExistingFileInSwarm(sf *SwarmFile, content []byte, offset int64, length int64) error {
   113  	fkey, mhash, err := sf.mountInfo.swarmApi.AppendFile(context.TODO(), sf.mountInfo.LatestManifest, sf.path, sf.name, sf.fileSize, content, sf.addr, offset, length, true)
   114  	if err != nil {
   115  		return err
   116  	}
   117  
   118  	sf.lock.Lock()
   119  	defer sf.lock.Unlock()
   120  	sf.addr = fkey
   121  	sf.fileSize = sf.fileSize + int64(len(content))
   122  
   123  	sf.mountInfo.lock.Lock()
   124  	defer sf.mountInfo.lock.Unlock()
   125  	sf.mountInfo.LatestManifest = mhash
   126  
   127  	log.Info("swarmfs appended file:", "fname", sf.name, "new Manifest hash", mhash)
   128  	return nil
   129  }