github.com/alexdevranger/node-1.8.27@v0.0.0-20221128213301-aa5841e41d2d/swarm/fuse/swarmfs_util.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of the go-dubxcoin library.
     3  //
     4  // The go-dubxcoin 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-dubxcoin 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-dubxcoin 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/alexdevranger/node-1.8.27/swarm/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", mountPoint).Run()
    42  	case "linux":
    43  		return exec.CommandContext(ctx, "fusermount", "-u", mountPoint).Run()
    44  	default:
    45  		return fmt.Errorf("swarmfs unmount: unimplemented")
    46  	}
    47  }
    48  
    49  func addFileToSwarm(sf *SwarmFile, content []byte, size int) error {
    50  	fkey, mhash, err := sf.mountInfo.swarmApi.AddFile(context.TODO(), sf.mountInfo.LatestManifest, sf.path, sf.name, content, true)
    51  	if err != nil {
    52  		return err
    53  	}
    54  
    55  	sf.lock.Lock()
    56  	defer sf.lock.Unlock()
    57  	sf.addr = fkey
    58  	sf.fileSize = int64(size)
    59  
    60  	sf.mountInfo.lock.Lock()
    61  	defer sf.mountInfo.lock.Unlock()
    62  	sf.mountInfo.LatestManifest = mhash
    63  
    64  	log.Info("swarmfs added new file:", "fname", sf.name, "new Manifest hash", mhash)
    65  	return nil
    66  }
    67  
    68  func removeFileFromSwarm(sf *SwarmFile) error {
    69  	mkey, err := sf.mountInfo.swarmApi.RemoveFile(context.TODO(), sf.mountInfo.LatestManifest, sf.path, sf.name, true)
    70  	if err != nil {
    71  		return err
    72  	}
    73  
    74  	sf.mountInfo.lock.Lock()
    75  	defer sf.mountInfo.lock.Unlock()
    76  	sf.mountInfo.LatestManifest = mkey
    77  
    78  	log.Info("swarmfs removed file:", "fname", sf.name, "new Manifest hash", mkey)
    79  	return nil
    80  }
    81  
    82  func removeDirectoryFromSwarm(sd *SwarmDir) error {
    83  	if len(sd.directories) == 0 && len(sd.files) == 0 {
    84  		return nil
    85  	}
    86  
    87  	for _, d := range sd.directories {
    88  		err := removeDirectoryFromSwarm(d)
    89  		if err != nil {
    90  			return err
    91  		}
    92  	}
    93  
    94  	for _, f := range sd.files {
    95  		err := removeFileFromSwarm(f)
    96  		if err != nil {
    97  			return err
    98  		}
    99  	}
   100  
   101  	return nil
   102  }
   103  
   104  func appendToExistingFileInSwarm(sf *SwarmFile, content []byte, offset int64, length int64) error {
   105  	fkey, mhash, err := sf.mountInfo.swarmApi.AppendFile(context.TODO(), sf.mountInfo.LatestManifest, sf.path, sf.name, sf.fileSize, content, sf.addr, offset, length, true)
   106  	if err != nil {
   107  		return err
   108  	}
   109  
   110  	sf.lock.Lock()
   111  	defer sf.lock.Unlock()
   112  	sf.addr = fkey
   113  	sf.fileSize = sf.fileSize + int64(len(content))
   114  
   115  	sf.mountInfo.lock.Lock()
   116  	defer sf.mountInfo.lock.Unlock()
   117  	sf.mountInfo.LatestManifest = mhash
   118  
   119  	log.Info("swarmfs appended file:", "fname", sf.name, "new Manifest hash", mhash)
   120  	return nil
   121  }