github.com/alanchchen/go-ethereum@v1.6.6-0.20170601190819-6171d01b1195/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  	"fmt"
    23  	"github.com/ethereum/go-ethereum/log"
    24  	"os/exec"
    25  	"runtime"
    26  	"time"
    27  )
    28  
    29  func externalUnMount(mountPoint string) error {
    30  
    31  	var cmd *exec.Cmd
    32  
    33  	switch runtime.GOOS {
    34  
    35  	case "darwin":
    36  		cmd = exec.Command("/usr/bin/diskutil", "umount", "force", mountPoint)
    37  
    38  	case "linux":
    39  		cmd = exec.Command("fusermount", "-u", mountPoint)
    40  
    41  	default:
    42  		return fmt.Errorf("unmount: unimplemented")
    43  	}
    44  
    45  	errc := make(chan error, 1)
    46  	go func() {
    47  		defer close(errc)
    48  
    49  		if err := exec.Command("umount", mountPoint).Run(); err == nil {
    50  			return
    51  		}
    52  		errc <- cmd.Run()
    53  	}()
    54  
    55  	select {
    56  
    57  	case <-time.After(unmountTimeout):
    58  		return fmt.Errorf("umount timeout")
    59  
    60  	case err := <-errc:
    61  		return err
    62  	}
    63  }
    64  
    65  func addFileToSwarm(sf *SwarmFile, content []byte, size int) error {
    66  
    67  	fkey, mhash, err := sf.mountInfo.swarmApi.AddFile(sf.mountInfo.LatestManifest, sf.path, sf.name, content, true)
    68  	if err != nil {
    69  		return err
    70  	}
    71  
    72  	sf.lock.Lock()
    73  	defer sf.lock.Unlock()
    74  	sf.key = fkey
    75  	sf.fileSize = int64(size)
    76  
    77  	sf.mountInfo.lock.Lock()
    78  	defer sf.mountInfo.lock.Unlock()
    79  	sf.mountInfo.LatestManifest = mhash
    80  
    81  	log.Info("Added new file:", "fname", sf.name, "New Manifest hash", mhash)
    82  	return nil
    83  
    84  }
    85  
    86  func removeFileFromSwarm(sf *SwarmFile) error {
    87  
    88  	mkey, err := sf.mountInfo.swarmApi.RemoveFile(sf.mountInfo.LatestManifest, sf.path, sf.name, true)
    89  	if err != nil {
    90  		return err
    91  	}
    92  
    93  	sf.mountInfo.lock.Lock()
    94  	defer sf.mountInfo.lock.Unlock()
    95  	sf.mountInfo.LatestManifest = mkey
    96  
    97  	log.Info("Removed file:", "fname", sf.name, "New Manifest hash", mkey)
    98  	return nil
    99  }
   100  
   101  func removeDirectoryFromSwarm(sd *SwarmDir) error {
   102  
   103  	if len(sd.directories) == 0 && len(sd.files) == 0 {
   104  		return nil
   105  	}
   106  
   107  	for _, d := range sd.directories {
   108  		err := removeDirectoryFromSwarm(d)
   109  		if err != nil {
   110  			return err
   111  		}
   112  	}
   113  
   114  	for _, f := range sd.files {
   115  		err := removeFileFromSwarm(f)
   116  		if err != nil {
   117  			return err
   118  		}
   119  	}
   120  
   121  	return nil
   122  
   123  }
   124  
   125  func appendToExistingFileInSwarm(sf *SwarmFile, content []byte, offset int64, length int64) error {
   126  
   127  	fkey, mhash, err := sf.mountInfo.swarmApi.AppendFile(sf.mountInfo.LatestManifest, sf.path, sf.name, sf.fileSize, content, sf.key, offset, length, true)
   128  	if err != nil {
   129  		return err
   130  	}
   131  
   132  	sf.lock.Lock()
   133  	defer sf.lock.Unlock()
   134  	sf.key = fkey
   135  	sf.fileSize = sf.fileSize + int64(len(content))
   136  
   137  	sf.mountInfo.lock.Lock()
   138  	defer sf.mountInfo.lock.Unlock()
   139  	sf.mountInfo.LatestManifest = mhash
   140  
   141  	log.Info("Appended file:", "fname", sf.name, "New Manifest hash", mhash)
   142  	return nil
   143  
   144  }