| 
									
										
										
										
											2021-04-19 03:41:13 +08:00
										 |  |  | // Copyright (c) 2015-2021 MinIO, Inc.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This file is part of MinIO Object Storage stack
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This program is free software: you can redistribute it and/or modify
 | 
					
						
							|  |  |  | // it under the terms of the GNU Affero General Public License as published by
 | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or
 | 
					
						
							|  |  |  | // (at your option) any later version.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This program is distributed in the hope that it will be useful
 | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
					
						
							|  |  |  | // GNU Affero General Public License for more details.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // You should have received a copy of the GNU Affero General Public License
 | 
					
						
							|  |  |  | // along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							| 
									
										
										
										
											2016-04-20 23:32:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 07:23:42 +08:00
										 |  |  | package cmd | 
					
						
							| 
									
										
										
										
											2016-04-20 23:32:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-01 11:23:31 +08:00
										 |  |  | import ( | 
					
						
							| 
									
										
										
										
											2021-01-05 10:51:52 +08:00
										 |  |  | 	"bytes" | 
					
						
							| 
									
										
										
										
											2018-04-06 06:04:40 +08:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2021-03-16 11:03:13 +08:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2016-06-01 11:23:31 +08:00
										 |  |  | 	"io" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/klauspost/reedsolomon" | 
					
						
							| 
									
										
										
										
											2021-11-02 23:11:50 +08:00
										 |  |  | 	xioutil "github.com/minio/minio/internal/ioutil" | 
					
						
							| 
									
										
										
										
											2021-06-02 05:59:40 +08:00
										 |  |  | 	"github.com/minio/minio/internal/logger" | 
					
						
							| 
									
										
										
										
											2016-06-01 11:23:31 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-23 03:55:23 +08:00
										 |  |  | // getDataBlockLen - get length of data blocks from encoded blocks.
 | 
					
						
							|  |  |  | func getDataBlockLen(enBlocks [][]byte, dataBlocks int) int { | 
					
						
							| 
									
										
										
										
											2016-05-30 06:38:14 +08:00
										 |  |  | 	size := 0 | 
					
						
							| 
									
										
										
										
											2016-06-23 03:55:23 +08:00
										 |  |  | 	// Figure out the data block length.
 | 
					
						
							|  |  |  | 	for _, block := range enBlocks[:dataBlocks] { | 
					
						
							| 
									
										
										
										
											2016-05-30 06:38:14 +08:00
										 |  |  | 		size += len(block) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-06-23 03:55:23 +08:00
										 |  |  | 	return size | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Writes all the data blocks from encoded blocks until requested
 | 
					
						
							|  |  |  | // outSize length. Provides a way to skip bytes until the offset.
 | 
					
						
							| 
									
										
										
										
											2018-04-06 06:04:40 +08:00
										 |  |  | func writeDataBlocks(ctx context.Context, dst io.Writer, enBlocks [][]byte, dataBlocks int, offset int64, length int64) (int64, error) { | 
					
						
							| 
									
										
										
										
											2016-07-07 16:30:34 +08:00
										 |  |  | 	// Offset and out size cannot be negative.
 | 
					
						
							| 
									
										
										
										
											2016-07-20 16:30:30 +08:00
										 |  |  | 	if offset < 0 || length < 0 { | 
					
						
							| 
									
										
										
										
											2018-04-06 06:04:40 +08:00
										 |  |  | 		logger.LogIf(ctx, errUnexpected) | 
					
						
							|  |  |  | 		return 0, errUnexpected | 
					
						
							| 
									
										
										
										
											2016-07-07 16:30:34 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-23 03:55:23 +08:00
										 |  |  | 	// Do we have enough blocks?
 | 
					
						
							|  |  |  | 	if len(enBlocks) < dataBlocks { | 
					
						
							| 
									
										
										
										
											2021-03-16 11:03:13 +08:00
										 |  |  | 		logger.LogIf(ctx, fmt.Errorf("diskBlocks(%d)/dataBlocks(%d) - %w", len(enBlocks), dataBlocks, reedsolomon.ErrTooFewShards)) | 
					
						
							| 
									
										
										
										
											2018-04-06 06:04:40 +08:00
										 |  |  | 		return 0, reedsolomon.ErrTooFewShards | 
					
						
							| 
									
										
										
										
											2016-05-30 06:38:14 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-06-01 11:23:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-23 03:55:23 +08:00
										 |  |  | 	// Do we have enough data?
 | 
					
						
							| 
									
										
										
										
											2016-07-20 16:30:30 +08:00
										 |  |  | 	if int64(getDataBlockLen(enBlocks, dataBlocks)) < length { | 
					
						
							| 
									
										
										
										
											2021-03-16 11:03:13 +08:00
										 |  |  | 		logger.LogIf(ctx, fmt.Errorf("getDataBlockLen(enBlocks, dataBlocks)(%d)/length(%d) - %w", getDataBlockLen(enBlocks, dataBlocks), length, reedsolomon.ErrShortData)) | 
					
						
							| 
									
										
										
										
											2018-04-06 06:04:40 +08:00
										 |  |  | 		return 0, reedsolomon.ErrShortData | 
					
						
							| 
									
										
										
										
											2016-06-23 03:55:23 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Counter to decrement total left to write.
 | 
					
						
							| 
									
										
										
										
											2016-07-20 16:30:30 +08:00
										 |  |  | 	write := length | 
					
						
							| 
									
										
										
										
											2016-06-23 03:55:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Counter to increment total written.
 | 
					
						
							| 
									
										
										
										
											2017-02-25 01:20:40 +08:00
										 |  |  | 	var totalWritten int64 | 
					
						
							| 
									
										
										
										
											2016-06-23 03:55:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Write all data blocks to dst.
 | 
					
						
							|  |  |  | 	for _, block := range enBlocks[:dataBlocks] { | 
					
						
							|  |  |  | 		// Skip blocks until we have reached our offset.
 | 
					
						
							| 
									
										
										
										
											2016-07-20 16:30:30 +08:00
										 |  |  | 		if offset >= int64(len(block)) { | 
					
						
							| 
									
										
										
										
											2016-06-23 03:55:23 +08:00
										 |  |  | 			// Decrement offset.
 | 
					
						
							| 
									
										
										
										
											2016-07-20 16:30:30 +08:00
										 |  |  | 			offset -= int64(len(block)) | 
					
						
							| 
									
										
										
										
											2016-06-23 03:55:23 +08:00
										 |  |  | 			continue | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			// Skip until offset.
 | 
					
						
							| 
									
										
										
										
											2016-07-20 16:30:30 +08:00
										 |  |  | 			block = block[offset:] | 
					
						
							| 
									
										
										
										
											2016-06-23 03:55:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			// Reset the offset for next iteration to read everything
 | 
					
						
							|  |  |  | 			// from subsequent blocks.
 | 
					
						
							| 
									
										
										
										
											2016-07-20 16:30:30 +08:00
										 |  |  | 			offset = 0 | 
					
						
							| 
									
										
										
										
											2016-06-23 03:55:23 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-01-04 08:27:34 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-23 03:55:23 +08:00
										 |  |  | 		// We have written all the blocks, write the last remaining block.
 | 
					
						
							|  |  |  | 		if write < int64(len(block)) { | 
					
						
							| 
									
										
										
										
											2021-11-02 23:11:50 +08:00
										 |  |  | 			n, err := xioutil.Copy(dst, bytes.NewReader(block[:write])) | 
					
						
							| 
									
										
										
										
											2016-06-23 03:55:23 +08:00
										 |  |  | 			if err != nil { | 
					
						
							| 
									
										
										
										
											2021-05-21 00:00:11 +08:00
										 |  |  | 				// The writer will be closed incase of range queries, which will emit ErrClosedPipe.
 | 
					
						
							|  |  |  | 				// The reader pipe might be closed at ListObjects io.EOF ignore it.
 | 
					
						
							|  |  |  | 				if err != io.ErrClosedPipe && err != io.EOF { | 
					
						
							| 
									
										
										
										
											2018-10-18 06:52:18 +08:00
										 |  |  | 					logger.LogIf(ctx, err) | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-04-06 06:04:40 +08:00
										 |  |  | 				return 0, err | 
					
						
							| 
									
										
										
										
											2016-06-23 03:55:23 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-01-05 10:51:52 +08:00
										 |  |  | 			totalWritten += n | 
					
						
							| 
									
										
										
										
											2016-06-23 03:55:23 +08:00
										 |  |  | 			break | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-01-04 08:27:34 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-23 03:55:23 +08:00
										 |  |  | 		// Copy the block.
 | 
					
						
							| 
									
										
										
										
											2021-11-02 23:11:50 +08:00
										 |  |  | 		n, err := xioutil.Copy(dst, bytes.NewReader(block)) | 
					
						
							| 
									
										
										
										
											2016-06-23 03:55:23 +08:00
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2018-09-28 11:36:17 +08:00
										 |  |  | 			// The writer will be closed incase of range queries, which will emit ErrClosedPipe.
 | 
					
						
							| 
									
										
										
										
											2021-05-21 00:00:11 +08:00
										 |  |  | 			// The reader pipe might be closed at ListObjects io.EOF ignore it.
 | 
					
						
							|  |  |  | 			if err != io.ErrClosedPipe && err != io.EOF { | 
					
						
							| 
									
										
										
										
											2018-09-26 03:39:46 +08:00
										 |  |  | 				logger.LogIf(ctx, err) | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-04-06 06:04:40 +08:00
										 |  |  | 			return 0, err | 
					
						
							| 
									
										
										
										
											2016-05-30 06:38:14 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-06-23 03:55:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// Decrement output size.
 | 
					
						
							| 
									
										
										
										
											2021-01-05 10:51:52 +08:00
										 |  |  | 		write -= n | 
					
						
							| 
									
										
										
										
											2016-06-23 03:55:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// Increment written.
 | 
					
						
							| 
									
										
										
										
											2021-01-05 10:51:52 +08:00
										 |  |  | 		totalWritten += n | 
					
						
							| 
									
										
										
										
											2016-05-07 07:25:08 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-06-23 03:55:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Success.
 | 
					
						
							|  |  |  | 	return totalWritten, nil | 
					
						
							| 
									
										
										
										
											2016-05-07 07:25:08 +08:00
										 |  |  | } |