github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/swarm/fuse/swarmfs_util.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  // +build linux darwin freebsd
    13  
    14  package fuse
    15  
    16  import (
    17  	"context"
    18  	"fmt"
    19  	"os/exec"
    20  	"runtime"
    21  
    22  	"github.com/Sberex/go-sberex/log"
    23  )
    24  
    25  func externalUnmount(mountPoint string) error {
    26  	ctx, cancel := context.WithTimeout(context.Background(), unmountTimeout)
    27  	defer cancel()
    28  
    29  	// Try generic umount.
    30  	if err := exec.CommandContext(ctx, "umount", mountPoint).Run(); err == nil {
    31  		return nil
    32  	}
    33  	// Try FUSE-specific commands if umount didn't work.
    34  	switch runtime.GOOS {
    35  	case "darwin":
    36  		return exec.CommandContext(ctx, "diskutil", "umount", "force", mountPoint).Run()
    37  	case "linux":
    38  		return exec.CommandContext(ctx, "fusermount", "-u", mountPoint).Run()
    39  	default:
    40  		return fmt.Errorf("unmount: unimplemented")
    41  	}
    42  }
    43  
    44  func addFileToSwarm(sf *SwarmFile, content []byte, size int) error {
    45  	fkey, mhash, err := sf.mountInfo.swarmApi.AddFile(sf.mountInfo.LatestManifest, sf.path, sf.name, content, true)
    46  	if err != nil {
    47  		return err
    48  	}
    49  
    50  	sf.lock.Lock()
    51  	defer sf.lock.Unlock()
    52  	sf.key = fkey
    53  	sf.fileSize = int64(size)
    54  
    55  	sf.mountInfo.lock.Lock()
    56  	defer sf.mountInfo.lock.Unlock()
    57  	sf.mountInfo.LatestManifest = mhash
    58  
    59  	log.Info("Added new file:", "fname", sf.name, "New Manifest hash", mhash)
    60  	return nil
    61  }
    62  
    63  func removeFileFromSwarm(sf *SwarmFile) error {
    64  	mkey, err := sf.mountInfo.swarmApi.RemoveFile(sf.mountInfo.LatestManifest, sf.path, sf.name, true)
    65  	if err != nil {
    66  		return err
    67  	}
    68  
    69  	sf.mountInfo.lock.Lock()
    70  	defer sf.mountInfo.lock.Unlock()
    71  	sf.mountInfo.LatestManifest = mkey
    72  
    73  	log.Info("Removed file:", "fname", sf.name, "New Manifest hash", mkey)
    74  	return nil
    75  }
    76  
    77  func removeDirectoryFromSwarm(sd *SwarmDir) error {
    78  	if len(sd.directories) == 0 && len(sd.files) == 0 {
    79  		return nil
    80  	}
    81  
    82  	for _, d := range sd.directories {
    83  		err := removeDirectoryFromSwarm(d)
    84  		if err != nil {
    85  			return err
    86  		}
    87  	}
    88  
    89  	for _, f := range sd.files {
    90  		err := removeFileFromSwarm(f)
    91  		if err != nil {
    92  			return err
    93  		}
    94  	}
    95  
    96  	return nil
    97  }
    98  
    99  func appendToExistingFileInSwarm(sf *SwarmFile, content []byte, offset int64, length int64) error {
   100  	fkey, mhash, err := sf.mountInfo.swarmApi.AppendFile(sf.mountInfo.LatestManifest, sf.path, sf.name, sf.fileSize, content, sf.key, offset, length, true)
   101  	if err != nil {
   102  		return err
   103  	}
   104  
   105  	sf.lock.Lock()
   106  	defer sf.lock.Unlock()
   107  	sf.key = fkey
   108  	sf.fileSize = sf.fileSize + int64(len(content))
   109  
   110  	sf.mountInfo.lock.Lock()
   111  	defer sf.mountInfo.lock.Unlock()
   112  	sf.mountInfo.LatestManifest = mhash
   113  
   114  	log.Info("Appended file:", "fname", sf.name, "New Manifest hash", mhash)
   115  	return nil
   116  }