You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
3.1 KiB

  1. // Copyright (c) 2015-2021 MinIO, Inc.
  2. //
  3. // This file is part of MinIO Object Storage stack
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. package cmd
  18. import (
  19. "context"
  20. "encoding/base64"
  21. "fmt"
  22. "io"
  23. "strings"
  24. "github.com/klauspost/reedsolomon"
  25. )
  26. // getDataBlockLen - get length of data blocks from encoded blocks.
  27. func getDataBlockLen(enBlocks [][]byte, dataBlocks int) int {
  28. size := 0
  29. // Figure out the data block length.
  30. for _, block := range enBlocks[:dataBlocks] {
  31. size += len(block)
  32. }
  33. return size
  34. }
  35. // Writes all the data blocks from encoded blocks until requested
  36. // outSize length. Provides a way to skip bytes until the offset.
  37. func writeDataBlocks(ctx context.Context, dst io.Writer, enBlocks [][]byte, dataBlocks int, offset int64, length int64) (int64, error) {
  38. // Offset and out size cannot be negative.
  39. if offset < 0 || length < 0 {
  40. return 0, errUnexpected
  41. }
  42. // Do we have enough blocks?
  43. if len(enBlocks) < dataBlocks {
  44. return 0, reedsolomon.ErrTooFewShards
  45. }
  46. // Do we have enough data?
  47. if int64(getDataBlockLen(enBlocks, dataBlocks)) < length {
  48. return 0, reedsolomon.ErrShortData
  49. }
  50. // Counter to decrement total left to write.
  51. write := length
  52. // Counter to increment total written.
  53. var totalWritten int64
  54. // Write all data blocks to dst.
  55. for _, block := range enBlocks[:dataBlocks] {
  56. // Skip blocks until we have reached our offset.
  57. if offset >= int64(len(block)) {
  58. // Decrement offset.
  59. offset -= int64(len(block))
  60. continue
  61. }
  62. // Skip until offset.
  63. block = block[offset:]
  64. // Reset the offset for next iteration to read everything
  65. // from subsequent blocks.
  66. offset = 0
  67. // We have written all the blocks, write the last remaining block.
  68. if write < int64(len(block)) {
  69. n, err := dst.Write(block[:write])
  70. if err != nil {
  71. return 0, err
  72. }
  73. totalWritten += int64(n)
  74. break
  75. }
  76. // Copy the block.
  77. n, err := dst.Write(block)
  78. if err != nil {
  79. return 0, err
  80. }
  81. // Decrement output size.
  82. write -= int64(n)
  83. // Increment written.
  84. totalWritten += int64(n)
  85. }
  86. // Success.
  87. return totalWritten, nil
  88. }
  89. // returns deploymentID from uploadID
  90. func getDeplIDFromUpload(uploadID string) (string, error) {
  91. uploadBytes, err := base64.RawURLEncoding.DecodeString(uploadID)
  92. if err != nil {
  93. return "", fmt.Errorf("error parsing uploadID %s (%w)", uploadID, err)
  94. }
  95. slc := strings.SplitN(string(uploadBytes), ".", 2)
  96. if len(slc) != 2 {
  97. return "", fmt.Errorf("uploadID %s has incorrect format", uploadID)
  98. }
  99. return slc[0], nil
  100. }