Commit e7f14b15 authored by Eric's avatar Eric

fix download many files bug

parent 69c04033
...@@ -105,7 +105,15 @@ func (fs *sftpHandler) Filewrite(r *sftp.Request) (io.WriterAt, error) { ...@@ -105,7 +105,15 @@ func (fs *sftpHandler) Filewrite(r *sftp.Request) (io.WriterAt, error) {
func (fs *sftpHandler) Fileread(r *sftp.Request) (io.ReaderAt, error) { func (fs *sftpHandler) Fileread(r *sftp.Request) (io.ReaderAt, error) {
logger.Debug("File read: ", r.Filepath) logger.Debug("File read: ", r.Filepath)
f, err := fs.Open(r.Filepath) f, err := fs.Open(r.Filepath)
return NewReaderAt(f), err if err != nil {
return nil, err
}
fi, err := f.Stat()
if err != nil {
_ = f.Close()
return nil, err
}
return NewReaderAt(f, fi), err
} }
func (fs *sftpHandler) Close() { func (fs *sftpHandler) Close() {
...@@ -130,33 +138,27 @@ func NewWriterAt(f *sftp.File) io.WriterAt { ...@@ -130,33 +138,27 @@ func NewWriterAt(f *sftp.File) io.WriterAt {
return &clientReadWritAt{f: f, mu: new(sync.RWMutex)} return &clientReadWritAt{f: f, mu: new(sync.RWMutex)}
} }
func NewReaderAt(f *sftp.File) io.ReaderAt { func NewReaderAt(f *sftp.File, fi os.FileInfo) io.ReaderAt {
return &clientReadWritAt{f: f, mu: new(sync.RWMutex)} return &clientReadWritAt{f: f, mu: new(sync.RWMutex), fi: fi}
} }
type clientReadWritAt struct { type clientReadWritAt struct {
f *sftp.File f *sftp.File
mu *sync.RWMutex mu *sync.RWMutex
closed bool fi os.FileInfo
firstErr error firstErr error
} }
func (c *clientReadWritAt) WriteAt(p []byte, off int64) (n int, err error) { func (c *clientReadWritAt) WriteAt(p []byte, off int64) (n int, err error) {
c.mu.Lock() c.mu.Lock()
defer c.mu.Unlock() defer c.mu.Unlock()
if c.closed { if c.firstErr != nil {
return 0, c.firstErr return 0, c.firstErr
} }
if _, err = c.f.Seek(off, 0); err != nil { _, _ = c.f.Seek(off, 0)
c.firstErr = err
c.closed = true
_ = c.f.Close()
return
}
nw, err := c.f.Write(p) nw, err := c.f.Write(p)
if err != nil { if err != nil {
c.firstErr = err c.firstErr = err
c.closed = true
_ = c.f.Close() _ = c.f.Close()
} }
return nw, err return nw, err
...@@ -165,19 +167,16 @@ func (c *clientReadWritAt) WriteAt(p []byte, off int64) (n int, err error) { ...@@ -165,19 +167,16 @@ func (c *clientReadWritAt) WriteAt(p []byte, off int64) (n int, err error) {
func (c *clientReadWritAt) ReadAt(p []byte, off int64) (n int, err error) { func (c *clientReadWritAt) ReadAt(p []byte, off int64) (n int, err error) {
c.mu.Lock() c.mu.Lock()
defer c.mu.Unlock() defer c.mu.Unlock()
if c.closed { if c.firstErr != nil {
return 0, c.firstErr return 0, c.firstErr
} }
if _, err = c.f.Seek(off, 0); err != nil { if off >= c.fi.Size() {
c.firstErr = err return 0, io.EOF
c.closed = true
_ = c.f.Close()
return
} }
_, _ = c.f.Seek(off, 0)
nr, err := c.f.Read(p) nr, err := c.f.Read(p)
if err != nil { if err != nil {
c.firstErr = err c.firstErr = err
c.closed = true
_ = c.f.Close() _ = c.f.Close()
} }
return nr, err return nr, err
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment