| 1 |
package fs_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"errors" |
| 6 |
"io" |
| 7 |
"io/ioutil" |
| 8 |
"log" |
| 9 |
"os" |
| 10 |
"os/exec" |
| 11 |
"runtime" |
| 12 |
"strings" |
| 13 |
"sync" |
| 14 |
"syscall" |
| 15 |
"testing" |
| 16 |
"time" |
| 17 |
|
| 18 |
"github.com/jacobsa/bazilfuse" |
| 19 |
"github.com/jacobsa/bazilfuse/fs" |
| 20 |
"github.com/jacobsa/bazilfuse/fs/fstestutil" |
| 21 |
"github.com/jacobsa/bazilfuse/fs/fstestutil/record" |
| 22 |
"github.com/jacobsa/bazilfuse/fuseutil" |
| 23 |
"github.com/jacobsa/bazilfuse/syscallx" |
| 24 |
"golang.org/x/net/context" |
| 25 |
) |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
func init() { |
| 38 |
fstestutil.DebugByDefault() |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
type symlink struct { |
| 43 |
target string |
| 44 |
} |
| 45 |
|
| 46 |
func (f symlink) Attr(ctx context.Context, a *fuse.Attr) error { |
| 47 |
a.Mode = os.ModeSymlink | 0666 |
| 48 |
return nil |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
type fifo struct{} |
| 53 |
|
| 54 |
func (f fifo) Attr(ctx context.Context, a *fuse.Attr) error { |
| 55 |
a.Mode = os.ModeNamedPipe | 0666 |
| 56 |
return nil |
| 57 |
} |
| 58 |
|
| 59 |
type badRootFS struct{} |
| 60 |
|
| 61 |
func (badRootFS) Root() (fs.Node, error) { |
| 62 |
|
| 63 |
return nil, fuse.Errno(syscall.ENAMETOOLONG) |
| 64 |
} |
| 65 |
|
| 66 |
func TestRootErr(t *testing.T) { |
| 67 |
t.Parallel() |
| 68 |
mnt, err := fstestutil.MountedT(t, badRootFS{}) |
| 69 |
if err == nil { |
| 70 |
|
| 71 |
|
| 72 |
err = <-mnt.Error |
| 73 |
|
| 74 |
|
| 75 |
mnt.Conn.Close() |
| 76 |
mnt.Close() |
| 77 |
} |
| 78 |
|
| 79 |
if err == nil { |
| 80 |
t.Fatal("expected an error") |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
if err.Error() != "cannot obtain root node: file name too long" { |
| 85 |
t.Errorf("Unexpected error: %v", err) |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
type testPanic struct{} |
| 90 |
|
| 91 |
type panicSentinel struct{} |
| 92 |
|
| 93 |
var _ error = panicSentinel{} |
| 94 |
|
| 95 |
func (panicSentinel) Error() string { return "just a test" } |
| 96 |
|
| 97 |
var _ fuse.ErrorNumber = panicSentinel{} |
| 98 |
|
| 99 |
func (panicSentinel) Errno() fuse.Errno { |
| 100 |
return fuse.Errno(syscall.ENAMETOOLONG) |
| 101 |
} |
| 102 |
|
| 103 |
func (f testPanic) Root() (fs.Node, error) { |
| 104 |
return f, nil |
| 105 |
} |
| 106 |
|
| 107 |
func (f testPanic) Attr(ctx context.Context, a *fuse.Attr) error { |
| 108 |
a.Inode = 1 |
| 109 |
a.Mode = os.ModeDir | 0777 |
| 110 |
return nil |
| 111 |
} |
| 112 |
|
| 113 |
func (f testPanic) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs.Node, error) { |
| 114 |
panic(panicSentinel{}) |
| 115 |
} |
| 116 |
|
| 117 |
func TestPanic(t *testing.T) { |
| 118 |
t.Parallel() |
| 119 |
mnt, err := fstestutil.MountedT(t, testPanic{}) |
| 120 |
if err != nil { |
| 121 |
t.Fatal(err) |
| 122 |
} |
| 123 |
defer mnt.Close() |
| 124 |
|
| 125 |
err = os.Mkdir(mnt.Dir+"/trigger-a-panic", 0700) |
| 126 |
if nerr, ok := err.(*os.PathError); !ok || nerr.Err != syscall.ENAMETOOLONG { |
| 127 |
t.Fatalf("wrong error from panicking handler: %T: %v", err, err) |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
type testStatFS struct{} |
| 132 |
|
| 133 |
func (f testStatFS) Root() (fs.Node, error) { |
| 134 |
return f, nil |
| 135 |
} |
| 136 |
|
| 137 |
func (f testStatFS) Attr(ctx context.Context, a *fuse.Attr) error { |
| 138 |
a.Inode = 1 |
| 139 |
a.Mode = os.ModeDir | 0777 |
| 140 |
return nil |
| 141 |
} |
| 142 |
|
| 143 |
func (f testStatFS) Statfs(ctx context.Context, req *fuse.StatfsRequest, resp *fuse.StatfsResponse) error { |
| 144 |
resp.Blocks = 42 |
| 145 |
resp.Files = 13 |
| 146 |
return nil |
| 147 |
} |
| 148 |
|
| 149 |
func TestStatfs(t *testing.T) { |
| 150 |
t.Parallel() |
| 151 |
mnt, err := fstestutil.MountedT(t, testStatFS{}) |
| 152 |
if err != nil { |
| 153 |
t.Fatal(err) |
| 154 |
} |
| 155 |
defer mnt.Close() |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
if _, err := os.Stat(mnt.Dir + "/does-not-exist"); !os.IsNotExist(err) { |
| 163 |
t.Fatal(err) |
| 164 |
} |
| 165 |
|
| 166 |
{ |
| 167 |
var st syscall.Statfs_t |
| 168 |
err = syscall.Statfs(mnt.Dir, &st) |
| 169 |
if err != nil { |
| 170 |
t.Errorf("Statfs failed: %v", err) |
| 171 |
} |
| 172 |
t.Logf("Statfs got: %#v", st) |
| 173 |
if g, e := st.Blocks, uint64(42); g != e { |
| 174 |
t.Errorf("got Blocks = %d; want %d", g, e) |
| 175 |
} |
| 176 |
if g, e := st.Files, uint64(13); g != e { |
| 177 |
t.Errorf("got Files = %d; want %d", g, e) |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
{ |
| 182 |
var st syscall.Statfs_t |
| 183 |
f, err := os.Open(mnt.Dir) |
| 184 |
if err != nil { |
| 185 |
t.Errorf("Open for fstatfs failed: %v", err) |
| 186 |
} |
| 187 |
defer f.Close() |
| 188 |
err = syscall.Fstatfs(int(f.Fd()), &st) |
| 189 |
if err != nil { |
| 190 |
t.Errorf("Fstatfs failed: %v", err) |
| 191 |
} |
| 192 |
t.Logf("Fstatfs got: %#v", st) |
| 193 |
if g, e := st.Blocks, uint64(42); g != e { |
| 194 |
t.Errorf("got Blocks = %d; want %d", g, e) |
| 195 |
} |
| 196 |
if g, e := st.Files, uint64(13); g != e { |
| 197 |
t.Errorf("got Files = %d; want %d", g, e) |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
type root struct{} |
| 205 |
|
| 206 |
func (f root) Root() (fs.Node, error) { |
| 207 |
return f, nil |
| 208 |
} |
| 209 |
|
| 210 |
func (root) Attr(ctx context.Context, a *fuse.Attr) error { |
| 211 |
a.Inode = 1 |
| 212 |
a.Mode = os.ModeDir | 0555 |
| 213 |
|
| 214 |
a.BlockSize = 65536 |
| 215 |
return nil |
| 216 |
} |
| 217 |
|
| 218 |
func TestStatRoot(t *testing.T) { |
| 219 |
t.Parallel() |
| 220 |
mnt, err := fstestutil.MountedT(t, root{}) |
| 221 |
if err != nil { |
| 222 |
t.Fatal(err) |
| 223 |
} |
| 224 |
defer mnt.Close() |
| 225 |
|
| 226 |
fi, err := os.Stat(mnt.Dir) |
| 227 |
if err != nil { |
| 228 |
t.Fatalf("root getattr failed with %v", err) |
| 229 |
} |
| 230 |
mode := fi.Mode() |
| 231 |
if (mode & os.ModeType) != os.ModeDir { |
| 232 |
t.Errorf("root is not a directory: %#v", fi) |
| 233 |
} |
| 234 |
if mode.Perm() != 0555 { |
| 235 |
t.Errorf("root has weird access mode: %v", mode.Perm()) |
| 236 |
} |
| 237 |
switch stat := fi.Sys().(type) { |
| 238 |
case *syscall.Stat_t: |
| 239 |
if stat.Ino != 1 { |
| 240 |
t.Errorf("root has wrong inode: %v", stat.Ino) |
| 241 |
} |
| 242 |
if stat.Nlink != 1 { |
| 243 |
t.Errorf("root has wrong link count: %v", stat.Nlink) |
| 244 |
} |
| 245 |
if stat.Uid != 0 { |
| 246 |
t.Errorf("root has wrong uid: %d", stat.Uid) |
| 247 |
} |
| 248 |
if stat.Gid != 0 { |
| 249 |
t.Errorf("root has wrong gid: %d", stat.Gid) |
| 250 |
} |
| 251 |
if mnt.Conn.Protocol().HasAttrBlockSize() { |
| 252 |
|
| 253 |
|
| 254 |
if g, e := int64(stat.Blksize), int64(65536); g != e { |
| 255 |
t.Errorf("root has wrong blocksize: %d != %d", g, e) |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
type readAll struct { |
| 264 |
fstestutil.File |
| 265 |
} |
| 266 |
|
| 267 |
const hi = "hello, world" |
| 268 |
|
| 269 |
func (readAll) Attr(ctx context.Context, a *fuse.Attr) error { |
| 270 |
a.Mode = 0666 |
| 271 |
a.Size = uint64(len(hi)) |
| 272 |
return nil |
| 273 |
} |
| 274 |
|
| 275 |
func (readAll) ReadAll(ctx context.Context) ([]byte, error) { |
| 276 |
return []byte(hi), nil |
| 277 |
} |
| 278 |
|
| 279 |
func testReadAll(t *testing.T, path string) { |
| 280 |
data, err := ioutil.ReadFile(path) |
| 281 |
if err != nil { |
| 282 |
t.Fatalf("readAll: %v", err) |
| 283 |
} |
| 284 |
if string(data) != hi { |
| 285 |
t.Errorf("readAll = %q, want %q", data, hi) |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
func TestReadAll(t *testing.T) { |
| 290 |
t.Parallel() |
| 291 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{fstestutil.ChildMap{"child": readAll{}}}) |
| 292 |
if err != nil { |
| 293 |
t.Fatal(err) |
| 294 |
} |
| 295 |
defer mnt.Close() |
| 296 |
|
| 297 |
testReadAll(t, mnt.Dir+"/child") |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
type readWithHandleRead struct { |
| 303 |
fstestutil.File |
| 304 |
} |
| 305 |
|
| 306 |
func (readWithHandleRead) Attr(ctx context.Context, a *fuse.Attr) error { |
| 307 |
a.Mode = 0666 |
| 308 |
a.Size = uint64(len(hi)) |
| 309 |
return nil |
| 310 |
} |
| 311 |
|
| 312 |
func (readWithHandleRead) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error { |
| 313 |
fuseutil.HandleRead(req, resp, []byte(hi)) |
| 314 |
return nil |
| 315 |
} |
| 316 |
|
| 317 |
func TestReadAllWithHandleRead(t *testing.T) { |
| 318 |
t.Parallel() |
| 319 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{fstestutil.ChildMap{"child": readWithHandleRead{}}}) |
| 320 |
if err != nil { |
| 321 |
t.Fatal(err) |
| 322 |
} |
| 323 |
defer mnt.Close() |
| 324 |
|
| 325 |
testReadAll(t, mnt.Dir+"/child") |
| 326 |
} |
| 327 |
|
| 328 |
type readFlags struct { |
| 329 |
fstestutil.File |
| 330 |
fileFlags record.Recorder |
| 331 |
} |
| 332 |
|
| 333 |
func (r *readFlags) Attr(ctx context.Context, a *fuse.Attr) error { |
| 334 |
a.Mode = 0666 |
| 335 |
a.Size = uint64(len(hi)) |
| 336 |
return nil |
| 337 |
} |
| 338 |
|
| 339 |
func (r *readFlags) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error { |
| 340 |
r.fileFlags.Record(req.FileFlags) |
| 341 |
fuseutil.HandleRead(req, resp, []byte(hi)) |
| 342 |
return nil |
| 343 |
} |
| 344 |
|
| 345 |
func TestReadFileFlags(t *testing.T) { |
| 346 |
t.Parallel() |
| 347 |
r := &readFlags{} |
| 348 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{fstestutil.ChildMap{"child": r}}) |
| 349 |
if err != nil { |
| 350 |
t.Fatal(err) |
| 351 |
} |
| 352 |
defer mnt.Close() |
| 353 |
|
| 354 |
if !mnt.Conn.Protocol().HasReadWriteFlags() { |
| 355 |
t.Skip("Old FUSE protocol") |
| 356 |
} |
| 357 |
|
| 358 |
f, err := os.OpenFile(mnt.Dir+"/child", os.O_RDWR|os.O_APPEND, 0666) |
| 359 |
if err != nil { |
| 360 |
t.Fatal(err) |
| 361 |
} |
| 362 |
defer f.Close() |
| 363 |
if _, err := f.Read(make([]byte, 4096)); err != nil { |
| 364 |
t.Fatal(err) |
| 365 |
} |
| 366 |
_ = f.Close() |
| 367 |
|
| 368 |
want := fuse.OpenReadWrite | fuse.OpenAppend |
| 369 |
if g, e := r.fileFlags.Recorded().(fuse.OpenFlags), want; g != e { |
| 370 |
t.Errorf("read saw file flags %+v, want %+v", g, e) |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
type writeFlags struct { |
| 375 |
fstestutil.File |
| 376 |
fileFlags record.Recorder |
| 377 |
} |
| 378 |
|
| 379 |
func (r *writeFlags) Attr(ctx context.Context, a *fuse.Attr) error { |
| 380 |
a.Mode = 0666 |
| 381 |
a.Size = uint64(len(hi)) |
| 382 |
return nil |
| 383 |
} |
| 384 |
|
| 385 |
func (r *writeFlags) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.WriteResponse) error { |
| 386 |
r.fileFlags.Record(req.FileFlags) |
| 387 |
resp.Size = len(req.Data) |
| 388 |
return nil |
| 389 |
} |
| 390 |
|
| 391 |
func TestWriteFileFlags(t *testing.T) { |
| 392 |
t.Parallel() |
| 393 |
r := &writeFlags{} |
| 394 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{fstestutil.ChildMap{"child": r}}) |
| 395 |
if err != nil { |
| 396 |
t.Fatal(err) |
| 397 |
} |
| 398 |
defer mnt.Close() |
| 399 |
|
| 400 |
if !mnt.Conn.Protocol().HasReadWriteFlags() { |
| 401 |
t.Skip("Old FUSE protocol") |
| 402 |
} |
| 403 |
|
| 404 |
f, err := os.OpenFile(mnt.Dir+"/child", os.O_RDWR|os.O_APPEND, 0666) |
| 405 |
if err != nil { |
| 406 |
t.Fatal(err) |
| 407 |
} |
| 408 |
defer f.Close() |
| 409 |
if _, err := f.Write(make([]byte, 4096)); err != nil { |
| 410 |
t.Fatal(err) |
| 411 |
} |
| 412 |
_ = f.Close() |
| 413 |
|
| 414 |
want := fuse.OpenReadWrite | fuse.OpenAppend |
| 415 |
if g, e := r.fileFlags.Recorded().(fuse.OpenFlags), want; g != e { |
| 416 |
t.Errorf("write saw file flags %+v, want %+v", g, e) |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
type release struct { |
| 423 |
fstestutil.File |
| 424 |
record.ReleaseWaiter |
| 425 |
} |
| 426 |
|
| 427 |
func TestRelease(t *testing.T) { |
| 428 |
t.Parallel() |
| 429 |
r := &release{} |
| 430 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{fstestutil.ChildMap{"child": r}}) |
| 431 |
if err != nil { |
| 432 |
t.Fatal(err) |
| 433 |
} |
| 434 |
defer mnt.Close() |
| 435 |
|
| 436 |
f, err := os.Open(mnt.Dir + "/child") |
| 437 |
if err != nil { |
| 438 |
t.Fatal(err) |
| 439 |
} |
| 440 |
f.Close() |
| 441 |
if !r.WaitForRelease(1 * time.Second) { |
| 442 |
t.Error("Close did not Release in time") |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
type write struct { |
| 449 |
fstestutil.File |
| 450 |
record.Writes |
| 451 |
record.Fsyncs |
| 452 |
} |
| 453 |
|
| 454 |
func TestWrite(t *testing.T) { |
| 455 |
t.Parallel() |
| 456 |
w := &write{} |
| 457 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{fstestutil.ChildMap{"child": w}}) |
| 458 |
if err != nil { |
| 459 |
t.Fatal(err) |
| 460 |
} |
| 461 |
defer mnt.Close() |
| 462 |
|
| 463 |
f, err := os.Create(mnt.Dir + "/child") |
| 464 |
if err != nil { |
| 465 |
t.Fatalf("Create: %v", err) |
| 466 |
} |
| 467 |
defer f.Close() |
| 468 |
n, err := f.Write([]byte(hi)) |
| 469 |
if err != nil { |
| 470 |
t.Fatalf("Write: %v", err) |
| 471 |
} |
| 472 |
if n != len(hi) { |
| 473 |
t.Fatalf("short write; n=%d; hi=%d", n, len(hi)) |
| 474 |
} |
| 475 |
|
| 476 |
err = syscall.Fsync(int(f.Fd())) |
| 477 |
if err != nil { |
| 478 |
t.Fatalf("Fsync = %v", err) |
| 479 |
} |
| 480 |
if w.RecordedFsync() == (fuse.FsyncRequest{}) { |
| 481 |
t.Errorf("never received expected fsync call") |
| 482 |
} |
| 483 |
|
| 484 |
err = f.Close() |
| 485 |
if err != nil { |
| 486 |
t.Fatalf("Close: %v", err) |
| 487 |
} |
| 488 |
|
| 489 |
if got := string(w.RecordedWriteData()); got != hi { |
| 490 |
t.Errorf("write = %q, want %q", got, hi) |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
type writeLarge struct { |
| 497 |
fstestutil.File |
| 498 |
record.Writes |
| 499 |
} |
| 500 |
|
| 501 |
func TestWriteLarge(t *testing.T) { |
| 502 |
t.Parallel() |
| 503 |
w := &write{} |
| 504 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{fstestutil.ChildMap{"child": w}}) |
| 505 |
if err != nil { |
| 506 |
t.Fatal(err) |
| 507 |
} |
| 508 |
defer mnt.Close() |
| 509 |
|
| 510 |
f, err := os.Create(mnt.Dir + "/child") |
| 511 |
if err != nil { |
| 512 |
t.Fatalf("Create: %v", err) |
| 513 |
} |
| 514 |
defer f.Close() |
| 515 |
const one = "xyzzyfoo" |
| 516 |
large := bytes.Repeat([]byte(one), 8192) |
| 517 |
n, err := f.Write(large) |
| 518 |
if err != nil { |
| 519 |
t.Fatalf("Write: %v", err) |
| 520 |
} |
| 521 |
if g, e := n, len(large); g != e { |
| 522 |
t.Fatalf("short write: %d != %d", g, e) |
| 523 |
} |
| 524 |
|
| 525 |
err = f.Close() |
| 526 |
if err != nil { |
| 527 |
t.Fatalf("Close: %v", err) |
| 528 |
} |
| 529 |
|
| 530 |
got := w.RecordedWriteData() |
| 531 |
if g, e := len(got), len(large); g != e { |
| 532 |
t.Errorf("write wrong length: %d != %d", g, e) |
| 533 |
} |
| 534 |
if g := strings.Replace(string(got), one, "", -1); g != "" { |
| 535 |
t.Errorf("write wrong data: expected repeats of %q, also got %q", one, g) |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
type writeTruncateFlush struct { |
| 542 |
fstestutil.File |
| 543 |
record.Writes |
| 544 |
record.Setattrs |
| 545 |
record.Flushes |
| 546 |
} |
| 547 |
|
| 548 |
func TestWriteTruncateFlush(t *testing.T) { |
| 549 |
t.Parallel() |
| 550 |
w := &writeTruncateFlush{} |
| 551 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{fstestutil.ChildMap{"child": w}}) |
| 552 |
if err != nil { |
| 553 |
t.Fatal(err) |
| 554 |
} |
| 555 |
defer mnt.Close() |
| 556 |
|
| 557 |
err = ioutil.WriteFile(mnt.Dir+"/child", []byte(hi), 0666) |
| 558 |
if err != nil { |
| 559 |
t.Fatalf("WriteFile: %v", err) |
| 560 |
} |
| 561 |
if w.RecordedSetattr() == (fuse.SetattrRequest{}) { |
| 562 |
t.Errorf("writeTruncateFlush expected Setattr") |
| 563 |
} |
| 564 |
if !w.RecordedFlush() { |
| 565 |
t.Errorf("writeTruncateFlush expected Setattr") |
| 566 |
} |
| 567 |
if got := string(w.RecordedWriteData()); got != hi { |
| 568 |
t.Errorf("writeTruncateFlush = %q, want %q", got, hi) |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
type mkdir1 struct { |
| 575 |
fstestutil.Dir |
| 576 |
record.Mkdirs |
| 577 |
} |
| 578 |
|
| 579 |
func (f *mkdir1) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs.Node, error) { |
| 580 |
f.Mkdirs.Mkdir(ctx, req) |
| 581 |
return &mkdir1{}, nil |
| 582 |
} |
| 583 |
|
| 584 |
func TestMkdir(t *testing.T) { |
| 585 |
t.Parallel() |
| 586 |
f := &mkdir1{} |
| 587 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{f}) |
| 588 |
if err != nil { |
| 589 |
t.Fatal(err) |
| 590 |
} |
| 591 |
defer mnt.Close() |
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
defer syscall.Umask(syscall.Umask(0022)) |
| 596 |
err = os.Mkdir(mnt.Dir+"/foo", 0771) |
| 597 |
if err != nil { |
| 598 |
t.Fatalf("mkdir: %v", err) |
| 599 |
} |
| 600 |
want := fuse.MkdirRequest{Name: "foo", Mode: os.ModeDir | 0751} |
| 601 |
if mnt.Conn.Protocol().HasUmask() { |
| 602 |
want.Umask = 0022 |
| 603 |
} |
| 604 |
if g, e := f.RecordedMkdir(), want; g != e { |
| 605 |
t.Errorf("mkdir saw %+v, want %+v", g, e) |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
type create1file struct { |
| 612 |
fstestutil.File |
| 613 |
record.Fsyncs |
| 614 |
} |
| 615 |
|
| 616 |
type create1 struct { |
| 617 |
fstestutil.Dir |
| 618 |
f create1file |
| 619 |
} |
| 620 |
|
| 621 |
func (f *create1) Create(ctx context.Context, req *fuse.CreateRequest, resp *fuse.CreateResponse) (fs.Node, fs.Handle, error) { |
| 622 |
if req.Name != "foo" { |
| 623 |
log.Printf("ERROR create1.Create unexpected name: %q\n", req.Name) |
| 624 |
return nil, nil, fuse.EPERM |
| 625 |
} |
| 626 |
flags := req.Flags |
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
flags &^= fuse.OpenTruncate |
| 631 |
|
| 632 |
if runtime.GOOS == "linux" { |
| 633 |
|
| 634 |
|
| 635 |
flags &^= fuse.OpenFlags(syscall.O_CLOEXEC) |
| 636 |
} |
| 637 |
|
| 638 |
if g, e := flags, fuse.OpenReadWrite|fuse.OpenCreate; g != e { |
| 639 |
log.Printf("ERROR create1.Create unexpected flags: %v != %v\n", g, e) |
| 640 |
return nil, nil, fuse.EPERM |
| 641 |
} |
| 642 |
if g, e := req.Mode, os.FileMode(0644); g != e { |
| 643 |
log.Printf("ERROR create1.Create unexpected mode: %v != %v\n", g, e) |
| 644 |
return nil, nil, fuse.EPERM |
| 645 |
} |
| 646 |
return &f.f, &f.f, nil |
| 647 |
} |
| 648 |
|
| 649 |
func TestCreate(t *testing.T) { |
| 650 |
t.Parallel() |
| 651 |
f := &create1{} |
| 652 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{f}) |
| 653 |
if err != nil { |
| 654 |
t.Fatal(err) |
| 655 |
} |
| 656 |
defer mnt.Close() |
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
defer syscall.Umask(syscall.Umask(0022)) |
| 661 |
ff, err := os.Create(mnt.Dir + "/foo") |
| 662 |
if err != nil { |
| 663 |
t.Fatalf("create1 WriteFile: %v", err) |
| 664 |
} |
| 665 |
defer ff.Close() |
| 666 |
|
| 667 |
err = syscall.Fsync(int(ff.Fd())) |
| 668 |
if err != nil { |
| 669 |
t.Fatalf("Fsync = %v", err) |
| 670 |
} |
| 671 |
|
| 672 |
if f.f.RecordedFsync() == (fuse.FsyncRequest{}) { |
| 673 |
t.Errorf("never received expected fsync call") |
| 674 |
} |
| 675 |
|
| 676 |
ff.Close() |
| 677 |
} |
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
type create3file struct { |
| 682 |
fstestutil.File |
| 683 |
record.Writes |
| 684 |
} |
| 685 |
|
| 686 |
type create3 struct { |
| 687 |
fstestutil.Dir |
| 688 |
f create3file |
| 689 |
fooCreated record.MarkRecorder |
| 690 |
fooRemoved record.MarkRecorder |
| 691 |
} |
| 692 |
|
| 693 |
func (f *create3) Create(ctx context.Context, req *fuse.CreateRequest, resp *fuse.CreateResponse) (fs.Node, fs.Handle, error) { |
| 694 |
if req.Name != "foo" { |
| 695 |
log.Printf("ERROR create3.Create unexpected name: %q\n", req.Name) |
| 696 |
return nil, nil, fuse.EPERM |
| 697 |
} |
| 698 |
f.fooCreated.Mark() |
| 699 |
return &f.f, &f.f, nil |
| 700 |
} |
| 701 |
|
| 702 |
func (f *create3) Lookup(ctx context.Context, name string) (fs.Node, error) { |
| 703 |
if f.fooCreated.Recorded() && !f.fooRemoved.Recorded() && name == "foo" { |
| 704 |
return &f.f, nil |
| 705 |
} |
| 706 |
return nil, fuse.ENOENT |
| 707 |
} |
| 708 |
|
| 709 |
func (f *create3) Remove(ctx context.Context, r *fuse.RemoveRequest) error { |
| 710 |
if f.fooCreated.Recorded() && !f.fooRemoved.Recorded() && |
| 711 |
r.Name == "foo" && !r.Dir { |
| 712 |
f.fooRemoved.Mark() |
| 713 |
return nil |
| 714 |
} |
| 715 |
return fuse.ENOENT |
| 716 |
} |
| 717 |
|
| 718 |
func TestCreateWriteRemove(t *testing.T) { |
| 719 |
t.Parallel() |
| 720 |
f := &create3{} |
| 721 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{f}) |
| 722 |
if err != nil { |
| 723 |
t.Fatal(err) |
| 724 |
} |
| 725 |
defer mnt.Close() |
| 726 |
|
| 727 |
err = ioutil.WriteFile(mnt.Dir+"/foo", []byte(hi), 0666) |
| 728 |
if err != nil { |
| 729 |
t.Fatalf("create3 WriteFile: %v", err) |
| 730 |
} |
| 731 |
if got := string(f.f.RecordedWriteData()); got != hi { |
| 732 |
t.Fatalf("create3 write = %q, want %q", got, hi) |
| 733 |
} |
| 734 |
|
| 735 |
err = os.Remove(mnt.Dir + "/foo") |
| 736 |
if err != nil { |
| 737 |
t.Fatalf("Remove: %v", err) |
| 738 |
} |
| 739 |
err = os.Remove(mnt.Dir + "/foo") |
| 740 |
if err == nil { |
| 741 |
t.Fatalf("second Remove = nil; want some error") |
| 742 |
} |
| 743 |
} |
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
type symlink1link struct { |
| 749 |
symlink |
| 750 |
target string |
| 751 |
} |
| 752 |
|
| 753 |
func (f symlink1link) Readlink(ctx context.Context, req *fuse.ReadlinkRequest) (string, error) { |
| 754 |
return f.target, nil |
| 755 |
} |
| 756 |
|
| 757 |
type symlink1 struct { |
| 758 |
fstestutil.Dir |
| 759 |
record.Symlinks |
| 760 |
} |
| 761 |
|
| 762 |
func (f *symlink1) Symlink(ctx context.Context, req *fuse.SymlinkRequest) (fs.Node, error) { |
| 763 |
f.Symlinks.Symlink(ctx, req) |
| 764 |
return symlink1link{target: req.Target}, nil |
| 765 |
} |
| 766 |
|
| 767 |
func TestSymlink(t *testing.T) { |
| 768 |
t.Parallel() |
| 769 |
f := &symlink1{} |
| 770 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{f}) |
| 771 |
if err != nil { |
| 772 |
t.Fatal(err) |
| 773 |
} |
| 774 |
defer mnt.Close() |
| 775 |
|
| 776 |
const target = "/some-target" |
| 777 |
|
| 778 |
err = os.Symlink(target, mnt.Dir+"/symlink.file") |
| 779 |
if err != nil { |
| 780 |
t.Fatalf("os.Symlink: %v", err) |
| 781 |
} |
| 782 |
|
| 783 |
want := fuse.SymlinkRequest{NewName: "symlink.file", Target: target} |
| 784 |
if g, e := f.RecordedSymlink(), want; g != e { |
| 785 |
t.Errorf("symlink saw %+v, want %+v", g, e) |
| 786 |
} |
| 787 |
|
| 788 |
gotName, err := os.Readlink(mnt.Dir + "/symlink.file") |
| 789 |
if err != nil { |
| 790 |
t.Fatalf("os.Readlink: %v", err) |
| 791 |
} |
| 792 |
if gotName != target { |
| 793 |
t.Errorf("os.Readlink = %q; want %q", gotName, target) |
| 794 |
} |
| 795 |
} |
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
type link1 struct { |
| 800 |
fstestutil.Dir |
| 801 |
record.Links |
| 802 |
} |
| 803 |
|
| 804 |
func (f *link1) Lookup(ctx context.Context, name string) (fs.Node, error) { |
| 805 |
if name == "old" { |
| 806 |
return fstestutil.File{}, nil |
| 807 |
} |
| 808 |
return nil, fuse.ENOENT |
| 809 |
} |
| 810 |
|
| 811 |
func (f *link1) Link(ctx context.Context, r *fuse.LinkRequest, old fs.Node) (fs.Node, error) { |
| 812 |
f.Links.Link(ctx, r, old) |
| 813 |
return fstestutil.File{}, nil |
| 814 |
} |
| 815 |
|
| 816 |
func TestLink(t *testing.T) { |
| 817 |
t.Parallel() |
| 818 |
f := &link1{} |
| 819 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{f}) |
| 820 |
if err != nil { |
| 821 |
t.Fatal(err) |
| 822 |
} |
| 823 |
defer mnt.Close() |
| 824 |
|
| 825 |
err = os.Link(mnt.Dir+"/old", mnt.Dir+"/new") |
| 826 |
if err != nil { |
| 827 |
t.Fatalf("Link: %v", err) |
| 828 |
} |
| 829 |
|
| 830 |
got := f.RecordedLink() |
| 831 |
want := fuse.LinkRequest{ |
| 832 |
NewName: "new", |
| 833 |
|
| 834 |
OldNode: got.OldNode, |
| 835 |
} |
| 836 |
if g, e := got, want; g != e { |
| 837 |
t.Fatalf("link saw %+v, want %+v", g, e) |
| 838 |
} |
| 839 |
} |
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
type rename1 struct { |
| 844 |
fstestutil.Dir |
| 845 |
renamed record.Counter |
| 846 |
} |
| 847 |
|
| 848 |
func (f *rename1) Lookup(ctx context.Context, name string) (fs.Node, error) { |
| 849 |
if name == "old" { |
| 850 |
return fstestutil.File{}, nil |
| 851 |
} |
| 852 |
return nil, fuse.ENOENT |
| 853 |
} |
| 854 |
|
| 855 |
func (f *rename1) Rename(ctx context.Context, r *fuse.RenameRequest, newDir fs.Node) error { |
| 856 |
if r.OldName == "old" && r.NewName == "new" && newDir == f { |
| 857 |
f.renamed.Inc() |
| 858 |
return nil |
| 859 |
} |
| 860 |
return fuse.EIO |
| 861 |
} |
| 862 |
|
| 863 |
func TestRename(t *testing.T) { |
| 864 |
t.Parallel() |
| 865 |
f := &rename1{} |
| 866 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{f}) |
| 867 |
if err != nil { |
| 868 |
t.Fatal(err) |
| 869 |
} |
| 870 |
defer mnt.Close() |
| 871 |
|
| 872 |
err = os.Rename(mnt.Dir+"/old", mnt.Dir+"/new") |
| 873 |
if err != nil { |
| 874 |
t.Fatalf("Rename: %v", err) |
| 875 |
} |
| 876 |
if g, e := f.renamed.Count(), uint32(1); g != e { |
| 877 |
t.Fatalf("expected rename didn't happen: %d != %d", g, e) |
| 878 |
} |
| 879 |
err = os.Rename(mnt.Dir+"/old2", mnt.Dir+"/new2") |
| 880 |
if err == nil { |
| 881 |
t.Fatal("expected error on second Rename; got nil") |
| 882 |
} |
| 883 |
} |
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
type mknod1 struct { |
| 888 |
fstestutil.Dir |
| 889 |
record.Mknods |
| 890 |
} |
| 891 |
|
| 892 |
func (f *mknod1) Mknod(ctx context.Context, r *fuse.MknodRequest) (fs.Node, error) { |
| 893 |
f.Mknods.Mknod(ctx, r) |
| 894 |
return fifo{}, nil |
| 895 |
} |
| 896 |
|
| 897 |
func TestMknod(t *testing.T) { |
| 898 |
t.Parallel() |
| 899 |
if os.Getuid() != 0 { |
| 900 |
t.Skip("skipping unless root") |
| 901 |
} |
| 902 |
|
| 903 |
f := &mknod1{} |
| 904 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{f}) |
| 905 |
if err != nil { |
| 906 |
t.Fatal(err) |
| 907 |
} |
| 908 |
defer mnt.Close() |
| 909 |
|
| 910 |
defer syscall.Umask(syscall.Umask(0)) |
| 911 |
err = syscall.Mknod(mnt.Dir+"/node", syscall.S_IFIFO|0666, 123) |
| 912 |
if err != nil { |
| 913 |
t.Fatalf("Mknod: %v", err) |
| 914 |
} |
| 915 |
|
| 916 |
want := fuse.MknodRequest{ |
| 917 |
Name: "node", |
| 918 |
Mode: os.FileMode(os.ModeNamedPipe | 0666), |
| 919 |
Rdev: uint32(123), |
| 920 |
} |
| 921 |
if runtime.GOOS == "linux" { |
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
want.Rdev = 0 |
| 926 |
} |
| 927 |
if g, e := f.RecordedMknod(), want; g != e { |
| 928 |
t.Fatalf("mknod saw %+v, want %+v", g, e) |
| 929 |
} |
| 930 |
} |
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
type dataHandleTest struct { |
| 935 |
fstestutil.File |
| 936 |
} |
| 937 |
|
| 938 |
func (dataHandleTest) Attr(ctx context.Context, a *fuse.Attr) error { |
| 939 |
a.Mode = 0666 |
| 940 |
a.Size = uint64(len(hi)) |
| 941 |
return nil |
| 942 |
} |
| 943 |
|
| 944 |
func (dataHandleTest) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) { |
| 945 |
return fs.DataHandle([]byte(hi)), nil |
| 946 |
} |
| 947 |
|
| 948 |
func TestDataHandle(t *testing.T) { |
| 949 |
t.Parallel() |
| 950 |
f := &dataHandleTest{} |
| 951 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{fstestutil.ChildMap{"child": f}}) |
| 952 |
if err != nil { |
| 953 |
t.Fatal(err) |
| 954 |
} |
| 955 |
defer mnt.Close() |
| 956 |
|
| 957 |
data, err := ioutil.ReadFile(mnt.Dir + "/child") |
| 958 |
if err != nil { |
| 959 |
t.Errorf("readAll: %v", err) |
| 960 |
return |
| 961 |
} |
| 962 |
if string(data) != hi { |
| 963 |
t.Errorf("readAll = %q, want %q", data, hi) |
| 964 |
} |
| 965 |
} |
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
type interrupt struct { |
| 970 |
fstestutil.File |
| 971 |
|
| 972 |
|
| 973 |
hanging chan struct{} |
| 974 |
} |
| 975 |
|
| 976 |
func (interrupt) Attr(ctx context.Context, a *fuse.Attr) error { |
| 977 |
a.Mode = 0666 |
| 978 |
a.Size = 1 |
| 979 |
return nil |
| 980 |
} |
| 981 |
|
| 982 |
func (it *interrupt) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error { |
| 983 |
select { |
| 984 |
case it.hanging <- struct{}{}: |
| 985 |
default: |
| 986 |
} |
| 987 |
<-ctx.Done() |
| 988 |
return fuse.EINTR |
| 989 |
} |
| 990 |
|
| 991 |
func TestInterrupt(t *testing.T) { |
| 992 |
t.Parallel() |
| 993 |
f := &interrupt{} |
| 994 |
f.hanging = make(chan struct{}, 1) |
| 995 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{fstestutil.ChildMap{"child": f}}) |
| 996 |
if err != nil { |
| 997 |
t.Fatal(err) |
| 998 |
} |
| 999 |
defer mnt.Close() |
| 1000 |
|
| 1001 |
|
| 1002 |
cmd := exec.Command("cat", mnt.Dir+"/child") |
| 1003 |
|
| 1004 |
err = cmd.Start() |
| 1005 |
if err != nil { |
| 1006 |
t.Errorf("interrupt: cannot start cat: %v", err) |
| 1007 |
return |
| 1008 |
} |
| 1009 |
|
| 1010 |
|
| 1011 |
defer cmd.Process.Kill() |
| 1012 |
|
| 1013 |
|
| 1014 |
<-f.hanging |
| 1015 |
|
| 1016 |
err = cmd.Process.Signal(os.Interrupt) |
| 1017 |
if err != nil { |
| 1018 |
t.Errorf("interrupt: cannot interrupt cat: %v", err) |
| 1019 |
return |
| 1020 |
} |
| 1021 |
|
| 1022 |
p, err := cmd.Process.Wait() |
| 1023 |
if err != nil { |
| 1024 |
t.Errorf("interrupt: cat bork: %v", err) |
| 1025 |
return |
| 1026 |
} |
| 1027 |
switch ws := p.Sys().(type) { |
| 1028 |
case syscall.WaitStatus: |
| 1029 |
if ws.CoreDump() { |
| 1030 |
t.Errorf("interrupt: didn't expect cat to dump core: %v", ws) |
| 1031 |
} |
| 1032 |
|
| 1033 |
if ws.Exited() { |
| 1034 |
t.Errorf("interrupt: didn't expect cat to exit normally: %v", ws) |
| 1035 |
} |
| 1036 |
|
| 1037 |
if !ws.Signaled() { |
| 1038 |
t.Errorf("interrupt: expected cat to get a signal: %v", ws) |
| 1039 |
} else { |
| 1040 |
if ws.Signal() != os.Interrupt { |
| 1041 |
t.Errorf("interrupt: cat got wrong signal: %v", ws) |
| 1042 |
} |
| 1043 |
} |
| 1044 |
default: |
| 1045 |
t.Logf("interrupt: this platform has no test coverage") |
| 1046 |
} |
| 1047 |
} |
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
type truncate struct { |
| 1052 |
fstestutil.File |
| 1053 |
record.Setattrs |
| 1054 |
} |
| 1055 |
|
| 1056 |
func testTruncate(t *testing.T, toSize int64) { |
| 1057 |
t.Parallel() |
| 1058 |
f := &truncate{} |
| 1059 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{fstestutil.ChildMap{"child": f}}) |
| 1060 |
if err != nil { |
| 1061 |
t.Fatal(err) |
| 1062 |
} |
| 1063 |
defer mnt.Close() |
| 1064 |
|
| 1065 |
err = os.Truncate(mnt.Dir+"/child", toSize) |
| 1066 |
if err != nil { |
| 1067 |
t.Fatalf("Truncate: %v", err) |
| 1068 |
} |
| 1069 |
gotr := f.RecordedSetattr() |
| 1070 |
if gotr == (fuse.SetattrRequest{}) { |
| 1071 |
t.Fatalf("no recorded SetattrRequest") |
| 1072 |
} |
| 1073 |
if g, e := gotr.Size, uint64(toSize); g != e { |
| 1074 |
t.Errorf("got Size = %q; want %q", g, e) |
| 1075 |
} |
| 1076 |
if g, e := gotr.Valid&^fuse.SetattrLockOwner, fuse.SetattrSize; g != e { |
| 1077 |
t.Errorf("got Valid = %q; want %q", g, e) |
| 1078 |
} |
| 1079 |
t.Logf("Got request: %#v", gotr) |
| 1080 |
} |
| 1081 |
|
| 1082 |
func TestTruncate42(t *testing.T) { |
| 1083 |
testTruncate(t, 42) |
| 1084 |
} |
| 1085 |
|
| 1086 |
func TestTruncate0(t *testing.T) { |
| 1087 |
testTruncate(t, 0) |
| 1088 |
} |
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
type ftruncate struct { |
| 1093 |
fstestutil.File |
| 1094 |
record.Setattrs |
| 1095 |
} |
| 1096 |
|
| 1097 |
func testFtruncate(t *testing.T, toSize int64) { |
| 1098 |
t.Parallel() |
| 1099 |
f := &ftruncate{} |
| 1100 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{fstestutil.ChildMap{"child": f}}) |
| 1101 |
if err != nil { |
| 1102 |
t.Fatal(err) |
| 1103 |
} |
| 1104 |
defer mnt.Close() |
| 1105 |
|
| 1106 |
{ |
| 1107 |
fil, err := os.OpenFile(mnt.Dir+"/child", os.O_WRONLY, 0666) |
| 1108 |
if err != nil { |
| 1109 |
t.Error(err) |
| 1110 |
return |
| 1111 |
} |
| 1112 |
defer fil.Close() |
| 1113 |
|
| 1114 |
err = fil.Truncate(toSize) |
| 1115 |
if err != nil { |
| 1116 |
t.Fatalf("Ftruncate: %v", err) |
| 1117 |
} |
| 1118 |
} |
| 1119 |
gotr := f.RecordedSetattr() |
| 1120 |
if gotr == (fuse.SetattrRequest{}) { |
| 1121 |
t.Fatalf("no recorded SetattrRequest") |
| 1122 |
} |
| 1123 |
if g, e := gotr.Size, uint64(toSize); g != e { |
| 1124 |
t.Errorf("got Size = %q; want %q", g, e) |
| 1125 |
} |
| 1126 |
if g, e := gotr.Valid&^fuse.SetattrLockOwner, fuse.SetattrHandle|fuse.SetattrSize; g != e { |
| 1127 |
t.Errorf("got Valid = %q; want %q", g, e) |
| 1128 |
} |
| 1129 |
t.Logf("Got request: %#v", gotr) |
| 1130 |
} |
| 1131 |
|
| 1132 |
func TestFtruncate42(t *testing.T) { |
| 1133 |
testFtruncate(t, 42) |
| 1134 |
} |
| 1135 |
|
| 1136 |
func TestFtruncate0(t *testing.T) { |
| 1137 |
testFtruncate(t, 0) |
| 1138 |
} |
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
type truncateWithOpen struct { |
| 1143 |
fstestutil.File |
| 1144 |
record.Setattrs |
| 1145 |
} |
| 1146 |
|
| 1147 |
func TestTruncateWithOpen(t *testing.T) { |
| 1148 |
t.Parallel() |
| 1149 |
f := &truncateWithOpen{} |
| 1150 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{fstestutil.ChildMap{"child": f}}) |
| 1151 |
if err != nil { |
| 1152 |
t.Fatal(err) |
| 1153 |
} |
| 1154 |
defer mnt.Close() |
| 1155 |
|
| 1156 |
fil, err := os.OpenFile(mnt.Dir+"/child", os.O_WRONLY|os.O_TRUNC, 0666) |
| 1157 |
if err != nil { |
| 1158 |
t.Error(err) |
| 1159 |
return |
| 1160 |
} |
| 1161 |
fil.Close() |
| 1162 |
|
| 1163 |
gotr := f.RecordedSetattr() |
| 1164 |
if gotr == (fuse.SetattrRequest{}) { |
| 1165 |
t.Fatalf("no recorded SetattrRequest") |
| 1166 |
} |
| 1167 |
if g, e := gotr.Size, uint64(0); g != e { |
| 1168 |
t.Errorf("got Size = %q; want %q", g, e) |
| 1169 |
} |
| 1170 |
|
| 1171 |
if g, e := gotr.Valid&^(fuse.SetattrLockOwner|fuse.SetattrHandle), fuse.SetattrSize; g != e { |
| 1172 |
t.Errorf("got Valid = %q; want %q", g, e) |
| 1173 |
} |
| 1174 |
t.Logf("Got request: %#v", gotr) |
| 1175 |
} |
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
type readDirAll struct { |
| 1180 |
fstestutil.Dir |
| 1181 |
} |
| 1182 |
|
| 1183 |
func (d *readDirAll) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) { |
| 1184 |
return []fuse.Dirent{ |
| 1185 |
{Name: "one", Inode: 11, Type: fuse.DT_Dir}, |
| 1186 |
{Name: "three", Inode: 13}, |
| 1187 |
{Name: "two", Inode: 12, Type: fuse.DT_File}, |
| 1188 |
}, nil |
| 1189 |
} |
| 1190 |
|
| 1191 |
func TestReadDirAll(t *testing.T) { |
| 1192 |
t.Parallel() |
| 1193 |
f := &readDirAll{} |
| 1194 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{f}) |
| 1195 |
if err != nil { |
| 1196 |
t.Fatal(err) |
| 1197 |
} |
| 1198 |
defer mnt.Close() |
| 1199 |
|
| 1200 |
fil, err := os.Open(mnt.Dir) |
| 1201 |
if err != nil { |
| 1202 |
t.Error(err) |
| 1203 |
return |
| 1204 |
} |
| 1205 |
defer fil.Close() |
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
names, err := fil.Readdirnames(100) |
| 1211 |
if err != nil { |
| 1212 |
t.Error(err) |
| 1213 |
return |
| 1214 |
} |
| 1215 |
|
| 1216 |
t.Logf("Got readdir: %q", names) |
| 1217 |
|
| 1218 |
if len(names) != 3 || |
| 1219 |
names[0] != "one" || |
| 1220 |
names[1] != "three" || |
| 1221 |
names[2] != "two" { |
| 1222 |
t.Errorf(`expected 3 entries of "one", "three", "two", got: %q`, names) |
| 1223 |
return |
| 1224 |
} |
| 1225 |
} |
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
type readDirNotImplemented struct { |
| 1230 |
fstestutil.Dir |
| 1231 |
} |
| 1232 |
|
| 1233 |
func TestReadDirNotImplemented(t *testing.T) { |
| 1234 |
t.Parallel() |
| 1235 |
f := &readDirNotImplemented{} |
| 1236 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{f}) |
| 1237 |
if err != nil { |
| 1238 |
t.Fatal(err) |
| 1239 |
} |
| 1240 |
defer mnt.Close() |
| 1241 |
|
| 1242 |
fil, err := os.Open(mnt.Dir) |
| 1243 |
if err != nil { |
| 1244 |
t.Error(err) |
| 1245 |
return |
| 1246 |
} |
| 1247 |
defer fil.Close() |
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
names, err := fil.Readdirnames(100) |
| 1253 |
if len(names) > 0 || err != io.EOF { |
| 1254 |
t.Fatalf("expected EOF got names=%v err=%v", names, err) |
| 1255 |
} |
| 1256 |
} |
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
type chmod struct { |
| 1261 |
fstestutil.File |
| 1262 |
record.Setattrs |
| 1263 |
} |
| 1264 |
|
| 1265 |
func (f *chmod) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error { |
| 1266 |
if !req.Valid.Mode() { |
| 1267 |
log.Printf("setattr not a chmod: %v", req.Valid) |
| 1268 |
return fuse.EIO |
| 1269 |
} |
| 1270 |
f.Setattrs.Setattr(ctx, req, resp) |
| 1271 |
return nil |
| 1272 |
} |
| 1273 |
|
| 1274 |
func TestChmod(t *testing.T) { |
| 1275 |
t.Parallel() |
| 1276 |
f := &chmod{} |
| 1277 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{fstestutil.ChildMap{"child": f}}) |
| 1278 |
if err != nil { |
| 1279 |
t.Fatal(err) |
| 1280 |
} |
| 1281 |
defer mnt.Close() |
| 1282 |
|
| 1283 |
err = os.Chmod(mnt.Dir+"/child", 0764) |
| 1284 |
if err != nil { |
| 1285 |
t.Errorf("chmod: %v", err) |
| 1286 |
return |
| 1287 |
} |
| 1288 |
got := f.RecordedSetattr() |
| 1289 |
if g, e := got.Mode, os.FileMode(0764); g != e { |
| 1290 |
t.Errorf("wrong mode: %v != %v", g, e) |
| 1291 |
} |
| 1292 |
} |
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
type open struct { |
| 1297 |
fstestutil.File |
| 1298 |
record.Opens |
| 1299 |
} |
| 1300 |
|
| 1301 |
func (f *open) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) { |
| 1302 |
f.Opens.Open(ctx, req, resp) |
| 1303 |
|
| 1304 |
return nil, fuse.Errno(syscall.ENAMETOOLONG) |
| 1305 |
} |
| 1306 |
|
| 1307 |
func TestOpen(t *testing.T) { |
| 1308 |
t.Parallel() |
| 1309 |
f := &open{} |
| 1310 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{fstestutil.ChildMap{"child": f}}) |
| 1311 |
if err != nil { |
| 1312 |
t.Fatal(err) |
| 1313 |
} |
| 1314 |
defer mnt.Close() |
| 1315 |
|
| 1316 |
|
| 1317 |
fil, err := os.OpenFile(mnt.Dir+"/child", os.O_WRONLY|os.O_APPEND, 0) |
| 1318 |
if err == nil { |
| 1319 |
t.Error("Open err == nil, expected ENAMETOOLONG") |
| 1320 |
fil.Close() |
| 1321 |
return |
| 1322 |
} |
| 1323 |
|
| 1324 |
switch err2 := err.(type) { |
| 1325 |
case *os.PathError: |
| 1326 |
if err2.Err == syscall.ENAMETOOLONG { |
| 1327 |
break |
| 1328 |
} |
| 1329 |
t.Errorf("unexpected inner error: %#v", err2) |
| 1330 |
default: |
| 1331 |
t.Errorf("unexpected error: %v", err) |
| 1332 |
} |
| 1333 |
|
| 1334 |
want := fuse.OpenRequest{Dir: false, Flags: fuse.OpenWriteOnly | fuse.OpenAppend} |
| 1335 |
if runtime.GOOS == "darwin" { |
| 1336 |
|
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
want.Flags &^= fuse.OpenAppend |
| 1342 |
} |
| 1343 |
got := f.RecordedOpen() |
| 1344 |
|
| 1345 |
if runtime.GOOS == "linux" { |
| 1346 |
|
| 1347 |
|
| 1348 |
got.Flags &^= fuse.OpenFlags(syscall.O_CLOEXEC) |
| 1349 |
} |
| 1350 |
|
| 1351 |
if g, e := got, want; g != e { |
| 1352 |
t.Errorf("open saw %v, want %v", g, e) |
| 1353 |
return |
| 1354 |
} |
| 1355 |
} |
| 1356 |
|
| 1357 |
type openNonSeekable struct { |
| 1358 |
fstestutil.File |
| 1359 |
} |
| 1360 |
|
| 1361 |
func (f *openNonSeekable) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) { |
| 1362 |
resp.Flags |= fuse.OpenNonSeekable |
| 1363 |
return f, nil |
| 1364 |
} |
| 1365 |
|
| 1366 |
func TestOpenNonSeekable(t *testing.T) { |
| 1367 |
t.Parallel() |
| 1368 |
f := &openNonSeekable{} |
| 1369 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{fstestutil.ChildMap{"child": f}}) |
| 1370 |
if err != nil { |
| 1371 |
t.Fatal(err) |
| 1372 |
} |
| 1373 |
defer mnt.Close() |
| 1374 |
|
| 1375 |
if !mnt.Conn.Protocol().HasOpenNonSeekable() { |
| 1376 |
t.Skip("Old FUSE protocol") |
| 1377 |
} |
| 1378 |
|
| 1379 |
fil, err := os.Open(mnt.Dir + "/child") |
| 1380 |
if err != nil { |
| 1381 |
t.Fatal(err) |
| 1382 |
} |
| 1383 |
defer fil.Close() |
| 1384 |
|
| 1385 |
_, err = fil.Seek(0, os.SEEK_SET) |
| 1386 |
if nerr, ok := err.(*os.PathError); !ok || nerr.Err != syscall.ESPIPE { |
| 1387 |
t.Fatalf("wrong error: %v", err) |
| 1388 |
} |
| 1389 |
} |
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
type fsyncDir struct { |
| 1394 |
fstestutil.Dir |
| 1395 |
record.Fsyncs |
| 1396 |
} |
| 1397 |
|
| 1398 |
func TestFsyncDir(t *testing.T) { |
| 1399 |
t.Parallel() |
| 1400 |
f := &fsyncDir{} |
| 1401 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{f}) |
| 1402 |
if err != nil { |
| 1403 |
t.Fatal(err) |
| 1404 |
} |
| 1405 |
defer mnt.Close() |
| 1406 |
|
| 1407 |
fil, err := os.Open(mnt.Dir) |
| 1408 |
if err != nil { |
| 1409 |
t.Errorf("fsyncDir open: %v", err) |
| 1410 |
return |
| 1411 |
} |
| 1412 |
defer fil.Close() |
| 1413 |
err = fil.Sync() |
| 1414 |
if err != nil { |
| 1415 |
t.Errorf("fsyncDir sync: %v", err) |
| 1416 |
return |
| 1417 |
} |
| 1418 |
|
| 1419 |
got := f.RecordedFsync() |
| 1420 |
want := fuse.FsyncRequest{ |
| 1421 |
Flags: 0, |
| 1422 |
Dir: true, |
| 1423 |
|
| 1424 |
Handle: got.Handle, |
| 1425 |
} |
| 1426 |
if runtime.GOOS == "darwin" { |
| 1427 |
|
| 1428 |
|
| 1429 |
want.Flags = 1 |
| 1430 |
} |
| 1431 |
if g, e := got, want; g != e { |
| 1432 |
t.Fatalf("fsyncDir saw %+v, want %+v", g, e) |
| 1433 |
} |
| 1434 |
} |
| 1435 |
|
| 1436 |
|
| 1437 |
|
| 1438 |
type getxattr struct { |
| 1439 |
fstestutil.File |
| 1440 |
record.Getxattrs |
| 1441 |
} |
| 1442 |
|
| 1443 |
func (f *getxattr) Getxattr(ctx context.Context, req *fuse.GetxattrRequest, resp *fuse.GetxattrResponse) error { |
| 1444 |
f.Getxattrs.Getxattr(ctx, req, resp) |
| 1445 |
resp.Xattr = []byte("hello, world") |
| 1446 |
return nil |
| 1447 |
} |
| 1448 |
|
| 1449 |
func TestGetxattr(t *testing.T) { |
| 1450 |
t.Parallel() |
| 1451 |
f := &getxattr{} |
| 1452 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{fstestutil.ChildMap{"child": f}}) |
| 1453 |
if err != nil { |
| 1454 |
t.Fatal(err) |
| 1455 |
} |
| 1456 |
defer mnt.Close() |
| 1457 |
|
| 1458 |
buf := make([]byte, 8192) |
| 1459 |
n, err := syscallx.Getxattr(mnt.Dir+"/child", "not-there", buf) |
| 1460 |
if err != nil { |
| 1461 |
t.Errorf("unexpected error: %v", err) |
| 1462 |
return |
| 1463 |
} |
| 1464 |
buf = buf[:n] |
| 1465 |
if g, e := string(buf), "hello, world"; g != e { |
| 1466 |
t.Errorf("wrong getxattr content: %#v != %#v", g, e) |
| 1467 |
} |
| 1468 |
seen := f.RecordedGetxattr() |
| 1469 |
if g, e := seen.Name, "not-there"; g != e { |
| 1470 |
t.Errorf("wrong getxattr name: %#v != %#v", g, e) |
| 1471 |
} |
| 1472 |
} |
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
type getxattrTooSmall struct { |
| 1477 |
fstestutil.File |
| 1478 |
} |
| 1479 |
|
| 1480 |
func (f *getxattrTooSmall) Getxattr(ctx context.Context, req *fuse.GetxattrRequest, resp *fuse.GetxattrResponse) error { |
| 1481 |
resp.Xattr = []byte("hello, world") |
| 1482 |
return nil |
| 1483 |
} |
| 1484 |
|
| 1485 |
func TestGetxattrTooSmall(t *testing.T) { |
| 1486 |
t.Parallel() |
| 1487 |
f := &getxattrTooSmall{} |
| 1488 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{fstestutil.ChildMap{"child": f}}) |
| 1489 |
if err != nil { |
| 1490 |
t.Fatal(err) |
| 1491 |
} |
| 1492 |
defer mnt.Close() |
| 1493 |
|
| 1494 |
buf := make([]byte, 3) |
| 1495 |
_, err = syscallx.Getxattr(mnt.Dir+"/child", "whatever", buf) |
| 1496 |
if err == nil { |
| 1497 |
t.Error("Getxattr = nil; want some error") |
| 1498 |
} |
| 1499 |
if err != syscall.ERANGE { |
| 1500 |
t.Errorf("unexpected error: %v", err) |
| 1501 |
return |
| 1502 |
} |
| 1503 |
} |
| 1504 |
|
| 1505 |
|
| 1506 |
|
| 1507 |
type getxattrSize struct { |
| 1508 |
fstestutil.File |
| 1509 |
} |
| 1510 |
|
| 1511 |
func (f *getxattrSize) Getxattr(ctx context.Context, req *fuse.GetxattrRequest, resp *fuse.GetxattrResponse) error { |
| 1512 |
resp.Xattr = []byte("hello, world") |
| 1513 |
return nil |
| 1514 |
} |
| 1515 |
|
| 1516 |
func TestGetxattrSize(t *testing.T) { |
| 1517 |
t.Parallel() |
| 1518 |
f := &getxattrSize{} |
| 1519 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{fstestutil.ChildMap{"child": f}}) |
| 1520 |
if err != nil { |
| 1521 |
t.Fatal(err) |
| 1522 |
} |
| 1523 |
defer mnt.Close() |
| 1524 |
|
| 1525 |
n, err := syscallx.Getxattr(mnt.Dir+"/child", "whatever", nil) |
| 1526 |
if err != nil { |
| 1527 |
t.Errorf("Getxattr unexpected error: %v", err) |
| 1528 |
return |
| 1529 |
} |
| 1530 |
if g, e := n, len("hello, world"); g != e { |
| 1531 |
t.Errorf("Getxattr incorrect size: %d != %d", g, e) |
| 1532 |
} |
| 1533 |
} |
| 1534 |
|
| 1535 |
|
| 1536 |
|
| 1537 |
type listxattr struct { |
| 1538 |
fstestutil.File |
| 1539 |
record.Listxattrs |
| 1540 |
} |
| 1541 |
|
| 1542 |
func (f *listxattr) Listxattr(ctx context.Context, req *fuse.ListxattrRequest, resp *fuse.ListxattrResponse) error { |
| 1543 |
f.Listxattrs.Listxattr(ctx, req, resp) |
| 1544 |
resp.Append("one", "two") |
| 1545 |
return nil |
| 1546 |
} |
| 1547 |
|
| 1548 |
func TestListxattr(t *testing.T) { |
| 1549 |
t.Parallel() |
| 1550 |
f := &listxattr{} |
| 1551 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{fstestutil.ChildMap{"child": f}}) |
| 1552 |
if err != nil { |
| 1553 |
t.Fatal(err) |
| 1554 |
} |
| 1555 |
defer mnt.Close() |
| 1556 |
|
| 1557 |
buf := make([]byte, 8192) |
| 1558 |
n, err := syscallx.Listxattr(mnt.Dir+"/child", buf) |
| 1559 |
if err != nil { |
| 1560 |
t.Errorf("unexpected error: %v", err) |
| 1561 |
return |
| 1562 |
} |
| 1563 |
buf = buf[:n] |
| 1564 |
if g, e := string(buf), "one\x00two\x00"; g != e { |
| 1565 |
t.Errorf("wrong listxattr content: %#v != %#v", g, e) |
| 1566 |
} |
| 1567 |
|
| 1568 |
want := fuse.ListxattrRequest{ |
| 1569 |
Size: 8192, |
| 1570 |
} |
| 1571 |
if g, e := f.RecordedListxattr(), want; g != e { |
| 1572 |
t.Fatalf("listxattr saw %+v, want %+v", g, e) |
| 1573 |
} |
| 1574 |
} |
| 1575 |
|
| 1576 |
|
| 1577 |
|
| 1578 |
type listxattrTooSmall struct { |
| 1579 |
fstestutil.File |
| 1580 |
} |
| 1581 |
|
| 1582 |
func (f *listxattrTooSmall) Listxattr(ctx context.Context, req *fuse.ListxattrRequest, resp *fuse.ListxattrResponse) error { |
| 1583 |
resp.Xattr = []byte("one\x00two\x00") |
| 1584 |
return nil |
| 1585 |
} |
| 1586 |
|
| 1587 |
func TestListxattrTooSmall(t *testing.T) { |
| 1588 |
t.Parallel() |
| 1589 |
f := &listxattrTooSmall{} |
| 1590 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{fstestutil.ChildMap{"child": f}}) |
| 1591 |
if err != nil { |
| 1592 |
t.Fatal(err) |
| 1593 |
} |
| 1594 |
defer mnt.Close() |
| 1595 |
|
| 1596 |
buf := make([]byte, 3) |
| 1597 |
_, err = syscallx.Listxattr(mnt.Dir+"/child", buf) |
| 1598 |
if err == nil { |
| 1599 |
t.Error("Listxattr = nil; want some error") |
| 1600 |
} |
| 1601 |
if err != syscall.ERANGE { |
| 1602 |
t.Errorf("unexpected error: %v", err) |
| 1603 |
return |
| 1604 |
} |
| 1605 |
} |
| 1606 |
|
| 1607 |
|
| 1608 |
|
| 1609 |
type listxattrSize struct { |
| 1610 |
fstestutil.File |
| 1611 |
} |
| 1612 |
|
| 1613 |
func (f *listxattrSize) Listxattr(ctx context.Context, req *fuse.ListxattrRequest, resp *fuse.ListxattrResponse) error { |
| 1614 |
resp.Xattr = []byte("one\x00two\x00") |
| 1615 |
return nil |
| 1616 |
} |
| 1617 |
|
| 1618 |
func TestListxattrSize(t *testing.T) { |
| 1619 |
t.Parallel() |
| 1620 |
f := &listxattrSize{} |
| 1621 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{fstestutil.ChildMap{"child": f}}) |
| 1622 |
if err != nil { |
| 1623 |
t.Fatal(err) |
| 1624 |
} |
| 1625 |
defer mnt.Close() |
| 1626 |
|
| 1627 |
n, err := syscallx.Listxattr(mnt.Dir+"/child", nil) |
| 1628 |
if err != nil { |
| 1629 |
t.Errorf("Listxattr unexpected error: %v", err) |
| 1630 |
return |
| 1631 |
} |
| 1632 |
if g, e := n, len("one\x00two\x00"); g != e { |
| 1633 |
t.Errorf("Getxattr incorrect size: %d != %d", g, e) |
| 1634 |
} |
| 1635 |
} |
| 1636 |
|
| 1637 |
|
| 1638 |
|
| 1639 |
type setxattr struct { |
| 1640 |
fstestutil.File |
| 1641 |
record.Setxattrs |
| 1642 |
} |
| 1643 |
|
| 1644 |
func testSetxattr(t *testing.T, size int) { |
| 1645 |
const linux_XATTR_NAME_MAX = 64 * 1024 |
| 1646 |
if size > linux_XATTR_NAME_MAX && runtime.GOOS == "linux" { |
| 1647 |
t.Skip("large xattrs are not supported by linux") |
| 1648 |
} |
| 1649 |
|
| 1650 |
t.Parallel() |
| 1651 |
f := &setxattr{} |
| 1652 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{fstestutil.ChildMap{"child": f}}) |
| 1653 |
if err != nil { |
| 1654 |
t.Fatal(err) |
| 1655 |
} |
| 1656 |
defer mnt.Close() |
| 1657 |
|
| 1658 |
const g = "hello, world" |
| 1659 |
greeting := strings.Repeat(g, size/len(g)+1)[:size] |
| 1660 |
err = syscallx.Setxattr(mnt.Dir+"/child", "greeting", []byte(greeting), 0) |
| 1661 |
if err != nil { |
| 1662 |
t.Errorf("unexpected error: %v", err) |
| 1663 |
return |
| 1664 |
} |
| 1665 |
|
| 1666 |
|
| 1667 |
|
| 1668 |
got := f.RecordedSetxattr() |
| 1669 |
|
| 1670 |
if g, e := got.Name, "greeting"; g != e { |
| 1671 |
t.Errorf("Setxattr incorrect name: %q != %q", g, e) |
| 1672 |
} |
| 1673 |
|
| 1674 |
if g, e := got.Flags, uint32(0); g != e { |
| 1675 |
t.Errorf("Setxattr incorrect flags: %d != %d", g, e) |
| 1676 |
} |
| 1677 |
|
| 1678 |
if g, e := string(got.Xattr), greeting; g != e { |
| 1679 |
t.Errorf("Setxattr incorrect data: %q != %q", g, e) |
| 1680 |
} |
| 1681 |
} |
| 1682 |
|
| 1683 |
func TestSetxattr(t *testing.T) { |
| 1684 |
testSetxattr(t, 20) |
| 1685 |
} |
| 1686 |
|
| 1687 |
func TestSetxattr64kB(t *testing.T) { |
| 1688 |
testSetxattr(t, 64*1024) |
| 1689 |
} |
| 1690 |
|
| 1691 |
func TestSetxattr16MB(t *testing.T) { |
| 1692 |
testSetxattr(t, 16*1024*1024) |
| 1693 |
} |
| 1694 |
|
| 1695 |
|
| 1696 |
|
| 1697 |
type removexattr struct { |
| 1698 |
fstestutil.File |
| 1699 |
record.Removexattrs |
| 1700 |
} |
| 1701 |
|
| 1702 |
func TestRemovexattr(t *testing.T) { |
| 1703 |
t.Parallel() |
| 1704 |
f := &removexattr{} |
| 1705 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{fstestutil.ChildMap{"child": f}}) |
| 1706 |
if err != nil { |
| 1707 |
t.Fatal(err) |
| 1708 |
} |
| 1709 |
defer mnt.Close() |
| 1710 |
|
| 1711 |
err = syscallx.Removexattr(mnt.Dir+"/child", "greeting") |
| 1712 |
if err != nil { |
| 1713 |
t.Errorf("unexpected error: %v", err) |
| 1714 |
return |
| 1715 |
} |
| 1716 |
|
| 1717 |
want := fuse.RemovexattrRequest{Name: "greeting"} |
| 1718 |
if g, e := f.RecordedRemovexattr(), want; g != e { |
| 1719 |
t.Errorf("removexattr saw %v, want %v", g, e) |
| 1720 |
} |
| 1721 |
} |
| 1722 |
|
| 1723 |
|
| 1724 |
|
| 1725 |
type defaultErrno struct { |
| 1726 |
fstestutil.Dir |
| 1727 |
} |
| 1728 |
|
| 1729 |
func (f defaultErrno) Lookup(ctx context.Context, name string) (fs.Node, error) { |
| 1730 |
return nil, errors.New("bork") |
| 1731 |
} |
| 1732 |
|
| 1733 |
func TestDefaultErrno(t *testing.T) { |
| 1734 |
t.Parallel() |
| 1735 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{defaultErrno{}}) |
| 1736 |
if err != nil { |
| 1737 |
t.Fatal(err) |
| 1738 |
} |
| 1739 |
defer mnt.Close() |
| 1740 |
|
| 1741 |
_, err = os.Stat(mnt.Dir + "/trigger") |
| 1742 |
if err == nil { |
| 1743 |
t.Fatalf("expected error") |
| 1744 |
} |
| 1745 |
|
| 1746 |
switch err2 := err.(type) { |
| 1747 |
case *os.PathError: |
| 1748 |
if err2.Err == syscall.EIO { |
| 1749 |
break |
| 1750 |
} |
| 1751 |
t.Errorf("unexpected inner error: Err=%v %#v", err2.Err, err2) |
| 1752 |
default: |
| 1753 |
t.Errorf("unexpected error: %v", err) |
| 1754 |
} |
| 1755 |
} |
| 1756 |
|
| 1757 |
|
| 1758 |
|
| 1759 |
type customErrNode struct { |
| 1760 |
fstestutil.Dir |
| 1761 |
} |
| 1762 |
|
| 1763 |
type myCustomError struct { |
| 1764 |
fuse.ErrorNumber |
| 1765 |
} |
| 1766 |
|
| 1767 |
var _ = fuse.ErrorNumber(myCustomError{}) |
| 1768 |
|
| 1769 |
func (myCustomError) Error() string { |
| 1770 |
return "bork" |
| 1771 |
} |
| 1772 |
|
| 1773 |
func (f customErrNode) Lookup(ctx context.Context, name string) (fs.Node, error) { |
| 1774 |
return nil, myCustomError{ |
| 1775 |
ErrorNumber: fuse.Errno(syscall.ENAMETOOLONG), |
| 1776 |
} |
| 1777 |
} |
| 1778 |
|
| 1779 |
func TestCustomErrno(t *testing.T) { |
| 1780 |
t.Parallel() |
| 1781 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{customErrNode{}}) |
| 1782 |
if err != nil { |
| 1783 |
t.Fatal(err) |
| 1784 |
} |
| 1785 |
defer mnt.Close() |
| 1786 |
|
| 1787 |
_, err = os.Stat(mnt.Dir + "/trigger") |
| 1788 |
if err == nil { |
| 1789 |
t.Fatalf("expected error") |
| 1790 |
} |
| 1791 |
|
| 1792 |
switch err2 := err.(type) { |
| 1793 |
case *os.PathError: |
| 1794 |
if err2.Err == syscall.ENAMETOOLONG { |
| 1795 |
break |
| 1796 |
} |
| 1797 |
t.Errorf("unexpected inner error: %#v", err2) |
| 1798 |
default: |
| 1799 |
t.Errorf("unexpected error: %v", err) |
| 1800 |
} |
| 1801 |
} |
| 1802 |
|
| 1803 |
|
| 1804 |
|
| 1805 |
type inMemoryFile struct { |
| 1806 |
mu sync.Mutex |
| 1807 |
data []byte |
| 1808 |
} |
| 1809 |
|
| 1810 |
func (f *inMemoryFile) bytes() []byte { |
| 1811 |
f.mu.Lock() |
| 1812 |
defer f.mu.Unlock() |
| 1813 |
|
| 1814 |
return f.data |
| 1815 |
} |
| 1816 |
|
| 1817 |
func (f *inMemoryFile) Attr(ctx context.Context, a *fuse.Attr) error { |
| 1818 |
f.mu.Lock() |
| 1819 |
defer f.mu.Unlock() |
| 1820 |
|
| 1821 |
a.Mode = 0666 |
| 1822 |
a.Size = uint64(len(f.data)) |
| 1823 |
return nil |
| 1824 |
} |
| 1825 |
|
| 1826 |
func (f *inMemoryFile) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error { |
| 1827 |
f.mu.Lock() |
| 1828 |
defer f.mu.Unlock() |
| 1829 |
|
| 1830 |
fuseutil.HandleRead(req, resp, f.data) |
| 1831 |
return nil |
| 1832 |
} |
| 1833 |
|
| 1834 |
func (f *inMemoryFile) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.WriteResponse) error { |
| 1835 |
f.mu.Lock() |
| 1836 |
defer f.mu.Unlock() |
| 1837 |
|
| 1838 |
resp.Size = copy(f.data[req.Offset:], req.Data) |
| 1839 |
return nil |
| 1840 |
} |
| 1841 |
|
| 1842 |
const mmapSize = 16 * 4096 |
| 1843 |
|
| 1844 |
var mmapWrites = map[int]byte{ |
| 1845 |
10: 'a', |
| 1846 |
4096: 'b', |
| 1847 |
4097: 'c', |
| 1848 |
mmapSize - 4096: 'd', |
| 1849 |
mmapSize - 1: 'z', |
| 1850 |
} |
| 1851 |
|
| 1852 |
func helperMmap() { |
| 1853 |
f, err := os.Create("child") |
| 1854 |
if err != nil { |
| 1855 |
log.Fatalf("Create: %v", err) |
| 1856 |
} |
| 1857 |
defer f.Close() |
| 1858 |
|
| 1859 |
data, err := syscall.Mmap(int(f.Fd()), 0, mmapSize, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED) |
| 1860 |
if err != nil { |
| 1861 |
log.Fatalf("Mmap: %v", err) |
| 1862 |
} |
| 1863 |
|
| 1864 |
for i, b := range mmapWrites { |
| 1865 |
data[i] = b |
| 1866 |
} |
| 1867 |
|
| 1868 |
if err := syscallx.Msync(data, syscall.MS_SYNC); err != nil { |
| 1869 |
log.Fatalf("Msync: %v", err) |
| 1870 |
} |
| 1871 |
|
| 1872 |
if err := syscall.Munmap(data); err != nil { |
| 1873 |
log.Fatalf("Munmap: %v", err) |
| 1874 |
} |
| 1875 |
|
| 1876 |
if err := f.Sync(); err != nil { |
| 1877 |
log.Fatalf("Fsync = %v", err) |
| 1878 |
} |
| 1879 |
|
| 1880 |
err = f.Close() |
| 1881 |
if err != nil { |
| 1882 |
log.Fatalf("Close: %v", err) |
| 1883 |
} |
| 1884 |
} |
| 1885 |
|
| 1886 |
func init() { |
| 1887 |
childHelpers["mmap"] = helperMmap |
| 1888 |
} |
| 1889 |
|
| 1890 |
type mmap struct { |
| 1891 |
inMemoryFile |
| 1892 |
|
| 1893 |
|
| 1894 |
|
| 1895 |
record.Fsyncs |
| 1896 |
} |
| 1897 |
|
| 1898 |
func TestMmap(t *testing.T) { |
| 1899 |
|
| 1900 |
w := &mmap{} |
| 1901 |
w.data = make([]byte, mmapSize) |
| 1902 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{fstestutil.ChildMap{"child": w}}) |
| 1903 |
if err != nil { |
| 1904 |
t.Fatal(err) |
| 1905 |
} |
| 1906 |
defer mnt.Close() |
| 1907 |
|
| 1908 |
|
| 1909 |
|
| 1910 |
|
| 1911 |
|
| 1912 |
|
| 1913 |
child, err := childCmd("mmap") |
| 1914 |
if err != nil { |
| 1915 |
t.Fatal(err) |
| 1916 |
} |
| 1917 |
child.Dir = mnt.Dir |
| 1918 |
if err := child.Run(); err != nil { |
| 1919 |
t.Fatal(err) |
| 1920 |
} |
| 1921 |
|
| 1922 |
got := w.bytes() |
| 1923 |
if g, e := len(got), mmapSize; g != e { |
| 1924 |
t.Fatalf("bad write length: %d != %d", g, e) |
| 1925 |
} |
| 1926 |
for i, g := range got { |
| 1927 |
|
| 1928 |
if e := mmapWrites[i]; g != e { |
| 1929 |
t.Errorf("wrong byte at offset %d: %q != %q", i, g, e) |
| 1930 |
} |
| 1931 |
} |
| 1932 |
} |
| 1933 |
|
| 1934 |
|
| 1935 |
|
| 1936 |
type directRead struct { |
| 1937 |
fstestutil.File |
| 1938 |
} |
| 1939 |
|
| 1940 |
|
| 1941 |
|
| 1942 |
func (f directRead) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) { |
| 1943 |
|
| 1944 |
resp.Flags |= fuse.OpenDirectIO |
| 1945 |
return f, nil |
| 1946 |
} |
| 1947 |
|
| 1948 |
func (directRead) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error { |
| 1949 |
fuseutil.HandleRead(req, resp, []byte(hi)) |
| 1950 |
return nil |
| 1951 |
} |
| 1952 |
|
| 1953 |
func TestDirectRead(t *testing.T) { |
| 1954 |
t.Parallel() |
| 1955 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{fstestutil.ChildMap{"child": directRead{}}}) |
| 1956 |
if err != nil { |
| 1957 |
t.Fatal(err) |
| 1958 |
} |
| 1959 |
defer mnt.Close() |
| 1960 |
|
| 1961 |
testReadAll(t, mnt.Dir+"/child") |
| 1962 |
} |
| 1963 |
|
| 1964 |
|
| 1965 |
|
| 1966 |
type directWrite struct { |
| 1967 |
fstestutil.File |
| 1968 |
record.Writes |
| 1969 |
} |
| 1970 |
|
| 1971 |
|
| 1972 |
|
| 1973 |
func (f *directWrite) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) { |
| 1974 |
|
| 1975 |
resp.Flags |= fuse.OpenDirectIO |
| 1976 |
return f, nil |
| 1977 |
} |
| 1978 |
|
| 1979 |
func TestDirectWrite(t *testing.T) { |
| 1980 |
t.Parallel() |
| 1981 |
w := &directWrite{} |
| 1982 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{fstestutil.ChildMap{"child": w}}) |
| 1983 |
if err != nil { |
| 1984 |
t.Fatal(err) |
| 1985 |
} |
| 1986 |
defer mnt.Close() |
| 1987 |
|
| 1988 |
f, err := os.OpenFile(mnt.Dir+"/child", os.O_RDWR, 0666) |
| 1989 |
if err != nil { |
| 1990 |
t.Fatalf("Create: %v", err) |
| 1991 |
} |
| 1992 |
defer f.Close() |
| 1993 |
n, err := f.Write([]byte(hi)) |
| 1994 |
if err != nil { |
| 1995 |
t.Fatalf("Write: %v", err) |
| 1996 |
} |
| 1997 |
if n != len(hi) { |
| 1998 |
t.Fatalf("short write; n=%d; hi=%d", n, len(hi)) |
| 1999 |
} |
| 2000 |
|
| 2001 |
err = f.Close() |
| 2002 |
if err != nil { |
| 2003 |
t.Fatalf("Close: %v", err) |
| 2004 |
} |
| 2005 |
|
| 2006 |
if got := string(w.RecordedWriteData()); got != hi { |
| 2007 |
t.Errorf("write = %q, want %q", got, hi) |
| 2008 |
} |
| 2009 |
} |
| 2010 |
|
| 2011 |
|
| 2012 |
|
| 2013 |
|
| 2014 |
type attrUnlinked struct { |
| 2015 |
fstestutil.File |
| 2016 |
} |
| 2017 |
|
| 2018 |
var _ fs.Node = attrUnlinked{} |
| 2019 |
|
| 2020 |
func (f attrUnlinked) Attr(ctx context.Context, a *fuse.Attr) error { |
| 2021 |
if err := f.File.Attr(ctx, a); err != nil { |
| 2022 |
return err |
| 2023 |
} |
| 2024 |
a.Nlink = 0 |
| 2025 |
return nil |
| 2026 |
} |
| 2027 |
|
| 2028 |
func TestAttrUnlinked(t *testing.T) { |
| 2029 |
t.Parallel() |
| 2030 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{fstestutil.ChildMap{"child": attrUnlinked{}}}) |
| 2031 |
if err != nil { |
| 2032 |
t.Fatal(err) |
| 2033 |
} |
| 2034 |
defer mnt.Close() |
| 2035 |
|
| 2036 |
fi, err := os.Stat(mnt.Dir + "/child") |
| 2037 |
if err != nil { |
| 2038 |
t.Fatalf("Stat failed with %v", err) |
| 2039 |
} |
| 2040 |
switch stat := fi.Sys().(type) { |
| 2041 |
case *syscall.Stat_t: |
| 2042 |
if stat.Nlink != 0 { |
| 2043 |
t.Errorf("wrong link count: %v", stat.Nlink) |
| 2044 |
} |
| 2045 |
} |
| 2046 |
} |
| 2047 |
|
| 2048 |
|
| 2049 |
|
| 2050 |
type attrBad struct { |
| 2051 |
} |
| 2052 |
|
| 2053 |
var _ fs.Node = attrBad{} |
| 2054 |
|
| 2055 |
func (attrBad) Attr(ctx context.Context, attr *fuse.Attr) error { |
| 2056 |
return fuse.Errno(syscall.ENAMETOOLONG) |
| 2057 |
} |
| 2058 |
|
| 2059 |
func TestAttrBad(t *testing.T) { |
| 2060 |
t.Parallel() |
| 2061 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{fstestutil.ChildMap{"child": attrBad{}}}) |
| 2062 |
if err != nil { |
| 2063 |
t.Fatal(err) |
| 2064 |
} |
| 2065 |
defer mnt.Close() |
| 2066 |
|
| 2067 |
_, err = os.Stat(mnt.Dir + "/child") |
| 2068 |
if nerr, ok := err.(*os.PathError); !ok || nerr.Err != syscall.ENAMETOOLONG { |
| 2069 |
t.Fatalf("wrong error: %v", err) |
| 2070 |
} |
| 2071 |
} |
| 2072 |
|
| 2073 |
|
| 2074 |
|
| 2075 |
type invalidateAttr struct { |
| 2076 |
fs.NodeRef |
| 2077 |
t testing.TB |
| 2078 |
attr record.Counter |
| 2079 |
} |
| 2080 |
|
| 2081 |
var _ fs.Node = (*invalidateAttr)(nil) |
| 2082 |
|
| 2083 |
func (i *invalidateAttr) Attr(ctx context.Context, a *fuse.Attr) error { |
| 2084 |
i.attr.Inc() |
| 2085 |
i.t.Logf("Attr called, #%d", i.attr.Count()) |
| 2086 |
a.Mode = 0600 |
| 2087 |
return nil |
| 2088 |
} |
| 2089 |
|
| 2090 |
func TestInvalidateNodeAttr(t *testing.T) { |
| 2091 |
|
| 2092 |
|
| 2093 |
t.Parallel() |
| 2094 |
a := &invalidateAttr{ |
| 2095 |
t: t, |
| 2096 |
} |
| 2097 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{fstestutil.ChildMap{"child": a}}) |
| 2098 |
if err != nil { |
| 2099 |
t.Fatal(err) |
| 2100 |
} |
| 2101 |
defer mnt.Close() |
| 2102 |
|
| 2103 |
if !mnt.Conn.Protocol().HasInvalidate() { |
| 2104 |
t.Skip("Old FUSE protocol") |
| 2105 |
} |
| 2106 |
|
| 2107 |
for i := 0; i < 10; i++ { |
| 2108 |
if _, err := os.Stat(mnt.Dir + "/child"); err != nil { |
| 2109 |
t.Fatalf("stat error: %v", err) |
| 2110 |
} |
| 2111 |
} |
| 2112 |
if g, e := a.attr.Count(), uint32(1); g != e { |
| 2113 |
t.Errorf("wrong Attr call count: %d != %d", g, e) |
| 2114 |
} |
| 2115 |
|
| 2116 |
t.Logf("invalidating...") |
| 2117 |
if err := mnt.Server.InvalidateNodeAttr(a); err != nil { |
| 2118 |
t.Fatalf("invalidate error: %v", err) |
| 2119 |
} |
| 2120 |
|
| 2121 |
for i := 0; i < 10; i++ { |
| 2122 |
if _, err := os.Stat(mnt.Dir + "/child"); err != nil { |
| 2123 |
t.Fatalf("stat error: %v", err) |
| 2124 |
} |
| 2125 |
} |
| 2126 |
if g, e := a.attr.Count(), uint32(2); g != e { |
| 2127 |
t.Errorf("wrong Attr call count: %d != %d", g, e) |
| 2128 |
} |
| 2129 |
} |
| 2130 |
|
| 2131 |
type invalidateData struct { |
| 2132 |
fs.NodeRef |
| 2133 |
t testing.TB |
| 2134 |
attr record.Counter |
| 2135 |
read record.Counter |
| 2136 |
} |
| 2137 |
|
| 2138 |
const invalidateDataContent = "hello, world\n" |
| 2139 |
|
| 2140 |
var _ fs.Node = (*invalidateData)(nil) |
| 2141 |
|
| 2142 |
func (i *invalidateData) Attr(ctx context.Context, a *fuse.Attr) error { |
| 2143 |
i.attr.Inc() |
| 2144 |
i.t.Logf("Attr called, #%d", i.attr.Count()) |
| 2145 |
a.Mode = 0600 |
| 2146 |
a.Size = uint64(len(invalidateDataContent)) |
| 2147 |
return nil |
| 2148 |
} |
| 2149 |
|
| 2150 |
var _ fs.HandleReader = (*invalidateData)(nil) |
| 2151 |
|
| 2152 |
func (i *invalidateData) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error { |
| 2153 |
i.read.Inc() |
| 2154 |
i.t.Logf("Read called, #%d", i.read.Count()) |
| 2155 |
fuseutil.HandleRead(req, resp, []byte(invalidateDataContent)) |
| 2156 |
return nil |
| 2157 |
} |
| 2158 |
|
| 2159 |
func TestInvalidateNodeData(t *testing.T) { |
| 2160 |
|
| 2161 |
|
| 2162 |
t.Parallel() |
| 2163 |
a := &invalidateData{ |
| 2164 |
t: t, |
| 2165 |
} |
| 2166 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{fstestutil.ChildMap{"child": a}}) |
| 2167 |
if err != nil { |
| 2168 |
t.Fatal(err) |
| 2169 |
} |
| 2170 |
defer mnt.Close() |
| 2171 |
|
| 2172 |
if !mnt.Conn.Protocol().HasInvalidate() { |
| 2173 |
t.Skip("Old FUSE protocol") |
| 2174 |
} |
| 2175 |
|
| 2176 |
f, err := os.Open(mnt.Dir + "/child") |
| 2177 |
if err != nil { |
| 2178 |
t.Fatal(err) |
| 2179 |
} |
| 2180 |
defer f.Close() |
| 2181 |
|
| 2182 |
buf := make([]byte, 4) |
| 2183 |
for i := 0; i < 10; i++ { |
| 2184 |
if _, err := f.ReadAt(buf, 0); err != nil { |
| 2185 |
t.Fatalf("readat error: %v", err) |
| 2186 |
} |
| 2187 |
} |
| 2188 |
if g, e := a.attr.Count(), uint32(1); g != e { |
| 2189 |
t.Errorf("wrong Attr call count: %d != %d", g, e) |
| 2190 |
} |
| 2191 |
if g, e := a.read.Count(), uint32(1); g != e { |
| 2192 |
t.Errorf("wrong Read call count: %d != %d", g, e) |
| 2193 |
} |
| 2194 |
|
| 2195 |
t.Logf("invalidating...") |
| 2196 |
if err := mnt.Server.InvalidateNodeData(a); err != nil { |
| 2197 |
t.Fatalf("invalidate error: %v", err) |
| 2198 |
} |
| 2199 |
|
| 2200 |
for i := 0; i < 10; i++ { |
| 2201 |
if _, err := f.ReadAt(buf, 0); err != nil { |
| 2202 |
t.Fatalf("readat error: %v", err) |
| 2203 |
} |
| 2204 |
} |
| 2205 |
if g, e := a.attr.Count(), uint32(1); g != e { |
| 2206 |
t.Errorf("wrong Attr call count: %d != %d", g, e) |
| 2207 |
} |
| 2208 |
if g, e := a.read.Count(), uint32(2); g != e { |
| 2209 |
t.Errorf("wrong Read call count: %d != %d", g, e) |
| 2210 |
} |
| 2211 |
} |
| 2212 |
|
| 2213 |
type invalidateDataPartial struct { |
| 2214 |
fs.NodeRef |
| 2215 |
t testing.TB |
| 2216 |
attr record.Counter |
| 2217 |
read record.Counter |
| 2218 |
} |
| 2219 |
|
| 2220 |
var invalidateDataPartialContent = strings.Repeat("hello, world\n", 1000) |
| 2221 |
|
| 2222 |
var _ fs.Node = (*invalidateDataPartial)(nil) |
| 2223 |
|
| 2224 |
func (i *invalidateDataPartial) Attr(ctx context.Context, a *fuse.Attr) error { |
| 2225 |
i.attr.Inc() |
| 2226 |
i.t.Logf("Attr called, #%d", i.attr.Count()) |
| 2227 |
a.Mode = 0600 |
| 2228 |
a.Size = uint64(len(invalidateDataPartialContent)) |
| 2229 |
return nil |
| 2230 |
} |
| 2231 |
|
| 2232 |
var _ fs.HandleReader = (*invalidateDataPartial)(nil) |
| 2233 |
|
| 2234 |
func (i *invalidateDataPartial) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error { |
| 2235 |
i.read.Inc() |
| 2236 |
i.t.Logf("Read called, #%d", i.read.Count()) |
| 2237 |
fuseutil.HandleRead(req, resp, []byte(invalidateDataPartialContent)) |
| 2238 |
return nil |
| 2239 |
} |
| 2240 |
|
| 2241 |
func TestInvalidateNodeDataRange(t *testing.T) { |
| 2242 |
|
| 2243 |
|
| 2244 |
t.Parallel() |
| 2245 |
a := &invalidateDataPartial{ |
| 2246 |
t: t, |
| 2247 |
} |
| 2248 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{fstestutil.ChildMap{"child": a}}) |
| 2249 |
if err != nil { |
| 2250 |
t.Fatal(err) |
| 2251 |
} |
| 2252 |
defer mnt.Close() |
| 2253 |
|
| 2254 |
if !mnt.Conn.Protocol().HasInvalidate() { |
| 2255 |
t.Skip("Old FUSE protocol") |
| 2256 |
} |
| 2257 |
|
| 2258 |
f, err := os.Open(mnt.Dir + "/child") |
| 2259 |
if err != nil { |
| 2260 |
t.Fatal(err) |
| 2261 |
} |
| 2262 |
defer f.Close() |
| 2263 |
|
| 2264 |
buf := make([]byte, 4) |
| 2265 |
for i := 0; i < 10; i++ { |
| 2266 |
if _, err := f.ReadAt(buf, 0); err != nil { |
| 2267 |
t.Fatalf("readat error: %v", err) |
| 2268 |
} |
| 2269 |
} |
| 2270 |
if g, e := a.attr.Count(), uint32(1); g != e { |
| 2271 |
t.Errorf("wrong Attr call count: %d != %d", g, e) |
| 2272 |
} |
| 2273 |
if g, e := a.read.Count(), uint32(1); g != e { |
| 2274 |
t.Errorf("wrong Read call count: %d != %d", g, e) |
| 2275 |
} |
| 2276 |
|
| 2277 |
t.Logf("invalidating...") |
| 2278 |
if err := mnt.Server.InvalidateNodeDataRange(a, 4096, 4096); err != nil { |
| 2279 |
t.Fatalf("invalidate error: %v", err) |
| 2280 |
} |
| 2281 |
|
| 2282 |
for i := 0; i < 10; i++ { |
| 2283 |
if _, err := f.ReadAt(buf, 0); err != nil { |
| 2284 |
t.Fatalf("readat error: %v", err) |
| 2285 |
} |
| 2286 |
} |
| 2287 |
if g, e := a.attr.Count(), uint32(1); g != e { |
| 2288 |
t.Errorf("wrong Attr call count: %d != %d", g, e) |
| 2289 |
} |
| 2290 |
|
| 2291 |
|
| 2292 |
if g, e := a.read.Count(), uint32(1); g != e { |
| 2293 |
t.Errorf("wrong Read call count: %d != %d", g, e) |
| 2294 |
} |
| 2295 |
} |
| 2296 |
|
| 2297 |
type invalidateEntryRoot struct { |
| 2298 |
fs.NodeRef |
| 2299 |
t testing.TB |
| 2300 |
lookup record.Counter |
| 2301 |
} |
| 2302 |
|
| 2303 |
var _ fs.Node = (*invalidateEntryRoot)(nil) |
| 2304 |
|
| 2305 |
func (i *invalidateEntryRoot) Attr(ctx context.Context, a *fuse.Attr) error { |
| 2306 |
a.Mode = 0600 | os.ModeDir |
| 2307 |
return nil |
| 2308 |
} |
| 2309 |
|
| 2310 |
var _ fs.NodeStringLookuper = (*invalidateEntryRoot)(nil) |
| 2311 |
|
| 2312 |
func (i *invalidateEntryRoot) Lookup(ctx context.Context, name string) (fs.Node, error) { |
| 2313 |
if name != "child" { |
| 2314 |
return nil, fuse.ENOENT |
| 2315 |
} |
| 2316 |
i.lookup.Inc() |
| 2317 |
i.t.Logf("Lookup called, #%d", i.lookup.Count()) |
| 2318 |
return fstestutil.File{}, nil |
| 2319 |
} |
| 2320 |
|
| 2321 |
func TestInvalidateEntry(t *testing.T) { |
| 2322 |
|
| 2323 |
|
| 2324 |
t.Parallel() |
| 2325 |
a := &invalidateEntryRoot{ |
| 2326 |
t: t, |
| 2327 |
} |
| 2328 |
mnt, err := fstestutil.MountedT(t, fstestutil.SimpleFS{a}) |
| 2329 |
if err != nil { |
| 2330 |
t.Fatal(err) |
| 2331 |
} |
| 2332 |
defer mnt.Close() |
| 2333 |
|
| 2334 |
if !mnt.Conn.Protocol().HasInvalidate() { |
| 2335 |
t.Skip("Old FUSE protocol") |
| 2336 |
} |
| 2337 |
|
| 2338 |
for i := 0; i < 10; i++ { |
| 2339 |
if _, err := os.Stat(mnt.Dir + "/child"); err != nil { |
| 2340 |
t.Fatalf("stat error: %v", err) |
| 2341 |
} |
| 2342 |
} |
| 2343 |
if g, e := a.lookup.Count(), uint32(1); g != e { |
| 2344 |
t.Errorf("wrong Lookup call count: %d != %d", g, e) |
| 2345 |
} |
| 2346 |
|
| 2347 |
t.Logf("invalidating...") |
| 2348 |
if err := mnt.Server.InvalidateEntry(a, "child"); err != nil { |
| 2349 |
t.Fatalf("invalidate error: %v", err) |
| 2350 |
} |
| 2351 |
|
| 2352 |
for i := 0; i < 10; i++ { |
| 2353 |
if _, err := os.Stat(mnt.Dir + "/child"); err != nil { |
| 2354 |
t.Fatalf("stat error: %v", err) |
| 2355 |
} |
| 2356 |
} |
| 2357 |
if g, e := a.lookup.Count(), uint32(2); g != e { |
| 2358 |
t.Errorf("wrong Lookup call count: %d != %d", g, e) |
| 2359 |
} |
| 2360 |
} |