| 1 | package sftp |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "encoding/binary" |
| 7 | "errors" |
| 8 | "fmt" |
| 9 | "io" |
| 10 | "math" |
| 11 | "os" |
| 12 | "path" |
| 13 | "sync" |
| 14 | "sync/atomic" |
| 15 | "syscall" |
| 16 | "time" |
| 17 | |
| 18 | "github.com/kr/fs" |
| 19 | "golang.org/x/crypto/ssh" |
| 20 | |
| 21 | "github.com/pkg/sftp/internal/encoding/ssh/filexfer/openssh" |
| 22 | ) |
| 23 | |
| 24 | var ( |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | ErrInternalInconsistency = errors.New("internal inconsistency") |
| 31 | |
| 32 | |
| 33 | |
| 34 | InternalInconsistency = ErrInternalInconsistency |
| 35 | ) |
| 36 | |
| 37 | |
| 38 | type ClientOption func(*Client) error |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | func MaxPacketChecked(size int) ClientOption { |
| 48 | return func(c *Client) error { |
| 49 | if size < 1 { |
| 50 | return errors.New("size must be greater or equal to 1") |
| 51 | } |
| 52 | if size > 32768 { |
| 53 | return errors.New("sizes larger than 32KB might not work with all servers") |
| 54 | } |
| 55 | c.maxPacket = size |
| 56 | return nil |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | func MaxPacketUnchecked(size int) ClientOption { |
| 70 | return func(c *Client) error { |
| 71 | if size < 1 { |
| 72 | return errors.New("size must be greater or equal to 1") |
| 73 | } |
| 74 | c.maxPacket = size |
| 75 | return nil |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | func MaxPacket(size int) ClientOption { |
| 88 | return MaxPacketChecked(size) |
| 89 | } |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | func MaxConcurrentRequestsPerFile(n int) ClientOption { |
| 95 | return func(c *Client) error { |
| 96 | if n < 1 { |
| 97 | return errors.New("n must be greater or equal to 1") |
| 98 | } |
| 99 | c.maxConcurrentRequests = n |
| 100 | return nil |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | func UseConcurrentWrites(value bool) ClientOption { |
| 113 | return func(c *Client) error { |
| 114 | c.useConcurrentWrites = value |
| 115 | return nil |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | func UseConcurrentReads(value bool) ClientOption { |
| 131 | return func(c *Client) error { |
| 132 | c.disableConcurrentReads = !value |
| 133 | return nil |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | func UseFstat(value bool) ClientOption { |
| 155 | return func(c *Client) error { |
| 156 | c.useFstat = value |
| 157 | return nil |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | func CopyStderrTo(wr io.Writer) ClientOption { |
| 166 | return func(c *Client) error { |
| 167 | c.stderrTo = wr |
| 168 | return nil |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | type Client struct { |
| 178 | clientConn |
| 179 | |
| 180 | stderrTo io.Writer |
| 181 | |
| 182 | ext map[string]string |
| 183 | |
| 184 | maxPacket int |
| 185 | maxConcurrentRequests int |
| 186 | nextid uint32 |
| 187 | |
| 188 | |
| 189 | |
| 190 | useConcurrentWrites bool |
| 191 | useFstat bool |
| 192 | disableConcurrentReads bool |
| 193 | } |
| 194 | |
| 195 | |
| 196 | |
| 197 | func NewClient(conn *ssh.Client, opts ...ClientOption) (*Client, error) { |
| 198 | s, err := conn.NewSession() |
| 199 | if err != nil { |
| 200 | return nil, err |
| 201 | } |
| 202 | |
| 203 | pw, err := s.StdinPipe() |
| 204 | if err != nil { |
| 205 | return nil, err |
| 206 | } |
| 207 | pr, err := s.StdoutPipe() |
| 208 | if err != nil { |
| 209 | return nil, err |
| 210 | } |
| 211 | perr, err := s.StderrPipe() |
| 212 | if err != nil { |
| 213 | return nil, err |
| 214 | } |
| 215 | |
| 216 | if err := s.RequestSubsystem("sftp"); err != nil { |
| 217 | return nil, err |
| 218 | } |
| 219 | |
| 220 | return newClientPipe(pr, perr, pw, s.Wait, opts...) |
| 221 | } |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | func NewClientPipe(rd io.Reader, wr io.WriteCloser, opts ...ClientOption) (*Client, error) { |
| 227 | return newClientPipe(rd, nil, wr, nil, opts...) |
| 228 | } |
| 229 | |
| 230 | func newClientPipe(rd, stderr io.Reader, wr io.WriteCloser, wait func() error, opts ...ClientOption) (*Client, error) { |
| 231 | c := &Client{ |
| 232 | clientConn: clientConn{ |
| 233 | conn: conn{ |
| 234 | Reader: rd, |
| 235 | WriteCloser: wr, |
| 236 | }, |
| 237 | inflight: make(map[uint32]chan<- result), |
| 238 | closed: make(chan struct{}), |
| 239 | wait: wait, |
| 240 | }, |
| 241 | |
| 242 | ext: make(map[string]string), |
| 243 | |
| 244 | maxPacket: 1 << 15, |
| 245 | maxConcurrentRequests: 64, |
| 246 | } |
| 247 | |
| 248 | for _, opt := range opts { |
| 249 | if err := opt(c); err != nil { |
| 250 | wr.Close() |
| 251 | return nil, err |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | if stderr != nil { |
| 256 | wr := io.Discard |
| 257 | if c.stderrTo != nil { |
| 258 | wr = c.stderrTo |
| 259 | } |
| 260 | |
| 261 | go func() { |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | if _, err := io.Copy(wr, stderr); err != nil { |
| 268 | debug("error copying stderr: %v", err) |
| 269 | } |
| 270 | }() |
| 271 | } |
| 272 | |
| 273 | if err := c.sendInit(); err != nil { |
| 274 | wr.Close() |
| 275 | return nil, fmt.Errorf("error sending init packet to server: %w", err) |
| 276 | } |
| 277 | |
| 278 | if err := c.recvVersion(); err != nil { |
| 279 | wr.Close() |
| 280 | return nil, fmt.Errorf("error receiving version packet from server: %w", err) |
| 281 | } |
| 282 | |
| 283 | c.clientConn.wg.Add(1) |
| 284 | go func() { |
| 285 | defer c.clientConn.wg.Done() |
| 286 | |
| 287 | if err := c.clientConn.recv(); err != nil { |
| 288 | c.clientConn.broadcastErr(err) |
| 289 | } |
| 290 | }() |
| 291 | |
| 292 | return c, nil |
| 293 | } |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | func (c *Client) Create(path string) (*File, error) { |
| 304 | return c.open(path, toPflags(os.O_RDWR|os.O_CREATE|os.O_TRUNC)) |
| 305 | } |
| 306 | |
| 307 | const sftpProtocolVersion = 3 |
| 308 | |
| 309 | func (c *Client) sendInit() error { |
| 310 | return c.clientConn.conn.sendPacket(&sshFxInitPacket{ |
| 311 | Version: sftpProtocolVersion, |
| 312 | }) |
| 313 | } |
| 314 | |
| 315 | |
| 316 | func (c *Client) nextID() uint32 { |
| 317 | return atomic.AddUint32(&c.nextid, 1) |
| 318 | } |
| 319 | |
| 320 | func (c *Client) recvVersion() error { |
| 321 | typ, data, err := c.recvPacket(0) |
| 322 | if err != nil { |
| 323 | if err == io.EOF { |
| 324 | return fmt.Errorf("server unexpectedly closed connection: %w", io.ErrUnexpectedEOF) |
| 325 | } |
| 326 | |
| 327 | return err |
| 328 | } |
| 329 | |
| 330 | if typ != sshFxpVersion { |
| 331 | return &unexpectedPacketErr{sshFxpVersion, typ} |
| 332 | } |
| 333 | |
| 334 | version, data, err := unmarshalUint32Safe(data) |
| 335 | if err != nil { |
| 336 | return err |
| 337 | } |
| 338 | |
| 339 | if version != sftpProtocolVersion { |
| 340 | return &unexpectedVersionErr{sftpProtocolVersion, version} |
| 341 | } |
| 342 | |
| 343 | for len(data) > 0 { |
| 344 | var ext extensionPair |
| 345 | ext, data, err = unmarshalExtensionPair(data) |
| 346 | if err != nil { |
| 347 | return err |
| 348 | } |
| 349 | c.ext[ext.Name] = ext.Data |
| 350 | } |
| 351 | |
| 352 | return nil |
| 353 | } |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | func (c *Client) HasExtension(name string) (string, bool) { |
| 360 | data, ok := c.ext[name] |
| 361 | return data, ok |
| 362 | } |
| 363 | |
| 364 | |
| 365 | func (c *Client) Walk(root string) *fs.Walker { |
| 366 | return fs.WalkFS(root, c) |
| 367 | } |
| 368 | |
| 369 | |
| 370 | |
| 371 | func (c *Client) ReadDir(p string) ([]os.FileInfo, error) { |
| 372 | return c.ReadDirContext(context.Background(), p) |
| 373 | } |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | func (c *Client) ReadDirContext(ctx context.Context, p string) ([]os.FileInfo, error) { |
| 380 | handle, err := c.opendir(ctx, p) |
| 381 | if err != nil { |
| 382 | return nil, err |
| 383 | } |
| 384 | defer c.close(handle) |
| 385 | var entries []os.FileInfo |
| 386 | var done = false |
| 387 | for !done { |
| 388 | id := c.nextID() |
| 389 | typ, data, err1 := c.sendPacket(ctx, nil, &sshFxpReaddirPacket{ |
| 390 | ID: id, |
| 391 | Handle: handle, |
| 392 | }) |
| 393 | if err1 != nil { |
| 394 | err = err1 |
| 395 | done = true |
| 396 | break |
| 397 | } |
| 398 | switch typ { |
| 399 | case sshFxpName: |
| 400 | sid, data := unmarshalUint32(data) |
| 401 | if sid != id { |
| 402 | return nil, &unexpectedIDErr{id, sid} |
| 403 | } |
| 404 | count, data := unmarshalUint32(data) |
| 405 | for i := uint32(0); i < count; i++ { |
| 406 | var filename string |
| 407 | filename, data = unmarshalString(data) |
| 408 | _, data = unmarshalString(data) |
| 409 | var attr *FileStat |
| 410 | attr, data, err = unmarshalAttrs(data) |
| 411 | if err != nil { |
| 412 | return nil, err |
| 413 | } |
| 414 | if filename == "." || filename == ".." { |
| 415 | continue |
| 416 | } |
| 417 | entries = append(entries, fileInfoFromStat(attr, path.Base(filename))) |
| 418 | } |
| 419 | case sshFxpStatus: |
| 420 | |
| 421 | err = normaliseError(unmarshalStatus(id, data)) |
| 422 | done = true |
| 423 | default: |
| 424 | return nil, unimplementedPacketErr(typ) |
| 425 | } |
| 426 | } |
| 427 | if err == io.EOF { |
| 428 | err = nil |
| 429 | } |
| 430 | return entries, err |
| 431 | } |
| 432 | |
| 433 | func (c *Client) opendir(ctx context.Context, path string) (string, error) { |
| 434 | id := c.nextID() |
| 435 | typ, data, err := c.sendPacket(ctx, nil, &sshFxpOpendirPacket{ |
| 436 | ID: id, |
| 437 | Path: path, |
| 438 | }) |
| 439 | if err != nil { |
| 440 | return "", err |
| 441 | } |
| 442 | switch typ { |
| 443 | case sshFxpHandle: |
| 444 | sid, data := unmarshalUint32(data) |
| 445 | if sid != id { |
| 446 | return "", &unexpectedIDErr{id, sid} |
| 447 | } |
| 448 | handle, _ := unmarshalString(data) |
| 449 | return handle, nil |
| 450 | case sshFxpStatus: |
| 451 | return "", normaliseError(unmarshalStatus(id, data)) |
| 452 | default: |
| 453 | return "", unimplementedPacketErr(typ) |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | |
| 458 | |
| 459 | func (c *Client) Stat(p string) (os.FileInfo, error) { |
| 460 | fs, err := c.stat(p) |
| 461 | if err != nil { |
| 462 | return nil, err |
| 463 | } |
| 464 | return fileInfoFromStat(fs, path.Base(p)), nil |
| 465 | } |
| 466 | |
| 467 | |
| 468 | |
| 469 | func (c *Client) Lstat(p string) (os.FileInfo, error) { |
| 470 | id := c.nextID() |
| 471 | typ, data, err := c.sendPacket(context.Background(), nil, &sshFxpLstatPacket{ |
| 472 | ID: id, |
| 473 | Path: p, |
| 474 | }) |
| 475 | if err != nil { |
| 476 | return nil, err |
| 477 | } |
| 478 | switch typ { |
| 479 | case sshFxpAttrs: |
| 480 | sid, data := unmarshalUint32(data) |
| 481 | if sid != id { |
| 482 | return nil, &unexpectedIDErr{id, sid} |
| 483 | } |
| 484 | attr, _, err := unmarshalAttrs(data) |
| 485 | if err != nil { |
| 486 | |
| 487 | return nil, err |
| 488 | } |
| 489 | return fileInfoFromStat(attr, path.Base(p)), nil |
| 490 | case sshFxpStatus: |
| 491 | return nil, normaliseError(unmarshalStatus(id, data)) |
| 492 | default: |
| 493 | return nil, unimplementedPacketErr(typ) |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | |
| 498 | func (c *Client) ReadLink(p string) (string, error) { |
| 499 | id := c.nextID() |
| 500 | typ, data, err := c.sendPacket(context.Background(), nil, &sshFxpReadlinkPacket{ |
| 501 | ID: id, |
| 502 | Path: p, |
| 503 | }) |
| 504 | if err != nil { |
| 505 | return "", err |
| 506 | } |
| 507 | switch typ { |
| 508 | case sshFxpName: |
| 509 | sid, data := unmarshalUint32(data) |
| 510 | if sid != id { |
| 511 | return "", &unexpectedIDErr{id, sid} |
| 512 | } |
| 513 | count, data := unmarshalUint32(data) |
| 514 | if count != 1 { |
| 515 | return "", unexpectedCount(1, count) |
| 516 | } |
| 517 | filename, _ := unmarshalString(data) |
| 518 | return filename, nil |
| 519 | case sshFxpStatus: |
| 520 | return "", normaliseError(unmarshalStatus(id, data)) |
| 521 | default: |
| 522 | return "", unimplementedPacketErr(typ) |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | |
| 527 | func (c *Client) Link(oldname, newname string) error { |
| 528 | id := c.nextID() |
| 529 | typ, data, err := c.sendPacket(context.Background(), nil, &sshFxpHardlinkPacket{ |
| 530 | ID: id, |
| 531 | Oldpath: oldname, |
| 532 | Newpath: newname, |
| 533 | }) |
| 534 | if err != nil { |
| 535 | return err |
| 536 | } |
| 537 | switch typ { |
| 538 | case sshFxpStatus: |
| 539 | return normaliseError(unmarshalStatus(id, data)) |
| 540 | default: |
| 541 | return unimplementedPacketErr(typ) |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | |
| 546 | func (c *Client) Symlink(oldname, newname string) error { |
| 547 | id := c.nextID() |
| 548 | typ, data, err := c.sendPacket(context.Background(), nil, &sshFxpSymlinkPacket{ |
| 549 | ID: id, |
| 550 | Linkpath: newname, |
| 551 | Targetpath: oldname, |
| 552 | }) |
| 553 | if err != nil { |
| 554 | return err |
| 555 | } |
| 556 | switch typ { |
| 557 | case sshFxpStatus: |
| 558 | return normaliseError(unmarshalStatus(id, data)) |
| 559 | default: |
| 560 | return unimplementedPacketErr(typ) |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | func (c *Client) fsetstat(handle string, flags uint32, attrs any) error { |
| 565 | id := c.nextID() |
| 566 | typ, data, err := c.sendPacket(context.Background(), nil, &sshFxpFsetstatPacket{ |
| 567 | ID: id, |
| 568 | Handle: handle, |
| 569 | Flags: flags, |
| 570 | Attrs: attrs, |
| 571 | }) |
| 572 | if err != nil { |
| 573 | return err |
| 574 | } |
| 575 | switch typ { |
| 576 | case sshFxpStatus: |
| 577 | return normaliseError(unmarshalStatus(id, data)) |
| 578 | default: |
| 579 | return unimplementedPacketErr(typ) |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | |
| 584 | func (c *Client) setstat(path string, flags uint32, attrs any) error { |
| 585 | id := c.nextID() |
| 586 | typ, data, err := c.sendPacket(context.Background(), nil, &sshFxpSetstatPacket{ |
| 587 | ID: id, |
| 588 | Path: path, |
| 589 | Flags: flags, |
| 590 | Attrs: attrs, |
| 591 | }) |
| 592 | if err != nil { |
| 593 | return err |
| 594 | } |
| 595 | switch typ { |
| 596 | case sshFxpStatus: |
| 597 | return normaliseError(unmarshalStatus(id, data)) |
| 598 | default: |
| 599 | return unimplementedPacketErr(typ) |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | |
| 604 | func (c *Client) Chtimes(path string, atime time.Time, mtime time.Time) error { |
| 605 | type times struct { |
| 606 | Atime uint32 |
| 607 | Mtime uint32 |
| 608 | } |
| 609 | attrs := times{uint32(atime.Unix()), uint32(mtime.Unix())} |
| 610 | return c.setstat(path, sshFileXferAttrACmodTime, attrs) |
| 611 | } |
| 612 | |
| 613 | |
| 614 | func (c *Client) Chown(path string, uid, gid int) error { |
| 615 | type owner struct { |
| 616 | UID uint32 |
| 617 | GID uint32 |
| 618 | } |
| 619 | attrs := owner{uint32(uid), uint32(gid)} |
| 620 | return c.setstat(path, sshFileXferAttrUIDGID, attrs) |
| 621 | } |
| 622 | |
| 623 | |
| 624 | |
| 625 | |
| 626 | |
| 627 | |
| 628 | func (c *Client) Chmod(path string, mode os.FileMode) error { |
| 629 | return c.setstat(path, sshFileXferAttrPermissions, toChmodPerm(mode)) |
| 630 | } |
| 631 | |
| 632 | |
| 633 | |
| 634 | |
| 635 | |
| 636 | func (c *Client) Truncate(path string, size int64) error { |
| 637 | return c.setstat(path, sshFileXferAttrSize, uint64(size)) |
| 638 | } |
| 639 | |
| 640 | |
| 641 | |
| 642 | |
| 643 | |
| 644 | |
| 645 | |
| 646 | |
| 647 | func (c *Client) SetExtendedData(path string, extended []StatExtended) error { |
| 648 | attrs := &FileStat{ |
| 649 | Extended: extended, |
| 650 | } |
| 651 | return c.setstat(path, sshFileXferAttrExtended, attrs) |
| 652 | } |
| 653 | |
| 654 | |
| 655 | |
| 656 | |
| 657 | func (c *Client) Open(path string) (*File, error) { |
| 658 | return c.open(path, toPflags(os.O_RDONLY)) |
| 659 | } |
| 660 | |
| 661 | |
| 662 | |
| 663 | |
| 664 | func (c *Client) OpenFile(path string, f int) (*File, error) { |
| 665 | return c.open(path, toPflags(f)) |
| 666 | } |
| 667 | |
| 668 | func (c *Client) open(path string, pflags uint32) (*File, error) { |
| 669 | id := c.nextID() |
| 670 | typ, data, err := c.sendPacket(context.Background(), nil, &sshFxpOpenPacket{ |
| 671 | ID: id, |
| 672 | Path: path, |
| 673 | Pflags: pflags, |
| 674 | }) |
| 675 | if err != nil { |
| 676 | return nil, err |
| 677 | } |
| 678 | switch typ { |
| 679 | case sshFxpHandle: |
| 680 | sid, data := unmarshalUint32(data) |
| 681 | if sid != id { |
| 682 | return nil, &unexpectedIDErr{id, sid} |
| 683 | } |
| 684 | handle, _ := unmarshalString(data) |
| 685 | return &File{c: c, path: path, handle: handle}, nil |
| 686 | case sshFxpStatus: |
| 687 | return nil, normaliseError(unmarshalStatus(id, data)) |
| 688 | default: |
| 689 | return nil, unimplementedPacketErr(typ) |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | |
| 694 | |
| 695 | |
| 696 | func (c *Client) close(handle string) error { |
| 697 | id := c.nextID() |
| 698 | typ, data, err := c.sendPacket(context.Background(), nil, &sshFxpClosePacket{ |
| 699 | ID: id, |
| 700 | Handle: handle, |
| 701 | }) |
| 702 | if err != nil { |
| 703 | return err |
| 704 | } |
| 705 | switch typ { |
| 706 | case sshFxpStatus: |
| 707 | return normaliseError(unmarshalStatus(id, data)) |
| 708 | default: |
| 709 | return unimplementedPacketErr(typ) |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | func (c *Client) stat(path string) (*FileStat, error) { |
| 714 | id := c.nextID() |
| 715 | typ, data, err := c.sendPacket(context.Background(), nil, &sshFxpStatPacket{ |
| 716 | ID: id, |
| 717 | Path: path, |
| 718 | }) |
| 719 | if err != nil { |
| 720 | return nil, err |
| 721 | } |
| 722 | switch typ { |
| 723 | case sshFxpAttrs: |
| 724 | sid, data := unmarshalUint32(data) |
| 725 | if sid != id { |
| 726 | return nil, &unexpectedIDErr{id, sid} |
| 727 | } |
| 728 | attr, _, err := unmarshalAttrs(data) |
| 729 | return attr, err |
| 730 | case sshFxpStatus: |
| 731 | return nil, normaliseError(unmarshalStatus(id, data)) |
| 732 | default: |
| 733 | return nil, unimplementedPacketErr(typ) |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | func (c *Client) fstat(handle string) (*FileStat, error) { |
| 738 | id := c.nextID() |
| 739 | typ, data, err := c.sendPacket(context.Background(), nil, &sshFxpFstatPacket{ |
| 740 | ID: id, |
| 741 | Handle: handle, |
| 742 | }) |
| 743 | if err != nil { |
| 744 | return nil, err |
| 745 | } |
| 746 | switch typ { |
| 747 | case sshFxpAttrs: |
| 748 | sid, data := unmarshalUint32(data) |
| 749 | if sid != id { |
| 750 | return nil, &unexpectedIDErr{id, sid} |
| 751 | } |
| 752 | attr, _, err := unmarshalAttrs(data) |
| 753 | return attr, err |
| 754 | case sshFxpStatus: |
| 755 | return nil, normaliseError(unmarshalStatus(id, data)) |
| 756 | default: |
| 757 | return nil, unimplementedPacketErr(typ) |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | |
| 762 | |
| 763 | |
| 764 | |
| 765 | func (c *Client) StatVFS(path string) (*StatVFS, error) { |
| 766 | |
| 767 | id := c.nextID() |
| 768 | typ, data, err := c.sendPacket(context.Background(), nil, &sshFxpStatvfsPacket{ |
| 769 | ID: id, |
| 770 | Path: path, |
| 771 | }) |
| 772 | if err != nil { |
| 773 | return nil, err |
| 774 | } |
| 775 | |
| 776 | switch typ { |
| 777 | |
| 778 | case sshFxpExtendedReply: |
| 779 | var response StatVFS |
| 780 | err = binary.Read(bytes.NewReader(data), binary.BigEndian, &response) |
| 781 | if err != nil { |
| 782 | return nil, errors.New("can not parse reply") |
| 783 | } |
| 784 | |
| 785 | return &response, nil |
| 786 | |
| 787 | |
| 788 | case sshFxpStatus: |
| 789 | return nil, normaliseError(unmarshalStatus(id, data)) |
| 790 | |
| 791 | default: |
| 792 | return nil, unimplementedPacketErr(typ) |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | |
| 797 | |
| 798 | |
| 799 | func (c *Client) Join(elem ...string) string { return path.Join(elem...) } |
| 800 | |
| 801 | |
| 802 | |
| 803 | |
| 804 | func (c *Client) Remove(path string) error { |
| 805 | errF := c.removeFile(path) |
| 806 | if errF == nil { |
| 807 | return nil |
| 808 | } |
| 809 | |
| 810 | errD := c.RemoveDirectory(path) |
| 811 | if errD == nil { |
| 812 | return nil |
| 813 | } |
| 814 | |
| 815 | |
| 816 | |
| 817 | if errF, ok := errF.(*os.PathError); ok { |
| 818 | |
| 819 | |
| 820 | |
| 821 | if errD, ok := errD.(*os.PathError); ok && errors.Is(errF.Err, errD.Err) { |
| 822 | |
| 823 | |
| 824 | return errF |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | fi, err := c.Stat(path) |
| 829 | if err != nil { |
| 830 | return err |
| 831 | } |
| 832 | |
| 833 | if fi.IsDir() { |
| 834 | return errD |
| 835 | } |
| 836 | |
| 837 | return errF |
| 838 | } |
| 839 | |
| 840 | func (c *Client) removeFile(path string) error { |
| 841 | id := c.nextID() |
| 842 | typ, data, err := c.sendPacket(context.Background(), nil, &sshFxpRemovePacket{ |
| 843 | ID: id, |
| 844 | Filename: path, |
| 845 | }) |
| 846 | if err != nil { |
| 847 | return err |
| 848 | } |
| 849 | switch typ { |
| 850 | case sshFxpStatus: |
| 851 | err = normaliseError(unmarshalStatus(id, data)) |
| 852 | if err == nil { |
| 853 | return nil |
| 854 | } |
| 855 | return &os.PathError{ |
| 856 | Op: "remove", |
| 857 | Path: path, |
| 858 | Err: err, |
| 859 | } |
| 860 | default: |
| 861 | return unimplementedPacketErr(typ) |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | |
| 866 | func (c *Client) RemoveDirectory(path string) error { |
| 867 | id := c.nextID() |
| 868 | typ, data, err := c.sendPacket(context.Background(), nil, &sshFxpRmdirPacket{ |
| 869 | ID: id, |
| 870 | Path: path, |
| 871 | }) |
| 872 | if err != nil { |
| 873 | return err |
| 874 | } |
| 875 | switch typ { |
| 876 | case sshFxpStatus: |
| 877 | err = normaliseError(unmarshalStatus(id, data)) |
| 878 | if err == nil { |
| 879 | return nil |
| 880 | } |
| 881 | return &os.PathError{ |
| 882 | Op: "remove", |
| 883 | Path: path, |
| 884 | Err: err, |
| 885 | } |
| 886 | default: |
| 887 | return unimplementedPacketErr(typ) |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | |
| 892 | func (c *Client) Rename(oldname, newname string) error { |
| 893 | id := c.nextID() |
| 894 | typ, data, err := c.sendPacket(context.Background(), nil, &sshFxpRenamePacket{ |
| 895 | ID: id, |
| 896 | Oldpath: oldname, |
| 897 | Newpath: newname, |
| 898 | }) |
| 899 | if err != nil { |
| 900 | return err |
| 901 | } |
| 902 | switch typ { |
| 903 | case sshFxpStatus: |
| 904 | return normaliseError(unmarshalStatus(id, data)) |
| 905 | default: |
| 906 | return unimplementedPacketErr(typ) |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | |
| 911 | |
| 912 | func (c *Client) PosixRename(oldname, newname string) error { |
| 913 | id := c.nextID() |
| 914 | typ, data, err := c.sendPacket(context.Background(), nil, &sshFxpPosixRenamePacket{ |
| 915 | ID: id, |
| 916 | Oldpath: oldname, |
| 917 | Newpath: newname, |
| 918 | }) |
| 919 | if err != nil { |
| 920 | return err |
| 921 | } |
| 922 | switch typ { |
| 923 | case sshFxpStatus: |
| 924 | return normaliseError(unmarshalStatus(id, data)) |
| 925 | default: |
| 926 | return unimplementedPacketErr(typ) |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | |
| 931 | |
| 932 | |
| 933 | |
| 934 | func (c *Client) RealPath(path string) (string, error) { |
| 935 | id := c.nextID() |
| 936 | typ, data, err := c.sendPacket(context.Background(), nil, &sshFxpRealpathPacket{ |
| 937 | ID: id, |
| 938 | Path: path, |
| 939 | }) |
| 940 | if err != nil { |
| 941 | return "", err |
| 942 | } |
| 943 | switch typ { |
| 944 | case sshFxpName: |
| 945 | sid, data := unmarshalUint32(data) |
| 946 | if sid != id { |
| 947 | return "", &unexpectedIDErr{id, sid} |
| 948 | } |
| 949 | count, data := unmarshalUint32(data) |
| 950 | if count != 1 { |
| 951 | return "", unexpectedCount(1, count) |
| 952 | } |
| 953 | filename, _ := unmarshalString(data) |
| 954 | return filename, nil |
| 955 | case sshFxpStatus: |
| 956 | return "", normaliseError(unmarshalStatus(id, data)) |
| 957 | default: |
| 958 | return "", unimplementedPacketErr(typ) |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | |
| 963 | |
| 964 | func (c *Client) Getwd() (string, error) { |
| 965 | return c.RealPath(".") |
| 966 | } |
| 967 | |
| 968 | |
| 969 | |
| 970 | |
| 971 | func (c *Client) Mkdir(path string) error { |
| 972 | id := c.nextID() |
| 973 | typ, data, err := c.sendPacket(context.Background(), nil, &sshFxpMkdirPacket{ |
| 974 | ID: id, |
| 975 | Path: path, |
| 976 | }) |
| 977 | if err != nil { |
| 978 | return err |
| 979 | } |
| 980 | switch typ { |
| 981 | case sshFxpStatus: |
| 982 | return normaliseError(unmarshalStatus(id, data)) |
| 983 | default: |
| 984 | return unimplementedPacketErr(typ) |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | |
| 989 | |
| 990 | |
| 991 | |
| 992 | func (c *Client) MkdirAll(path string) error { |
| 993 | |
| 994 | |
| 995 | dir, err := c.Stat(path) |
| 996 | if err == nil { |
| 997 | if dir.IsDir() { |
| 998 | return nil |
| 999 | } |
| 1000 | return &os.PathError{Op: "mkdir", Path: path, Err: syscall.ENOTDIR} |
| 1001 | } |
| 1002 | |
| 1003 | |
| 1004 | i := len(path) |
| 1005 | for i > 0 && path[i-1] == '/' { |
| 1006 | i-- |
| 1007 | } |
| 1008 | |
| 1009 | j := i |
| 1010 | for j > 0 && path[j-1] != '/' { |
| 1011 | j-- |
| 1012 | } |
| 1013 | |
| 1014 | if j > 1 { |
| 1015 | |
| 1016 | err = c.MkdirAll(path[0 : j-1]) |
| 1017 | if err != nil { |
| 1018 | return err |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | |
| 1023 | err = c.Mkdir(path) |
| 1024 | if err != nil { |
| 1025 | |
| 1026 | |
| 1027 | dir, err1 := c.Lstat(path) |
| 1028 | if err1 == nil && dir.IsDir() { |
| 1029 | return nil |
| 1030 | } |
| 1031 | return err |
| 1032 | } |
| 1033 | return nil |
| 1034 | } |
| 1035 | |
| 1036 | |
| 1037 | |
| 1038 | func (c *Client) RemoveAll(path string) error { |
| 1039 | |
| 1040 | |
| 1041 | fi, err := c.Stat(path) |
| 1042 | if err != nil { |
| 1043 | return err |
| 1044 | } |
| 1045 | |
| 1046 | if fi.IsDir() { |
| 1047 | |
| 1048 | files, err := c.ReadDir(path) |
| 1049 | if err != nil { |
| 1050 | return err |
| 1051 | } |
| 1052 | |
| 1053 | for _, file := range files { |
| 1054 | if file.IsDir() { |
| 1055 | |
| 1056 | err = c.RemoveAll(path + "/" + file.Name()) |
| 1057 | if err != nil { |
| 1058 | return err |
| 1059 | } |
| 1060 | } else { |
| 1061 | |
| 1062 | err = c.Remove(path + "/" + file.Name()) |
| 1063 | if err != nil { |
| 1064 | return err |
| 1065 | } |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | } |
| 1070 | |
| 1071 | return c.Remove(path) |
| 1072 | |
| 1073 | } |
| 1074 | |
| 1075 | |
| 1076 | type File struct { |
| 1077 | c *Client |
| 1078 | path string |
| 1079 | |
| 1080 | mu sync.RWMutex |
| 1081 | handle string |
| 1082 | offset int64 |
| 1083 | } |
| 1084 | |
| 1085 | |
| 1086 | |
| 1087 | func (f *File) Close() error { |
| 1088 | f.mu.Lock() |
| 1089 | defer f.mu.Unlock() |
| 1090 | |
| 1091 | if f.handle == "" { |
| 1092 | return os.ErrClosed |
| 1093 | } |
| 1094 | |
| 1095 | |
| 1096 | |
| 1097 | |
| 1098 | |
| 1099 | |
| 1100 | |
| 1101 | handle := f.handle |
| 1102 | f.handle = "" |
| 1103 | |
| 1104 | return f.c.close(handle) |
| 1105 | } |
| 1106 | |
| 1107 | |
| 1108 | func (f *File) Name() string { |
| 1109 | return f.path |
| 1110 | } |
| 1111 | |
| 1112 | |
| 1113 | |
| 1114 | |
| 1115 | |
| 1116 | |
| 1117 | |
| 1118 | |
| 1119 | |
| 1120 | |
| 1121 | func (f *File) Read(b []byte) (int, error) { |
| 1122 | f.mu.Lock() |
| 1123 | defer f.mu.Unlock() |
| 1124 | |
| 1125 | n, err := f.readAt(b, f.offset) |
| 1126 | f.offset += int64(n) |
| 1127 | return n, err |
| 1128 | } |
| 1129 | |
| 1130 | |
| 1131 | |
| 1132 | func (f *File) readChunkAt(ch chan result, b []byte, off int64) (n int, err error) { |
| 1133 | for err == nil && n < len(b) { |
| 1134 | id := f.c.nextID() |
| 1135 | typ, data, err := f.c.sendPacket(context.Background(), ch, &sshFxpReadPacket{ |
| 1136 | ID: id, |
| 1137 | Handle: f.handle, |
| 1138 | Offset: uint64(off) + uint64(n), |
| 1139 | Len: uint32(len(b) - n), |
| 1140 | }) |
| 1141 | if err != nil { |
| 1142 | return n, err |
| 1143 | } |
| 1144 | |
| 1145 | switch typ { |
| 1146 | case sshFxpStatus: |
| 1147 | return n, normaliseError(unmarshalStatus(id, data)) |
| 1148 | |
| 1149 | case sshFxpData: |
| 1150 | sid, data := unmarshalUint32(data) |
| 1151 | if id != sid { |
| 1152 | return n, &unexpectedIDErr{id, sid} |
| 1153 | } |
| 1154 | |
| 1155 | l, data := unmarshalUint32(data) |
| 1156 | n += copy(b[n:], data[:l]) |
| 1157 | |
| 1158 | default: |
| 1159 | return n, unimplementedPacketErr(typ) |
| 1160 | } |
| 1161 | } |
| 1162 | |
| 1163 | return |
| 1164 | } |
| 1165 | |
| 1166 | func (f *File) readAtSequential(b []byte, off int64) (read int, err error) { |
| 1167 | for read < len(b) { |
| 1168 | rb := b[read:] |
| 1169 | if len(rb) > f.c.maxPacket { |
| 1170 | rb = rb[:f.c.maxPacket] |
| 1171 | } |
| 1172 | n, err := f.readChunkAt(nil, rb, off+int64(read)) |
| 1173 | if n < 0 { |
| 1174 | panic("sftp.File: returned negative count from readChunkAt") |
| 1175 | } |
| 1176 | if n > 0 { |
| 1177 | read += n |
| 1178 | } |
| 1179 | if err != nil { |
| 1180 | return read, err |
| 1181 | } |
| 1182 | } |
| 1183 | return read, nil |
| 1184 | } |
| 1185 | |
| 1186 | |
| 1187 | |
| 1188 | |
| 1189 | func (f *File) ReadAt(b []byte, off int64) (int, error) { |
| 1190 | f.mu.RLock() |
| 1191 | defer f.mu.RUnlock() |
| 1192 | |
| 1193 | return f.readAt(b, off) |
| 1194 | } |
| 1195 | |
| 1196 | |
| 1197 | |
| 1198 | func (f *File) readAt(b []byte, off int64) (int, error) { |
| 1199 | if f.handle == "" { |
| 1200 | return 0, os.ErrClosed |
| 1201 | } |
| 1202 | |
| 1203 | if len(b) <= f.c.maxPacket { |
| 1204 | |
| 1205 | |
| 1206 | return f.readChunkAt(nil, b, off) |
| 1207 | } |
| 1208 | |
| 1209 | if f.c.disableConcurrentReads { |
| 1210 | return f.readAtSequential(b, off) |
| 1211 | } |
| 1212 | |
| 1213 | |
| 1214 | |
| 1215 | |
| 1216 | |
| 1217 | cancel := make(chan struct{}) |
| 1218 | |
| 1219 | concurrency := len(b)/f.c.maxPacket + 1 |
| 1220 | if concurrency > f.c.maxConcurrentRequests || concurrency < 1 { |
| 1221 | concurrency = f.c.maxConcurrentRequests |
| 1222 | } |
| 1223 | |
| 1224 | resPool := newResChanPool(concurrency) |
| 1225 | |
| 1226 | type work struct { |
| 1227 | id uint32 |
| 1228 | res chan result |
| 1229 | |
| 1230 | b []byte |
| 1231 | off int64 |
| 1232 | } |
| 1233 | workCh := make(chan work) |
| 1234 | |
| 1235 | |
| 1236 | go func() { |
| 1237 | defer close(workCh) |
| 1238 | |
| 1239 | b := b |
| 1240 | offset := off |
| 1241 | chunkSize := f.c.maxPacket |
| 1242 | |
| 1243 | for len(b) > 0 { |
| 1244 | rb := b |
| 1245 | if len(rb) > chunkSize { |
| 1246 | rb = rb[:chunkSize] |
| 1247 | } |
| 1248 | |
| 1249 | id := f.c.nextID() |
| 1250 | res := resPool.Get() |
| 1251 | |
| 1252 | f.c.dispatchRequest(res, &sshFxpReadPacket{ |
| 1253 | ID: id, |
| 1254 | Handle: f.handle, |
| 1255 | Offset: uint64(offset), |
| 1256 | Len: uint32(len(rb)), |
| 1257 | }) |
| 1258 | |
| 1259 | select { |
| 1260 | case workCh <- work{id, res, rb, offset}: |
| 1261 | case <-cancel: |
| 1262 | return |
| 1263 | } |
| 1264 | |
| 1265 | offset += int64(len(rb)) |
| 1266 | b = b[len(rb):] |
| 1267 | } |
| 1268 | }() |
| 1269 | |
| 1270 | type rErr struct { |
| 1271 | off int64 |
| 1272 | err error |
| 1273 | } |
| 1274 | errCh := make(chan rErr) |
| 1275 | |
| 1276 | var wg sync.WaitGroup |
| 1277 | wg.Add(concurrency) |
| 1278 | for i := 0; i < concurrency; i++ { |
| 1279 | |
| 1280 | go func() { |
| 1281 | defer wg.Done() |
| 1282 | |
| 1283 | for packet := range workCh { |
| 1284 | var n int |
| 1285 | |
| 1286 | s := <-packet.res |
| 1287 | resPool.Put(packet.res) |
| 1288 | |
| 1289 | err := s.err |
| 1290 | if err == nil { |
| 1291 | switch s.typ { |
| 1292 | case sshFxpStatus: |
| 1293 | err = normaliseError(unmarshalStatus(packet.id, s.data)) |
| 1294 | |
| 1295 | case sshFxpData: |
| 1296 | sid, data := unmarshalUint32(s.data) |
| 1297 | if packet.id != sid { |
| 1298 | err = &unexpectedIDErr{packet.id, sid} |
| 1299 | |
| 1300 | } else { |
| 1301 | l, data := unmarshalUint32(data) |
| 1302 | n = copy(packet.b, data[:l]) |
| 1303 | |
| 1304 | |
| 1305 | |
| 1306 | |
| 1307 | if n < len(packet.b) { |
| 1308 | err = io.EOF |
| 1309 | } |
| 1310 | } |
| 1311 | |
| 1312 | default: |
| 1313 | err = unimplementedPacketErr(s.typ) |
| 1314 | } |
| 1315 | } |
| 1316 | |
| 1317 | if err != nil { |
| 1318 | |
| 1319 | errCh <- rErr{packet.off + int64(n), err} |
| 1320 | |
| 1321 | |
| 1322 | |
| 1323 | } |
| 1324 | } |
| 1325 | }() |
| 1326 | } |
| 1327 | |
| 1328 | |
| 1329 | go func() { |
| 1330 | wg.Wait() |
| 1331 | close(errCh) |
| 1332 | }() |
| 1333 | |
| 1334 | |
| 1335 | firstErr := rErr{math.MaxInt64, nil} |
| 1336 | for rErr := range errCh { |
| 1337 | if rErr.off <= firstErr.off { |
| 1338 | firstErr = rErr |
| 1339 | } |
| 1340 | |
| 1341 | select { |
| 1342 | case <-cancel: |
| 1343 | default: |
| 1344 | |
| 1345 | close(cancel) |
| 1346 | } |
| 1347 | } |
| 1348 | |
| 1349 | if firstErr.err != nil { |
| 1350 | |
| 1351 | return int(firstErr.off - off), firstErr.err |
| 1352 | } |
| 1353 | |
| 1354 | |
| 1355 | return len(b), nil |
| 1356 | } |
| 1357 | |
| 1358 | |
| 1359 | func (f *File) writeToSequential(w io.Writer) (written int64, err error) { |
| 1360 | b := make([]byte, f.c.maxPacket) |
| 1361 | ch := make(chan result, 1) |
| 1362 | |
| 1363 | for { |
| 1364 | n, err := f.readChunkAt(ch, b, f.offset) |
| 1365 | if n < 0 { |
| 1366 | panic("sftp.File: returned negative count from readChunkAt") |
| 1367 | } |
| 1368 | |
| 1369 | if n > 0 { |
| 1370 | f.offset += int64(n) |
| 1371 | |
| 1372 | m, err := w.Write(b[:n]) |
| 1373 | written += int64(m) |
| 1374 | |
| 1375 | if err != nil { |
| 1376 | return written, err |
| 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | if err != nil { |
| 1381 | if err == io.EOF { |
| 1382 | return written, nil |
| 1383 | } |
| 1384 | |
| 1385 | return written, err |
| 1386 | } |
| 1387 | } |
| 1388 | } |
| 1389 | |
| 1390 | |
| 1391 | |
| 1392 | |
| 1393 | |
| 1394 | |
| 1395 | |
| 1396 | |
| 1397 | func (f *File) WriteTo(w io.Writer) (written int64, err error) { |
| 1398 | f.mu.Lock() |
| 1399 | defer f.mu.Unlock() |
| 1400 | |
| 1401 | if f.handle == "" { |
| 1402 | return 0, os.ErrClosed |
| 1403 | } |
| 1404 | |
| 1405 | if f.c.disableConcurrentReads { |
| 1406 | return f.writeToSequential(w) |
| 1407 | } |
| 1408 | |
| 1409 | |
| 1410 | var fileStat *FileStat |
| 1411 | if f.c.useFstat { |
| 1412 | fileStat, err = f.c.fstat(f.handle) |
| 1413 | } else { |
| 1414 | fileStat, err = f.c.stat(f.path) |
| 1415 | } |
| 1416 | if err != nil { |
| 1417 | return 0, err |
| 1418 | } |
| 1419 | |
| 1420 | fileSize := fileStat.Size |
| 1421 | if fileSize <= uint64(f.c.maxPacket) || !isRegular(fileStat.Mode) { |
| 1422 | |
| 1423 | return f.writeToSequential(w) |
| 1424 | } |
| 1425 | |
| 1426 | concurrency64 := fileSize/uint64(f.c.maxPacket) + 1 |
| 1427 | if concurrency64 > uint64(f.c.maxConcurrentRequests) || concurrency64 < 1 { |
| 1428 | concurrency64 = uint64(f.c.maxConcurrentRequests) |
| 1429 | } |
| 1430 | |
| 1431 | concurrency := int(concurrency64) |
| 1432 | |
| 1433 | chunkSize := f.c.maxPacket |
| 1434 | pool := newBufPool(concurrency, chunkSize) |
| 1435 | resPool := newResChanPool(concurrency) |
| 1436 | |
| 1437 | cancel := make(chan struct{}) |
| 1438 | var wg sync.WaitGroup |
| 1439 | defer func() { |
| 1440 | |
| 1441 | close(cancel) |
| 1442 | |
| 1443 | |
| 1444 | |
| 1445 | wg.Wait() |
| 1446 | }() |
| 1447 | |
| 1448 | type writeWork struct { |
| 1449 | b []byte |
| 1450 | off int64 |
| 1451 | err error |
| 1452 | |
| 1453 | next chan writeWork |
| 1454 | } |
| 1455 | writeCh := make(chan writeWork) |
| 1456 | |
| 1457 | type readWork struct { |
| 1458 | id uint32 |
| 1459 | res chan result |
| 1460 | off int64 |
| 1461 | |
| 1462 | cur, next chan writeWork |
| 1463 | } |
| 1464 | readCh := make(chan readWork) |
| 1465 | |
| 1466 | |
| 1467 | go func() { |
| 1468 | defer close(readCh) |
| 1469 | |
| 1470 | off := f.offset |
| 1471 | |
| 1472 | cur := writeCh |
| 1473 | for { |
| 1474 | id := f.c.nextID() |
| 1475 | res := resPool.Get() |
| 1476 | |
| 1477 | next := make(chan writeWork) |
| 1478 | readWork := readWork{ |
| 1479 | id: id, |
| 1480 | res: res, |
| 1481 | off: off, |
| 1482 | |
| 1483 | cur: cur, |
| 1484 | next: next, |
| 1485 | } |
| 1486 | |
| 1487 | f.c.dispatchRequest(res, &sshFxpReadPacket{ |
| 1488 | ID: id, |
| 1489 | Handle: f.handle, |
| 1490 | Offset: uint64(off), |
| 1491 | Len: uint32(chunkSize), |
| 1492 | }) |
| 1493 | |
| 1494 | select { |
| 1495 | case readCh <- readWork: |
| 1496 | case <-cancel: |
| 1497 | return |
| 1498 | } |
| 1499 | |
| 1500 | off += int64(chunkSize) |
| 1501 | cur = next |
| 1502 | } |
| 1503 | }() |
| 1504 | |
| 1505 | wg.Add(concurrency) |
| 1506 | for i := 0; i < concurrency; i++ { |
| 1507 | |
| 1508 | go func() { |
| 1509 | defer wg.Done() |
| 1510 | |
| 1511 | for readWork := range readCh { |
| 1512 | var b []byte |
| 1513 | var n int |
| 1514 | |
| 1515 | s := <-readWork.res |
| 1516 | resPool.Put(readWork.res) |
| 1517 | |
| 1518 | err := s.err |
| 1519 | if err == nil { |
| 1520 | switch s.typ { |
| 1521 | case sshFxpStatus: |
| 1522 | err = normaliseError(unmarshalStatus(readWork.id, s.data)) |
| 1523 | |
| 1524 | case sshFxpData: |
| 1525 | sid, data := unmarshalUint32(s.data) |
| 1526 | if readWork.id != sid { |
| 1527 | err = &unexpectedIDErr{readWork.id, sid} |
| 1528 | |
| 1529 | } else { |
| 1530 | l, data := unmarshalUint32(data) |
| 1531 | b = pool.Get()[:l] |
| 1532 | n = copy(b, data[:l]) |
| 1533 | b = b[:n] |
| 1534 | } |
| 1535 | |
| 1536 | default: |
| 1537 | err = unimplementedPacketErr(s.typ) |
| 1538 | } |
| 1539 | } |
| 1540 | |
| 1541 | writeWork := writeWork{ |
| 1542 | b: b, |
| 1543 | off: readWork.off, |
| 1544 | err: err, |
| 1545 | |
| 1546 | next: readWork.next, |
| 1547 | } |
| 1548 | |
| 1549 | select { |
| 1550 | case readWork.cur <- writeWork: |
| 1551 | case <-cancel: |
| 1552 | } |
| 1553 | |
| 1554 | |
| 1555 | |
| 1556 | } |
| 1557 | }() |
| 1558 | } |
| 1559 | |
| 1560 | |
| 1561 | cur := writeCh |
| 1562 | for { |
| 1563 | packet, ok := <-cur |
| 1564 | if !ok { |
| 1565 | return written, errors.New("sftp.File.WriteTo: unexpectedly closed channel") |
| 1566 | } |
| 1567 | |
| 1568 | |
| 1569 | f.offset = packet.off + int64(len(packet.b)) |
| 1570 | |
| 1571 | if len(packet.b) > 0 { |
| 1572 | n, err := w.Write(packet.b) |
| 1573 | written += int64(n) |
| 1574 | if err != nil { |
| 1575 | return written, err |
| 1576 | } |
| 1577 | } |
| 1578 | |
| 1579 | if packet.err != nil { |
| 1580 | if packet.err == io.EOF { |
| 1581 | return written, nil |
| 1582 | } |
| 1583 | |
| 1584 | return written, packet.err |
| 1585 | } |
| 1586 | |
| 1587 | pool.Put(packet.b) |
| 1588 | cur = packet.next |
| 1589 | } |
| 1590 | } |
| 1591 | |
| 1592 | |
| 1593 | |
| 1594 | func (f *File) Stat() (os.FileInfo, error) { |
| 1595 | f.mu.RLock() |
| 1596 | defer f.mu.RUnlock() |
| 1597 | |
| 1598 | if f.handle == "" { |
| 1599 | return nil, os.ErrClosed |
| 1600 | } |
| 1601 | |
| 1602 | return f.stat() |
| 1603 | } |
| 1604 | |
| 1605 | func (f *File) stat() (os.FileInfo, error) { |
| 1606 | fs, err := f.c.fstat(f.handle) |
| 1607 | if err != nil { |
| 1608 | return nil, err |
| 1609 | } |
| 1610 | return fileInfoFromStat(fs, path.Base(f.path)), nil |
| 1611 | } |
| 1612 | |
| 1613 | |
| 1614 | |
| 1615 | |
| 1616 | |
| 1617 | |
| 1618 | |
| 1619 | |
| 1620 | |
| 1621 | func (f *File) Write(b []byte) (int, error) { |
| 1622 | f.mu.Lock() |
| 1623 | defer f.mu.Unlock() |
| 1624 | |
| 1625 | if f.handle == "" { |
| 1626 | return 0, os.ErrClosed |
| 1627 | } |
| 1628 | |
| 1629 | n, err := f.writeAt(b, f.offset) |
| 1630 | f.offset += int64(n) |
| 1631 | return n, err |
| 1632 | } |
| 1633 | |
| 1634 | func (f *File) writeChunkAt(ch chan result, b []byte, off int64) (int, error) { |
| 1635 | typ, data, err := f.c.sendPacket(context.Background(), ch, &sshFxpWritePacket{ |
| 1636 | ID: f.c.nextID(), |
| 1637 | Handle: f.handle, |
| 1638 | Offset: uint64(off), |
| 1639 | Length: uint32(len(b)), |
| 1640 | Data: b, |
| 1641 | }) |
| 1642 | if err != nil { |
| 1643 | return 0, err |
| 1644 | } |
| 1645 | |
| 1646 | switch typ { |
| 1647 | case sshFxpStatus: |
| 1648 | id, _ := unmarshalUint32(data) |
| 1649 | err := normaliseError(unmarshalStatus(id, data)) |
| 1650 | if err != nil { |
| 1651 | return 0, err |
| 1652 | } |
| 1653 | |
| 1654 | default: |
| 1655 | return 0, unimplementedPacketErr(typ) |
| 1656 | } |
| 1657 | |
| 1658 | return len(b), nil |
| 1659 | } |
| 1660 | |
| 1661 | |
| 1662 | func (f *File) writeAtConcurrent(b []byte, off int64) (int, error) { |
| 1663 | |
| 1664 | |
| 1665 | |
| 1666 | |
| 1667 | |
| 1668 | cancel := make(chan struct{}) |
| 1669 | |
| 1670 | type work struct { |
| 1671 | id uint32 |
| 1672 | res chan result |
| 1673 | |
| 1674 | off int64 |
| 1675 | } |
| 1676 | workCh := make(chan work) |
| 1677 | |
| 1678 | concurrency := len(b)/f.c.maxPacket + 1 |
| 1679 | if concurrency > f.c.maxConcurrentRequests || concurrency < 1 { |
| 1680 | concurrency = f.c.maxConcurrentRequests |
| 1681 | } |
| 1682 | |
| 1683 | pool := newResChanPool(concurrency) |
| 1684 | |
| 1685 | |
| 1686 | go func() { |
| 1687 | defer close(workCh) |
| 1688 | |
| 1689 | var read int |
| 1690 | chunkSize := f.c.maxPacket |
| 1691 | |
| 1692 | for read < len(b) { |
| 1693 | wb := b[read:] |
| 1694 | if len(wb) > chunkSize { |
| 1695 | wb = wb[:chunkSize] |
| 1696 | } |
| 1697 | |
| 1698 | id := f.c.nextID() |
| 1699 | res := pool.Get() |
| 1700 | off := off + int64(read) |
| 1701 | |
| 1702 | f.c.dispatchRequest(res, &sshFxpWritePacket{ |
| 1703 | ID: id, |
| 1704 | Handle: f.handle, |
| 1705 | Offset: uint64(off), |
| 1706 | Length: uint32(len(wb)), |
| 1707 | Data: wb, |
| 1708 | }) |
| 1709 | |
| 1710 | select { |
| 1711 | case workCh <- work{id, res, off}: |
| 1712 | case <-cancel: |
| 1713 | return |
| 1714 | } |
| 1715 | |
| 1716 | read += len(wb) |
| 1717 | } |
| 1718 | }() |
| 1719 | |
| 1720 | type wErr struct { |
| 1721 | off int64 |
| 1722 | err error |
| 1723 | } |
| 1724 | errCh := make(chan wErr) |
| 1725 | |
| 1726 | var wg sync.WaitGroup |
| 1727 | wg.Add(concurrency) |
| 1728 | for i := 0; i < concurrency; i++ { |
| 1729 | |
| 1730 | go func() { |
| 1731 | defer wg.Done() |
| 1732 | |
| 1733 | for work := range workCh { |
| 1734 | s := <-work.res |
| 1735 | pool.Put(work.res) |
| 1736 | |
| 1737 | err := s.err |
| 1738 | if err == nil { |
| 1739 | switch s.typ { |
| 1740 | case sshFxpStatus: |
| 1741 | err = normaliseError(unmarshalStatus(work.id, s.data)) |
| 1742 | default: |
| 1743 | err = unimplementedPacketErr(s.typ) |
| 1744 | } |
| 1745 | } |
| 1746 | |
| 1747 | if err != nil { |
| 1748 | errCh <- wErr{work.off, err} |
| 1749 | } |
| 1750 | } |
| 1751 | }() |
| 1752 | } |
| 1753 | |
| 1754 | |
| 1755 | go func() { |
| 1756 | wg.Wait() |
| 1757 | close(errCh) |
| 1758 | }() |
| 1759 | |
| 1760 | |
| 1761 | firstErr := wErr{math.MaxInt64, nil} |
| 1762 | for wErr := range errCh { |
| 1763 | if wErr.off <= firstErr.off { |
| 1764 | firstErr = wErr |
| 1765 | } |
| 1766 | |
| 1767 | select { |
| 1768 | case <-cancel: |
| 1769 | default: |
| 1770 | |
| 1771 | close(cancel) |
| 1772 | } |
| 1773 | } |
| 1774 | |
| 1775 | if firstErr.err != nil { |
| 1776 | |
| 1777 | return int(firstErr.off - off), firstErr.err |
| 1778 | } |
| 1779 | |
| 1780 | return len(b), nil |
| 1781 | } |
| 1782 | |
| 1783 | |
| 1784 | |
| 1785 | |
| 1786 | func (f *File) WriteAt(b []byte, off int64) (written int, err error) { |
| 1787 | f.mu.RLock() |
| 1788 | defer f.mu.RUnlock() |
| 1789 | |
| 1790 | if f.handle == "" { |
| 1791 | return 0, os.ErrClosed |
| 1792 | } |
| 1793 | |
| 1794 | return f.writeAt(b, off) |
| 1795 | } |
| 1796 | |
| 1797 | |
| 1798 | |
| 1799 | func (f *File) writeAt(b []byte, off int64) (written int, err error) { |
| 1800 | if len(b) <= f.c.maxPacket { |
| 1801 | |
| 1802 | return f.writeChunkAt(nil, b, off) |
| 1803 | } |
| 1804 | |
| 1805 | if f.c.useConcurrentWrites { |
| 1806 | return f.writeAtConcurrent(b, off) |
| 1807 | } |
| 1808 | |
| 1809 | ch := make(chan result, 1) |
| 1810 | |
| 1811 | chunkSize := f.c.maxPacket |
| 1812 | |
| 1813 | for written < len(b) { |
| 1814 | wb := b[written:] |
| 1815 | if len(wb) > chunkSize { |
| 1816 | wb = wb[:chunkSize] |
| 1817 | } |
| 1818 | |
| 1819 | n, err := f.writeChunkAt(ch, wb, off+int64(written)) |
| 1820 | if n > 0 { |
| 1821 | written += n |
| 1822 | } |
| 1823 | |
| 1824 | if err != nil { |
| 1825 | return written, err |
| 1826 | } |
| 1827 | } |
| 1828 | |
| 1829 | return len(b), nil |
| 1830 | } |
| 1831 | |
| 1832 | |
| 1833 | |
| 1834 | |
| 1835 | |
| 1836 | |
| 1837 | |
| 1838 | |
| 1839 | |
| 1840 | |
| 1841 | func (f *File) ReadFromWithConcurrency(r io.Reader, concurrency int) (read int64, err error) { |
| 1842 | f.mu.Lock() |
| 1843 | defer f.mu.Unlock() |
| 1844 | |
| 1845 | return f.readFromWithConcurrency(r, concurrency) |
| 1846 | } |
| 1847 | |
| 1848 | func (f *File) readFromWithConcurrency(r io.Reader, concurrency int) (read int64, err error) { |
| 1849 | if f.handle == "" { |
| 1850 | return 0, os.ErrClosed |
| 1851 | } |
| 1852 | |
| 1853 | |
| 1854 | |
| 1855 | |
| 1856 | |
| 1857 | cancel := make(chan struct{}) |
| 1858 | |
| 1859 | type work struct { |
| 1860 | id uint32 |
| 1861 | res chan result |
| 1862 | |
| 1863 | off int64 |
| 1864 | } |
| 1865 | workCh := make(chan work) |
| 1866 | |
| 1867 | type rwErr struct { |
| 1868 | off int64 |
| 1869 | err error |
| 1870 | } |
| 1871 | errCh := make(chan rwErr) |
| 1872 | |
| 1873 | if concurrency > f.c.maxConcurrentRequests || concurrency < 1 { |
| 1874 | concurrency = f.c.maxConcurrentRequests |
| 1875 | } |
| 1876 | |
| 1877 | pool := newResChanPool(concurrency) |
| 1878 | |
| 1879 | |
| 1880 | go func() { |
| 1881 | defer close(workCh) |
| 1882 | |
| 1883 | b := make([]byte, f.c.maxPacket) |
| 1884 | off := f.offset |
| 1885 | |
| 1886 | for { |
| 1887 | |
| 1888 | n, err := io.ReadFull(r, b) |
| 1889 | |
| 1890 | if n > 0 { |
| 1891 | read += int64(n) |
| 1892 | |
| 1893 | id := f.c.nextID() |
| 1894 | res := pool.Get() |
| 1895 | |
| 1896 | f.c.dispatchRequest(res, &sshFxpWritePacket{ |
| 1897 | ID: id, |
| 1898 | Handle: f.handle, |
| 1899 | Offset: uint64(off), |
| 1900 | Length: uint32(n), |
| 1901 | Data: b[:n], |
| 1902 | }) |
| 1903 | |
| 1904 | select { |
| 1905 | case workCh <- work{id, res, off}: |
| 1906 | case <-cancel: |
| 1907 | return |
| 1908 | } |
| 1909 | |
| 1910 | off += int64(n) |
| 1911 | } |
| 1912 | |
| 1913 | if err != nil { |
| 1914 | if !errors.Is(err, io.EOF) && !errors.Is(err, io.ErrUnexpectedEOF) { |
| 1915 | errCh <- rwErr{off, err} |
| 1916 | } |
| 1917 | return |
| 1918 | } |
| 1919 | } |
| 1920 | }() |
| 1921 | |
| 1922 | var wg sync.WaitGroup |
| 1923 | wg.Add(concurrency) |
| 1924 | for i := 0; i < concurrency; i++ { |
| 1925 | |
| 1926 | go func() { |
| 1927 | defer wg.Done() |
| 1928 | |
| 1929 | for work := range workCh { |
| 1930 | s := <-work.res |
| 1931 | pool.Put(work.res) |
| 1932 | |
| 1933 | err := s.err |
| 1934 | if err == nil { |
| 1935 | switch s.typ { |
| 1936 | case sshFxpStatus: |
| 1937 | err = normaliseError(unmarshalStatus(work.id, s.data)) |
| 1938 | default: |
| 1939 | err = unimplementedPacketErr(s.typ) |
| 1940 | } |
| 1941 | } |
| 1942 | |
| 1943 | if err != nil { |
| 1944 | errCh <- rwErr{work.off, err} |
| 1945 | |
| 1946 | |
| 1947 | |
| 1948 | } |
| 1949 | } |
| 1950 | }() |
| 1951 | } |
| 1952 | |
| 1953 | |
| 1954 | go func() { |
| 1955 | wg.Wait() |
| 1956 | close(errCh) |
| 1957 | }() |
| 1958 | |
| 1959 | |
| 1960 | firstErr := rwErr{math.MaxInt64, nil} |
| 1961 | for rwErr := range errCh { |
| 1962 | if rwErr.off <= firstErr.off { |
| 1963 | firstErr = rwErr |
| 1964 | } |
| 1965 | |
| 1966 | select { |
| 1967 | case <-cancel: |
| 1968 | default: |
| 1969 | |
| 1970 | close(cancel) |
| 1971 | } |
| 1972 | } |
| 1973 | |
| 1974 | if firstErr.err != nil { |
| 1975 | |
| 1976 | |
| 1977 | |
| 1978 | |
| 1979 | |
| 1980 | |
| 1981 | |
| 1982 | |
| 1983 | |
| 1984 | |
| 1985 | f.offset = firstErr.off |
| 1986 | |
| 1987 | |
| 1988 | return read, firstErr.err |
| 1989 | } |
| 1990 | |
| 1991 | f.offset += read |
| 1992 | return read, nil |
| 1993 | } |
| 1994 | |
| 1995 | |
| 1996 | |
| 1997 | |
| 1998 | |
| 1999 | |
| 2000 | |
| 2001 | |
| 2002 | |
| 2003 | |
| 2004 | |
| 2005 | |
| 2006 | |
| 2007 | |
| 2008 | |
| 2009 | |
| 2010 | |
| 2011 | |
| 2012 | |
| 2013 | |
| 2014 | func (f *File) ReadFrom(r io.Reader) (int64, error) { |
| 2015 | f.mu.Lock() |
| 2016 | defer f.mu.Unlock() |
| 2017 | |
| 2018 | if f.handle == "" { |
| 2019 | return 0, os.ErrClosed |
| 2020 | } |
| 2021 | |
| 2022 | if f.c.useConcurrentWrites { |
| 2023 | var remain int64 |
| 2024 | switch r := r.(type) { |
| 2025 | case interface{ Len() int }: |
| 2026 | remain = int64(r.Len()) |
| 2027 | |
| 2028 | case interface{ Size() int64 }: |
| 2029 | remain = r.Size() |
| 2030 | |
| 2031 | case *io.LimitedReader: |
| 2032 | remain = r.N |
| 2033 | |
| 2034 | case interface{ Stat() (os.FileInfo, error) }: |
| 2035 | info, err := r.Stat() |
| 2036 | if err == nil { |
| 2037 | remain = info.Size() |
| 2038 | } |
| 2039 | } |
| 2040 | |
| 2041 | if remain < 0 { |
| 2042 | |
| 2043 | return f.readFromWithConcurrency(r, f.c.maxConcurrentRequests) |
| 2044 | } |
| 2045 | |
| 2046 | if remain > int64(f.c.maxPacket) { |
| 2047 | |
| 2048 | |
| 2049 | |
| 2050 | concurrency64 := remain/int64(f.c.maxPacket) + 1 |
| 2051 | |
| 2052 | |
| 2053 | |
| 2054 | if concurrency64 > int64(f.c.maxConcurrentRequests) { |
| 2055 | concurrency64 = int64(f.c.maxConcurrentRequests) |
| 2056 | } |
| 2057 | |
| 2058 | return f.readFromWithConcurrency(r, int(concurrency64)) |
| 2059 | } |
| 2060 | } |
| 2061 | |
| 2062 | ch := make(chan result, 1) |
| 2063 | |
| 2064 | b := make([]byte, f.c.maxPacket) |
| 2065 | |
| 2066 | var read int64 |
| 2067 | for { |
| 2068 | |
| 2069 | n, err := io.ReadFull(r, b) |
| 2070 | if n < 0 { |
| 2071 | panic("sftp.File: reader returned negative count from Read") |
| 2072 | } |
| 2073 | |
| 2074 | if n > 0 { |
| 2075 | read += int64(n) |
| 2076 | |
| 2077 | m, err2 := f.writeChunkAt(ch, b[:n], f.offset) |
| 2078 | f.offset += int64(m) |
| 2079 | |
| 2080 | if err == nil { |
| 2081 | err = err2 |
| 2082 | } |
| 2083 | } |
| 2084 | |
| 2085 | if err != nil { |
| 2086 | if errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) { |
| 2087 | return read, nil |
| 2088 | } |
| 2089 | |
| 2090 | return read, err |
| 2091 | } |
| 2092 | } |
| 2093 | } |
| 2094 | |
| 2095 | |
| 2096 | |
| 2097 | |
| 2098 | func (f *File) Seek(offset int64, whence int) (int64, error) { |
| 2099 | f.mu.Lock() |
| 2100 | defer f.mu.Unlock() |
| 2101 | |
| 2102 | if f.handle == "" { |
| 2103 | return 0, os.ErrClosed |
| 2104 | } |
| 2105 | |
| 2106 | switch whence { |
| 2107 | case io.SeekStart: |
| 2108 | case io.SeekCurrent: |
| 2109 | offset += f.offset |
| 2110 | case io.SeekEnd: |
| 2111 | fi, err := f.stat() |
| 2112 | if err != nil { |
| 2113 | return f.offset, err |
| 2114 | } |
| 2115 | offset += fi.Size() |
| 2116 | default: |
| 2117 | return f.offset, unimplementedSeekWhence(whence) |
| 2118 | } |
| 2119 | |
| 2120 | if offset < 0 { |
| 2121 | return f.offset, os.ErrInvalid |
| 2122 | } |
| 2123 | |
| 2124 | f.offset = offset |
| 2125 | return f.offset, nil |
| 2126 | } |
| 2127 | |
| 2128 | |
| 2129 | func (f *File) Chown(uid, gid int) error { |
| 2130 | f.mu.RLock() |
| 2131 | defer f.mu.RUnlock() |
| 2132 | |
| 2133 | if f.handle == "" { |
| 2134 | return os.ErrClosed |
| 2135 | } |
| 2136 | |
| 2137 | return f.c.fsetstat(f.handle, sshFileXferAttrUIDGID, &FileStat{ |
| 2138 | UID: uint32(uid), |
| 2139 | GID: uint32(gid), |
| 2140 | }) |
| 2141 | } |
| 2142 | |
| 2143 | |
| 2144 | |
| 2145 | |
| 2146 | func (f *File) Chmod(mode os.FileMode) error { |
| 2147 | f.mu.RLock() |
| 2148 | defer f.mu.RUnlock() |
| 2149 | |
| 2150 | if f.handle == "" { |
| 2151 | return os.ErrClosed |
| 2152 | } |
| 2153 | |
| 2154 | return f.c.fsetstat(f.handle, sshFileXferAttrPermissions, toChmodPerm(mode)) |
| 2155 | } |
| 2156 | |
| 2157 | |
| 2158 | |
| 2159 | |
| 2160 | |
| 2161 | |
| 2162 | |
| 2163 | |
| 2164 | func (f *File) SetExtendedData(path string, extended []StatExtended) error { |
| 2165 | f.mu.RLock() |
| 2166 | defer f.mu.RUnlock() |
| 2167 | |
| 2168 | if f.handle == "" { |
| 2169 | return os.ErrClosed |
| 2170 | } |
| 2171 | |
| 2172 | attrs := &FileStat{ |
| 2173 | Extended: extended, |
| 2174 | } |
| 2175 | |
| 2176 | return f.c.fsetstat(f.handle, sshFileXferAttrExtended, attrs) |
| 2177 | } |
| 2178 | |
| 2179 | |
| 2180 | |
| 2181 | |
| 2182 | |
| 2183 | |
| 2184 | func (f *File) Truncate(size int64) error { |
| 2185 | f.mu.RLock() |
| 2186 | defer f.mu.RUnlock() |
| 2187 | |
| 2188 | if f.handle == "" { |
| 2189 | return os.ErrClosed |
| 2190 | } |
| 2191 | |
| 2192 | return f.c.fsetstat(f.handle, sshFileXferAttrSize, uint64(size)) |
| 2193 | } |
| 2194 | |
| 2195 | |
| 2196 | |
| 2197 | |
| 2198 | func (f *File) Sync() error { |
| 2199 | f.mu.Lock() |
| 2200 | defer f.mu.Unlock() |
| 2201 | |
| 2202 | if f.handle == "" { |
| 2203 | return os.ErrClosed |
| 2204 | } |
| 2205 | |
| 2206 | if data, ok := f.c.HasExtension(openssh.ExtensionFSync().Name); !ok || data != "1" { |
| 2207 | return &StatusError{ |
| 2208 | Code: sshFxOPUnsupported, |
| 2209 | msg: "fsync not supported", |
| 2210 | } |
| 2211 | } |
| 2212 | |
| 2213 | id := f.c.nextID() |
| 2214 | typ, data, err := f.c.sendPacket(context.Background(), nil, &sshFxpFsyncPacket{ |
| 2215 | ID: id, |
| 2216 | Handle: f.handle, |
| 2217 | }) |
| 2218 | |
| 2219 | switch { |
| 2220 | case err != nil: |
| 2221 | return err |
| 2222 | case typ == sshFxpStatus: |
| 2223 | return normaliseError(unmarshalStatus(id, data)) |
| 2224 | default: |
| 2225 | return &unexpectedPacketErr{want: sshFxpStatus, got: typ} |
| 2226 | } |
| 2227 | } |
| 2228 | |
| 2229 | |
| 2230 | |
| 2231 | func normaliseError(err error) error { |
| 2232 | switch err := err.(type) { |
| 2233 | case *StatusError: |
| 2234 | switch err.Code { |
| 2235 | case sshFxEOF: |
| 2236 | return io.EOF |
| 2237 | case sshFxNoSuchFile: |
| 2238 | return os.ErrNotExist |
| 2239 | case sshFxPermissionDenied: |
| 2240 | return os.ErrPermission |
| 2241 | case sshFxOk: |
| 2242 | return nil |
| 2243 | default: |
| 2244 | return err |
| 2245 | } |
| 2246 | default: |
| 2247 | return err |
| 2248 | } |
| 2249 | } |
| 2250 | |
| 2251 | |
| 2252 | |
| 2253 | func toPflags(f int) uint32 { |
| 2254 | var out uint32 |
| 2255 | switch f & (os.O_RDONLY | os.O_WRONLY | os.O_RDWR) { |
| 2256 | case os.O_RDONLY: |
| 2257 | out |= sshFxfRead |
| 2258 | case os.O_WRONLY: |
| 2259 | out |= sshFxfWrite |
| 2260 | case os.O_RDWR: |
| 2261 | out |= sshFxfRead | sshFxfWrite |
| 2262 | } |
| 2263 | if f&os.O_APPEND == os.O_APPEND { |
| 2264 | out |= sshFxfAppend |
| 2265 | } |
| 2266 | if f&os.O_CREATE == os.O_CREATE { |
| 2267 | out |= sshFxfCreat |
| 2268 | } |
| 2269 | if f&os.O_TRUNC == os.O_TRUNC { |
| 2270 | out |= sshFxfTrunc |
| 2271 | } |
| 2272 | if f&os.O_EXCL == os.O_EXCL { |
| 2273 | out |= sshFxfExcl |
| 2274 | } |
| 2275 | return out |
| 2276 | } |
| 2277 | |
| 2278 | |
| 2279 | |
| 2280 | |
| 2281 | |
| 2282 | |
| 2283 | func toChmodPerm(m os.FileMode) (perm uint32) { |
| 2284 | const mask = os.ModePerm | os.FileMode(s_ISUID|s_ISGID|s_ISVTX) |
| 2285 | perm = uint32(m & mask) |
| 2286 | |
| 2287 | if m&os.ModeSetuid != 0 { |
| 2288 | perm |= s_ISUID |
| 2289 | } |
| 2290 | if m&os.ModeSetgid != 0 { |
| 2291 | perm |= s_ISGID |
| 2292 | } |
| 2293 | if m&os.ModeSticky != 0 { |
| 2294 | perm |= s_ISVTX |
| 2295 | } |
| 2296 | |
| 2297 | return perm |
| 2298 | } |