Add nil-receiver awareness to all Reader/Writer

This commit is contained in:
Alexander Neumann 2015-05-01 17:13:03 +02:00
parent dbc41bb805
commit 98dc811536
5 changed files with 20 additions and 3 deletions

View File

@ -115,6 +115,10 @@ func (l *blobReader) Read(p []byte) (int, error) {
} }
func (l *blobReader) Close() error { func (l *blobReader) Close() error {
if l == nil {
return nil
}
if !l.closed { if !l.closed {
err := l.f.Close() err := l.f.Close()
l.closed = true l.closed = true

View File

@ -554,6 +554,10 @@ func (r *SFTP) ID() string {
// Close closes the sftp connection and terminates the underlying command. // Close closes the sftp connection and terminates the underlying command.
func (s *SFTP) Close() error { func (s *SFTP) Close() error {
if s == nil {
return nil
}
s.c.Close() s.c.Close()
// TODO: add timeout after which the process is killed // TODO: add timeout after which the process is killed
return s.cmd.Wait() return s.cmd.Wait()

View File

@ -24,6 +24,10 @@ func NewHashAppendWriter(w io.Writer, h hash.Hash) *HashAppendWriter {
} }
func (h *HashAppendWriter) Close() error { func (h *HashAppendWriter) Close() error {
if h == nil {
return nil
}
if !h.closed { if !h.closed {
h.closed = true h.closed = true

View File

@ -30,7 +30,7 @@ func (d *decryptReader) free() {
} }
func (d *decryptReader) Close() error { func (d *decryptReader) Close() error {
if d.buf == nil { if d == nil || d.buf == nil {
return nil return nil
} }
@ -72,14 +72,15 @@ func DecryptFrom(ks *Key, rd io.Reader) (io.ReadCloser, error) {
buf := bytes.NewBuffer(getBuffer()[:0]) buf := bytes.NewBuffer(getBuffer()[:0])
_, err := buf.ReadFrom(rd) _, err := buf.ReadFrom(rd)
if err != nil { if err != nil {
return nil, err return (*decryptReader)(nil), err
} }
ciphertext := buf.Bytes() ciphertext := buf.Bytes()
ciphertext, err = Decrypt(ks, ciphertext, ciphertext) ciphertext, err = Decrypt(ks, ciphertext, ciphertext)
if err != nil { if err != nil {
return nil, err freeBuffer(ciphertext)
return (*decryptReader)(nil), err
} }
return &decryptReader{buf: ciphertext, rd: bytes.NewReader(ciphertext)}, nil return &decryptReader{buf: ciphertext, rd: bytes.NewReader(ciphertext)}, nil

View File

@ -17,6 +17,10 @@ type encryptWriter struct {
} }
func (e *encryptWriter) Close() error { func (e *encryptWriter) Close() error {
if e == nil {
return nil
}
if e.closed { if e.closed {
return errors.New("Close() called on already closed writer") return errors.New("Close() called on already closed writer")
} }