2018-10-05 02:44:06 +02:00
|
|
|
/*
|
2020-06-13 05:04:01 +02:00
|
|
|
* MinIO Cloud Storage, (C) 2018-2020 MinIO, Inc.
|
2018-10-05 02:44:06 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-11-13 21:17:45 +01:00
|
|
|
"context"
|
2018-10-05 02:44:06 +02:00
|
|
|
"crypto/tls"
|
2019-09-26 08:08:24 +02:00
|
|
|
"encoding/gob"
|
|
|
|
"encoding/hex"
|
2020-06-17 23:49:26 +02:00
|
|
|
"errors"
|
2018-10-05 02:44:06 +02:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/url"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
2019-12-24 01:31:03 +01:00
|
|
|
"strings"
|
2020-06-17 23:49:26 +02:00
|
|
|
"time"
|
2019-01-30 19:53:57 +01:00
|
|
|
|
2019-02-06 21:07:03 +01:00
|
|
|
"github.com/minio/minio/cmd/http"
|
2020-06-17 23:49:26 +02:00
|
|
|
xhttp "github.com/minio/minio/cmd/http"
|
2019-11-13 21:17:45 +01:00
|
|
|
"github.com/minio/minio/cmd/logger"
|
2018-10-05 02:44:06 +02:00
|
|
|
"github.com/minio/minio/cmd/rest"
|
|
|
|
xnet "github.com/minio/minio/pkg/net"
|
|
|
|
)
|
|
|
|
|
2019-02-14 00:29:46 +01:00
|
|
|
func isNetworkError(err error) bool {
|
2018-10-05 02:44:06 +02:00
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
}
|
2019-07-29 23:48:18 +02:00
|
|
|
if nerr, ok := err.(*rest.NetworkError); ok {
|
2019-09-13 01:44:51 +02:00
|
|
|
return xnet.IsNetworkOrHostDown(nerr.Err)
|
2019-05-02 16:09:57 +02:00
|
|
|
}
|
2019-05-29 19:21:47 +02:00
|
|
|
return false
|
2019-05-02 16:09:57 +02:00
|
|
|
}
|
|
|
|
|
2020-01-15 03:45:17 +01:00
|
|
|
// Converts network error to storageErr. This function is
|
|
|
|
// written so that the storageAPI errors are consistent
|
|
|
|
// across network disks.
|
2018-10-05 02:44:06 +02:00
|
|
|
func toStorageErr(err error) error {
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-14 00:29:46 +01:00
|
|
|
if isNetworkError(err) {
|
2019-05-29 19:21:47 +02:00
|
|
|
return errDiskNotFound
|
2018-10-05 02:44:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch err.Error() {
|
2019-10-01 22:12:15 +02:00
|
|
|
case errFaultyDisk.Error():
|
|
|
|
return errFaultyDisk
|
|
|
|
case errFileCorrupt.Error():
|
|
|
|
return errFileCorrupt
|
2018-10-05 02:44:06 +02:00
|
|
|
case errUnexpected.Error():
|
|
|
|
return errUnexpected
|
|
|
|
case errDiskFull.Error():
|
|
|
|
return errDiskFull
|
|
|
|
case errVolumeNotFound.Error():
|
|
|
|
return errVolumeNotFound
|
|
|
|
case errVolumeExists.Error():
|
|
|
|
return errVolumeExists
|
|
|
|
case errFileNotFound.Error():
|
|
|
|
return errFileNotFound
|
|
|
|
case errFileNameTooLong.Error():
|
|
|
|
return errFileNameTooLong
|
|
|
|
case errFileAccessDenied.Error():
|
|
|
|
return errFileAccessDenied
|
|
|
|
case errIsNotRegular.Error():
|
|
|
|
return errIsNotRegular
|
|
|
|
case errVolumeNotEmpty.Error():
|
|
|
|
return errVolumeNotEmpty
|
|
|
|
case errVolumeAccessDenied.Error():
|
|
|
|
return errVolumeAccessDenied
|
|
|
|
case errCorruptedFormat.Error():
|
|
|
|
return errCorruptedFormat
|
|
|
|
case errUnformattedDisk.Error():
|
|
|
|
return errUnformattedDisk
|
|
|
|
case errInvalidAccessKeyID.Error():
|
|
|
|
return errInvalidAccessKeyID
|
|
|
|
case errAuthentication.Error():
|
|
|
|
return errAuthentication
|
|
|
|
case errRPCAPIVersionUnsupported.Error():
|
|
|
|
return errRPCAPIVersionUnsupported
|
|
|
|
case errServerTimeMismatch.Error():
|
|
|
|
return errServerTimeMismatch
|
2019-10-01 22:12:15 +02:00
|
|
|
case io.EOF.Error():
|
|
|
|
return io.EOF
|
|
|
|
case io.ErrUnexpectedEOF.Error():
|
|
|
|
return io.ErrUnexpectedEOF
|
2019-10-25 19:37:53 +02:00
|
|
|
case errDiskStale.Error():
|
|
|
|
return errDiskNotFound
|
2020-07-21 22:54:06 +02:00
|
|
|
case errDiskNotFound.Error():
|
|
|
|
return errDiskNotFound
|
2019-01-30 19:53:57 +01:00
|
|
|
}
|
2018-10-05 02:44:06 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Abstracts a remote disk.
|
|
|
|
type storageRESTClient struct {
|
|
|
|
endpoint Endpoint
|
|
|
|
restClient *rest.Client
|
2019-10-25 19:37:53 +02:00
|
|
|
diskID string
|
2018-10-05 02:44:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wrapper to restClient.Call to handle network errors, in case of network error the connection is makred disconnected
|
|
|
|
// permanently. The only way to restore the storage connection is at the xl-sets layer by xlsets.monitorAndConnectEndpoints()
|
|
|
|
// after verifying format.json
|
2020-09-04 18:45:06 +02:00
|
|
|
func (client *storageRESTClient) call(ctx context.Context, method string, values url.Values, body io.Reader, length int64) (io.ReadCloser, error) {
|
2019-10-09 08:11:15 +02:00
|
|
|
if !client.IsOnline() {
|
2018-10-05 02:44:06 +02:00
|
|
|
return nil, errDiskNotFound
|
|
|
|
}
|
2019-02-14 00:29:46 +01:00
|
|
|
if values == nil {
|
|
|
|
values = make(url.Values)
|
|
|
|
}
|
2019-10-25 19:37:53 +02:00
|
|
|
values.Set(storageRESTDiskID, client.diskID)
|
2020-09-04 18:45:06 +02:00
|
|
|
respBody, err := client.restClient.Call(ctx, method, values, body, length)
|
2018-10-05 02:44:06 +02:00
|
|
|
if err == nil {
|
|
|
|
return respBody, nil
|
|
|
|
}
|
2020-01-15 03:45:17 +01:00
|
|
|
|
|
|
|
err = toStorageErr(err)
|
2018-10-05 02:44:06 +02:00
|
|
|
|
2020-01-15 03:45:17 +01:00
|
|
|
return nil, err
|
2018-10-05 02:44:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stringer provides a canonicalized representation of network device.
|
|
|
|
func (client *storageRESTClient) String() string {
|
|
|
|
return client.endpoint.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsOnline - returns whether RPC client failed to connect or not.
|
|
|
|
func (client *storageRESTClient) IsOnline() bool {
|
2020-06-17 03:59:32 +02:00
|
|
|
return client.restClient.IsOnline()
|
2018-10-05 02:44:06 +02:00
|
|
|
}
|
|
|
|
|
2020-05-19 23:27:20 +02:00
|
|
|
func (client *storageRESTClient) IsLocal() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-01-13 22:09:10 +01:00
|
|
|
func (client *storageRESTClient) Hostname() string {
|
|
|
|
return client.endpoint.Host
|
|
|
|
}
|
|
|
|
|
2020-03-19 00:19:29 +01:00
|
|
|
func (client *storageRESTClient) CrawlAndGetDataUsage(ctx context.Context, cache dataUsageCache) (dataUsageCache, error) {
|
|
|
|
b := cache.serialize()
|
2020-09-04 18:45:06 +02:00
|
|
|
respBody, err := client.call(ctx, storageRESTMethodCrawlAndGetDataUsage, url.Values{}, bytes.NewBuffer(b), int64(len(b)))
|
2019-12-12 15:02:37 +01:00
|
|
|
defer http.DrainBody(respBody)
|
|
|
|
if err != nil {
|
2020-03-19 00:19:29 +01:00
|
|
|
return cache, err
|
2019-12-12 15:02:37 +01:00
|
|
|
}
|
2020-03-19 00:19:29 +01:00
|
|
|
reader, err := waitForHTTPResponse(respBody)
|
|
|
|
if err != nil {
|
|
|
|
return cache, err
|
|
|
|
}
|
|
|
|
var newCache dataUsageCache
|
2020-06-12 19:28:21 +02:00
|
|
|
return newCache, newCache.deserialize(reader)
|
2019-12-12 15:02:37 +01:00
|
|
|
}
|
|
|
|
|
2020-03-27 22:48:30 +01:00
|
|
|
func (client *storageRESTClient) GetDiskID() (string, error) {
|
2020-05-28 22:03:04 +02:00
|
|
|
// This call should never be over the network, this is always
|
|
|
|
// a cached value - caller should make sure to use this
|
|
|
|
// function on a fresh disk or make sure to look at the error
|
|
|
|
// from a different networked call to validate the GetDiskID()
|
|
|
|
return client.diskID, nil
|
2020-03-27 22:48:30 +01:00
|
|
|
}
|
|
|
|
|
2019-10-25 19:37:53 +02:00
|
|
|
func (client *storageRESTClient) SetDiskID(id string) {
|
|
|
|
client.diskID = id
|
|
|
|
}
|
|
|
|
|
2018-10-05 02:44:06 +02:00
|
|
|
// DiskInfo - fetch disk information for a remote disk.
|
2020-09-04 18:45:06 +02:00
|
|
|
func (client *storageRESTClient) DiskInfo(ctx context.Context) (info DiskInfo, err error) {
|
|
|
|
respBody, err := client.call(ctx, storageRESTMethodDiskInfo, nil, nil, -1)
|
2018-10-05 02:44:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-02-06 21:07:03 +01:00
|
|
|
defer http.DrainBody(respBody)
|
2018-10-05 02:44:06 +02:00
|
|
|
err = gob.NewDecoder(respBody).Decode(&info)
|
2020-07-13 18:51:07 +02:00
|
|
|
if err != nil {
|
|
|
|
return info, err
|
|
|
|
}
|
|
|
|
if info.Error != "" {
|
|
|
|
return info, toStorageErr(errors.New(info.Error))
|
|
|
|
}
|
|
|
|
return info, nil
|
2018-10-05 02:44:06 +02:00
|
|
|
}
|
|
|
|
|
2019-12-24 01:31:03 +01:00
|
|
|
// MakeVolBulk - create multiple volumes in a bulk operation.
|
2020-09-04 18:45:06 +02:00
|
|
|
func (client *storageRESTClient) MakeVolBulk(ctx context.Context, volumes ...string) (err error) {
|
2019-12-24 01:31:03 +01:00
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTVolumes, strings.Join(volumes, ","))
|
2020-09-04 18:45:06 +02:00
|
|
|
respBody, err := client.call(ctx, storageRESTMethodMakeVolBulk, values, nil, -1)
|
2019-12-24 01:31:03 +01:00
|
|
|
defer http.DrainBody(respBody)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-05 02:44:06 +02:00
|
|
|
// MakeVol - create a volume on a remote disk.
|
2020-09-04 18:45:06 +02:00
|
|
|
func (client *storageRESTClient) MakeVol(ctx context.Context, volume string) (err error) {
|
2018-10-05 02:44:06 +02:00
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTVolume, volume)
|
2020-09-04 18:45:06 +02:00
|
|
|
respBody, err := client.call(ctx, storageRESTMethodMakeVol, values, nil, -1)
|
2019-02-06 21:07:03 +01:00
|
|
|
defer http.DrainBody(respBody)
|
2018-10-05 02:44:06 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListVols - List all volumes on a remote disk.
|
2020-09-04 18:45:06 +02:00
|
|
|
func (client *storageRESTClient) ListVols(ctx context.Context) (vols []VolInfo, err error) {
|
|
|
|
respBody, err := client.call(ctx, storageRESTMethodListVols, nil, nil, -1)
|
2018-10-05 02:44:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-02-06 21:07:03 +01:00
|
|
|
defer http.DrainBody(respBody)
|
2020-09-04 18:45:06 +02:00
|
|
|
err = gob.NewDecoder(respBody).Decode(&vols)
|
|
|
|
return vols, err
|
2018-10-05 02:44:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// StatVol - get volume info over the network.
|
2020-09-04 18:45:06 +02:00
|
|
|
func (client *storageRESTClient) StatVol(ctx context.Context, volume string) (vol VolInfo, err error) {
|
2018-10-05 02:44:06 +02:00
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTVolume, volume)
|
2020-09-04 18:45:06 +02:00
|
|
|
respBody, err := client.call(ctx, storageRESTMethodStatVol, values, nil, -1)
|
2018-10-05 02:44:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-02-06 21:07:03 +01:00
|
|
|
defer http.DrainBody(respBody)
|
2020-09-04 18:45:06 +02:00
|
|
|
err = gob.NewDecoder(respBody).Decode(&vol)
|
|
|
|
return vol, err
|
2018-10-05 02:44:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteVol - Deletes a volume over the network.
|
2020-09-04 18:45:06 +02:00
|
|
|
func (client *storageRESTClient) DeleteVol(ctx context.Context, volume string, forceDelete bool) (err error) {
|
2018-10-05 02:44:06 +02:00
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTVolume, volume)
|
2020-03-28 05:52:59 +01:00
|
|
|
if forceDelete {
|
|
|
|
values.Set(storageRESTForceDelete, "true")
|
|
|
|
}
|
2020-09-04 18:45:06 +02:00
|
|
|
respBody, err := client.call(ctx, storageRESTMethodDeleteVol, values, nil, -1)
|
2019-02-06 21:07:03 +01:00
|
|
|
defer http.DrainBody(respBody)
|
2018-10-05 02:44:06 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-17 13:58:18 +01:00
|
|
|
// AppendFile - append to a file.
|
2020-09-04 18:45:06 +02:00
|
|
|
func (client *storageRESTClient) AppendFile(ctx context.Context, volume string, path string, buf []byte) error {
|
2018-10-05 02:44:06 +02:00
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTVolume, volume)
|
|
|
|
values.Set(storageRESTFilePath, path)
|
2020-09-04 18:45:06 +02:00
|
|
|
reader := bytes.NewReader(buf)
|
|
|
|
respBody, err := client.call(ctx, storageRESTMethodAppendFile, values, reader, -1)
|
2019-02-06 21:07:03 +01:00
|
|
|
defer http.DrainBody(respBody)
|
2018-10-05 02:44:06 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:45:06 +02:00
|
|
|
func (client *storageRESTClient) CreateFile(ctx context.Context, volume, path string, size int64, reader io.Reader) error {
|
2018-10-05 02:44:06 +02:00
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTVolume, volume)
|
|
|
|
values.Set(storageRESTFilePath, path)
|
2020-09-04 18:45:06 +02:00
|
|
|
values.Set(storageRESTLength, strconv.Itoa(int(size)))
|
|
|
|
respBody, err := client.call(ctx, storageRESTMethodCreateFile, values, ioutil.NopCloser(reader), size)
|
2019-02-06 21:07:03 +01:00
|
|
|
defer http.DrainBody(respBody)
|
2018-10-05 02:44:06 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:45:06 +02:00
|
|
|
func (client *storageRESTClient) WriteMetadata(ctx context.Context, volume, path string, fi FileInfo) error {
|
2020-06-13 05:04:01 +02:00
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTVolume, volume)
|
|
|
|
values.Set(storageRESTFilePath, path)
|
|
|
|
|
|
|
|
var reader bytes.Buffer
|
|
|
|
if err := gob.NewEncoder(&reader).Encode(fi); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:45:06 +02:00
|
|
|
respBody, err := client.call(ctx, storageRESTMethodWriteMetadata, values, &reader, -1)
|
2020-06-13 05:04:01 +02:00
|
|
|
defer http.DrainBody(respBody)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:45:06 +02:00
|
|
|
func (client *storageRESTClient) DeleteVersion(ctx context.Context, volume, path string, fi FileInfo) error {
|
2020-06-13 05:04:01 +02:00
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTVolume, volume)
|
|
|
|
values.Set(storageRESTFilePath, path)
|
|
|
|
|
2020-06-23 19:20:31 +02:00
|
|
|
var buffer bytes.Buffer
|
2020-06-27 01:49:49 +02:00
|
|
|
if err := gob.NewEncoder(&buffer).Encode(fi); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-23 19:20:31 +02:00
|
|
|
|
2020-09-04 18:45:06 +02:00
|
|
|
respBody, err := client.call(ctx, storageRESTMethodDeleteVersion, values, &buffer, -1)
|
2020-06-13 05:04:01 +02:00
|
|
|
defer http.DrainBody(respBody)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-11-14 15:18:35 +01:00
|
|
|
// WriteAll - write all data to a file.
|
2020-09-04 18:45:06 +02:00
|
|
|
func (client *storageRESTClient) WriteAll(ctx context.Context, volume string, path string, reader io.Reader) error {
|
2018-11-14 15:18:35 +01:00
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTVolume, volume)
|
|
|
|
values.Set(storageRESTFilePath, path)
|
2020-09-04 18:45:06 +02:00
|
|
|
respBody, err := client.call(ctx, storageRESTMethodWriteAll, values, reader, -1)
|
2019-02-06 21:07:03 +01:00
|
|
|
defer http.DrainBody(respBody)
|
2018-11-14 15:18:35 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-13 05:04:01 +02:00
|
|
|
// CheckFile - stat a file metadata.
|
2020-09-04 18:45:06 +02:00
|
|
|
func (client *storageRESTClient) CheckFile(ctx context.Context, volume string, path string) error {
|
2018-10-05 02:44:06 +02:00
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTVolume, volume)
|
|
|
|
values.Set(storageRESTFilePath, path)
|
2020-09-04 18:45:06 +02:00
|
|
|
respBody, err := client.call(ctx, storageRESTMethodCheckFile, values, nil, -1)
|
2020-06-13 05:04:01 +02:00
|
|
|
defer http.DrainBody(respBody)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckParts - stat all file parts.
|
2020-09-04 18:45:06 +02:00
|
|
|
func (client *storageRESTClient) CheckParts(ctx context.Context, volume string, path string, fi FileInfo) error {
|
2020-06-13 05:04:01 +02:00
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTVolume, volume)
|
|
|
|
values.Set(storageRESTFilePath, path)
|
|
|
|
|
|
|
|
var reader bytes.Buffer
|
|
|
|
if err := gob.NewEncoder(&reader).Encode(fi); err != nil {
|
2020-08-24 22:47:01 +02:00
|
|
|
logger.LogIf(context.Background(), err)
|
2020-06-13 05:04:01 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:45:06 +02:00
|
|
|
respBody, err := client.call(ctx, storageRESTMethodCheckParts, values, &reader, -1)
|
2020-06-13 05:04:01 +02:00
|
|
|
defer http.DrainBody(respBody)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// RenameData - rename source path to destination path atomically, metadata and data file.
|
2020-09-04 18:45:06 +02:00
|
|
|
func (client *storageRESTClient) RenameData(ctx context.Context, srcVolume, srcPath, dataDir, dstVolume, dstPath string) (err error) {
|
2020-06-13 05:04:01 +02:00
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTSrcVolume, srcVolume)
|
|
|
|
values.Set(storageRESTSrcPath, srcPath)
|
|
|
|
values.Set(storageRESTDataDir, dataDir)
|
|
|
|
values.Set(storageRESTDstVolume, dstVolume)
|
|
|
|
values.Set(storageRESTDstPath, dstPath)
|
2020-09-04 18:45:06 +02:00
|
|
|
respBody, err := client.call(ctx, storageRESTMethodRenameData, values, nil, -1)
|
2020-06-13 05:04:01 +02:00
|
|
|
defer http.DrainBody(respBody)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:45:06 +02:00
|
|
|
func (client *storageRESTClient) ReadVersion(ctx context.Context, volume, path, versionID string) (fi FileInfo, err error) {
|
2020-06-13 05:04:01 +02:00
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTVolume, volume)
|
|
|
|
values.Set(storageRESTFilePath, path)
|
|
|
|
values.Set(storageRESTVersionID, versionID)
|
|
|
|
|
2020-09-04 18:45:06 +02:00
|
|
|
respBody, err := client.call(ctx, storageRESTMethodReadVersion, values, nil, -1)
|
2018-10-05 02:44:06 +02:00
|
|
|
if err != nil {
|
2020-06-13 05:04:01 +02:00
|
|
|
return fi, err
|
2018-10-05 02:44:06 +02:00
|
|
|
}
|
2019-02-06 21:07:03 +01:00
|
|
|
defer http.DrainBody(respBody)
|
2020-06-13 05:04:01 +02:00
|
|
|
|
|
|
|
err = gob.NewDecoder(respBody).Decode(&fi)
|
|
|
|
return fi, err
|
2018-10-05 02:44:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadAll - reads all contents of a file.
|
2020-09-04 18:45:06 +02:00
|
|
|
func (client *storageRESTClient) ReadAll(ctx context.Context, volume string, path string) ([]byte, error) {
|
2018-10-05 02:44:06 +02:00
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTVolume, volume)
|
|
|
|
values.Set(storageRESTFilePath, path)
|
2020-09-04 18:45:06 +02:00
|
|
|
respBody, err := client.call(ctx, storageRESTMethodReadAll, values, nil, -1)
|
2018-10-05 02:44:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-02-06 21:07:03 +01:00
|
|
|
defer http.DrainBody(respBody)
|
2018-10-05 02:44:06 +02:00
|
|
|
return ioutil.ReadAll(respBody)
|
|
|
|
}
|
|
|
|
|
2019-01-17 13:58:18 +01:00
|
|
|
// ReadFileStream - returns a reader for the requested file.
|
2020-09-04 18:45:06 +02:00
|
|
|
func (client *storageRESTClient) ReadFileStream(ctx context.Context, volume, path string, offset, length int64) (io.ReadCloser, error) {
|
2019-01-17 13:58:18 +01:00
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTVolume, volume)
|
|
|
|
values.Set(storageRESTFilePath, path)
|
|
|
|
values.Set(storageRESTOffset, strconv.Itoa(int(offset)))
|
|
|
|
values.Set(storageRESTLength, strconv.Itoa(int(length)))
|
2020-09-04 18:45:06 +02:00
|
|
|
respBody, err := client.call(ctx, storageRESTMethodReadFileStream, values, nil, -1)
|
2019-01-17 13:58:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return respBody, nil
|
|
|
|
}
|
|
|
|
|
2018-10-05 02:44:06 +02:00
|
|
|
// ReadFile - reads section of a file.
|
2020-09-04 18:45:06 +02:00
|
|
|
func (client *storageRESTClient) ReadFile(ctx context.Context, volume string, path string, offset int64, buf []byte, verifier *BitrotVerifier) (int64, error) {
|
2018-10-05 02:44:06 +02:00
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTVolume, volume)
|
|
|
|
values.Set(storageRESTFilePath, path)
|
|
|
|
values.Set(storageRESTOffset, strconv.Itoa(int(offset)))
|
2020-09-04 18:45:06 +02:00
|
|
|
values.Set(storageRESTLength, strconv.Itoa(len(buf)))
|
2018-10-05 02:44:06 +02:00
|
|
|
if verifier != nil {
|
|
|
|
values.Set(storageRESTBitrotAlgo, verifier.algorithm.String())
|
|
|
|
values.Set(storageRESTBitrotHash, hex.EncodeToString(verifier.sum))
|
|
|
|
} else {
|
|
|
|
values.Set(storageRESTBitrotAlgo, "")
|
|
|
|
values.Set(storageRESTBitrotHash, "")
|
|
|
|
}
|
2020-09-04 18:45:06 +02:00
|
|
|
respBody, err := client.call(ctx, storageRESTMethodReadFile, values, nil, -1)
|
2018-10-05 02:44:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2019-02-06 21:07:03 +01:00
|
|
|
defer http.DrainBody(respBody)
|
2020-09-04 18:45:06 +02:00
|
|
|
n, err := io.ReadFull(respBody, buf)
|
2018-10-05 02:44:06 +02:00
|
|
|
return int64(n), err
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:45:06 +02:00
|
|
|
func (client *storageRESTClient) WalkSplunk(ctx context.Context, volume, dirPath, marker string, endWalkCh <-chan struct{}) (chan FileInfo, error) {
|
2020-03-23 03:23:47 +01:00
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTVolume, volume)
|
|
|
|
values.Set(storageRESTDirPath, dirPath)
|
|
|
|
values.Set(storageRESTMarkerPath, marker)
|
2020-09-04 18:45:06 +02:00
|
|
|
respBody, err := client.call(ctx, storageRESTMethodWalkSplunk, values, nil, -1)
|
2020-03-23 03:23:47 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ch := make(chan FileInfo)
|
|
|
|
go func() {
|
|
|
|
defer close(ch)
|
|
|
|
defer http.DrainBody(respBody)
|
|
|
|
|
|
|
|
decoder := gob.NewDecoder(respBody)
|
|
|
|
for {
|
|
|
|
var fi FileInfo
|
|
|
|
if gerr := decoder.Decode(&fi); gerr != nil {
|
|
|
|
// Upon error return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case ch <- fi:
|
|
|
|
case <-endWalkCh:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return ch, nil
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:45:06 +02:00
|
|
|
func (client *storageRESTClient) WalkVersions(ctx context.Context, volume, dirPath, marker string, recursive bool, endWalkCh <-chan struct{}) (chan FileInfoVersions, error) {
|
2020-06-13 05:04:01 +02:00
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTVolume, volume)
|
|
|
|
values.Set(storageRESTDirPath, dirPath)
|
|
|
|
values.Set(storageRESTMarkerPath, marker)
|
|
|
|
values.Set(storageRESTRecursive, strconv.FormatBool(recursive))
|
2020-09-04 18:45:06 +02:00
|
|
|
respBody, err := client.call(ctx, storageRESTMethodWalkVersions, values, nil, -1)
|
2020-06-13 05:04:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ch := make(chan FileInfoVersions)
|
|
|
|
go func() {
|
|
|
|
defer close(ch)
|
|
|
|
defer http.DrainBody(respBody)
|
|
|
|
|
|
|
|
decoder := gob.NewDecoder(respBody)
|
|
|
|
for {
|
|
|
|
var fi FileInfoVersions
|
|
|
|
if gerr := decoder.Decode(&fi); gerr != nil {
|
|
|
|
// Upon error return
|
2020-07-02 19:29:50 +02:00
|
|
|
if gerr != io.EOF {
|
2020-08-13 18:16:01 +02:00
|
|
|
logger.LogIf(GlobalContext, gerr)
|
2020-07-02 19:29:50 +02:00
|
|
|
}
|
2020-06-13 05:04:01 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case ch <- fi:
|
|
|
|
case <-endWalkCh:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return ch, nil
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:45:06 +02:00
|
|
|
func (client *storageRESTClient) Walk(ctx context.Context, volume, dirPath, marker string, recursive bool, endWalkCh <-chan struct{}) (chan FileInfo, error) {
|
2019-05-14 22:49:10 +02:00
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTVolume, volume)
|
|
|
|
values.Set(storageRESTDirPath, dirPath)
|
|
|
|
values.Set(storageRESTMarkerPath, marker)
|
|
|
|
values.Set(storageRESTRecursive, strconv.FormatBool(recursive))
|
2020-09-04 18:45:06 +02:00
|
|
|
respBody, err := client.call(ctx, storageRESTMethodWalk, values, nil, -1)
|
2019-05-14 22:49:10 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ch := make(chan FileInfo)
|
|
|
|
go func() {
|
|
|
|
defer close(ch)
|
|
|
|
defer http.DrainBody(respBody)
|
|
|
|
|
|
|
|
decoder := gob.NewDecoder(respBody)
|
|
|
|
for {
|
|
|
|
var fi FileInfo
|
|
|
|
if gerr := decoder.Decode(&fi); gerr != nil {
|
|
|
|
// Upon error return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case ch <- fi:
|
|
|
|
case <-endWalkCh:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return ch, nil
|
|
|
|
}
|
|
|
|
|
2018-10-05 02:44:06 +02:00
|
|
|
// ListDir - lists a directory.
|
2020-09-04 18:45:06 +02:00
|
|
|
func (client *storageRESTClient) ListDir(ctx context.Context, volume, dirPath string, count int) (entries []string, err error) {
|
2018-10-05 02:44:06 +02:00
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTVolume, volume)
|
|
|
|
values.Set(storageRESTDirPath, dirPath)
|
|
|
|
values.Set(storageRESTCount, strconv.Itoa(count))
|
2020-09-04 18:45:06 +02:00
|
|
|
respBody, err := client.call(ctx, storageRESTMethodListDir, values, nil, -1)
|
2018-10-05 02:44:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-02-06 21:07:03 +01:00
|
|
|
defer http.DrainBody(respBody)
|
2018-10-05 02:44:06 +02:00
|
|
|
err = gob.NewDecoder(respBody).Decode(&entries)
|
|
|
|
return entries, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteFile - deletes a file.
|
2020-09-04 18:45:06 +02:00
|
|
|
func (client *storageRESTClient) DeleteFile(ctx context.Context, volume string, path string) error {
|
2018-10-05 02:44:06 +02:00
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTVolume, volume)
|
|
|
|
values.Set(storageRESTFilePath, path)
|
2020-09-04 18:45:06 +02:00
|
|
|
respBody, err := client.call(ctx, storageRESTMethodDeleteFile, values, nil, -1)
|
2019-02-06 21:07:03 +01:00
|
|
|
defer http.DrainBody(respBody)
|
2018-10-05 02:44:06 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-13 05:04:01 +02:00
|
|
|
// DeleteVersions - deletes list of specified versions if present
|
2020-09-04 18:45:06 +02:00
|
|
|
func (client *storageRESTClient) DeleteVersions(ctx context.Context, volume string, versions []FileInfo) (errs []error) {
|
2020-06-13 05:04:01 +02:00
|
|
|
if len(versions) == 0 {
|
|
|
|
return errs
|
2019-05-13 21:25:49 +02:00
|
|
|
}
|
|
|
|
|
2020-03-11 16:56:36 +01:00
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTVolume, volume)
|
2020-06-13 05:04:01 +02:00
|
|
|
values.Set(storageRESTTotalVersions, strconv.Itoa(len(versions)))
|
2020-03-11 16:56:36 +01:00
|
|
|
|
|
|
|
var buffer bytes.Buffer
|
2020-06-13 05:04:01 +02:00
|
|
|
encoder := gob.NewEncoder(&buffer)
|
|
|
|
for _, version := range versions {
|
|
|
|
encoder.Encode(&version)
|
2020-03-11 16:56:36 +01:00
|
|
|
}
|
|
|
|
|
2020-06-13 05:04:01 +02:00
|
|
|
errs = make([]error, len(versions))
|
|
|
|
|
2020-09-04 18:45:06 +02:00
|
|
|
respBody, err := client.call(ctx, storageRESTMethodDeleteVersions, values, &buffer, -1)
|
2020-03-11 16:56:36 +01:00
|
|
|
defer http.DrainBody(respBody)
|
|
|
|
if err != nil {
|
2020-06-13 05:04:01 +02:00
|
|
|
for i := range errs {
|
|
|
|
errs[i] = err
|
|
|
|
}
|
|
|
|
return errs
|
2020-03-11 16:56:36 +01:00
|
|
|
}
|
|
|
|
|
2020-03-19 00:19:29 +01:00
|
|
|
reader, err := waitForHTTPResponse(respBody)
|
2020-03-11 16:56:36 +01:00
|
|
|
if err != nil {
|
2020-06-13 05:04:01 +02:00
|
|
|
for i := range errs {
|
|
|
|
errs[i] = err
|
|
|
|
}
|
|
|
|
return errs
|
2020-03-11 16:56:36 +01:00
|
|
|
}
|
|
|
|
|
2020-06-13 05:04:01 +02:00
|
|
|
dErrResp := &DeleteVersionsErrsResp{}
|
2020-03-11 16:56:36 +01:00
|
|
|
if err = gob.NewDecoder(reader).Decode(dErrResp); err != nil {
|
2020-06-13 05:04:01 +02:00
|
|
|
for i := range errs {
|
|
|
|
errs[i] = err
|
|
|
|
}
|
|
|
|
return errs
|
2019-05-13 21:25:49 +02:00
|
|
|
}
|
|
|
|
|
2020-06-13 05:04:01 +02:00
|
|
|
for i, dErr := range dErrResp.Errs {
|
|
|
|
errs[i] = toStorageErr(dErr)
|
2019-05-13 21:25:49 +02:00
|
|
|
}
|
|
|
|
|
2020-06-13 05:04:01 +02:00
|
|
|
return errs
|
2019-05-13 21:25:49 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 02:44:06 +02:00
|
|
|
// RenameFile - renames a file.
|
2020-09-04 18:45:06 +02:00
|
|
|
func (client *storageRESTClient) RenameFile(ctx context.Context, srcVolume, srcPath, dstVolume, dstPath string) (err error) {
|
2018-10-05 02:44:06 +02:00
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTSrcVolume, srcVolume)
|
|
|
|
values.Set(storageRESTSrcPath, srcPath)
|
|
|
|
values.Set(storageRESTDstVolume, dstVolume)
|
|
|
|
values.Set(storageRESTDstPath, dstPath)
|
2020-09-04 18:45:06 +02:00
|
|
|
respBody, err := client.call(ctx, storageRESTMethodRenameFile, values, nil, -1)
|
2019-02-06 21:07:03 +01:00
|
|
|
defer http.DrainBody(respBody)
|
2018-10-05 02:44:06 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:45:06 +02:00
|
|
|
func (client *storageRESTClient) VerifyFile(ctx context.Context, volume, path string, fi FileInfo) error {
|
2019-07-08 22:51:18 +02:00
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTVolume, volume)
|
|
|
|
values.Set(storageRESTFilePath, path)
|
2019-09-12 22:08:02 +02:00
|
|
|
|
2020-06-13 05:04:01 +02:00
|
|
|
var reader bytes.Buffer
|
|
|
|
if err := gob.NewEncoder(&reader).Encode(fi); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:45:06 +02:00
|
|
|
respBody, err := client.call(ctx, storageRESTMethodVerifyFile, values, &reader, -1)
|
2019-07-08 22:51:18 +02:00
|
|
|
defer http.DrainBody(respBody)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-13 05:04:01 +02:00
|
|
|
|
|
|
|
respReader, err := waitForHTTPResponse(respBody)
|
2020-03-11 16:56:36 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-07-08 22:51:18 +02:00
|
|
|
}
|
2020-06-13 05:04:01 +02:00
|
|
|
|
2019-07-08 22:51:18 +02:00
|
|
|
verifyResp := &VerifyFileResp{}
|
2020-06-13 05:04:01 +02:00
|
|
|
if err = gob.NewDecoder(respReader).Decode(verifyResp); err != nil {
|
2019-07-08 22:51:18 +02:00
|
|
|
return err
|
|
|
|
}
|
2020-06-13 05:04:01 +02:00
|
|
|
|
2019-07-08 22:51:18 +02:00
|
|
|
return toStorageErr(verifyResp.Err)
|
|
|
|
}
|
|
|
|
|
2018-10-05 02:44:06 +02:00
|
|
|
// Close - marks the client as closed.
|
|
|
|
func (client *storageRESTClient) Close() error {
|
2018-11-20 20:07:19 +01:00
|
|
|
client.restClient.Close()
|
2018-10-05 02:44:06 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a storage rest client.
|
2019-11-13 21:17:45 +01:00
|
|
|
func newStorageRESTClient(endpoint Endpoint) *storageRESTClient {
|
2018-10-05 02:44:06 +02:00
|
|
|
serverURL := &url.URL{
|
2019-11-13 21:17:45 +01:00
|
|
|
Scheme: endpoint.Scheme,
|
2018-10-05 02:44:06 +02:00
|
|
|
Host: endpoint.Host,
|
2019-11-04 18:30:59 +01:00
|
|
|
Path: path.Join(storageRESTPrefix, endpoint.Path, storageRESTVersion),
|
2018-10-05 02:44:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var tlsConfig *tls.Config
|
|
|
|
if globalIsSSL {
|
|
|
|
tlsConfig = &tls.Config{
|
2019-11-13 21:17:45 +01:00
|
|
|
ServerName: endpoint.Hostname(),
|
2018-10-05 02:44:06 +02:00
|
|
|
RootCAs: globalRootCAs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-04 23:55:53 +02:00
|
|
|
trFn := newInternodeHTTPTransport(tlsConfig, rest.DefaultRESTTimeout)
|
2020-07-12 07:19:38 +02:00
|
|
|
restClient := rest.NewClient(serverURL, trFn, newAuthToken)
|
2020-06-17 23:49:26 +02:00
|
|
|
restClient.HealthCheckInterval = 500 * time.Millisecond
|
|
|
|
restClient.HealthCheckFn = func() bool {
|
|
|
|
ctx, cancel := context.WithTimeout(GlobalContext, restClient.HealthCheckTimeout)
|
2020-07-12 07:19:38 +02:00
|
|
|
// Instantiate a new rest client for healthcheck
|
|
|
|
// to avoid recursive healthCheckFn()
|
2020-09-04 18:45:06 +02:00
|
|
|
respBody, err := rest.NewClient(serverURL, trFn, newAuthToken).Call(ctx, storageRESTMethodHealth, nil, nil, -1)
|
2020-06-17 23:49:26 +02:00
|
|
|
xhttp.DrainBody(respBody)
|
|
|
|
cancel()
|
|
|
|
return !errors.Is(err, context.DeadlineExceeded) && toStorageErr(err) != errDiskNotFound
|
|
|
|
}
|
|
|
|
|
2020-06-17 03:59:32 +02:00
|
|
|
return &storageRESTClient{endpoint: endpoint, restClient: restClient}
|
2018-10-05 02:44:06 +02:00
|
|
|
}
|