github.com/waltonchain/waltonchain_gwtc_src@v1.1.4-0.20201225072101-8a298c95a819/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-wtc 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-wtc 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/wtc/go-wtc/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  
    51  	fkey, mhash, err := sf.mountInfo.swarmApi.AddFile(sf.mountInfo.LatestManifest, sf.path, sf.name, content, true)
    52  	if err != nil {
    53  		return err
    54  	}
    55  
    56  	sf.lock.Lock()
    57  	defer sf.lock.Unlock()
    58  	sf.key = fkey
    59  	sf.fileSize = int64(size)
    60  
    61  	sf.mountInfo.lock.Lock()
    62  	defer sf.mountInfo.lock.Unlock()
    63  	sf.mountInfo.LatestManifest = mhash
    64  
    65  	log.Info("Added new file:", "fname", sf.name, "New Manifest hash", mhash)
    66  	return nil
    67  
    68  }
    69  
    70  func removeFileFromSwarm(sf *SwarmFile) error {
    71  
    72  	mkey, err := sf.mountInfo.swarmApi.RemoveFile(sf.mountInfo.LatestManifest, sf.path, sf.name, true)
    73  	if err != nil {
    74  		return err
    75  	}
    76  
    77  	sf.mountInfo.lock.Lock()
    78  	defer sf.mountInfo.lock.Unlock()
    79  	sf.mountInfo.LatestManifest = mkey
    80  
    81  	log.Info("Removed file:", "fname", sf.name, "New Manifest hash", mkey)
    82  	return nil
    83  }
    84  
    85  func removeDirectoryFromSwarm(sd *SwarmDir) error {
    86  
    87  	if len(sd.directories) == 0 && len(sd.files) == 0 {
    88  		return nil
    89  	}
    90  
    91  	for _, d := range sd.directories {
    92  		err := removeDirectoryFromSwarm(d)
    93  		if err != nil {
    94  			return err
    95  		}
    96  	}
    97  
    98  	for _, f := range sd.files {
    99  		err := removeFileFromSwarm(f)
   100  		if err != nil {
   101  			return err
   102  		}
   103  	}
   104  
   105  	return nil
   106  
   107  }
   108  
   109  func appendToExistingFileInSwarm(sf *SwarmFile, content []byte, offset int64, length int64) error {
   110  
   111  	fkey, mhash, err := sf.mountInfo.swarmApi.AppendFile(sf.mountInfo.LatestManifest, sf.path, sf.name, sf.fileSize, content, sf.key, offset, length, true)
   112  	if err != nil {
   113  		return err
   114  	}
   115  
   116  	sf.lock.Lock()
   117  	defer sf.lock.Unlock()
   118  	sf.key = fkey
   119  	sf.fileSize = sf.fileSize + int64(len(content))
   120  
   121  	sf.mountInfo.lock.Lock()
   122  	defer sf.mountInfo.lock.Unlock()
   123  	sf.mountInfo.LatestManifest = mhash
   124  
   125  	log.Info("Appended file:", "fname", sf.name, "New Manifest hash", mhash)
   126  	return nil
   127  
   128  }