aboutsummaryrefslogtreecommitdiffstats
path: root/swarm/api/fuse.go
blob: 4b1f817f8c56610b3a05d6fad52a4e1971bfc6dd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

// +build !windows

package api

import (
    "io"
    "os"

    "bazil.org/fuse"
    "bazil.org/fuse/fs"
    "github.com/ethereum/go-ethereum/log"
    "github.com/ethereum/go-ethereum/swarm/storage"
    "golang.org/x/net/context"
)




// Data structures used for Fuse filesystem, serving directories and serving files to Fuse driver
type FS struct {
    root *Dir
}

type Dir struct {
    inode       uint64
    name        string
    path        string
    directories []*Dir
    files       []*File
}

type File struct {
    inode    uint64
    name     string
    path     string
    key      storage.Key
    swarmApi *Api
    fileSize uint64
    reader   storage.LazySectionReader
}


// Functions which satisfy the Fuse File System requests
func (filesystem *FS) Root() (fs.Node, error) {
    return filesystem.root, nil
}

func (directory *Dir) Attr(ctx context.Context, a *fuse.Attr) error {
    a.Inode = directory.inode
    //TODO: need to get permission as argument
    a.Mode = os.ModeDir | 0500
    a.Uid = uint32(os.Getuid())
    a.Gid = uint32(os.Getegid())
    return nil
}

func (directory *Dir) Lookup(ctx context.Context, name string) (fs.Node, error) {
    if directory.files != nil {
        for _, n := range directory.files {
            if n.name == name {
                return n, nil
            }
        }
    }
    if directory.directories != nil {
        for _, n := range directory.directories {
            if n.name == name {
                return n, nil
            }
        }
    }
    return nil, fuse.ENOENT
}

func (d *Dir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
    var children []fuse.Dirent
    if d.files != nil {
        for _, file := range d.files {
            children = append(children, fuse.Dirent{Inode: file.inode, Type: fuse.DT_File, Name: file.name})
        }
    }
    if d.directories != nil {
        for _, dir := range d.directories {
            children = append(children, fuse.Dirent{Inode: dir.inode, Type: fuse.DT_Dir, Name: dir.name})
        }
    }
    return children, nil
}

func (file *File) Attr(ctx context.Context, a *fuse.Attr) error {

    a.Inode = file.inode
    //TODO: need to get permission as argument
    a.Mode = 0500
    a.Uid = uint32(os.Getuid())
    a.Gid = uint32(os.Getegid())


    reader := file.swarmApi.Retrieve(file.key)
    quitC := make(chan bool)
    size, err := reader.Size(quitC)
    if err != nil {
        log.Warn("Couldnt file size of file %s : %v", file.path, err)
        a.Size = uint64(0)
    }
    a.Size = uint64(size)
    file.fileSize = a.Size
    return nil
}

var _ = fs.HandleReader(&File{})

func (file *File) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
    buf := make([]byte, req.Size)
    reader := file.swarmApi.Retrieve(file.key)
    n, err := reader.ReadAt(buf, req.Offset)
    if err == io.ErrUnexpectedEOF || err == io.EOF {
        err = nil
    }
    resp.Data = buf[:n]
    return err

}