2021-04-18 15:41:13 -04: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 11:32:47 -04:00
|
|
|
|
2016-08-18 19:23:42 -04:00
|
|
|
package cmd
|
2016-04-20 11:32:47 -04:00
|
|
|
|
2016-05-31 23:23:31 -04:00
|
|
|
import (
|
2018-04-05 18:04:40 -04:00
|
|
|
"context"
|
2022-10-25 13:52:29 -04:00
|
|
|
"encoding/base64"
|
2021-03-15 23:03:13 -04:00
|
|
|
"fmt"
|
2016-05-31 23:23:31 -04:00
|
|
|
"io"
|
2022-10-25 13:52:29 -04:00
|
|
|
"strings"
|
2016-05-31 23:23:31 -04:00
|
|
|
|
|
|
|
"github.com/klauspost/reedsolomon"
|
|
|
|
)
|
|
|
|
|
2016-06-22 15:55:23 -04:00
|
|
|
// getDataBlockLen - get length of data blocks from encoded blocks.
|
|
|
|
func getDataBlockLen(enBlocks [][]byte, dataBlocks int) int {
|
2016-05-29 18:38:14 -04:00
|
|
|
size := 0
|
2016-06-22 15:55:23 -04:00
|
|
|
// Figure out the data block length.
|
|
|
|
for _, block := range enBlocks[:dataBlocks] {
|
2016-05-29 18:38:14 -04:00
|
|
|
size += len(block)
|
|
|
|
}
|
2016-06-22 15:55:23 -04: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-05 18:04:40 -04:00
|
|
|
func writeDataBlocks(ctx context.Context, dst io.Writer, enBlocks [][]byte, dataBlocks int, offset int64, length int64) (int64, error) {
|
2016-07-07 04:30:34 -04:00
|
|
|
// Offset and out size cannot be negative.
|
2016-07-20 04:30:30 -04:00
|
|
|
if offset < 0 || length < 0 {
|
2018-04-05 18:04:40 -04:00
|
|
|
return 0, errUnexpected
|
2016-07-07 04:30:34 -04:00
|
|
|
}
|
|
|
|
|
2016-06-22 15:55:23 -04:00
|
|
|
// Do we have enough blocks?
|
|
|
|
if len(enBlocks) < dataBlocks {
|
2018-04-05 18:04:40 -04:00
|
|
|
return 0, reedsolomon.ErrTooFewShards
|
2016-05-29 18:38:14 -04:00
|
|
|
}
|
2016-05-31 23:23:31 -04:00
|
|
|
|
2016-06-22 15:55:23 -04:00
|
|
|
// Do we have enough data?
|
2016-07-20 04:30:30 -04:00
|
|
|
if int64(getDataBlockLen(enBlocks, dataBlocks)) < length {
|
2018-04-05 18:04:40 -04:00
|
|
|
return 0, reedsolomon.ErrShortData
|
2016-06-22 15:55:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Counter to decrement total left to write.
|
2016-07-20 04:30:30 -04:00
|
|
|
write := length
|
2016-06-22 15:55:23 -04:00
|
|
|
|
|
|
|
// Counter to increment total written.
|
2017-02-24 12:20:40 -05:00
|
|
|
var totalWritten int64
|
2016-06-22 15:55:23 -04:00
|
|
|
|
|
|
|
// Write all data blocks to dst.
|
|
|
|
for _, block := range enBlocks[:dataBlocks] {
|
|
|
|
// Skip blocks until we have reached our offset.
|
2016-07-20 04:30:30 -04:00
|
|
|
if offset >= int64(len(block)) {
|
2016-06-22 15:55:23 -04:00
|
|
|
// Decrement offset.
|
2016-07-20 04:30:30 -04:00
|
|
|
offset -= int64(len(block))
|
2016-06-22 15:55:23 -04:00
|
|
|
continue
|
|
|
|
}
|
2021-01-03 19:27:34 -05:00
|
|
|
|
2023-12-06 21:17:03 -05:00
|
|
|
// Skip until offset.
|
|
|
|
block = block[offset:]
|
|
|
|
|
|
|
|
// Reset the offset for next iteration to read everything
|
|
|
|
// from subsequent blocks.
|
|
|
|
offset = 0
|
|
|
|
|
2016-06-22 15:55:23 -04:00
|
|
|
// We have written all the blocks, write the last remaining block.
|
|
|
|
if write < int64(len(block)) {
|
2023-11-06 17:26:08 -05:00
|
|
|
n, err := dst.Write(block[:write])
|
2016-06-22 15:55:23 -04:00
|
|
|
if err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
return 0, err
|
2016-06-22 15:55:23 -04:00
|
|
|
}
|
2023-11-06 17:26:08 -05:00
|
|
|
totalWritten += int64(n)
|
2016-06-22 15:55:23 -04:00
|
|
|
break
|
|
|
|
}
|
2021-01-03 19:27:34 -05:00
|
|
|
|
2016-06-22 15:55:23 -04:00
|
|
|
// Copy the block.
|
2023-11-06 17:26:08 -05:00
|
|
|
n, err := dst.Write(block)
|
2016-06-22 15:55:23 -04:00
|
|
|
if err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
return 0, err
|
2016-05-29 18:38:14 -04:00
|
|
|
}
|
2016-06-22 15:55:23 -04:00
|
|
|
|
|
|
|
// Decrement output size.
|
2023-11-06 17:26:08 -05:00
|
|
|
write -= int64(n)
|
2016-06-22 15:55:23 -04:00
|
|
|
|
|
|
|
// Increment written.
|
2023-11-06 17:26:08 -05:00
|
|
|
totalWritten += int64(n)
|
2016-05-06 19:25:08 -04:00
|
|
|
}
|
2016-06-22 15:55:23 -04:00
|
|
|
|
|
|
|
// Success.
|
|
|
|
return totalWritten, nil
|
2016-05-06 19:25:08 -04:00
|
|
|
}
|
2022-10-25 13:52:29 -04:00
|
|
|
|
|
|
|
// returns deploymentID from uploadID
|
|
|
|
func getDeplIDFromUpload(uploadID string) (string, error) {
|
2022-11-10 19:17:45 -05:00
|
|
|
uploadBytes, err := base64.RawURLEncoding.DecodeString(uploadID)
|
2022-10-25 13:52:29 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("error parsing uploadID %s (%w)", uploadID, err)
|
|
|
|
}
|
|
|
|
slc := strings.SplitN(string(uploadBytes), ".", 2)
|
|
|
|
if len(slc) != 2 {
|
|
|
|
return "", fmt.Errorf("uploadID %s has incorrect format", uploadID)
|
|
|
|
}
|
|
|
|
return slc[0], nil
|
|
|
|
}
|