github.com/MerlinKodo/gvisor@v0.0.0-20231110090155-957f62ecf90e/pkg/p9/messages.go (about) 1 // Copyright 2018 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package p9 16 17 import ( 18 "fmt" 19 "math" 20 21 "github.com/MerlinKodo/gvisor/pkg/fd" 22 "github.com/MerlinKodo/gvisor/pkg/log" 23 ) 24 25 // ErrInvalidMsgType is returned when an unsupported message type is found. 26 type ErrInvalidMsgType struct { 27 MsgType 28 } 29 30 // Error returns a useful string. 31 func (e *ErrInvalidMsgType) Error() string { 32 return fmt.Sprintf("invalid message type: %d", e.MsgType) 33 } 34 35 // message is a generic 9P message. 36 type message interface { 37 encoder 38 fmt.Stringer 39 40 // Type returns the message type number. 41 Type() MsgType 42 } 43 44 // payloader is a special message which may include an inline payload. 45 type payloader interface { 46 // FixedSize returns the size of the fixed portion of this message. 47 FixedSize() uint32 48 49 // Payload returns the payload for sending. 50 Payload() []byte 51 52 // SetPayload returns the decoded message. 53 // 54 // This is going to be total message size - FixedSize. But this should 55 // be validated during decode, which will be called after SetPayload. 56 SetPayload([]byte) 57 } 58 59 // filer is a message capable of passing a file. 60 type filer interface { 61 // FilePayload returns the file payload. 62 FilePayload() *fd.FD 63 64 // SetFilePayload sets the file payload. 65 SetFilePayload(*fd.FD) 66 } 67 68 // filePayload embeds a File object. 69 type filePayload struct { 70 File *fd.FD 71 } 72 73 // FilePayload returns the file payload. 74 func (f *filePayload) FilePayload() *fd.FD { 75 return f.File 76 } 77 78 // SetFilePayload sets the received file. 79 func (f *filePayload) SetFilePayload(file *fd.FD) { 80 f.File = file 81 } 82 83 // Tversion is a version request. 84 type Tversion struct { 85 // MSize is the message size to use. 86 MSize uint32 87 88 // Version is the version string. 89 // 90 // For this implementation, this must be 9P2000.L. 91 Version string 92 } 93 94 // decode implements encoder.decode. 95 func (t *Tversion) decode(b *buffer) { 96 t.MSize = b.Read32() 97 t.Version = b.ReadString() 98 } 99 100 // encode implements encoder.encode. 101 func (t *Tversion) encode(b *buffer) { 102 b.Write32(t.MSize) 103 b.WriteString(t.Version) 104 } 105 106 // Type implements message.Type. 107 func (*Tversion) Type() MsgType { 108 return MsgTversion 109 } 110 111 // String implements fmt.Stringer. 112 func (t *Tversion) String() string { 113 return fmt.Sprintf("Tversion{MSize: %d, Version: %s}", t.MSize, t.Version) 114 } 115 116 // Rversion is a version response. 117 type Rversion struct { 118 // MSize is the negotiated size. 119 MSize uint32 120 121 // Version is the negotiated version. 122 Version string 123 } 124 125 // decode implements encoder.decode. 126 func (r *Rversion) decode(b *buffer) { 127 r.MSize = b.Read32() 128 r.Version = b.ReadString() 129 } 130 131 // encode implements encoder.encode. 132 func (r *Rversion) encode(b *buffer) { 133 b.Write32(r.MSize) 134 b.WriteString(r.Version) 135 } 136 137 // Type implements message.Type. 138 func (*Rversion) Type() MsgType { 139 return MsgRversion 140 } 141 142 // String implements fmt.Stringer. 143 func (r *Rversion) String() string { 144 return fmt.Sprintf("Rversion{MSize: %d, Version: %s}", r.MSize, r.Version) 145 } 146 147 // Tflush is a flush request. 148 type Tflush struct { 149 // OldTag is the tag to wait on. 150 OldTag Tag 151 } 152 153 // decode implements encoder.decode. 154 func (t *Tflush) decode(b *buffer) { 155 t.OldTag = b.ReadTag() 156 } 157 158 // encode implements encoder.encode. 159 func (t *Tflush) encode(b *buffer) { 160 b.WriteTag(t.OldTag) 161 } 162 163 // Type implements message.Type. 164 func (*Tflush) Type() MsgType { 165 return MsgTflush 166 } 167 168 // String implements fmt.Stringer. 169 func (t *Tflush) String() string { 170 return fmt.Sprintf("Tflush{OldTag: %d}", t.OldTag) 171 } 172 173 // Rflush is a flush response. 174 type Rflush struct { 175 } 176 177 // decode implements encoder.decode. 178 func (*Rflush) decode(*buffer) { 179 } 180 181 // encode implements encoder.encode. 182 func (*Rflush) encode(*buffer) { 183 } 184 185 // Type implements message.Type. 186 func (*Rflush) Type() MsgType { 187 return MsgRflush 188 } 189 190 // String implements fmt.Stringer. 191 func (r *Rflush) String() string { 192 return "RFlush{}" 193 } 194 195 // Twalk is a walk request. 196 type Twalk struct { 197 // FID is the FID to be walked. 198 FID FID 199 200 // NewFID is the resulting FID. 201 NewFID FID 202 203 // Names are the set of names to be walked. 204 Names []string 205 } 206 207 // decode implements encoder.decode. 208 func (t *Twalk) decode(b *buffer) { 209 t.FID = b.ReadFID() 210 t.NewFID = b.ReadFID() 211 n := b.Read16() 212 t.Names = t.Names[:0] 213 for i := 0; i < int(n); i++ { 214 t.Names = append(t.Names, b.ReadString()) 215 } 216 } 217 218 // encode implements encoder.encode. 219 func (t *Twalk) encode(b *buffer) { 220 b.WriteFID(t.FID) 221 b.WriteFID(t.NewFID) 222 b.Write16(uint16(len(t.Names))) 223 for _, name := range t.Names { 224 b.WriteString(name) 225 } 226 } 227 228 // Type implements message.Type. 229 func (*Twalk) Type() MsgType { 230 return MsgTwalk 231 } 232 233 // String implements fmt.Stringer. 234 func (t *Twalk) String() string { 235 return fmt.Sprintf("Twalk{FID: %d, NewFID: %d, Names: %v}", t.FID, t.NewFID, t.Names) 236 } 237 238 // Rwalk is a walk response. 239 type Rwalk struct { 240 // QIDs are the set of QIDs returned. 241 QIDs []QID 242 } 243 244 // decode implements encoder.decode. 245 func (r *Rwalk) decode(b *buffer) { 246 n := b.Read16() 247 r.QIDs = r.QIDs[:0] 248 for i := 0; i < int(n); i++ { 249 var q QID 250 q.decode(b) 251 r.QIDs = append(r.QIDs, q) 252 } 253 } 254 255 // encode implements encoder.encode. 256 func (r *Rwalk) encode(b *buffer) { 257 b.Write16(uint16(len(r.QIDs))) 258 for i := range r.QIDs { 259 r.QIDs[i].encode(b) 260 } 261 } 262 263 // Type implements message.Type. 264 func (*Rwalk) Type() MsgType { 265 return MsgRwalk 266 } 267 268 // String implements fmt.Stringer. 269 func (r *Rwalk) String() string { 270 return fmt.Sprintf("Rwalk{QIDs: %v}", r.QIDs) 271 } 272 273 // Tclunk is a close request. 274 type Tclunk struct { 275 // FID is the FID to be closed. 276 FID FID 277 } 278 279 // decode implements encoder.decode. 280 func (t *Tclunk) decode(b *buffer) { 281 t.FID = b.ReadFID() 282 } 283 284 // encode implements encoder.encode. 285 func (t *Tclunk) encode(b *buffer) { 286 b.WriteFID(t.FID) 287 } 288 289 // Type implements message.Type. 290 func (*Tclunk) Type() MsgType { 291 return MsgTclunk 292 } 293 294 // String implements fmt.Stringer. 295 func (t *Tclunk) String() string { 296 return fmt.Sprintf("Tclunk{FID: %d}", t.FID) 297 } 298 299 // Rclunk is a close response. 300 type Rclunk struct { 301 } 302 303 // decode implements encoder.decode. 304 func (*Rclunk) decode(*buffer) { 305 } 306 307 // encode implements encoder.encode. 308 func (*Rclunk) encode(*buffer) { 309 } 310 311 // Type implements message.Type. 312 func (*Rclunk) Type() MsgType { 313 return MsgRclunk 314 } 315 316 // String implements fmt.Stringer. 317 func (r *Rclunk) String() string { 318 return "Rclunk{}" 319 } 320 321 // Tsetattrclunk is a setattr+close request. 322 type Tsetattrclunk struct { 323 // FID is the FID to change. 324 FID FID 325 326 // Valid is the set of bits which will be used. 327 Valid SetAttrMask 328 329 // SetAttr is the set request. 330 SetAttr SetAttr 331 } 332 333 // decode implements encoder.decode. 334 func (t *Tsetattrclunk) decode(b *buffer) { 335 t.FID = b.ReadFID() 336 t.Valid.decode(b) 337 t.SetAttr.decode(b) 338 } 339 340 // encode implements encoder.encode. 341 func (t *Tsetattrclunk) encode(b *buffer) { 342 b.WriteFID(t.FID) 343 t.Valid.encode(b) 344 t.SetAttr.encode(b) 345 } 346 347 // Type implements message.Type. 348 func (*Tsetattrclunk) Type() MsgType { 349 return MsgTsetattrclunk 350 } 351 352 // String implements fmt.Stringer. 353 func (t *Tsetattrclunk) String() string { 354 return fmt.Sprintf("Tsetattrclunk{FID: %d, Valid: %v, SetAttr: %s}", t.FID, t.Valid, t.SetAttr) 355 } 356 357 // Rsetattrclunk is a setattr+close response. 358 type Rsetattrclunk struct { 359 } 360 361 // decode implements encoder.decode. 362 func (*Rsetattrclunk) decode(*buffer) { 363 } 364 365 // encode implements encoder.encode. 366 func (*Rsetattrclunk) encode(*buffer) { 367 } 368 369 // Type implements message.Type. 370 func (*Rsetattrclunk) Type() MsgType { 371 return MsgRsetattrclunk 372 } 373 374 // String implements fmt.Stringer. 375 func (r *Rsetattrclunk) String() string { 376 return "Rsetattrclunk{}" 377 } 378 379 // Tremove is a remove request. 380 // 381 // This will eventually be replaced by Tunlinkat. 382 type Tremove struct { 383 // FID is the FID to be removed. 384 FID FID 385 } 386 387 // decode implements encoder.decode. 388 func (t *Tremove) decode(b *buffer) { 389 t.FID = b.ReadFID() 390 } 391 392 // encode implements encoder.encode. 393 func (t *Tremove) encode(b *buffer) { 394 b.WriteFID(t.FID) 395 } 396 397 // Type implements message.Type. 398 func (*Tremove) Type() MsgType { 399 return MsgTremove 400 } 401 402 // String implements fmt.Stringer. 403 func (t *Tremove) String() string { 404 return fmt.Sprintf("Tremove{FID: %d}", t.FID) 405 } 406 407 // Rremove is a remove response. 408 type Rremove struct { 409 } 410 411 // decode implements encoder.decode. 412 func (*Rremove) decode(*buffer) { 413 } 414 415 // encode implements encoder.encode. 416 func (*Rremove) encode(*buffer) { 417 } 418 419 // Type implements message.Type. 420 func (*Rremove) Type() MsgType { 421 return MsgRremove 422 } 423 424 // String implements fmt.Stringer. 425 func (r *Rremove) String() string { 426 return "Rremove{}" 427 } 428 429 // Rlerror is an error response. 430 // 431 // Note that this replaces the error code used in 9p. 432 type Rlerror struct { 433 Error uint32 434 } 435 436 // decode implements encoder.decode. 437 func (r *Rlerror) decode(b *buffer) { 438 r.Error = b.Read32() 439 } 440 441 // encode implements encoder.encode. 442 func (r *Rlerror) encode(b *buffer) { 443 b.Write32(r.Error) 444 } 445 446 // Type implements message.Type. 447 func (*Rlerror) Type() MsgType { 448 return MsgRlerror 449 } 450 451 // String implements fmt.Stringer. 452 func (r *Rlerror) String() string { 453 return fmt.Sprintf("Rlerror{Error: %d}", r.Error) 454 } 455 456 // Tauth is an authentication request. 457 type Tauth struct { 458 // AuthenticationFID is the FID to attach the authentication result. 459 AuthenticationFID FID 460 461 // UserName is the user to attach. 462 UserName string 463 464 // AttachName is the attach name. 465 AttachName string 466 467 // UserID is the numeric identifier for UserName. 468 UID UID 469 } 470 471 // decode implements encoder.decode. 472 func (t *Tauth) decode(b *buffer) { 473 t.AuthenticationFID = b.ReadFID() 474 t.UserName = b.ReadString() 475 t.AttachName = b.ReadString() 476 t.UID = b.ReadUID() 477 } 478 479 // encode implements encoder.encode. 480 func (t *Tauth) encode(b *buffer) { 481 b.WriteFID(t.AuthenticationFID) 482 b.WriteString(t.UserName) 483 b.WriteString(t.AttachName) 484 b.WriteUID(t.UID) 485 } 486 487 // Type implements message.Type. 488 func (*Tauth) Type() MsgType { 489 return MsgTauth 490 } 491 492 // String implements fmt.Stringer. 493 func (t *Tauth) String() string { 494 return fmt.Sprintf("Tauth{AuthFID: %d, UserName: %s, AttachName: %s, UID: %d", t.AuthenticationFID, t.UserName, t.AttachName, t.UID) 495 } 496 497 // Rauth is an authentication response. 498 // 499 // encode and decode are inherited directly from QID. 500 type Rauth struct { 501 QID 502 } 503 504 // Type implements message.Type. 505 func (*Rauth) Type() MsgType { 506 return MsgRauth 507 } 508 509 // String implements fmt.Stringer. 510 func (r *Rauth) String() string { 511 return fmt.Sprintf("Rauth{QID: %s}", r.QID) 512 } 513 514 // Tattach is an attach request. 515 type Tattach struct { 516 // FID is the FID to be attached. 517 FID FID 518 519 // Auth is the embedded authentication request. 520 // 521 // See client.Attach for information regarding authentication. 522 Auth Tauth 523 } 524 525 // decode implements encoder.decode. 526 func (t *Tattach) decode(b *buffer) { 527 t.FID = b.ReadFID() 528 t.Auth.decode(b) 529 } 530 531 // encode implements encoder.encode. 532 func (t *Tattach) encode(b *buffer) { 533 b.WriteFID(t.FID) 534 t.Auth.encode(b) 535 } 536 537 // Type implements message.Type. 538 func (*Tattach) Type() MsgType { 539 return MsgTattach 540 } 541 542 // String implements fmt.Stringer. 543 func (t *Tattach) String() string { 544 return fmt.Sprintf("Tattach{FID: %d, AuthFID: %d, UserName: %s, AttachName: %s, UID: %d}", t.FID, t.Auth.AuthenticationFID, t.Auth.UserName, t.Auth.AttachName, t.Auth.UID) 545 } 546 547 // Rattach is an attach response. 548 type Rattach struct { 549 QID 550 } 551 552 // Type implements message.Type. 553 func (*Rattach) Type() MsgType { 554 return MsgRattach 555 } 556 557 // String implements fmt.Stringer. 558 func (r *Rattach) String() string { 559 return fmt.Sprintf("Rattach{QID: %s}", r.QID) 560 } 561 562 // Tlopen is an open request. 563 type Tlopen struct { 564 // FID is the FID to be opened. 565 FID FID 566 567 // Flags are the open flags. 568 Flags OpenFlags 569 } 570 571 // decode implements encoder.decode. 572 func (t *Tlopen) decode(b *buffer) { 573 t.FID = b.ReadFID() 574 t.Flags = b.ReadOpenFlags() 575 } 576 577 // encode implements encoder.encode. 578 func (t *Tlopen) encode(b *buffer) { 579 b.WriteFID(t.FID) 580 b.WriteOpenFlags(t.Flags) 581 } 582 583 // Type implements message.Type. 584 func (*Tlopen) Type() MsgType { 585 return MsgTlopen 586 } 587 588 // String implements fmt.Stringer. 589 func (t *Tlopen) String() string { 590 return fmt.Sprintf("Tlopen{FID: %d, Flags: %v}", t.FID, t.Flags) 591 } 592 593 // Rlopen is a open response. 594 type Rlopen struct { 595 // QID is the file's QID. 596 QID QID 597 598 // IoUnit is the recommended I/O unit. 599 IoUnit uint32 600 601 filePayload 602 } 603 604 // decode implements encoder.decode. 605 func (r *Rlopen) decode(b *buffer) { 606 r.QID.decode(b) 607 r.IoUnit = b.Read32() 608 } 609 610 // encode implements encoder.encode. 611 func (r *Rlopen) encode(b *buffer) { 612 r.QID.encode(b) 613 b.Write32(r.IoUnit) 614 } 615 616 // Type implements message.Type. 617 func (*Rlopen) Type() MsgType { 618 return MsgRlopen 619 } 620 621 // String implements fmt.Stringer. 622 func (r *Rlopen) String() string { 623 return fmt.Sprintf("Rlopen{QID: %s, IoUnit: %d, File: %v}", r.QID, r.IoUnit, r.File) 624 } 625 626 // Tlcreate is a create request. 627 type Tlcreate struct { 628 // FID is the parent FID. 629 // 630 // This becomes the new file. 631 FID FID 632 633 // Name is the file name to create. 634 Name string 635 636 // Mode is the open mode (O_RDWR, etc.). 637 // 638 // Note that flags like O_TRUNC are ignored, as is O_EXCL. All 639 // create operations are exclusive. 640 OpenFlags OpenFlags 641 642 // Permissions is the set of permission bits. 643 Permissions FileMode 644 645 // GID is the group ID to use for creating the file. 646 GID GID 647 } 648 649 // decode implements encoder.decode. 650 func (t *Tlcreate) decode(b *buffer) { 651 t.FID = b.ReadFID() 652 t.Name = b.ReadString() 653 t.OpenFlags = b.ReadOpenFlags() 654 t.Permissions = b.ReadPermissions() 655 t.GID = b.ReadGID() 656 } 657 658 // encode implements encoder.encode. 659 func (t *Tlcreate) encode(b *buffer) { 660 b.WriteFID(t.FID) 661 b.WriteString(t.Name) 662 b.WriteOpenFlags(t.OpenFlags) 663 b.WritePermissions(t.Permissions) 664 b.WriteGID(t.GID) 665 } 666 667 // Type implements message.Type. 668 func (*Tlcreate) Type() MsgType { 669 return MsgTlcreate 670 } 671 672 // String implements fmt.Stringer. 673 func (t *Tlcreate) String() string { 674 return fmt.Sprintf("Tlcreate{FID: %d, Name: %s, OpenFlags: %s, Permissions: 0o%o, GID: %d}", t.FID, t.Name, t.OpenFlags, t.Permissions, t.GID) 675 } 676 677 // Rlcreate is a create response. 678 // 679 // The encode, decode, etc. methods are inherited from Rlopen. 680 type Rlcreate struct { 681 Rlopen 682 } 683 684 // Type implements message.Type. 685 func (*Rlcreate) Type() MsgType { 686 return MsgRlcreate 687 } 688 689 // String implements fmt.Stringer. 690 func (r *Rlcreate) String() string { 691 return fmt.Sprintf("Rlcreate{QID: %s, IoUnit: %d, File: %v}", r.QID, r.IoUnit, r.File) 692 } 693 694 // Tsymlink is a symlink request. 695 type Tsymlink struct { 696 // Directory is the directory FID. 697 Directory FID 698 699 // Name is the new in the directory. 700 Name string 701 702 // Target is the symlink target. 703 Target string 704 705 // GID is the owning group. 706 GID GID 707 } 708 709 // decode implements encoder.decode. 710 func (t *Tsymlink) decode(b *buffer) { 711 t.Directory = b.ReadFID() 712 t.Name = b.ReadString() 713 t.Target = b.ReadString() 714 t.GID = b.ReadGID() 715 } 716 717 // encode implements encoder.encode. 718 func (t *Tsymlink) encode(b *buffer) { 719 b.WriteFID(t.Directory) 720 b.WriteString(t.Name) 721 b.WriteString(t.Target) 722 b.WriteGID(t.GID) 723 } 724 725 // Type implements message.Type. 726 func (*Tsymlink) Type() MsgType { 727 return MsgTsymlink 728 } 729 730 // String implements fmt.Stringer. 731 func (t *Tsymlink) String() string { 732 return fmt.Sprintf("Tsymlink{DirectoryFID: %d, Name: %s, Target: %s, GID: %d}", t.Directory, t.Name, t.Target, t.GID) 733 } 734 735 // Rsymlink is a symlink response. 736 type Rsymlink struct { 737 // QID is the new symlink's QID. 738 QID QID 739 } 740 741 // decode implements encoder.decode. 742 func (r *Rsymlink) decode(b *buffer) { 743 r.QID.decode(b) 744 } 745 746 // encode implements encoder.encode. 747 func (r *Rsymlink) encode(b *buffer) { 748 r.QID.encode(b) 749 } 750 751 // Type implements message.Type. 752 func (*Rsymlink) Type() MsgType { 753 return MsgRsymlink 754 } 755 756 // String implements fmt.Stringer. 757 func (r *Rsymlink) String() string { 758 return fmt.Sprintf("Rsymlink{QID: %s}", r.QID) 759 } 760 761 // Tlink is a link request. 762 type Tlink struct { 763 // Directory is the directory to contain the link. 764 Directory FID 765 766 // FID is the target. 767 Target FID 768 769 // Name is the new source name. 770 Name string 771 } 772 773 // decode implements encoder.decode. 774 func (t *Tlink) decode(b *buffer) { 775 t.Directory = b.ReadFID() 776 t.Target = b.ReadFID() 777 t.Name = b.ReadString() 778 } 779 780 // encode implements encoder.encode. 781 func (t *Tlink) encode(b *buffer) { 782 b.WriteFID(t.Directory) 783 b.WriteFID(t.Target) 784 b.WriteString(t.Name) 785 } 786 787 // Type implements message.Type. 788 func (*Tlink) Type() MsgType { 789 return MsgTlink 790 } 791 792 // String implements fmt.Stringer. 793 func (t *Tlink) String() string { 794 return fmt.Sprintf("Tlink{DirectoryFID: %d, TargetFID: %d, Name: %s}", t.Directory, t.Target, t.Name) 795 } 796 797 // Rlink is a link response. 798 type Rlink struct { 799 } 800 801 // Type implements message.Type. 802 func (*Rlink) Type() MsgType { 803 return MsgRlink 804 } 805 806 // decode implements encoder.decode. 807 func (*Rlink) decode(*buffer) { 808 } 809 810 // encode implements encoder.encode. 811 func (*Rlink) encode(*buffer) { 812 } 813 814 // String implements fmt.Stringer. 815 func (r *Rlink) String() string { 816 return "Rlink{}" 817 } 818 819 // Trenameat is a rename request. 820 type Trenameat struct { 821 // OldDirectory is the source directory. 822 OldDirectory FID 823 824 // OldName is the source file name. 825 OldName string 826 827 // NewDirectory is the target directory. 828 NewDirectory FID 829 830 // NewName is the new file name. 831 NewName string 832 } 833 834 // decode implements encoder.decode. 835 func (t *Trenameat) decode(b *buffer) { 836 t.OldDirectory = b.ReadFID() 837 t.OldName = b.ReadString() 838 t.NewDirectory = b.ReadFID() 839 t.NewName = b.ReadString() 840 } 841 842 // encode implements encoder.encode. 843 func (t *Trenameat) encode(b *buffer) { 844 b.WriteFID(t.OldDirectory) 845 b.WriteString(t.OldName) 846 b.WriteFID(t.NewDirectory) 847 b.WriteString(t.NewName) 848 } 849 850 // Type implements message.Type. 851 func (*Trenameat) Type() MsgType { 852 return MsgTrenameat 853 } 854 855 // String implements fmt.Stringer. 856 func (t *Trenameat) String() string { 857 return fmt.Sprintf("TrenameAt{OldDirectoryFID: %d, OldName: %s, NewDirectoryFID: %d, NewName: %s}", t.OldDirectory, t.OldName, t.NewDirectory, t.NewName) 858 } 859 860 // Rrenameat is a rename response. 861 type Rrenameat struct { 862 } 863 864 // decode implements encoder.decode. 865 func (*Rrenameat) decode(*buffer) { 866 } 867 868 // encode implements encoder.encode. 869 func (*Rrenameat) encode(*buffer) { 870 } 871 872 // Type implements message.Type. 873 func (*Rrenameat) Type() MsgType { 874 return MsgRrenameat 875 } 876 877 // String implements fmt.Stringer. 878 func (r *Rrenameat) String() string { 879 return "Rrenameat{}" 880 } 881 882 // Tunlinkat is an unlink request. 883 type Tunlinkat struct { 884 // Directory is the originating directory. 885 Directory FID 886 887 // Name is the name of the entry to unlink. 888 Name string 889 890 // Flags are extra flags (e.g. O_DIRECTORY). These are not interpreted by p9. 891 Flags uint32 892 } 893 894 // decode implements encoder.decode. 895 func (t *Tunlinkat) decode(b *buffer) { 896 t.Directory = b.ReadFID() 897 t.Name = b.ReadString() 898 t.Flags = b.Read32() 899 } 900 901 // encode implements encoder.encode. 902 func (t *Tunlinkat) encode(b *buffer) { 903 b.WriteFID(t.Directory) 904 b.WriteString(t.Name) 905 b.Write32(t.Flags) 906 } 907 908 // Type implements message.Type. 909 func (*Tunlinkat) Type() MsgType { 910 return MsgTunlinkat 911 } 912 913 // String implements fmt.Stringer. 914 func (t *Tunlinkat) String() string { 915 return fmt.Sprintf("Tunlinkat{DirectoryFID: %d, Name: %s, Flags: 0x%X}", t.Directory, t.Name, t.Flags) 916 } 917 918 // Runlinkat is an unlink response. 919 type Runlinkat struct { 920 } 921 922 // decode implements encoder.decode. 923 func (*Runlinkat) decode(*buffer) { 924 } 925 926 // encode implements encoder.encode. 927 func (*Runlinkat) encode(*buffer) { 928 } 929 930 // Type implements message.Type. 931 func (*Runlinkat) Type() MsgType { 932 return MsgRunlinkat 933 } 934 935 // String implements fmt.Stringer. 936 func (r *Runlinkat) String() string { 937 return "Runlinkat{}" 938 } 939 940 // Trename is a rename request. 941 // 942 // Note that this generally isn't used anymore, and ideally all rename calls 943 // should Trenameat below. 944 type Trename struct { 945 // FID is the FID to rename. 946 FID FID 947 948 // Directory is the target directory. 949 Directory FID 950 951 // Name is the new file name. 952 Name string 953 } 954 955 // decode implements encoder.decode. 956 func (t *Trename) decode(b *buffer) { 957 t.FID = b.ReadFID() 958 t.Directory = b.ReadFID() 959 t.Name = b.ReadString() 960 } 961 962 // encode implements encoder.encode. 963 func (t *Trename) encode(b *buffer) { 964 b.WriteFID(t.FID) 965 b.WriteFID(t.Directory) 966 b.WriteString(t.Name) 967 } 968 969 // Type implements message.Type. 970 func (*Trename) Type() MsgType { 971 return MsgTrename 972 } 973 974 // String implements fmt.Stringer. 975 func (t *Trename) String() string { 976 return fmt.Sprintf("Trename{FID: %d, DirectoryFID: %d, Name: %s}", t.FID, t.Directory, t.Name) 977 } 978 979 // Rrename is a rename response. 980 type Rrename struct { 981 } 982 983 // decode implements encoder.decode. 984 func (*Rrename) decode(*buffer) { 985 } 986 987 // encode implements encoder.encode. 988 func (*Rrename) encode(*buffer) { 989 } 990 991 // Type implements message.Type. 992 func (*Rrename) Type() MsgType { 993 return MsgRrename 994 } 995 996 // String implements fmt.Stringer. 997 func (r *Rrename) String() string { 998 return "Rrename{}" 999 } 1000 1001 // Treadlink is a readlink request. 1002 type Treadlink struct { 1003 // FID is the symlink. 1004 FID FID 1005 } 1006 1007 // decode implements encoder.decode. 1008 func (t *Treadlink) decode(b *buffer) { 1009 t.FID = b.ReadFID() 1010 } 1011 1012 // encode implements encoder.encode. 1013 func (t *Treadlink) encode(b *buffer) { 1014 b.WriteFID(t.FID) 1015 } 1016 1017 // Type implements message.Type. 1018 func (*Treadlink) Type() MsgType { 1019 return MsgTreadlink 1020 } 1021 1022 // String implements fmt.Stringer. 1023 func (t *Treadlink) String() string { 1024 return fmt.Sprintf("Treadlink{FID: %d}", t.FID) 1025 } 1026 1027 // Rreadlink is a readlink response. 1028 type Rreadlink struct { 1029 // Target is the symlink target. 1030 Target string 1031 } 1032 1033 // decode implements encoder.decode. 1034 func (r *Rreadlink) decode(b *buffer) { 1035 r.Target = b.ReadString() 1036 } 1037 1038 // encode implements encoder.encode. 1039 func (r *Rreadlink) encode(b *buffer) { 1040 b.WriteString(r.Target) 1041 } 1042 1043 // Type implements message.Type. 1044 func (*Rreadlink) Type() MsgType { 1045 return MsgRreadlink 1046 } 1047 1048 // String implements fmt.Stringer. 1049 func (r *Rreadlink) String() string { 1050 return fmt.Sprintf("Rreadlink{Target: %s}", r.Target) 1051 } 1052 1053 // Tread is a read request. 1054 type Tread struct { 1055 // FID is the FID to read. 1056 FID FID 1057 1058 // Offset indicates the file offset. 1059 Offset uint64 1060 1061 // Count indicates the number of bytes to read. 1062 Count uint32 1063 } 1064 1065 // decode implements encoder.decode. 1066 func (t *Tread) decode(b *buffer) { 1067 t.FID = b.ReadFID() 1068 t.Offset = b.Read64() 1069 t.Count = b.Read32() 1070 } 1071 1072 // encode implements encoder.encode. 1073 func (t *Tread) encode(b *buffer) { 1074 b.WriteFID(t.FID) 1075 b.Write64(t.Offset) 1076 b.Write32(t.Count) 1077 } 1078 1079 // Type implements message.Type. 1080 func (*Tread) Type() MsgType { 1081 return MsgTread 1082 } 1083 1084 // String implements fmt.Stringer. 1085 func (t *Tread) String() string { 1086 return fmt.Sprintf("Tread{FID: %d, Offset: %d, Count: %d}", t.FID, t.Offset, t.Count) 1087 } 1088 1089 // Rread is the response for a Tread. 1090 type Rread struct { 1091 // Data is the resulting data. 1092 Data []byte 1093 } 1094 1095 // decode implements encoder.decode. 1096 // 1097 // Data is automatically decoded via Payload. 1098 func (r *Rread) decode(b *buffer) { 1099 count := b.Read32() 1100 if count != uint32(len(r.Data)) { 1101 b.markOverrun() 1102 } 1103 } 1104 1105 // encode implements encoder.encode. 1106 // 1107 // Data is automatically encoded via Payload. 1108 func (r *Rread) encode(b *buffer) { 1109 b.Write32(uint32(len(r.Data))) 1110 } 1111 1112 // Type implements message.Type. 1113 func (*Rread) Type() MsgType { 1114 return MsgRread 1115 } 1116 1117 // FixedSize implements payloader.FixedSize. 1118 func (*Rread) FixedSize() uint32 { 1119 return 4 1120 } 1121 1122 // Payload implements payloader.Payload. 1123 func (r *Rread) Payload() []byte { 1124 return r.Data 1125 } 1126 1127 // SetPayload implements payloader.SetPayload. 1128 func (r *Rread) SetPayload(p []byte) { 1129 r.Data = p 1130 } 1131 1132 // String implements fmt.Stringer. 1133 func (r *Rread) String() string { 1134 return fmt.Sprintf("Rread{len(Data): %d}", len(r.Data)) 1135 } 1136 1137 // Twrite is a write request. 1138 type Twrite struct { 1139 // FID is the FID to read. 1140 FID FID 1141 1142 // Offset indicates the file offset. 1143 Offset uint64 1144 1145 // Data is the data to be written. 1146 Data []byte 1147 } 1148 1149 // decode implements encoder.decode. 1150 func (t *Twrite) decode(b *buffer) { 1151 t.FID = b.ReadFID() 1152 t.Offset = b.Read64() 1153 count := b.Read32() 1154 if count != uint32(len(t.Data)) { 1155 b.markOverrun() 1156 } 1157 } 1158 1159 // encode implements encoder.encode. 1160 // 1161 // This uses the buffer payload to avoid a copy. 1162 func (t *Twrite) encode(b *buffer) { 1163 b.WriteFID(t.FID) 1164 b.Write64(t.Offset) 1165 b.Write32(uint32(len(t.Data))) 1166 } 1167 1168 // Type implements message.Type. 1169 func (*Twrite) Type() MsgType { 1170 return MsgTwrite 1171 } 1172 1173 // FixedSize implements payloader.FixedSize. 1174 func (*Twrite) FixedSize() uint32 { 1175 return 16 1176 } 1177 1178 // Payload implements payloader.Payload. 1179 func (t *Twrite) Payload() []byte { 1180 return t.Data 1181 } 1182 1183 // SetPayload implements payloader.SetPayload. 1184 func (t *Twrite) SetPayload(p []byte) { 1185 t.Data = p 1186 } 1187 1188 // String implements fmt.Stringer. 1189 func (t *Twrite) String() string { 1190 return fmt.Sprintf("Twrite{FID: %v, Offset %d, len(Data): %d}", t.FID, t.Offset, len(t.Data)) 1191 } 1192 1193 // Rwrite is the response for a Twrite. 1194 type Rwrite struct { 1195 // Count indicates the number of bytes successfully written. 1196 Count uint32 1197 } 1198 1199 // decode implements encoder.decode. 1200 func (r *Rwrite) decode(b *buffer) { 1201 r.Count = b.Read32() 1202 } 1203 1204 // encode implements encoder.encode. 1205 func (r *Rwrite) encode(b *buffer) { 1206 b.Write32(r.Count) 1207 } 1208 1209 // Type implements message.Type. 1210 func (*Rwrite) Type() MsgType { 1211 return MsgRwrite 1212 } 1213 1214 // String implements fmt.Stringer. 1215 func (r *Rwrite) String() string { 1216 return fmt.Sprintf("Rwrite{Count: %d}", r.Count) 1217 } 1218 1219 // Tmknod is a mknod request. 1220 type Tmknod struct { 1221 // Directory is the parent directory. 1222 Directory FID 1223 1224 // Name is the device name. 1225 Name string 1226 1227 // Mode is the device mode and permissions. 1228 Mode FileMode 1229 1230 // Major is the device major number. 1231 Major uint32 1232 1233 // Minor is the device minor number. 1234 Minor uint32 1235 1236 // GID is the device GID. 1237 GID GID 1238 } 1239 1240 // decode implements encoder.decode. 1241 func (t *Tmknod) decode(b *buffer) { 1242 t.Directory = b.ReadFID() 1243 t.Name = b.ReadString() 1244 t.Mode = b.ReadFileMode() 1245 t.Major = b.Read32() 1246 t.Minor = b.Read32() 1247 t.GID = b.ReadGID() 1248 } 1249 1250 // encode implements encoder.encode. 1251 func (t *Tmknod) encode(b *buffer) { 1252 b.WriteFID(t.Directory) 1253 b.WriteString(t.Name) 1254 b.WriteFileMode(t.Mode) 1255 b.Write32(t.Major) 1256 b.Write32(t.Minor) 1257 b.WriteGID(t.GID) 1258 } 1259 1260 // Type implements message.Type. 1261 func (*Tmknod) Type() MsgType { 1262 return MsgTmknod 1263 } 1264 1265 // String implements fmt.Stringer. 1266 func (t *Tmknod) String() string { 1267 return fmt.Sprintf("Tmknod{DirectoryFID: %d, Name: %s, Mode: 0o%o, Major: %d, Minor: %d, GID: %d}", t.Directory, t.Name, t.Mode, t.Major, t.Minor, t.GID) 1268 } 1269 1270 // Rmknod is a mknod response. 1271 type Rmknod struct { 1272 // QID is the resulting QID. 1273 QID QID 1274 } 1275 1276 // decode implements encoder.decode. 1277 func (r *Rmknod) decode(b *buffer) { 1278 r.QID.decode(b) 1279 } 1280 1281 // encode implements encoder.encode. 1282 func (r *Rmknod) encode(b *buffer) { 1283 r.QID.encode(b) 1284 } 1285 1286 // Type implements message.Type. 1287 func (*Rmknod) Type() MsgType { 1288 return MsgRmknod 1289 } 1290 1291 // String implements fmt.Stringer. 1292 func (r *Rmknod) String() string { 1293 return fmt.Sprintf("Rmknod{QID: %s}", r.QID) 1294 } 1295 1296 // Tmkdir is a mkdir request. 1297 type Tmkdir struct { 1298 // Directory is the parent directory. 1299 Directory FID 1300 1301 // Name is the new directory name. 1302 Name string 1303 1304 // Permissions is the set of permission bits. 1305 Permissions FileMode 1306 1307 // GID is the owning group. 1308 GID GID 1309 } 1310 1311 // decode implements encoder.decode. 1312 func (t *Tmkdir) decode(b *buffer) { 1313 t.Directory = b.ReadFID() 1314 t.Name = b.ReadString() 1315 t.Permissions = b.ReadPermissions() 1316 t.GID = b.ReadGID() 1317 } 1318 1319 // encode implements encoder.encode. 1320 func (t *Tmkdir) encode(b *buffer) { 1321 b.WriteFID(t.Directory) 1322 b.WriteString(t.Name) 1323 b.WritePermissions(t.Permissions) 1324 b.WriteGID(t.GID) 1325 } 1326 1327 // Type implements message.Type. 1328 func (*Tmkdir) Type() MsgType { 1329 return MsgTmkdir 1330 } 1331 1332 // String implements fmt.Stringer. 1333 func (t *Tmkdir) String() string { 1334 return fmt.Sprintf("Tmkdir{DirectoryFID: %d, Name: %s, Permissions: 0o%o, GID: %d}", t.Directory, t.Name, t.Permissions, t.GID) 1335 } 1336 1337 // Rmkdir is a mkdir response. 1338 type Rmkdir struct { 1339 // QID is the resulting QID. 1340 QID QID 1341 } 1342 1343 // decode implements encoder.decode. 1344 func (r *Rmkdir) decode(b *buffer) { 1345 r.QID.decode(b) 1346 } 1347 1348 // encode implements encoder.encode. 1349 func (r *Rmkdir) encode(b *buffer) { 1350 r.QID.encode(b) 1351 } 1352 1353 // Type implements message.Type. 1354 func (*Rmkdir) Type() MsgType { 1355 return MsgRmkdir 1356 } 1357 1358 // String implements fmt.Stringer. 1359 func (r *Rmkdir) String() string { 1360 return fmt.Sprintf("Rmkdir{QID: %s}", r.QID) 1361 } 1362 1363 // Tgetattr is a getattr request. 1364 type Tgetattr struct { 1365 // FID is the FID to get attributes for. 1366 FID FID 1367 1368 // AttrMask is the set of attributes to get. 1369 AttrMask AttrMask 1370 } 1371 1372 // decode implements encoder.decode. 1373 func (t *Tgetattr) decode(b *buffer) { 1374 t.FID = b.ReadFID() 1375 t.AttrMask.decode(b) 1376 } 1377 1378 // encode implements encoder.encode. 1379 func (t *Tgetattr) encode(b *buffer) { 1380 b.WriteFID(t.FID) 1381 t.AttrMask.encode(b) 1382 } 1383 1384 // Type implements message.Type. 1385 func (*Tgetattr) Type() MsgType { 1386 return MsgTgetattr 1387 } 1388 1389 // String implements fmt.Stringer. 1390 func (t *Tgetattr) String() string { 1391 return fmt.Sprintf("Tgetattr{FID: %d, AttrMask: %s}", t.FID, t.AttrMask) 1392 } 1393 1394 // Rgetattr is a getattr response. 1395 type Rgetattr struct { 1396 // Valid indicates which fields are valid. 1397 Valid AttrMask 1398 1399 // QID is the QID for this file. 1400 QID 1401 1402 // Attr is the set of attributes. 1403 Attr Attr 1404 } 1405 1406 // decode implements encoder.decode. 1407 func (r *Rgetattr) decode(b *buffer) { 1408 r.Valid.decode(b) 1409 r.QID.decode(b) 1410 r.Attr.decode(b) 1411 } 1412 1413 // encode implements encoder.encode. 1414 func (r *Rgetattr) encode(b *buffer) { 1415 r.Valid.encode(b) 1416 r.QID.encode(b) 1417 r.Attr.encode(b) 1418 } 1419 1420 // Type implements message.Type. 1421 func (*Rgetattr) Type() MsgType { 1422 return MsgRgetattr 1423 } 1424 1425 // String implements fmt.Stringer. 1426 func (r *Rgetattr) String() string { 1427 return fmt.Sprintf("Rgetattr{Valid: %v, QID: %s, Attr: %s}", r.Valid, r.QID, r.Attr) 1428 } 1429 1430 // Tsetattr is a setattr request. 1431 type Tsetattr struct { 1432 // FID is the FID to change. 1433 FID FID 1434 1435 // Valid is the set of bits which will be used. 1436 Valid SetAttrMask 1437 1438 // SetAttr is the set request. 1439 SetAttr SetAttr 1440 } 1441 1442 // decode implements encoder.decode. 1443 func (t *Tsetattr) decode(b *buffer) { 1444 t.FID = b.ReadFID() 1445 t.Valid.decode(b) 1446 t.SetAttr.decode(b) 1447 } 1448 1449 // encode implements encoder.encode. 1450 func (t *Tsetattr) encode(b *buffer) { 1451 b.WriteFID(t.FID) 1452 t.Valid.encode(b) 1453 t.SetAttr.encode(b) 1454 } 1455 1456 // Type implements message.Type. 1457 func (*Tsetattr) Type() MsgType { 1458 return MsgTsetattr 1459 } 1460 1461 // String implements fmt.Stringer. 1462 func (t *Tsetattr) String() string { 1463 return fmt.Sprintf("Tsetattr{FID: %d, Valid: %v, SetAttr: %s}", t.FID, t.Valid, t.SetAttr) 1464 } 1465 1466 // Rsetattr is a setattr response. 1467 type Rsetattr struct { 1468 } 1469 1470 // decode implements encoder.decode. 1471 func (*Rsetattr) decode(*buffer) { 1472 } 1473 1474 // encode implements encoder.encode. 1475 func (*Rsetattr) encode(*buffer) { 1476 } 1477 1478 // Type implements message.Type. 1479 func (*Rsetattr) Type() MsgType { 1480 return MsgRsetattr 1481 } 1482 1483 // String implements fmt.Stringer. 1484 func (r *Rsetattr) String() string { 1485 return "Rsetattr{}" 1486 } 1487 1488 // Tallocate is an allocate request. This is an extension to 9P protocol, not 1489 // present in the 9P2000.L standard. 1490 type Tallocate struct { 1491 FID FID 1492 Mode AllocateMode 1493 Offset uint64 1494 Length uint64 1495 } 1496 1497 // decode implements encoder.decode. 1498 func (t *Tallocate) decode(b *buffer) { 1499 t.FID = b.ReadFID() 1500 t.Mode.decode(b) 1501 t.Offset = b.Read64() 1502 t.Length = b.Read64() 1503 } 1504 1505 // encode implements encoder.encode. 1506 func (t *Tallocate) encode(b *buffer) { 1507 b.WriteFID(t.FID) 1508 t.Mode.encode(b) 1509 b.Write64(t.Offset) 1510 b.Write64(t.Length) 1511 } 1512 1513 // Type implements message.Type. 1514 func (*Tallocate) Type() MsgType { 1515 return MsgTallocate 1516 } 1517 1518 // String implements fmt.Stringer. 1519 func (t *Tallocate) String() string { 1520 return fmt.Sprintf("Tallocate{FID: %d, Offset: %d, Length: %d}", t.FID, t.Offset, t.Length) 1521 } 1522 1523 // Rallocate is an allocate response. 1524 type Rallocate struct { 1525 } 1526 1527 // decode implements encoder.decode. 1528 func (*Rallocate) decode(*buffer) { 1529 } 1530 1531 // encode implements encoder.encode. 1532 func (*Rallocate) encode(*buffer) { 1533 } 1534 1535 // Type implements message.Type. 1536 func (*Rallocate) Type() MsgType { 1537 return MsgRallocate 1538 } 1539 1540 // String implements fmt.Stringer. 1541 func (r *Rallocate) String() string { 1542 return "Rallocate{}" 1543 } 1544 1545 // Tlistxattr is a listxattr request. 1546 type Tlistxattr struct { 1547 // FID refers to the file on which to list xattrs. 1548 FID FID 1549 1550 // Size is the buffer size for the xattr list. 1551 Size uint64 1552 } 1553 1554 // decode implements encoder.decode. 1555 func (t *Tlistxattr) decode(b *buffer) { 1556 t.FID = b.ReadFID() 1557 t.Size = b.Read64() 1558 } 1559 1560 // encode implements encoder.encode. 1561 func (t *Tlistxattr) encode(b *buffer) { 1562 b.WriteFID(t.FID) 1563 b.Write64(t.Size) 1564 } 1565 1566 // Type implements message.Type. 1567 func (*Tlistxattr) Type() MsgType { 1568 return MsgTlistxattr 1569 } 1570 1571 // String implements fmt.Stringer. 1572 func (t *Tlistxattr) String() string { 1573 return fmt.Sprintf("Tlistxattr{FID: %d, Size: %d}", t.FID, t.Size) 1574 } 1575 1576 // Rlistxattr is a listxattr response. 1577 type Rlistxattr struct { 1578 // Xattrs is a list of extended attribute names. 1579 Xattrs []string 1580 } 1581 1582 // decode implements encoder.decode. 1583 func (r *Rlistxattr) decode(b *buffer) { 1584 n := b.Read16() 1585 r.Xattrs = r.Xattrs[:0] 1586 for i := 0; i < int(n); i++ { 1587 r.Xattrs = append(r.Xattrs, b.ReadString()) 1588 } 1589 } 1590 1591 // encode implements encoder.encode. 1592 func (r *Rlistxattr) encode(b *buffer) { 1593 b.Write16(uint16(len(r.Xattrs))) 1594 for _, x := range r.Xattrs { 1595 b.WriteString(x) 1596 } 1597 } 1598 1599 // Type implements message.Type. 1600 func (*Rlistxattr) Type() MsgType { 1601 return MsgRlistxattr 1602 } 1603 1604 // String implements fmt.Stringer. 1605 func (r *Rlistxattr) String() string { 1606 return fmt.Sprintf("Rlistxattr{Xattrs: %v}", r.Xattrs) 1607 } 1608 1609 // Txattrwalk walks extended attributes. 1610 type Txattrwalk struct { 1611 // FID is the FID to check for attributes. 1612 FID FID 1613 1614 // NewFID is the new FID associated with the attributes. 1615 NewFID FID 1616 1617 // Name is the attribute name. 1618 Name string 1619 } 1620 1621 // decode implements encoder.decode. 1622 func (t *Txattrwalk) decode(b *buffer) { 1623 t.FID = b.ReadFID() 1624 t.NewFID = b.ReadFID() 1625 t.Name = b.ReadString() 1626 } 1627 1628 // encode implements encoder.encode. 1629 func (t *Txattrwalk) encode(b *buffer) { 1630 b.WriteFID(t.FID) 1631 b.WriteFID(t.NewFID) 1632 b.WriteString(t.Name) 1633 } 1634 1635 // Type implements message.Type. 1636 func (*Txattrwalk) Type() MsgType { 1637 return MsgTxattrwalk 1638 } 1639 1640 // String implements fmt.Stringer. 1641 func (t *Txattrwalk) String() string { 1642 return fmt.Sprintf("Txattrwalk{FID: %d, NewFID: %d, Name: %s}", t.FID, t.NewFID, t.Name) 1643 } 1644 1645 // Rxattrwalk is a xattrwalk response. 1646 type Rxattrwalk struct { 1647 // Size is the size of the extended attribute. 1648 Size uint64 1649 } 1650 1651 // decode implements encoder.decode. 1652 func (r *Rxattrwalk) decode(b *buffer) { 1653 r.Size = b.Read64() 1654 } 1655 1656 // encode implements encoder.encode. 1657 func (r *Rxattrwalk) encode(b *buffer) { 1658 b.Write64(r.Size) 1659 } 1660 1661 // Type implements message.Type. 1662 func (*Rxattrwalk) Type() MsgType { 1663 return MsgRxattrwalk 1664 } 1665 1666 // String implements fmt.Stringer. 1667 func (r *Rxattrwalk) String() string { 1668 return fmt.Sprintf("Rxattrwalk{Size: %d}", r.Size) 1669 } 1670 1671 // Txattrcreate prepare to set extended attributes. 1672 type Txattrcreate struct { 1673 // FID is input/output parameter, it identifies the file on which 1674 // extended attributes will be set but after successful Rxattrcreate 1675 // it is used to write the extended attribute value. 1676 FID FID 1677 1678 // Name is the attribute name. 1679 Name string 1680 1681 // Size of the attribute value. When the FID is clunked it has to match 1682 // the number of bytes written to the FID. 1683 AttrSize uint64 1684 1685 // Linux setxattr(2) flags. 1686 Flags uint32 1687 } 1688 1689 // decode implements encoder.decode. 1690 func (t *Txattrcreate) decode(b *buffer) { 1691 t.FID = b.ReadFID() 1692 t.Name = b.ReadString() 1693 t.AttrSize = b.Read64() 1694 t.Flags = b.Read32() 1695 } 1696 1697 // encode implements encoder.encode. 1698 func (t *Txattrcreate) encode(b *buffer) { 1699 b.WriteFID(t.FID) 1700 b.WriteString(t.Name) 1701 b.Write64(t.AttrSize) 1702 b.Write32(t.Flags) 1703 } 1704 1705 // Type implements message.Type. 1706 func (*Txattrcreate) Type() MsgType { 1707 return MsgTxattrcreate 1708 } 1709 1710 // String implements fmt.Stringer. 1711 func (t *Txattrcreate) String() string { 1712 return fmt.Sprintf("Txattrcreate{FID: %d, Name: %s, AttrSize: %d, Flags: %d}", t.FID, t.Name, t.AttrSize, t.Flags) 1713 } 1714 1715 // Rxattrcreate is a xattrcreate response. 1716 type Rxattrcreate struct { 1717 } 1718 1719 // decode implements encoder.decode. 1720 func (r *Rxattrcreate) decode(*buffer) { 1721 } 1722 1723 // encode implements encoder.encode. 1724 func (r *Rxattrcreate) encode(*buffer) { 1725 } 1726 1727 // Type implements message.Type. 1728 func (*Rxattrcreate) Type() MsgType { 1729 return MsgRxattrcreate 1730 } 1731 1732 // String implements fmt.Stringer. 1733 func (r *Rxattrcreate) String() string { 1734 return "Rxattrcreate{}" 1735 } 1736 1737 // Tgetxattr is a getxattr request. 1738 type Tgetxattr struct { 1739 // FID refers to the file for which to get xattrs. 1740 FID FID 1741 1742 // Name is the xattr to get. 1743 Name string 1744 1745 // Size is the buffer size for the xattr to get. 1746 Size uint64 1747 } 1748 1749 // decode implements encoder.decode. 1750 func (t *Tgetxattr) decode(b *buffer) { 1751 t.FID = b.ReadFID() 1752 t.Name = b.ReadString() 1753 t.Size = b.Read64() 1754 } 1755 1756 // encode implements encoder.encode. 1757 func (t *Tgetxattr) encode(b *buffer) { 1758 b.WriteFID(t.FID) 1759 b.WriteString(t.Name) 1760 b.Write64(t.Size) 1761 } 1762 1763 // Type implements message.Type. 1764 func (*Tgetxattr) Type() MsgType { 1765 return MsgTgetxattr 1766 } 1767 1768 // String implements fmt.Stringer. 1769 func (t *Tgetxattr) String() string { 1770 return fmt.Sprintf("Tgetxattr{FID: %d, Name: %s, Size: %d}", t.FID, t.Name, t.Size) 1771 } 1772 1773 // Rgetxattr is a getxattr response. 1774 type Rgetxattr struct { 1775 // Value is the extended attribute value. 1776 Value string 1777 } 1778 1779 // decode implements encoder.decode. 1780 func (r *Rgetxattr) decode(b *buffer) { 1781 r.Value = b.ReadString() 1782 } 1783 1784 // encode implements encoder.encode. 1785 func (r *Rgetxattr) encode(b *buffer) { 1786 b.WriteString(r.Value) 1787 } 1788 1789 // Type implements message.Type. 1790 func (*Rgetxattr) Type() MsgType { 1791 return MsgRgetxattr 1792 } 1793 1794 // String implements fmt.Stringer. 1795 func (r *Rgetxattr) String() string { 1796 return fmt.Sprintf("Rgetxattr{Value: %s}", r.Value) 1797 } 1798 1799 // Tsetxattr sets extended attributes. 1800 type Tsetxattr struct { 1801 // FID refers to the file on which to set xattrs. 1802 FID FID 1803 1804 // Name is the attribute name. 1805 Name string 1806 1807 // Value is the attribute value. 1808 Value string 1809 1810 // Linux setxattr(2) flags. 1811 Flags uint32 1812 } 1813 1814 // decode implements encoder.decode. 1815 func (t *Tsetxattr) decode(b *buffer) { 1816 t.FID = b.ReadFID() 1817 t.Name = b.ReadString() 1818 t.Value = b.ReadString() 1819 t.Flags = b.Read32() 1820 } 1821 1822 // encode implements encoder.encode. 1823 func (t *Tsetxattr) encode(b *buffer) { 1824 b.WriteFID(t.FID) 1825 b.WriteString(t.Name) 1826 b.WriteString(t.Value) 1827 b.Write32(t.Flags) 1828 } 1829 1830 // Type implements message.Type. 1831 func (*Tsetxattr) Type() MsgType { 1832 return MsgTsetxattr 1833 } 1834 1835 // String implements fmt.Stringer. 1836 func (t *Tsetxattr) String() string { 1837 return fmt.Sprintf("Tsetxattr{FID: %d, Name: %s, Value: %s, Flags: %d}", t.FID, t.Name, t.Value, t.Flags) 1838 } 1839 1840 // Rsetxattr is a setxattr response. 1841 type Rsetxattr struct { 1842 } 1843 1844 // decode implements encoder.decode. 1845 func (r *Rsetxattr) decode(*buffer) { 1846 } 1847 1848 // encode implements encoder.encode. 1849 func (r *Rsetxattr) encode(*buffer) { 1850 } 1851 1852 // Type implements message.Type. 1853 func (*Rsetxattr) Type() MsgType { 1854 return MsgRsetxattr 1855 } 1856 1857 // String implements fmt.Stringer. 1858 func (r *Rsetxattr) String() string { 1859 return "Rsetxattr{}" 1860 } 1861 1862 // Tremovexattr is a removexattr request. 1863 type Tremovexattr struct { 1864 // FID refers to the file on which to set xattrs. 1865 FID FID 1866 1867 // Name is the attribute name. 1868 Name string 1869 } 1870 1871 // decode implements encoder.decode. 1872 func (t *Tremovexattr) decode(b *buffer) { 1873 t.FID = b.ReadFID() 1874 t.Name = b.ReadString() 1875 } 1876 1877 // encode implements encoder.encode. 1878 func (t *Tremovexattr) encode(b *buffer) { 1879 b.WriteFID(t.FID) 1880 b.WriteString(t.Name) 1881 } 1882 1883 // Type implements message.Type. 1884 func (*Tremovexattr) Type() MsgType { 1885 return MsgTremovexattr 1886 } 1887 1888 // String implements fmt.Stringer. 1889 func (t *Tremovexattr) String() string { 1890 return fmt.Sprintf("Tremovexattr{FID: %d, Name: %s}", t.FID, t.Name) 1891 } 1892 1893 // Rremovexattr is a removexattr response. 1894 type Rremovexattr struct { 1895 } 1896 1897 // decode implements encoder.decode. 1898 func (r *Rremovexattr) decode(*buffer) { 1899 } 1900 1901 // encode implements encoder.encode. 1902 func (r *Rremovexattr) encode(*buffer) { 1903 } 1904 1905 // Type implements message.Type. 1906 func (*Rremovexattr) Type() MsgType { 1907 return MsgRremovexattr 1908 } 1909 1910 // String implements fmt.Stringer. 1911 func (r *Rremovexattr) String() string { 1912 return "Rremovexattr{}" 1913 } 1914 1915 // Treaddir is a readdir request. 1916 type Treaddir struct { 1917 // Directory is the directory FID to read. 1918 Directory FID 1919 1920 // DirentOffset is the dirent offset to read at. 1921 DirentOffset uint64 1922 1923 // Count is the number of bytes to read. 1924 Count uint32 1925 } 1926 1927 // decode implements encoder.decode. 1928 func (t *Treaddir) decode(b *buffer) { 1929 t.Directory = b.ReadFID() 1930 t.DirentOffset = b.Read64() 1931 t.Count = b.Read32() 1932 } 1933 1934 // encode implements encoder.encode. 1935 func (t *Treaddir) encode(b *buffer) { 1936 b.WriteFID(t.Directory) 1937 b.Write64(t.DirentOffset) 1938 b.Write32(t.Count) 1939 } 1940 1941 // Type implements message.Type. 1942 func (*Treaddir) Type() MsgType { 1943 return MsgTreaddir 1944 } 1945 1946 // String implements fmt.Stringer. 1947 func (t *Treaddir) String() string { 1948 return fmt.Sprintf("Treaddir{DirectoryFID: %d, DirentOffset: %d, Count: %d}", t.Directory, t.DirentOffset, t.Count) 1949 } 1950 1951 // Rreaddir is a readdir response. 1952 type Rreaddir struct { 1953 // Count is the byte limit. 1954 // 1955 // This should always be set from the Treaddir request. 1956 Count uint32 1957 1958 // Entries are the resulting entries. 1959 // 1960 // This may be constructed in decode. 1961 Entries []Dirent 1962 1963 // payload is the encoded payload. 1964 // 1965 // This is constructed by encode. 1966 payload []byte 1967 } 1968 1969 // decode implements encoder.decode. 1970 func (r *Rreaddir) decode(b *buffer) { 1971 r.Count = b.Read32() 1972 entriesBuf := buffer{data: r.payload} 1973 r.Entries = r.Entries[:0] 1974 for { 1975 var d Dirent 1976 d.decode(&entriesBuf) 1977 if entriesBuf.isOverrun() { 1978 // Couldn't decode a complete entry. 1979 break 1980 } 1981 r.Entries = append(r.Entries, d) 1982 } 1983 } 1984 1985 // encode implements encoder.encode. 1986 func (r *Rreaddir) encode(b *buffer) { 1987 entriesBuf := buffer{} 1988 payloadSize := 0 1989 for i, d := range r.Entries { 1990 d.encode(&entriesBuf) 1991 if len(entriesBuf.data) > int(r.Count) { 1992 log.Warningf("hit Rreaddir.Count limit while encoding dirents, discarding %d dirents", len(r.Entries)-i) 1993 break 1994 } 1995 payloadSize = len(entriesBuf.data) 1996 } 1997 r.Count = uint32(payloadSize) 1998 r.payload = entriesBuf.data[:payloadSize] 1999 b.Write32(r.Count) 2000 } 2001 2002 // Type implements message.Type. 2003 func (*Rreaddir) Type() MsgType { 2004 return MsgRreaddir 2005 } 2006 2007 // FixedSize implements payloader.FixedSize. 2008 func (*Rreaddir) FixedSize() uint32 { 2009 return 4 2010 } 2011 2012 // Payload implements payloader.Payload. 2013 func (r *Rreaddir) Payload() []byte { 2014 return r.payload 2015 } 2016 2017 // SetPayload implements payloader.SetPayload. 2018 func (r *Rreaddir) SetPayload(p []byte) { 2019 r.payload = p 2020 } 2021 2022 // String implements fmt.Stringer. 2023 func (r *Rreaddir) String() string { 2024 return fmt.Sprintf("Rreaddir{Count: %d, Entries: %s}", r.Count, r.Entries) 2025 } 2026 2027 // Tfsync is an fsync request. 2028 type Tfsync struct { 2029 // FID is the fid to sync. 2030 FID FID 2031 } 2032 2033 // decode implements encoder.decode. 2034 func (t *Tfsync) decode(b *buffer) { 2035 t.FID = b.ReadFID() 2036 } 2037 2038 // encode implements encoder.encode. 2039 func (t *Tfsync) encode(b *buffer) { 2040 b.WriteFID(t.FID) 2041 } 2042 2043 // Type implements message.Type. 2044 func (*Tfsync) Type() MsgType { 2045 return MsgTfsync 2046 } 2047 2048 // String implements fmt.Stringer. 2049 func (t *Tfsync) String() string { 2050 return fmt.Sprintf("Tfsync{FID: %d}", t.FID) 2051 } 2052 2053 // Rfsync is an fsync response. 2054 type Rfsync struct { 2055 } 2056 2057 // decode implements encoder.decode. 2058 func (*Rfsync) decode(*buffer) { 2059 } 2060 2061 // encode implements encoder.encode. 2062 func (*Rfsync) encode(*buffer) { 2063 } 2064 2065 // Type implements message.Type. 2066 func (*Rfsync) Type() MsgType { 2067 return MsgRfsync 2068 } 2069 2070 // String implements fmt.Stringer. 2071 func (r *Rfsync) String() string { 2072 return "Rfsync{}" 2073 } 2074 2075 // Tstatfs is a stat request. 2076 type Tstatfs struct { 2077 // FID is the root. 2078 FID FID 2079 } 2080 2081 // decode implements encoder.decode. 2082 func (t *Tstatfs) decode(b *buffer) { 2083 t.FID = b.ReadFID() 2084 } 2085 2086 // encode implements encoder.encode. 2087 func (t *Tstatfs) encode(b *buffer) { 2088 b.WriteFID(t.FID) 2089 } 2090 2091 // Type implements message.Type. 2092 func (*Tstatfs) Type() MsgType { 2093 return MsgTstatfs 2094 } 2095 2096 // String implements fmt.Stringer. 2097 func (t *Tstatfs) String() string { 2098 return fmt.Sprintf("Tstatfs{FID: %d}", t.FID) 2099 } 2100 2101 // Rstatfs is the response for a Tstatfs. 2102 type Rstatfs struct { 2103 // FSStat is the stat result. 2104 FSStat FSStat 2105 } 2106 2107 // decode implements encoder.decode. 2108 func (r *Rstatfs) decode(b *buffer) { 2109 r.FSStat.decode(b) 2110 } 2111 2112 // encode implements encoder.encode. 2113 func (r *Rstatfs) encode(b *buffer) { 2114 r.FSStat.encode(b) 2115 } 2116 2117 // Type implements message.Type. 2118 func (*Rstatfs) Type() MsgType { 2119 return MsgRstatfs 2120 } 2121 2122 // String implements fmt.Stringer. 2123 func (r *Rstatfs) String() string { 2124 return fmt.Sprintf("Rstatfs{FSStat: %v}", r.FSStat) 2125 } 2126 2127 // Tflushf is a flush file request, not to be confused with Tflush. 2128 type Tflushf struct { 2129 // FID is the FID to be flushed. 2130 FID FID 2131 } 2132 2133 // decode implements encoder.decode. 2134 func (t *Tflushf) decode(b *buffer) { 2135 t.FID = b.ReadFID() 2136 } 2137 2138 // encode implements encoder.encode. 2139 func (t *Tflushf) encode(b *buffer) { 2140 b.WriteFID(t.FID) 2141 } 2142 2143 // Type implements message.Type. 2144 func (*Tflushf) Type() MsgType { 2145 return MsgTflushf 2146 } 2147 2148 // String implements fmt.Stringer. 2149 func (t *Tflushf) String() string { 2150 return fmt.Sprintf("Tflushf{FID: %d}", t.FID) 2151 } 2152 2153 // Rflushf is a flush file response. 2154 type Rflushf struct { 2155 } 2156 2157 // decode implements encoder.decode. 2158 func (*Rflushf) decode(*buffer) { 2159 } 2160 2161 // encode implements encoder.encode. 2162 func (*Rflushf) encode(*buffer) { 2163 } 2164 2165 // Type implements message.Type. 2166 func (*Rflushf) Type() MsgType { 2167 return MsgRflushf 2168 } 2169 2170 // String implements fmt.Stringer. 2171 func (*Rflushf) String() string { 2172 return "Rflushf{}" 2173 } 2174 2175 // Twalkgetattr is a walk request. 2176 type Twalkgetattr struct { 2177 // FID is the FID to be walked. 2178 FID FID 2179 2180 // NewFID is the resulting FID. 2181 NewFID FID 2182 2183 // Names are the set of names to be walked. 2184 Names []string 2185 } 2186 2187 // decode implements encoder.decode. 2188 func (t *Twalkgetattr) decode(b *buffer) { 2189 t.FID = b.ReadFID() 2190 t.NewFID = b.ReadFID() 2191 n := b.Read16() 2192 t.Names = t.Names[:0] 2193 for i := 0; i < int(n); i++ { 2194 t.Names = append(t.Names, b.ReadString()) 2195 } 2196 } 2197 2198 // encode implements encoder.encode. 2199 func (t *Twalkgetattr) encode(b *buffer) { 2200 b.WriteFID(t.FID) 2201 b.WriteFID(t.NewFID) 2202 b.Write16(uint16(len(t.Names))) 2203 for _, name := range t.Names { 2204 b.WriteString(name) 2205 } 2206 } 2207 2208 // Type implements message.Type. 2209 func (*Twalkgetattr) Type() MsgType { 2210 return MsgTwalkgetattr 2211 } 2212 2213 // String implements fmt.Stringer. 2214 func (t *Twalkgetattr) String() string { 2215 return fmt.Sprintf("Twalkgetattr{FID: %d, NewFID: %d, Names: %v}", t.FID, t.NewFID, t.Names) 2216 } 2217 2218 // Rwalkgetattr is a walk response. 2219 type Rwalkgetattr struct { 2220 // Valid indicates which fields are valid in the Attr below. 2221 Valid AttrMask 2222 2223 // Attr is the set of attributes for the last QID (the file walked to). 2224 Attr Attr 2225 2226 // QIDs are the set of QIDs returned. 2227 QIDs []QID 2228 } 2229 2230 // decode implements encoder.decode. 2231 func (r *Rwalkgetattr) decode(b *buffer) { 2232 r.Valid.decode(b) 2233 r.Attr.decode(b) 2234 n := b.Read16() 2235 r.QIDs = r.QIDs[:0] 2236 for i := 0; i < int(n); i++ { 2237 var q QID 2238 q.decode(b) 2239 r.QIDs = append(r.QIDs, q) 2240 } 2241 } 2242 2243 // encode implements encoder.encode. 2244 func (r *Rwalkgetattr) encode(b *buffer) { 2245 r.Valid.encode(b) 2246 r.Attr.encode(b) 2247 b.Write16(uint16(len(r.QIDs))) 2248 for i := range r.QIDs { 2249 r.QIDs[i].encode(b) 2250 } 2251 } 2252 2253 // Type implements message.Type. 2254 func (*Rwalkgetattr) Type() MsgType { 2255 return MsgRwalkgetattr 2256 } 2257 2258 // String implements fmt.Stringer. 2259 func (r *Rwalkgetattr) String() string { 2260 return fmt.Sprintf("Rwalkgetattr{Valid: %s, Attr: %s, QIDs: %v}", r.Valid, r.Attr, r.QIDs) 2261 } 2262 2263 // Tucreate is a Tlcreate message that includes a UID. 2264 type Tucreate struct { 2265 Tlcreate 2266 2267 // UID is the UID to use as the effective UID in creation messages. 2268 UID UID 2269 } 2270 2271 // decode implements encoder.decode. 2272 func (t *Tucreate) decode(b *buffer) { 2273 t.Tlcreate.decode(b) 2274 t.UID = b.ReadUID() 2275 } 2276 2277 // encode implements encoder.encode. 2278 func (t *Tucreate) encode(b *buffer) { 2279 t.Tlcreate.encode(b) 2280 b.WriteUID(t.UID) 2281 } 2282 2283 // Type implements message.Type. 2284 func (t *Tucreate) Type() MsgType { 2285 return MsgTucreate 2286 } 2287 2288 // String implements fmt.Stringer. 2289 func (t *Tucreate) String() string { 2290 return fmt.Sprintf("Tucreate{Tlcreate: %v, UID: %d}", &t.Tlcreate, t.UID) 2291 } 2292 2293 // Rucreate is a file creation response. 2294 type Rucreate struct { 2295 Rlcreate 2296 } 2297 2298 // Type implements message.Type. 2299 func (*Rucreate) Type() MsgType { 2300 return MsgRucreate 2301 } 2302 2303 // String implements fmt.Stringer. 2304 func (r *Rucreate) String() string { 2305 return fmt.Sprintf("Rucreate{%v}", &r.Rlcreate) 2306 } 2307 2308 // Tumkdir is a Tmkdir message that includes a UID. 2309 type Tumkdir struct { 2310 Tmkdir 2311 2312 // UID is the UID to use as the effective UID in creation messages. 2313 UID UID 2314 } 2315 2316 // decode implements encoder.decode. 2317 func (t *Tumkdir) decode(b *buffer) { 2318 t.Tmkdir.decode(b) 2319 t.UID = b.ReadUID() 2320 } 2321 2322 // encode implements encoder.encode. 2323 func (t *Tumkdir) encode(b *buffer) { 2324 t.Tmkdir.encode(b) 2325 b.WriteUID(t.UID) 2326 } 2327 2328 // Type implements message.Type. 2329 func (t *Tumkdir) Type() MsgType { 2330 return MsgTumkdir 2331 } 2332 2333 // String implements fmt.Stringer. 2334 func (t *Tumkdir) String() string { 2335 return fmt.Sprintf("Tumkdir{Tmkdir: %v, UID: %d}", &t.Tmkdir, t.UID) 2336 } 2337 2338 // Rumkdir is a umkdir response. 2339 type Rumkdir struct { 2340 Rmkdir 2341 } 2342 2343 // Type implements message.Type. 2344 func (*Rumkdir) Type() MsgType { 2345 return MsgRumkdir 2346 } 2347 2348 // String implements fmt.Stringer. 2349 func (r *Rumkdir) String() string { 2350 return fmt.Sprintf("Rumkdir{%v}", &r.Rmkdir) 2351 } 2352 2353 // Tumknod is a Tmknod message that includes a UID. 2354 type Tumknod struct { 2355 Tmknod 2356 2357 // UID is the UID to use as the effective UID in creation messages. 2358 UID UID 2359 } 2360 2361 // decode implements encoder.decode. 2362 func (t *Tumknod) decode(b *buffer) { 2363 t.Tmknod.decode(b) 2364 t.UID = b.ReadUID() 2365 } 2366 2367 // encode implements encoder.encode. 2368 func (t *Tumknod) encode(b *buffer) { 2369 t.Tmknod.encode(b) 2370 b.WriteUID(t.UID) 2371 } 2372 2373 // Type implements message.Type. 2374 func (t *Tumknod) Type() MsgType { 2375 return MsgTumknod 2376 } 2377 2378 // String implements fmt.Stringer. 2379 func (t *Tumknod) String() string { 2380 return fmt.Sprintf("Tumknod{Tmknod: %v, UID: %d}", &t.Tmknod, t.UID) 2381 } 2382 2383 // Rumknod is a umknod response. 2384 type Rumknod struct { 2385 Rmknod 2386 } 2387 2388 // Type implements message.Type. 2389 func (*Rumknod) Type() MsgType { 2390 return MsgRumknod 2391 } 2392 2393 // String implements fmt.Stringer. 2394 func (r *Rumknod) String() string { 2395 return fmt.Sprintf("Rumknod{%v}", &r.Rmknod) 2396 } 2397 2398 // Tusymlink is a Tsymlink message that includes a UID. 2399 type Tusymlink struct { 2400 Tsymlink 2401 2402 // UID is the UID to use as the effective UID in creation messages. 2403 UID UID 2404 } 2405 2406 // decode implements encoder.decode. 2407 func (t *Tusymlink) decode(b *buffer) { 2408 t.Tsymlink.decode(b) 2409 t.UID = b.ReadUID() 2410 } 2411 2412 // encode implements encoder.encode. 2413 func (t *Tusymlink) encode(b *buffer) { 2414 t.Tsymlink.encode(b) 2415 b.WriteUID(t.UID) 2416 } 2417 2418 // Type implements message.Type. 2419 func (t *Tusymlink) Type() MsgType { 2420 return MsgTusymlink 2421 } 2422 2423 // String implements fmt.Stringer. 2424 func (t *Tusymlink) String() string { 2425 return fmt.Sprintf("Tusymlink{Tsymlink: %v, UID: %d}", &t.Tsymlink, t.UID) 2426 } 2427 2428 // Rusymlink is a usymlink response. 2429 type Rusymlink struct { 2430 Rsymlink 2431 } 2432 2433 // Type implements message.Type. 2434 func (*Rusymlink) Type() MsgType { 2435 return MsgRusymlink 2436 } 2437 2438 // String implements fmt.Stringer. 2439 func (r *Rusymlink) String() string { 2440 return fmt.Sprintf("Rusymlink{%v}", &r.Rsymlink) 2441 } 2442 2443 // Tbind is a bind request. 2444 type Tbind struct { 2445 // Directory is the directory inside which the bound socket file should be 2446 // created. 2447 Directory FID 2448 2449 // SockType is the type of socket to be used. This is passed as an argument 2450 // to socket(2). 2451 SockType uint32 2452 2453 // SockName is the name of the socket file to be created. 2454 SockName string 2455 2456 // UID is the owning user. 2457 UID UID 2458 2459 // GID is the owning group. 2460 GID GID 2461 2462 // NewFID is the resulting FID for the socket file. 2463 NewFID FID 2464 } 2465 2466 // decode implements encoder.decode. 2467 func (t *Tbind) decode(b *buffer) { 2468 t.Directory = b.ReadFID() 2469 t.SockType = b.Read32() 2470 t.SockName = b.ReadString() 2471 t.UID = b.ReadUID() 2472 t.GID = b.ReadGID() 2473 t.NewFID = b.ReadFID() 2474 } 2475 2476 // encode implements encoder.encode. 2477 func (t *Tbind) encode(b *buffer) { 2478 b.WriteFID(t.Directory) 2479 b.Write32(t.SockType) 2480 b.WriteString(t.SockName) 2481 b.WriteUID(t.UID) 2482 b.WriteGID(t.GID) 2483 b.WriteFID(t.NewFID) 2484 } 2485 2486 // Type implements message.Type. 2487 func (*Tbind) Type() MsgType { 2488 return MsgTbind 2489 } 2490 2491 // String implements fmt.Stringer. 2492 func (t *Tbind) String() string { 2493 return fmt.Sprintf("Tbind{Directory: %d, SockType: %d, SockName: %s, UID: %d, GID: %d, NewFID: %d}", t.Directory, t.SockType, t.SockName, t.UID, t.GID, t.NewFID) 2494 } 2495 2496 // Rbind is a bind response. 2497 type Rbind struct { 2498 // QID is the resulting QID of the created socket file. 2499 QID QID 2500 2501 // Valid indicates which fields are valid. 2502 Valid AttrMask 2503 2504 // Attr is the set of attributes of the created socket file. 2505 Attr Attr 2506 } 2507 2508 // decode implements encoder.decode. 2509 func (r *Rbind) decode(b *buffer) { 2510 r.QID.decode(b) 2511 r.Valid.decode(b) 2512 r.Attr.decode(b) 2513 } 2514 2515 // encode implements encoder.encode. 2516 func (r *Rbind) encode(b *buffer) { 2517 r.QID.encode(b) 2518 r.Valid.encode(b) 2519 r.Attr.encode(b) 2520 } 2521 2522 // Type implements message.Type. 2523 func (*Rbind) Type() MsgType { 2524 return MsgRbind 2525 } 2526 2527 // String implements fmt.Stringer. 2528 func (r *Rbind) String() string { 2529 return fmt.Sprintf("Rbind{QID: %s, Valid: %v, Attr: %s}", r.QID, r.Valid, r.Attr) 2530 } 2531 2532 // Tlconnect is a connect request. 2533 type Tlconnect struct { 2534 // FID is the FID to be connected. 2535 FID FID 2536 2537 // SocketType is the socket type to be connected to. 2538 SocketType SocketType 2539 } 2540 2541 // decode implements encoder.decode. 2542 func (t *Tlconnect) decode(b *buffer) { 2543 t.FID = b.ReadFID() 2544 t.SocketType = b.ReadSocketType() 2545 } 2546 2547 // encode implements encoder.encode. 2548 func (t *Tlconnect) encode(b *buffer) { 2549 b.WriteFID(t.FID) 2550 b.WriteSocketType(t.SocketType) 2551 } 2552 2553 // Type implements message.Type. 2554 func (*Tlconnect) Type() MsgType { 2555 return MsgTlconnect 2556 } 2557 2558 // String implements fmt.Stringer. 2559 func (t *Tlconnect) String() string { 2560 return fmt.Sprintf("Tlconnect{FID: %d, SocketType: %v}", t.FID, t.SocketType) 2561 } 2562 2563 // Rlconnect is a connect response. 2564 type Rlconnect struct { 2565 filePayload 2566 } 2567 2568 // decode implements encoder.decode. 2569 func (r *Rlconnect) decode(*buffer) {} 2570 2571 // encode implements encoder.encode. 2572 func (r *Rlconnect) encode(*buffer) {} 2573 2574 // Type implements message.Type. 2575 func (*Rlconnect) Type() MsgType { 2576 return MsgRlconnect 2577 } 2578 2579 // String implements fmt.Stringer. 2580 func (r *Rlconnect) String() string { 2581 return fmt.Sprintf("Rlconnect{File: %v}", r.File) 2582 } 2583 2584 // Tchannel creates a new channel. 2585 type Tchannel struct { 2586 // ID is the channel ID. 2587 ID uint32 2588 2589 // Control is 0 if the Rchannel response should provide the flipcall 2590 // component of the channel, and 1 if the Rchannel response should 2591 // provide the fdchannel component of the channel. 2592 Control uint32 2593 } 2594 2595 // decode implements encoder.decode. 2596 func (t *Tchannel) decode(b *buffer) { 2597 t.ID = b.Read32() 2598 t.Control = b.Read32() 2599 } 2600 2601 // encode implements encoder.encode. 2602 func (t *Tchannel) encode(b *buffer) { 2603 b.Write32(t.ID) 2604 b.Write32(t.Control) 2605 } 2606 2607 // Type implements message.Type. 2608 func (*Tchannel) Type() MsgType { 2609 return MsgTchannel 2610 } 2611 2612 // String implements fmt.Stringer. 2613 func (t *Tchannel) String() string { 2614 return fmt.Sprintf("Tchannel{ID: %d, Control: %d}", t.ID, t.Control) 2615 } 2616 2617 // Rchannel is the channel response. 2618 type Rchannel struct { 2619 Offset uint64 2620 Length uint64 2621 filePayload 2622 } 2623 2624 // decode implements encoder.decode. 2625 func (r *Rchannel) decode(b *buffer) { 2626 r.Offset = b.Read64() 2627 r.Length = b.Read64() 2628 } 2629 2630 // encode implements encoder.encode. 2631 func (r *Rchannel) encode(b *buffer) { 2632 b.Write64(r.Offset) 2633 b.Write64(r.Length) 2634 } 2635 2636 // Type implements message.Type. 2637 func (*Rchannel) Type() MsgType { 2638 return MsgRchannel 2639 } 2640 2641 // String implements fmt.Stringer. 2642 func (r *Rchannel) String() string { 2643 return fmt.Sprintf("Rchannel{Offset: %d, Length: %d}", r.Offset, r.Length) 2644 } 2645 2646 // Tmultigetattr is a multi-getattr request. 2647 type Tmultigetattr struct { 2648 // FID is the FID to be walked. 2649 FID FID 2650 2651 // Names are the set of names to be walked. 2652 Names []string 2653 } 2654 2655 // decode implements encoder.decode. 2656 func (t *Tmultigetattr) decode(b *buffer) { 2657 t.FID = b.ReadFID() 2658 n := b.Read16() 2659 t.Names = t.Names[:0] 2660 for i := 0; i < int(n); i++ { 2661 t.Names = append(t.Names, b.ReadString()) 2662 } 2663 } 2664 2665 // encode implements encoder.encode. 2666 func (t *Tmultigetattr) encode(b *buffer) { 2667 b.WriteFID(t.FID) 2668 b.Write16(uint16(len(t.Names))) 2669 for _, name := range t.Names { 2670 b.WriteString(name) 2671 } 2672 } 2673 2674 // Type implements message.Type. 2675 func (*Tmultigetattr) Type() MsgType { 2676 return MsgTmultigetattr 2677 } 2678 2679 // String implements fmt.Stringer. 2680 func (t *Tmultigetattr) String() string { 2681 return fmt.Sprintf("Tmultigetattr{FID: %d, Names: %v}", t.FID, t.Names) 2682 } 2683 2684 // Rmultigetattr is a multi-getattr response. 2685 type Rmultigetattr struct { 2686 // Stats are the set of FullStat returned for each of the names in the 2687 // request. 2688 Stats []FullStat 2689 } 2690 2691 // decode implements encoder.decode. 2692 func (r *Rmultigetattr) decode(b *buffer) { 2693 n := b.Read16() 2694 r.Stats = r.Stats[:0] 2695 for i := 0; i < int(n); i++ { 2696 var fs FullStat 2697 fs.decode(b) 2698 r.Stats = append(r.Stats, fs) 2699 } 2700 } 2701 2702 // encode implements encoder.encode. 2703 func (r *Rmultigetattr) encode(b *buffer) { 2704 b.Write16(uint16(len(r.Stats))) 2705 for i := range r.Stats { 2706 r.Stats[i].encode(b) 2707 } 2708 } 2709 2710 // Type implements message.Type. 2711 func (*Rmultigetattr) Type() MsgType { 2712 return MsgRmultigetattr 2713 } 2714 2715 // String implements fmt.Stringer. 2716 func (r *Rmultigetattr) String() string { 2717 return fmt.Sprintf("Rmultigetattr{Stats: %v}", r.Stats) 2718 } 2719 2720 const maxCacheSize = 3 2721 2722 // msgFactory is used to reduce allocations by caching messages for reuse. 2723 type msgFactory struct { 2724 create func() message 2725 cache chan message 2726 } 2727 2728 // msgRegistry indexes all message factories by type. 2729 var msgRegistry registry 2730 2731 type registry struct { 2732 factories [math.MaxUint8 + 1]msgFactory 2733 2734 // largestFixedSize is computed so that given some message size M, you can 2735 // compute the maximum payload size (e.g. for Twrite, Rread) with 2736 // M-largestFixedSize. You could do this individual on a per-message basis, 2737 // but it's easier to compute a single maximum safe payload. 2738 largestFixedSize uint32 2739 } 2740 2741 // get returns a new message by type. 2742 // 2743 // An error is returned in the case of an unknown message. 2744 // 2745 // This takes, and ignores, a message tag so that it may be used directly as a 2746 // lookupTagAndType function for recv (by design). 2747 func (r *registry) get(_ Tag, t MsgType) (message, error) { 2748 entry := &r.factories[t] 2749 if entry.create == nil { 2750 return nil, &ErrInvalidMsgType{t} 2751 } 2752 2753 select { 2754 case msg := <-entry.cache: 2755 return msg, nil 2756 default: 2757 return entry.create(), nil 2758 } 2759 } 2760 2761 func (r *registry) put(msg message) { 2762 if p, ok := msg.(payloader); ok { 2763 p.SetPayload(nil) 2764 } 2765 if f, ok := msg.(filer); ok { 2766 f.SetFilePayload(nil) 2767 } 2768 2769 entry := &r.factories[msg.Type()] 2770 select { 2771 case entry.cache <- msg: 2772 default: 2773 } 2774 } 2775 2776 // register registers the given message type. 2777 // 2778 // This may cause panic on failure and should only be used from init. 2779 func (r *registry) register(t MsgType, fn func() message) { 2780 if int(t) >= len(r.factories) { 2781 panic(fmt.Sprintf("message type %d is too large. It must be smaller than %d", t, len(r.factories))) 2782 } 2783 if r.factories[t].create != nil { 2784 panic(fmt.Sprintf("duplicate message type %d: first is %T, second is %T", t, r.factories[t].create(), fn())) 2785 } 2786 r.factories[t] = msgFactory{ 2787 create: fn, 2788 cache: make(chan message, maxCacheSize), 2789 } 2790 2791 if size := calculateSize(fn()); size > r.largestFixedSize { 2792 r.largestFixedSize = size 2793 } 2794 } 2795 2796 func calculateSize(m message) uint32 { 2797 if p, ok := m.(payloader); ok { 2798 return p.FixedSize() 2799 } 2800 var dataBuf buffer 2801 m.encode(&dataBuf) 2802 return uint32(len(dataBuf.data)) 2803 } 2804 2805 func init() { 2806 msgRegistry.register(MsgRlerror, func() message { return &Rlerror{} }) 2807 msgRegistry.register(MsgTstatfs, func() message { return &Tstatfs{} }) 2808 msgRegistry.register(MsgRstatfs, func() message { return &Rstatfs{} }) 2809 msgRegistry.register(MsgTlopen, func() message { return &Tlopen{} }) 2810 msgRegistry.register(MsgRlopen, func() message { return &Rlopen{} }) 2811 msgRegistry.register(MsgTlcreate, func() message { return &Tlcreate{} }) 2812 msgRegistry.register(MsgRlcreate, func() message { return &Rlcreate{} }) 2813 msgRegistry.register(MsgTsymlink, func() message { return &Tsymlink{} }) 2814 msgRegistry.register(MsgRsymlink, func() message { return &Rsymlink{} }) 2815 msgRegistry.register(MsgTmknod, func() message { return &Tmknod{} }) 2816 msgRegistry.register(MsgRmknod, func() message { return &Rmknod{} }) 2817 msgRegistry.register(MsgTrename, func() message { return &Trename{} }) 2818 msgRegistry.register(MsgRrename, func() message { return &Rrename{} }) 2819 msgRegistry.register(MsgTreadlink, func() message { return &Treadlink{} }) 2820 msgRegistry.register(MsgRreadlink, func() message { return &Rreadlink{} }) 2821 msgRegistry.register(MsgTgetattr, func() message { return &Tgetattr{} }) 2822 msgRegistry.register(MsgRgetattr, func() message { return &Rgetattr{} }) 2823 msgRegistry.register(MsgTsetattr, func() message { return &Tsetattr{} }) 2824 msgRegistry.register(MsgRsetattr, func() message { return &Rsetattr{} }) 2825 msgRegistry.register(MsgTlistxattr, func() message { return &Tlistxattr{} }) 2826 msgRegistry.register(MsgRlistxattr, func() message { return &Rlistxattr{} }) 2827 msgRegistry.register(MsgTxattrwalk, func() message { return &Txattrwalk{} }) 2828 msgRegistry.register(MsgRxattrwalk, func() message { return &Rxattrwalk{} }) 2829 msgRegistry.register(MsgTxattrcreate, func() message { return &Txattrcreate{} }) 2830 msgRegistry.register(MsgRxattrcreate, func() message { return &Rxattrcreate{} }) 2831 msgRegistry.register(MsgTgetxattr, func() message { return &Tgetxattr{} }) 2832 msgRegistry.register(MsgRgetxattr, func() message { return &Rgetxattr{} }) 2833 msgRegistry.register(MsgTsetxattr, func() message { return &Tsetxattr{} }) 2834 msgRegistry.register(MsgRsetxattr, func() message { return &Rsetxattr{} }) 2835 msgRegistry.register(MsgTremovexattr, func() message { return &Tremovexattr{} }) 2836 msgRegistry.register(MsgRremovexattr, func() message { return &Rremovexattr{} }) 2837 msgRegistry.register(MsgTreaddir, func() message { return &Treaddir{} }) 2838 msgRegistry.register(MsgRreaddir, func() message { return &Rreaddir{} }) 2839 msgRegistry.register(MsgTfsync, func() message { return &Tfsync{} }) 2840 msgRegistry.register(MsgRfsync, func() message { return &Rfsync{} }) 2841 msgRegistry.register(MsgTlink, func() message { return &Tlink{} }) 2842 msgRegistry.register(MsgRlink, func() message { return &Rlink{} }) 2843 msgRegistry.register(MsgTmkdir, func() message { return &Tmkdir{} }) 2844 msgRegistry.register(MsgRmkdir, func() message { return &Rmkdir{} }) 2845 msgRegistry.register(MsgTrenameat, func() message { return &Trenameat{} }) 2846 msgRegistry.register(MsgRrenameat, func() message { return &Rrenameat{} }) 2847 msgRegistry.register(MsgTunlinkat, func() message { return &Tunlinkat{} }) 2848 msgRegistry.register(MsgRunlinkat, func() message { return &Runlinkat{} }) 2849 msgRegistry.register(MsgTversion, func() message { return &Tversion{} }) 2850 msgRegistry.register(MsgRversion, func() message { return &Rversion{} }) 2851 msgRegistry.register(MsgTauth, func() message { return &Tauth{} }) 2852 msgRegistry.register(MsgRauth, func() message { return &Rauth{} }) 2853 msgRegistry.register(MsgTattach, func() message { return &Tattach{} }) 2854 msgRegistry.register(MsgRattach, func() message { return &Rattach{} }) 2855 msgRegistry.register(MsgTflush, func() message { return &Tflush{} }) 2856 msgRegistry.register(MsgRflush, func() message { return &Rflush{} }) 2857 msgRegistry.register(MsgTwalk, func() message { return &Twalk{} }) 2858 msgRegistry.register(MsgRwalk, func() message { return &Rwalk{} }) 2859 msgRegistry.register(MsgTread, func() message { return &Tread{} }) 2860 msgRegistry.register(MsgRread, func() message { return &Rread{} }) 2861 msgRegistry.register(MsgTwrite, func() message { return &Twrite{} }) 2862 msgRegistry.register(MsgRwrite, func() message { return &Rwrite{} }) 2863 msgRegistry.register(MsgTclunk, func() message { return &Tclunk{} }) 2864 msgRegistry.register(MsgRclunk, func() message { return &Rclunk{} }) 2865 msgRegistry.register(MsgTremove, func() message { return &Tremove{} }) 2866 msgRegistry.register(MsgRremove, func() message { return &Rremove{} }) 2867 msgRegistry.register(MsgTflushf, func() message { return &Tflushf{} }) 2868 msgRegistry.register(MsgRflushf, func() message { return &Rflushf{} }) 2869 msgRegistry.register(MsgTwalkgetattr, func() message { return &Twalkgetattr{} }) 2870 msgRegistry.register(MsgRwalkgetattr, func() message { return &Rwalkgetattr{} }) 2871 msgRegistry.register(MsgTucreate, func() message { return &Tucreate{} }) 2872 msgRegistry.register(MsgRucreate, func() message { return &Rucreate{} }) 2873 msgRegistry.register(MsgTumkdir, func() message { return &Tumkdir{} }) 2874 msgRegistry.register(MsgRumkdir, func() message { return &Rumkdir{} }) 2875 msgRegistry.register(MsgTumknod, func() message { return &Tumknod{} }) 2876 msgRegistry.register(MsgRumknod, func() message { return &Rumknod{} }) 2877 msgRegistry.register(MsgTusymlink, func() message { return &Tusymlink{} }) 2878 msgRegistry.register(MsgRusymlink, func() message { return &Rusymlink{} }) 2879 msgRegistry.register(MsgTbind, func() message { return &Tbind{} }) 2880 msgRegistry.register(MsgRbind, func() message { return &Rbind{} }) 2881 msgRegistry.register(MsgTlconnect, func() message { return &Tlconnect{} }) 2882 msgRegistry.register(MsgRlconnect, func() message { return &Rlconnect{} }) 2883 msgRegistry.register(MsgTallocate, func() message { return &Tallocate{} }) 2884 msgRegistry.register(MsgRallocate, func() message { return &Rallocate{} }) 2885 msgRegistry.register(MsgTsetattrclunk, func() message { return &Tsetattrclunk{} }) 2886 msgRegistry.register(MsgRsetattrclunk, func() message { return &Rsetattrclunk{} }) 2887 msgRegistry.register(MsgTmultigetattr, func() message { return &Tmultigetattr{} }) 2888 msgRegistry.register(MsgRmultigetattr, func() message { return &Rmultigetattr{} }) 2889 msgRegistry.register(MsgTchannel, func() message { return &Tchannel{} }) 2890 msgRegistry.register(MsgRchannel, func() message { return &Rchannel{} }) 2891 }