File: dirstream.go

package info (click to toggle)
golang-github-hanwen-go-fuse 2.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,584 kB
  • sloc: cpp: 78; sh: 47; makefile: 16
file content (216 lines) | stat: -rw-r--r-- 4,471 bytes parent folder | download | duplicates (2)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright 2019 the Go-FUSE Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package fs

import (
	"context"
	"sync"
	"syscall"

	"github.com/hanwen/go-fuse/v2/fuse"
	"golang.org/x/sys/unix"
)

type dirArray struct {
	idx     int
	entries []fuse.DirEntry
}

func (a *dirArray) HasNext() bool {
	return a.idx < len(a.entries)
}

func (a *dirArray) Next() (fuse.DirEntry, syscall.Errno) {
	e := a.entries[a.idx]
	a.idx++
	e.Off = uint64(a.idx)
	return e, 0
}

func (a *dirArray) Seekdir(ctx context.Context, off uint64) syscall.Errno {
	idx := int(off)
	if idx < 0 || idx > len(a.entries) {
		return syscall.EINVAL
	}
	a.idx = idx
	return 0
}

func (a *dirArray) Close() {

}

func (a *dirArray) Releasedir(ctx context.Context, releaseFlags uint32) {}

func (a *dirArray) Readdirent(ctx context.Context) (de *fuse.DirEntry, errno syscall.Errno) {
	if !a.HasNext() {
		return nil, 0
	}
	e, errno := a.Next()
	return &e, errno
}

// NewLoopbackDirStream opens a directory for reading as a DirStream
func NewLoopbackDirStream(name string) (DirStream, syscall.Errno) {
	// TODO: should return concrete type.
	fd, err := syscall.Open(name, syscall.O_DIRECTORY|syscall.O_CLOEXEC, 0755)
	if err != nil {
		return nil, ToErrno(err)
	}
	return NewLoopbackDirStreamFd(fd)
}

// NewListDirStream wraps a slice of DirEntry as a DirStream.
func NewListDirStream(list []fuse.DirEntry) DirStream {
	return &dirArray{entries: list}
}

// implement FileReaddirenter/FileReleasedirer
type dirStreamAsFile struct {
	creator func(context.Context) (DirStream, syscall.Errno)
	ds      DirStream
}

func (d *dirStreamAsFile) Releasedir(ctx context.Context, releaseFlags uint32) {
	if d.ds != nil {
		d.ds.Close()
	}
}

func (d *dirStreamAsFile) Readdirent(ctx context.Context) (de *fuse.DirEntry, errno syscall.Errno) {
	if d.ds == nil {
		d.ds, errno = d.creator(ctx)
		if errno != 0 {
			return nil, errno
		}
	}
	if !d.ds.HasNext() {
		return nil, 0
	}

	e, errno := d.ds.Next()
	return &e, errno
}

func (d *dirStreamAsFile) Seekdir(ctx context.Context, off uint64) syscall.Errno {
	if d.ds == nil {
		var errno syscall.Errno
		d.ds, errno = d.creator(ctx)
		if errno != 0 {
			return errno
		}
	}
	if sd, ok := d.ds.(FileSeekdirer); ok {
		return sd.Seekdir(ctx, off)
	}
	return syscall.ENOTSUP
}

type loopbackDirStream struct {
	buf []byte

	// Protects mutable members
	mu sync.Mutex

	// mutable
	todo      []byte
	todoErrno syscall.Errno
	fd        int
}

// NewLoopbackDirStreamFd reads the directory opened at file descriptor fd as
// a DirStream
func NewLoopbackDirStreamFd(fd int) (DirStream, syscall.Errno) {
	ds := &loopbackDirStream{
		buf: make([]byte, 4096),
		fd:  fd,
	}
	ds.load()
	return ds, OK
}

func (ds *loopbackDirStream) Close() {
	ds.mu.Lock()
	defer ds.mu.Unlock()
	if ds.fd != -1 {
		syscall.Close(ds.fd)
		ds.fd = -1
	}
}

var _ = (FileReleasedirer)((*loopbackDirStream)(nil))

func (ds *loopbackDirStream) Releasedir(ctx context.Context, flags uint32) {
	ds.Close()
}

var _ = (FileSeekdirer)((*loopbackDirStream)(nil))

func (ds *loopbackDirStream) Seekdir(ctx context.Context, off uint64) syscall.Errno {
	ds.mu.Lock()
	defer ds.mu.Unlock()
	_, errno := unix.Seek(ds.fd, int64(off), unix.SEEK_SET)
	if errno != nil {
		return ToErrno(errno)
	}

	ds.todo = nil
	ds.todoErrno = 0
	ds.load()
	return 0
}

var _ = (FileFsyncdirer)((*loopbackDirStream)(nil))

func (ds *loopbackDirStream) Fsyncdir(ctx context.Context, flags uint32) syscall.Errno {
	ds.mu.Lock()
	defer ds.mu.Unlock()
	return ToErrno(syscall.Fsync(ds.fd))
}

func (ds *loopbackDirStream) HasNext() bool {
	ds.mu.Lock()
	defer ds.mu.Unlock()
	return len(ds.todo) > 0 || ds.todoErrno != 0
}

var _ = (FileReaddirenter)((*loopbackDirStream)(nil))

func (ds *loopbackDirStream) Readdirent(ctx context.Context) (*fuse.DirEntry, syscall.Errno) {
	if !ds.HasNext() {
		return nil, 0
	}
	de, errno := ds.Next()
	return &de, errno
}

func (ds *loopbackDirStream) Next() (fuse.DirEntry, syscall.Errno) {
	ds.mu.Lock()
	defer ds.mu.Unlock()

	if ds.todoErrno != 0 {
		return fuse.DirEntry{}, ds.todoErrno
	}
	var res fuse.DirEntry
	n := res.Parse(ds.todo)
	ds.todo = ds.todo[n:]
	if len(ds.todo) == 0 {
		ds.load()
	}
	return res, 0
}

func (ds *loopbackDirStream) load() {
	if len(ds.todo) > 0 {
		return
	}

	n, err := getdents(ds.fd, ds.buf)
	if n < 0 {
		n = 0
	}
	ds.todo = ds.buf[:n]
	ds.todoErrno = ToErrno(err)
}