Replace ReadFull with Read and some cleanup

This commit is contained in:
Andrey Kostov 2014-12-20 10:32:48 +02:00
parent 576495ec3e
commit 1ffb5db12b

View file

@ -227,13 +227,16 @@ func (d *Driver) WriteStream(path string, offset int64, reader io.Reader) (total
bytesRead = 0 bytesRead = 0
for int64(bytesRead) < total { for int64(bytesRead) < total {
//The loop should very rarely enter a second iteration //The loop should very rarely enter a second iteration
nn, err := io.ReadFull(current, buf[bytesRead:total]) nn, err := current.Read(buf[bytesRead:total])
bytesRead += nn bytesRead += nn
if err != nil { if err != nil {
// If you can't read the total contents, this means you lack access to the current file if err != io.EOF {
return err return err
} }
break
}
} }
return nil return nil
} }
@ -241,13 +244,13 @@ func (d *Driver) WriteStream(path string, offset int64, reader io.Reader) (total
// Fills from parameter to chunkSize from reader // Fills from parameter to chunkSize from reader
fromReader := func(from int64) error { fromReader := func(from int64) error {
bytesRead = 0 bytesRead = 0
for int64(bytesRead) < chunkSize { for from+int64(bytesRead) < chunkSize {
nn, err := io.ReadFull(reader, buf[from+int64(bytesRead):]) nn, err := reader.Read(buf[from+int64(bytesRead):])
totalRead += int64(nn) totalRead += int64(nn)
bytesRead += nn bytesRead += nn
if err != nil { if err != nil {
if err != io.ErrUnexpectedEOF && err != io.EOF { if err != io.EOF {
return err return err
} }
@ -255,11 +258,16 @@ func (d *Driver) WriteStream(path string, offset int64, reader io.Reader) (total
} }
} }
if bytesRead > 0 {
part, err = multi.PutPart(int(partNumber), bytes.NewReader(buf[0:int64(bytesRead)+from])) part, err = multi.PutPart(int(partNumber), bytes.NewReader(buf[0:int64(bytesRead)+from]))
if err != nil { if err != nil {
return err return err
} }
parts = append(parts, part)
partNumber++
}
return nil return nil
} }
@ -286,6 +294,10 @@ func (d *Driver) WriteStream(path string, offset int64, reader io.Reader) (total
if err = fromReader(offset); err != nil { if err = fromReader(offset); err != nil {
return totalRead, err return totalRead, err
} }
if totalRead+offset < chunkSize {
return totalRead, nil
}
} else { } else {
// currentLength >= offset >= chunkSize // currentLength >= offset >= chunkSize
_, part, err = multi.PutPartCopy(partNumber, _, part, err = multi.PutPartCopy(partNumber,
@ -294,20 +306,16 @@ func (d *Driver) WriteStream(path string, offset int64, reader io.Reader) (total
if err != nil { if err != nil {
return 0, err return 0, err
} }
}
parts = append(parts, part) parts = append(parts, part)
partNumber++ partNumber++
if totalRead+offset < chunkSize {
return totalRead, nil
} }
} else { } else {
// Fills between parameters with 0s but only when to - from <= chunkSize // Fills between parameters with 0s but only when to - from <= chunkSize
fromZeroFillSmall := func(from, to int64) error { fromZeroFillSmall := func(from, to int64) error {
bytesRead = 0 bytesRead = 0
for from+int64(bytesRead) < to { for from+int64(bytesRead) < to {
nn, err := io.ReadFull(bytes.NewReader(zeroBuf), buf[from+int64(bytesRead):to]) nn, err := bytes.NewReader(zeroBuf).Read(buf[from+int64(bytesRead) : to])
bytesRead += nn bytesRead += nn
if err != nil { if err != nil {
return err return err
@ -331,16 +339,7 @@ func (d *Driver) WriteStream(path string, offset int64, reader io.Reader) (total
partNumber++ partNumber++
} }
bytesRead = 0 return fromZeroFillSmall(0, (to-from)%chunkSize)
for from+bytesRead64+int64(bytesRead) < to {
nn, err := io.ReadFull(bytes.NewReader(zeroBuf), buf[0+bytesRead:(to-from)%chunkSize])
bytesRead64 += int64(nn)
if err != nil {
return err
}
}
return nil
} }
// currentLength < offset // currentLength < offset
@ -430,14 +429,6 @@ func (d *Driver) WriteStream(path string, offset int64, reader io.Reader) (total
return totalRead, err return totalRead, err
} }
part, err = multi.PutPart(int(partNumber), bytes.NewReader(buf))
if err != nil {
return totalRead, err
}
parts = append(parts, part)
partNumber++
if totalRead+((offset-currentLength)%chunkSize) < chunkSize { if totalRead+((offset-currentLength)%chunkSize) < chunkSize {
return totalRead, nil return totalRead, nil
} }
@ -447,21 +438,10 @@ func (d *Driver) WriteStream(path string, offset int64, reader io.Reader) (total
} }
for { for {
bytesRead, err := io.ReadFull(reader, buf) if err = fromReader(0); err != nil {
totalRead += int64(bytesRead)
if err != nil && err != io.ErrUnexpectedEOF && err != io.EOF {
return totalRead, err return totalRead, err
} }
part, err := multi.PutPart(int(partNumber), bytes.NewReader(buf[0:bytesRead]))
if err != nil {
return totalRead, err
}
parts = append(parts, part)
partNumber++
if int64(bytesRead) < chunkSize { if int64(bytesRead) < chunkSize {
break break
} }