Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go-fuse
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Levin Zimmermann
go-fuse
Commits
a3bb2189
Commit
a3bb2189
authored
Aug 05, 2019
by
Han-Wen Nienhuys
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fs: implement Write for MemRegularFile
Change-Id: Iac5c1a8329ceb0d8538029bd737cfede2af4d9a2
parent
e9498b6b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
4 deletions
+45
-4
fs/mem.go
fs/mem.go
+34
-3
fs/mem_test.go
fs/mem_test.go
+11
-1
No files found.
fs/mem.go
View file @
a3bb2189
...
...
@@ -6,6 +6,7 @@ package fs
import
(
"context"
"sync"
"syscall"
"github.com/hanwen/go-fuse/v2/fuse"
...
...
@@ -15,35 +16,65 @@ import (
// slice in memory.
type
MemRegularFile
struct
{
Inode
mu
sync
.
Mutex
Data
[]
byte
Attr
fuse
.
Attr
}
var
_
=
(
NodeOpener
)((
*
MemRegularFile
)(
nil
))
var
_
=
(
NodeReader
)((
*
MemRegularFile
)(
nil
))
var
_
=
(
NodeWriter
)((
*
MemRegularFile
)(
nil
))
var
_
=
(
NodeSetattrer
)((
*
MemRegularFile
)(
nil
))
var
_
=
(
NodeFlusher
)((
*
MemRegularFile
)(
nil
))
func
(
f
*
MemRegularFile
)
Open
(
ctx
context
.
Context
,
flags
uint32
)
(
fh
FileHandle
,
fuseFlags
uint32
,
errno
syscall
.
Errno
)
{
if
flags
&
(
syscall
.
O_RDWR
)
!=
0
||
flags
&
syscall
.
O_WRONLY
!=
0
{
return
nil
,
0
,
syscall
.
EPERM
return
nil
,
fuse
.
FOPEN_KEEP_CACHE
,
OK
}
func
(
f
*
MemRegularFile
)
Write
(
ctx
context
.
Context
,
fh
FileHandle
,
data
[]
byte
,
off
int64
)
(
uint32
,
syscall
.
Errno
)
{
f
.
mu
.
Lock
()
defer
f
.
mu
.
Unlock
()
end
:=
int64
(
len
(
data
))
+
off
if
int64
(
len
(
f
.
Data
))
<
end
{
n
:=
make
([]
byte
,
end
)
copy
(
n
,
f
.
Data
)
f
.
Data
=
n
}
return
nil
,
fuse
.
FOPEN_KEEP_CACHE
,
OK
copy
(
f
.
Data
[
off
:
off
+
int64
(
len
(
data
))],
data
)
return
uint32
(
len
(
data
)),
0
}
var
_
=
(
NodeGetattrer
)((
*
MemRegularFile
)(
nil
))
func
(
f
*
MemRegularFile
)
Getattr
(
ctx
context
.
Context
,
fh
FileHandle
,
out
*
fuse
.
AttrOut
)
syscall
.
Errno
{
f
.
mu
.
Lock
()
defer
f
.
mu
.
Unlock
()
out
.
Attr
=
f
.
Attr
out
.
Attr
.
Size
=
uint64
(
len
(
f
.
Data
))
return
OK
}
func
(
f
*
MemRegularFile
)
Setattr
(
ctx
context
.
Context
,
fh
FileHandle
,
in
*
fuse
.
SetAttrIn
,
out
*
fuse
.
AttrOut
)
syscall
.
Errno
{
f
.
mu
.
Lock
()
defer
f
.
mu
.
Unlock
()
if
sz
,
ok
:=
in
.
GetSize
();
ok
{
f
.
Data
=
f
.
Data
[
:
sz
]
}
out
.
Attr
=
f
.
Attr
out
.
Size
=
uint64
(
len
(
f
.
Data
))
return
OK
}
func
(
f
*
MemRegularFile
)
Flush
(
ctx
context
.
Context
,
fh
FileHandle
)
syscall
.
Errno
{
return
0
}
func
(
f
*
MemRegularFile
)
Read
(
ctx
context
.
Context
,
fh
FileHandle
,
dest
[]
byte
,
off
int64
)
(
fuse
.
ReadResult
,
syscall
.
Errno
)
{
f
.
mu
.
Lock
()
defer
f
.
mu
.
Unlock
()
end
:=
int
(
off
)
+
len
(
dest
)
if
end
>
len
(
f
.
Data
)
{
end
=
len
(
f
.
Data
)
...
...
fs/mem_test.go
View file @
a3bb2189
...
...
@@ -64,7 +64,6 @@ func TestDefaultOwner(t *testing.T) {
}
else
if
st
.
Uid
!=
42
||
st
.
Gid
!=
43
{
t
.
Fatalf
(
"Got Lstat %d, %d want 42,43"
,
st
.
Uid
,
st
.
Gid
)
}
}
func
TestDataFile
(
t
*
testing
.
T
)
{
...
...
@@ -116,6 +115,17 @@ func TestDataFile(t *testing.T) {
if
got
!=
want
{
t
.
Errorf
(
"got %q want %q"
,
got
,
want
)
}
replace
:=
[]
byte
(
"replaced!"
)
if
err
:=
ioutil
.
WriteFile
(
mntDir
+
"/file"
,
replace
,
0644
);
err
!=
nil
{
t
.
Fatalf
(
"WriteFile: %v"
,
err
)
}
if
gotBytes
,
err
:=
ioutil
.
ReadFile
(
mntDir
+
"/file"
);
err
!=
nil
{
t
.
Fatalf
(
"ReadFile: %v"
,
err
)
}
else
if
bytes
.
Compare
(
replace
,
gotBytes
)
!=
0
{
t
.
Fatalf
(
"read: got %q want %q"
,
gotBytes
,
replace
)
}
}
func
TestDataFileLargeRead
(
t
*
testing
.
T
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment