mirror of https://github.com/pkg/sftp.git
				
				
				
			Fixed Read, removed ReadAt
ReadAt should be replaced by Seek when the fs interface provides it.
This commit is contained in:
		
							parent
							
								
									fd27263026
								
							
						
					
					
						commit
						b8aa2fe3d1
					
				
							
								
								
									
										21
									
								
								client.go
								
								
								
								
							
							
						
						
									
										21
									
								
								client.go
								
								
								
								
							| 
						 | 
					@ -493,18 +493,17 @@ func (f *File) Close() error {
 | 
				
			||||||
// bytes read and an error, if any. EOF is signaled by a zero count with
 | 
					// bytes read and an error, if any. EOF is signaled by a zero count with
 | 
				
			||||||
// err set to io.EOF.
 | 
					// err set to io.EOF.
 | 
				
			||||||
func (f *File) Read(b []byte) (int, error) {
 | 
					func (f *File) Read(b []byte) (int, error) {
 | 
				
			||||||
	n, err := f.c.readAt(f.handle, f.offset, b)
 | 
						var read int
 | 
				
			||||||
 | 
						for len(b) > 0 {
 | 
				
			||||||
 | 
							n, err := f.c.readAt(f.handle, f.offset, b[:min(len(b), maxWritePacket)])
 | 
				
			||||||
		f.offset += uint64(n)
 | 
							f.offset += uint64(n)
 | 
				
			||||||
	return int(n), err
 | 
							read += int(n)
 | 
				
			||||||
}
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return read, err
 | 
				
			||||||
// ReadAt reads len(b) bytes from the File starting at byte offset off. It
 | 
							}
 | 
				
			||||||
// returns the number of bytes read and the error, if any. ReadAt always
 | 
							b = b[n:]
 | 
				
			||||||
// returns a non-nil error when n < len(b). At end of file, that error is
 | 
						}
 | 
				
			||||||
// io.EOF.
 | 
						return read, nil
 | 
				
			||||||
func (f *File) ReadAt(b []byte, off int64) (int, error) {
 | 
					 | 
				
			||||||
	n, err := f.c.readAt(f.handle, uint64(off), b)
 | 
					 | 
				
			||||||
	return int(n), err
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Stat returns the FileInfo structure describing file. If there is an
 | 
					// Stat returns the FileInfo structure describing file. If there is an
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -146,41 +146,6 @@ var readAtTests = []struct {
 | 
				
			||||||
	{"Hello world!", 12, "", io.EOF},
 | 
						{"Hello world!", 12, "", io.EOF},
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestClientReadAt(t *testing.T) {
 | 
					 | 
				
			||||||
	sftp, cmd := testClient(t, READONLY)
 | 
					 | 
				
			||||||
	defer cmd.Wait()
 | 
					 | 
				
			||||||
	defer sftp.Close()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	for _, tt := range readAtTests {
 | 
					 | 
				
			||||||
		f, err := ioutil.TempFile("", "sftptest")
 | 
					 | 
				
			||||||
		if err != nil {
 | 
					 | 
				
			||||||
			t.Fatal(err)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		defer os.Remove(f.Name())
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		if _, err := f.WriteString(tt.s); err != nil {
 | 
					 | 
				
			||||||
			t.Fatal(err)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		if err := f.Close(); err != nil {
 | 
					 | 
				
			||||||
			t.Fatal(err)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		got, err := sftp.Open(f.Name())
 | 
					 | 
				
			||||||
		if err != nil {
 | 
					 | 
				
			||||||
			t.Fatal(err)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		defer got.Close()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		var b = make([]byte, 100)
 | 
					 | 
				
			||||||
		n, err := got.ReadAt(b, tt.at)
 | 
					 | 
				
			||||||
		b = b[:n]
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		if want, got := tt.want, string(b); got != want || tt.err != err {
 | 
					 | 
				
			||||||
			t.Fatalf("Read(): want %q %v, got %q %v", want, tt.err, got, err)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func TestClientCreate(t *testing.T) {
 | 
					func TestClientCreate(t *testing.T) {
 | 
				
			||||||
	sftp, cmd := testClient(t, READWRITE)
 | 
						sftp, cmd := testClient(t, READWRITE)
 | 
				
			||||||
	defer cmd.Wait()
 | 
						defer cmd.Wait()
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue