github.com/xfond/vision@v1.8.9-0.20180514135602-f6bc65fc6811/swarm/fuse/fuse_dir.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  	"os"
    23  	"path/filepath"
    24  	"sync"
    25  
    26  	"bazil.org/fuse"
    27  	"bazil.org/fuse/fs"
    28  	"golang.org/x/net/context"
    29  )
    30  
    31  var (
    32  	_ fs.Node                = (*SwarmDir)(nil)
    33  	_ fs.NodeRequestLookuper = (*SwarmDir)(nil)
    34  	_ fs.HandleReadDirAller  = (*SwarmDir)(nil)
    35  	_ fs.NodeCreater         = (*SwarmDir)(nil)
    36  	_ fs.NodeRemover         = (*SwarmDir)(nil)
    37  	_ fs.NodeMkdirer         = (*SwarmDir)(nil)
    38  )
    39  
    40  type SwarmDir struct {
    41  	inode       uint64
    42  	name        string
    43  	path        string
    44  	directories []*SwarmDir
    45  	files       []*SwarmFile
    46  
    47  	mountInfo *MountInfo
    48  	lock      *sync.RWMutex
    49  }
    50  
    51  func NewSwarmDir(fullpath string, minfo *MountInfo) *SwarmDir {
    52  	newdir := &SwarmDir{
    53  		inode:       NewInode(),
    54  		name:        filepath.Base(fullpath),
    55  		path:        fullpath,
    56  		directories: []*SwarmDir{},
    57  		files:       []*SwarmFile{},
    58  		mountInfo:   minfo,
    59  		lock:        &sync.RWMutex{},
    60  	}
    61  	return newdir
    62  }
    63  
    64  func (sd *SwarmDir) Attr(ctx context.Context, a *fuse.Attr) error {
    65  	a.Inode = sd.inode
    66  	a.Mode = os.ModeDir | 0700
    67  	a.Uid = uint32(os.Getuid())
    68  	a.Gid = uint32(os.Getegid())
    69  	return nil
    70  }
    71  
    72  func (sd *SwarmDir) Lookup(ctx context.Context, req *fuse.LookupRequest, resp *fuse.LookupResponse) (fs.Node, error) {
    73  
    74  	for _, n := range sd.files {
    75  		if n.name == req.Name {
    76  			return n, nil
    77  		}
    78  	}
    79  	for _, n := range sd.directories {
    80  		if n.name == req.Name {
    81  			return n, nil
    82  		}
    83  	}
    84  	return nil, fuse.ENOENT
    85  }
    86  
    87  func (sd *SwarmDir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
    88  	var children []fuse.Dirent
    89  	for _, file := range sd.files {
    90  		children = append(children, fuse.Dirent{Inode: file.inode, Type: fuse.DT_File, Name: file.name})
    91  	}
    92  	for _, dir := range sd.directories {
    93  		children = append(children, fuse.Dirent{Inode: dir.inode, Type: fuse.DT_Dir, Name: dir.name})
    94  	}
    95  	return children, nil
    96  }
    97  
    98  func (sd *SwarmDir) Create(ctx context.Context, req *fuse.CreateRequest, resp *fuse.CreateResponse) (fs.Node, fs.Handle, error) {
    99  
   100  	newFile := NewSwarmFile(sd.path, req.Name, sd.mountInfo)
   101  	newFile.fileSize = 0 // 0 means, file is not in swarm yet and it is just created
   102  
   103  	sd.lock.Lock()
   104  	defer sd.lock.Unlock()
   105  	sd.files = append(sd.files, newFile)
   106  
   107  	return newFile, newFile, nil
   108  }
   109  
   110  func (sd *SwarmDir) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
   111  
   112  	if req.Dir && sd.directories != nil {
   113  		newDirs := []*SwarmDir{}
   114  		for _, dir := range sd.directories {
   115  			if dir.name == req.Name {
   116  				removeDirectoryFromSwarm(dir)
   117  			} else {
   118  				newDirs = append(newDirs, dir)
   119  			}
   120  		}
   121  		if len(sd.directories) > len(newDirs) {
   122  			sd.lock.Lock()
   123  			defer sd.lock.Unlock()
   124  			sd.directories = newDirs
   125  		}
   126  		return nil
   127  	} else if !req.Dir && sd.files != nil {
   128  		newFiles := []*SwarmFile{}
   129  		for _, f := range sd.files {
   130  			if f.name == req.Name {
   131  				removeFileFromSwarm(f)
   132  			} else {
   133  				newFiles = append(newFiles, f)
   134  			}
   135  		}
   136  		if len(sd.files) > len(newFiles) {
   137  			sd.lock.Lock()
   138  			defer sd.lock.Unlock()
   139  			sd.files = newFiles
   140  		}
   141  		return nil
   142  	}
   143  	return fuse.ENOENT
   144  }
   145  
   146  func (sd *SwarmDir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs.Node, error) {
   147  
   148  	newDir := NewSwarmDir(req.Name, sd.mountInfo)
   149  
   150  	sd.lock.Lock()
   151  	defer sd.lock.Unlock()
   152  	sd.directories = append(sd.directories, newDir)
   153  
   154  	return newDir, nil
   155  
   156  }