2018-10-04 20:44:06 -04:00
|
|
|
/*
|
2019-10-09 02:11:15 -04:00
|
|
|
* MinIO Cloud Storage, (C) 2018-2019 MinIO, Inc.
|
2018-10-04 20:44:06 -04: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 15:17:45 -05:00
|
|
|
"context"
|
2018-10-04 20:44:06 -04:00
|
|
|
"crypto/tls"
|
2019-09-26 02:08:24 -04:00
|
|
|
"encoding/gob"
|
|
|
|
"encoding/hex"
|
2018-10-04 20:44:06 -04:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/url"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
2019-12-23 19:31:03 -05:00
|
|
|
"strings"
|
2019-10-09 02:11:15 -04:00
|
|
|
"sync/atomic"
|
2019-01-30 13:53:57 -05:00
|
|
|
|
2019-02-06 15:07:03 -05:00
|
|
|
"github.com/minio/minio/cmd/http"
|
2019-11-13 15:17:45 -05:00
|
|
|
"github.com/minio/minio/cmd/logger"
|
2018-10-04 20:44:06 -04:00
|
|
|
"github.com/minio/minio/cmd/rest"
|
|
|
|
xnet "github.com/minio/minio/pkg/net"
|
|
|
|
)
|
|
|
|
|
2019-02-13 18:29:46 -05:00
|
|
|
func isNetworkError(err error) bool {
|
2018-10-04 20:44:06 -04:00
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
}
|
2019-07-29 17:48:18 -04:00
|
|
|
if nerr, ok := err.(*rest.NetworkError); ok {
|
2019-09-12 19:44:51 -04:00
|
|
|
return xnet.IsNetworkOrHostDown(nerr.Err)
|
2019-05-02 10:09:57 -04:00
|
|
|
}
|
2019-05-29 13:21:47 -04:00
|
|
|
return false
|
2019-05-02 10:09:57 -04:00
|
|
|
}
|
|
|
|
|
2020-01-14 21:45:17 -05:00
|
|
|
// Converts network error to storageErr. This function is
|
|
|
|
// written so that the storageAPI errors are consistent
|
|
|
|
// across network disks.
|
2018-10-04 20:44:06 -04:00
|
|
|
func toStorageErr(err error) error {
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-13 18:29:46 -05:00
|
|
|
if isNetworkError(err) {
|
2019-05-29 13:21:47 -04:00
|
|
|
return errDiskNotFound
|
2018-10-04 20:44:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
switch err.Error() {
|
2019-10-01 16:12:15 -04:00
|
|
|
case errFaultyDisk.Error():
|
|
|
|
return errFaultyDisk
|
|
|
|
case errFileCorrupt.Error():
|
|
|
|
return errFileCorrupt
|
2018-10-04 20:44:06 -04: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 16:12:15 -04:00
|
|
|
case io.EOF.Error():
|
|
|
|
return io.EOF
|
|
|
|
case io.ErrUnexpectedEOF.Error():
|
|
|
|
return io.ErrUnexpectedEOF
|
2019-10-25 13:37:53 -04:00
|
|
|
case errDiskStale.Error():
|
|
|
|
return errDiskNotFound
|
2019-01-30 13:53:57 -05:00
|
|
|
}
|
2018-10-04 20:44:06 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Abstracts a remote disk.
|
|
|
|
type storageRESTClient struct {
|
|
|
|
endpoint Endpoint
|
|
|
|
restClient *rest.Client
|
2019-10-09 02:11:15 -04:00
|
|
|
connected int32
|
2019-10-25 13:37:53 -04:00
|
|
|
diskID string
|
2018-10-04 20:44:06 -04: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-01-14 21:45:17 -05:00
|
|
|
func (client *storageRESTClient) call(method string, values url.Values, body io.Reader, length int64) (io.ReadCloser, error) {
|
2019-10-09 02:11:15 -04:00
|
|
|
if !client.IsOnline() {
|
2018-10-04 20:44:06 -04:00
|
|
|
return nil, errDiskNotFound
|
|
|
|
}
|
2019-02-13 18:29:46 -05:00
|
|
|
if values == nil {
|
|
|
|
values = make(url.Values)
|
|
|
|
}
|
2019-10-25 13:37:53 -04:00
|
|
|
values.Set(storageRESTDiskID, client.diskID)
|
2020-01-14 21:45:17 -05:00
|
|
|
respBody, err := client.restClient.Call(method, values, body, length)
|
2018-10-04 20:44:06 -04:00
|
|
|
if err == nil {
|
|
|
|
return respBody, nil
|
|
|
|
}
|
2020-01-14 21:45:17 -05:00
|
|
|
|
|
|
|
err = toStorageErr(err)
|
|
|
|
if err == errDiskNotFound {
|
2019-10-09 02:11:15 -04:00
|
|
|
atomic.StoreInt32(&client.connected, 0)
|
2018-10-04 20:44:06 -04:00
|
|
|
}
|
|
|
|
|
2020-01-14 21:45:17 -05:00
|
|
|
return nil, err
|
2018-10-04 20:44:06 -04: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 {
|
2019-10-09 02:11:15 -04:00
|
|
|
return atomic.LoadInt32(&client.connected) == 1
|
2018-10-04 20:44:06 -04:00
|
|
|
}
|
|
|
|
|
2020-05-19 17:27:20 -04:00
|
|
|
func (client *storageRESTClient) IsLocal() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-01-13 16:09:10 -05:00
|
|
|
func (client *storageRESTClient) Hostname() string {
|
|
|
|
return client.endpoint.Host
|
|
|
|
}
|
|
|
|
|
2020-03-18 19:19:29 -04:00
|
|
|
func (client *storageRESTClient) CrawlAndGetDataUsage(ctx context.Context, cache dataUsageCache) (dataUsageCache, error) {
|
|
|
|
b := cache.serialize()
|
|
|
|
respBody, err := client.call(storageRESTMethodCrawlAndGetDataUsage,
|
|
|
|
url.Values{},
|
|
|
|
bytes.NewBuffer(b), int64(len(b)))
|
2019-12-12 09:02:37 -05:00
|
|
|
defer http.DrainBody(respBody)
|
|
|
|
if err != nil {
|
2020-03-18 19:19:29 -04:00
|
|
|
return cache, err
|
2019-12-12 09:02:37 -05:00
|
|
|
}
|
2020-03-18 19:19:29 -04:00
|
|
|
reader, err := waitForHTTPResponse(respBody)
|
|
|
|
if err != nil {
|
|
|
|
return cache, err
|
|
|
|
}
|
|
|
|
b, err = ioutil.ReadAll(reader)
|
|
|
|
if err != nil {
|
|
|
|
return cache, err
|
2019-12-12 09:02:37 -05:00
|
|
|
}
|
2020-03-18 19:19:29 -04:00
|
|
|
var newCache dataUsageCache
|
|
|
|
return newCache, newCache.deserialize(b)
|
2019-12-12 09:02:37 -05:00
|
|
|
}
|
|
|
|
|
2020-03-27 17:48:30 -04:00
|
|
|
func (client *storageRESTClient) GetDiskID() (string, error) {
|
2020-05-28 16:03:04 -04: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 17:48:30 -04:00
|
|
|
}
|
|
|
|
|
2019-10-25 13:37:53 -04:00
|
|
|
func (client *storageRESTClient) SetDiskID(id string) {
|
|
|
|
client.diskID = id
|
|
|
|
}
|
|
|
|
|
2018-10-04 20:44:06 -04:00
|
|
|
// DiskInfo - fetch disk information for a remote disk.
|
|
|
|
func (client *storageRESTClient) DiskInfo() (info DiskInfo, err error) {
|
2019-01-17 07:58:18 -05:00
|
|
|
respBody, err := client.call(storageRESTMethodDiskInfo, nil, nil, -1)
|
2018-10-04 20:44:06 -04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-02-06 15:07:03 -05:00
|
|
|
defer http.DrainBody(respBody)
|
2018-10-04 20:44:06 -04:00
|
|
|
err = gob.NewDecoder(respBody).Decode(&info)
|
|
|
|
return info, err
|
|
|
|
}
|
|
|
|
|
2019-12-23 19:31:03 -05:00
|
|
|
// MakeVolBulk - create multiple volumes in a bulk operation.
|
|
|
|
func (client *storageRESTClient) MakeVolBulk(volumes ...string) (err error) {
|
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTVolumes, strings.Join(volumes, ","))
|
|
|
|
respBody, err := client.call(storageRESTMethodMakeVolBulk, values, nil, -1)
|
|
|
|
defer http.DrainBody(respBody)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-04 20:44:06 -04:00
|
|
|
// MakeVol - create a volume on a remote disk.
|
|
|
|
func (client *storageRESTClient) MakeVol(volume string) (err error) {
|
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTVolume, volume)
|
2019-01-17 07:58:18 -05:00
|
|
|
respBody, err := client.call(storageRESTMethodMakeVol, values, nil, -1)
|
2019-02-06 15:07:03 -05:00
|
|
|
defer http.DrainBody(respBody)
|
2018-10-04 20:44:06 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListVols - List all volumes on a remote disk.
|
|
|
|
func (client *storageRESTClient) ListVols() (volinfo []VolInfo, err error) {
|
2019-01-17 07:58:18 -05:00
|
|
|
respBody, err := client.call(storageRESTMethodListVols, nil, nil, -1)
|
2018-10-04 20:44:06 -04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-02-06 15:07:03 -05:00
|
|
|
defer http.DrainBody(respBody)
|
2018-10-04 20:44:06 -04:00
|
|
|
err = gob.NewDecoder(respBody).Decode(&volinfo)
|
|
|
|
return volinfo, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// StatVol - get volume info over the network.
|
|
|
|
func (client *storageRESTClient) StatVol(volume string) (volInfo VolInfo, err error) {
|
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTVolume, volume)
|
2019-01-17 07:58:18 -05:00
|
|
|
respBody, err := client.call(storageRESTMethodStatVol, values, nil, -1)
|
2018-10-04 20:44:06 -04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-02-06 15:07:03 -05:00
|
|
|
defer http.DrainBody(respBody)
|
2018-10-04 20:44:06 -04:00
|
|
|
err = gob.NewDecoder(respBody).Decode(&volInfo)
|
|
|
|
return volInfo, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteVol - Deletes a volume over the network.
|
2020-03-28 00:52:59 -04:00
|
|
|
func (client *storageRESTClient) DeleteVol(volume string, forceDelete bool) (err error) {
|
2018-10-04 20:44:06 -04:00
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTVolume, volume)
|
2020-03-28 00:52:59 -04:00
|
|
|
if forceDelete {
|
|
|
|
values.Set(storageRESTForceDelete, "true")
|
|
|
|
}
|
2019-01-17 07:58:18 -05:00
|
|
|
respBody, err := client.call(storageRESTMethodDeleteVol, values, nil, -1)
|
2019-02-06 15:07:03 -05:00
|
|
|
defer http.DrainBody(respBody)
|
2018-10-04 20:44:06 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-17 07:58:18 -05:00
|
|
|
// AppendFile - append to a file.
|
|
|
|
func (client *storageRESTClient) AppendFile(volume, path string, buffer []byte) error {
|
2018-10-04 20:44:06 -04:00
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTVolume, volume)
|
|
|
|
values.Set(storageRESTFilePath, path)
|
2020-02-01 21:11:29 -05:00
|
|
|
reader := bytes.NewReader(buffer)
|
2019-01-17 07:58:18 -05:00
|
|
|
respBody, err := client.call(storageRESTMethodAppendFile, values, reader, -1)
|
2019-02-06 15:07:03 -05:00
|
|
|
defer http.DrainBody(respBody)
|
2018-10-04 20:44:06 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-17 07:58:18 -05:00
|
|
|
func (client *storageRESTClient) CreateFile(volume, path string, length int64, r io.Reader) error {
|
2018-10-04 20:44:06 -04:00
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTVolume, volume)
|
|
|
|
values.Set(storageRESTFilePath, path)
|
2019-01-17 07:58:18 -05:00
|
|
|
values.Set(storageRESTLength, strconv.Itoa(int(length)))
|
2019-05-02 10:09:57 -04:00
|
|
|
respBody, err := client.call(storageRESTMethodCreateFile, values, ioutil.NopCloser(r), length)
|
2019-02-06 15:07:03 -05:00
|
|
|
defer http.DrainBody(respBody)
|
2018-10-04 20:44:06 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-11-14 09:18:35 -05:00
|
|
|
// WriteAll - write all data to a file.
|
2019-05-22 16:47:15 -04:00
|
|
|
func (client *storageRESTClient) WriteAll(volume, path string, reader io.Reader) error {
|
2018-11-14 09:18:35 -05:00
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTVolume, volume)
|
|
|
|
values.Set(storageRESTFilePath, path)
|
2019-01-17 07:58:18 -05:00
|
|
|
respBody, err := client.call(storageRESTMethodWriteAll, values, reader, -1)
|
2019-02-06 15:07:03 -05:00
|
|
|
defer http.DrainBody(respBody)
|
2018-11-14 09:18:35 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-04 20:44:06 -04:00
|
|
|
// StatFile - stat a file.
|
|
|
|
func (client *storageRESTClient) StatFile(volume, path string) (info FileInfo, err error) {
|
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTVolume, volume)
|
|
|
|
values.Set(storageRESTFilePath, path)
|
2019-01-17 07:58:18 -05:00
|
|
|
respBody, err := client.call(storageRESTMethodStatFile, values, nil, -1)
|
2018-10-04 20:44:06 -04:00
|
|
|
if err != nil {
|
|
|
|
return info, err
|
|
|
|
}
|
2019-02-06 15:07:03 -05:00
|
|
|
defer http.DrainBody(respBody)
|
2018-10-04 20:44:06 -04:00
|
|
|
err = gob.NewDecoder(respBody).Decode(&info)
|
|
|
|
return info, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadAll - reads all contents of a file.
|
|
|
|
func (client *storageRESTClient) ReadAll(volume, path string) ([]byte, error) {
|
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTVolume, volume)
|
|
|
|
values.Set(storageRESTFilePath, path)
|
2019-01-17 07:58:18 -05:00
|
|
|
respBody, err := client.call(storageRESTMethodReadAll, values, nil, -1)
|
2018-10-04 20:44:06 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-02-06 15:07:03 -05:00
|
|
|
defer http.DrainBody(respBody)
|
2018-10-04 20:44:06 -04:00
|
|
|
return ioutil.ReadAll(respBody)
|
|
|
|
}
|
|
|
|
|
2019-01-17 07:58:18 -05:00
|
|
|
// ReadFileStream - returns a reader for the requested file.
|
|
|
|
func (client *storageRESTClient) ReadFileStream(volume, path string, offset, length int64) (io.ReadCloser, error) {
|
|
|
|
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)))
|
|
|
|
respBody, err := client.call(storageRESTMethodReadFileStream, values, nil, -1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return respBody, nil
|
|
|
|
}
|
|
|
|
|
2018-10-04 20:44:06 -04:00
|
|
|
// ReadFile - reads section of a file.
|
|
|
|
func (client *storageRESTClient) ReadFile(volume, path string, offset int64, buffer []byte, verifier *BitrotVerifier) (int64, error) {
|
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTVolume, volume)
|
|
|
|
values.Set(storageRESTFilePath, path)
|
|
|
|
values.Set(storageRESTOffset, strconv.Itoa(int(offset)))
|
|
|
|
values.Set(storageRESTLength, strconv.Itoa(len(buffer)))
|
|
|
|
if verifier != nil {
|
|
|
|
values.Set(storageRESTBitrotAlgo, verifier.algorithm.String())
|
|
|
|
values.Set(storageRESTBitrotHash, hex.EncodeToString(verifier.sum))
|
|
|
|
} else {
|
|
|
|
values.Set(storageRESTBitrotAlgo, "")
|
|
|
|
values.Set(storageRESTBitrotHash, "")
|
|
|
|
}
|
2019-01-17 07:58:18 -05:00
|
|
|
respBody, err := client.call(storageRESTMethodReadFile, values, nil, -1)
|
2018-10-04 20:44:06 -04:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2019-02-06 15:07:03 -05:00
|
|
|
defer http.DrainBody(respBody)
|
2018-10-04 20:44:06 -04:00
|
|
|
n, err := io.ReadFull(respBody, buffer)
|
|
|
|
return int64(n), err
|
|
|
|
}
|
|
|
|
|
2020-03-22 22:23:47 -04:00
|
|
|
func (client *storageRESTClient) WalkSplunk(volume, dirPath, marker string, endWalkCh <-chan struct{}) (chan FileInfo, error) {
|
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTVolume, volume)
|
|
|
|
values.Set(storageRESTDirPath, dirPath)
|
|
|
|
values.Set(storageRESTMarkerPath, marker)
|
|
|
|
respBody, err := client.call(storageRESTMethodWalkSplunk, values, nil, -1)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-05-14 16:49:10 -04:00
|
|
|
func (client *storageRESTClient) Walk(volume, dirPath, marker string, recursive bool, leafFile string,
|
2020-02-25 10:52:28 -05:00
|
|
|
readMetadataFn readMetadataFunc, endWalkCh <-chan struct{}) (chan FileInfo, error) {
|
2019-05-14 16:49:10 -04:00
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTVolume, volume)
|
|
|
|
values.Set(storageRESTDirPath, dirPath)
|
|
|
|
values.Set(storageRESTMarkerPath, marker)
|
|
|
|
values.Set(storageRESTRecursive, strconv.FormatBool(recursive))
|
|
|
|
values.Set(storageRESTLeafFile, leafFile)
|
|
|
|
respBody, err := client.call(storageRESTMethodWalk, values, nil, -1)
|
|
|
|
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-04 20:44:06 -04:00
|
|
|
// ListDir - lists a directory.
|
2019-04-23 17:54:28 -04:00
|
|
|
func (client *storageRESTClient) ListDir(volume, dirPath string, count int, leafFile string) (entries []string, err error) {
|
2018-10-04 20:44:06 -04:00
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTVolume, volume)
|
|
|
|
values.Set(storageRESTDirPath, dirPath)
|
|
|
|
values.Set(storageRESTCount, strconv.Itoa(count))
|
2019-04-23 17:54:28 -04:00
|
|
|
values.Set(storageRESTLeafFile, leafFile)
|
2019-01-17 07:58:18 -05:00
|
|
|
respBody, err := client.call(storageRESTMethodListDir, values, nil, -1)
|
2018-10-04 20:44:06 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-02-06 15:07:03 -05:00
|
|
|
defer http.DrainBody(respBody)
|
2018-10-04 20:44:06 -04:00
|
|
|
err = gob.NewDecoder(respBody).Decode(&entries)
|
|
|
|
return entries, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteFile - deletes a file.
|
|
|
|
func (client *storageRESTClient) DeleteFile(volume, path string) error {
|
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTVolume, volume)
|
|
|
|
values.Set(storageRESTFilePath, path)
|
2019-01-17 07:58:18 -05:00
|
|
|
respBody, err := client.call(storageRESTMethodDeleteFile, values, nil, -1)
|
2019-02-06 15:07:03 -05:00
|
|
|
defer http.DrainBody(respBody)
|
2018-10-04 20:44:06 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-13 15:25:49 -04:00
|
|
|
// DeleteFileBulk - deletes files in bulk.
|
|
|
|
func (client *storageRESTClient) DeleteFileBulk(volume string, paths []string) (errs []error, err error) {
|
2019-08-15 16:15:49 -04:00
|
|
|
if len(paths) == 0 {
|
|
|
|
return errs, err
|
|
|
|
}
|
2019-05-13 15:25:49 -04:00
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTVolume, volume)
|
2020-02-01 21:11:29 -05:00
|
|
|
|
|
|
|
var buffer bytes.Buffer
|
2019-05-13 15:25:49 -04:00
|
|
|
for _, path := range paths {
|
2020-02-01 21:11:29 -05:00
|
|
|
buffer.WriteString(path)
|
|
|
|
buffer.WriteString("\n")
|
2019-05-13 15:25:49 -04:00
|
|
|
}
|
2020-02-01 21:11:29 -05:00
|
|
|
|
|
|
|
respBody, err := client.call(storageRESTMethodDeleteFileBulk, values, &buffer, -1)
|
2019-05-13 15:25:49 -04:00
|
|
|
defer http.DrainBody(respBody)
|
2020-03-11 11:56:36 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-05-13 15:25:49 -04:00
|
|
|
|
2020-03-18 19:19:29 -04:00
|
|
|
reader, err := waitForHTTPResponse(respBody)
|
2019-05-13 15:25:49 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-09-30 22:01:28 -04:00
|
|
|
dErrResp := &DeleteFileBulkErrsResp{}
|
2020-03-11 11:56:36 -04:00
|
|
|
if err = gob.NewDecoder(reader).Decode(dErrResp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dErr := range dErrResp.Errs {
|
|
|
|
errs = append(errs, toStorageErr(dErr))
|
|
|
|
}
|
|
|
|
|
|
|
|
return errs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeletePrefixes - deletes prefixes in bulk.
|
|
|
|
func (client *storageRESTClient) DeletePrefixes(volume string, paths []string) (errs []error, err error) {
|
|
|
|
if len(paths) == 0 {
|
|
|
|
return errs, err
|
|
|
|
}
|
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTVolume, volume)
|
|
|
|
|
|
|
|
var buffer bytes.Buffer
|
|
|
|
for _, path := range paths {
|
|
|
|
buffer.WriteString(path)
|
|
|
|
buffer.WriteString("\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
respBody, err := client.call(storageRESTMethodDeletePrefixes, values, &buffer, -1)
|
|
|
|
defer http.DrainBody(respBody)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-18 19:19:29 -04:00
|
|
|
reader, err := waitForHTTPResponse(respBody)
|
2020-03-11 11:56:36 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
dErrResp := &DeletePrefixesErrsResp{}
|
|
|
|
if err = gob.NewDecoder(reader).Decode(dErrResp); err != nil {
|
2019-05-13 15:25:49 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-09-30 22:01:28 -04:00
|
|
|
for _, dErr := range dErrResp.Errs {
|
|
|
|
errs = append(errs, toStorageErr(dErr))
|
2019-05-13 15:25:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return errs, nil
|
|
|
|
}
|
|
|
|
|
2018-10-04 20:44:06 -04:00
|
|
|
// RenameFile - renames a file.
|
|
|
|
func (client *storageRESTClient) RenameFile(srcVolume, srcPath, dstVolume, dstPath string) (err error) {
|
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTSrcVolume, srcVolume)
|
|
|
|
values.Set(storageRESTSrcPath, srcPath)
|
|
|
|
values.Set(storageRESTDstVolume, dstVolume)
|
|
|
|
values.Set(storageRESTDstPath, dstPath)
|
2019-01-17 07:58:18 -05:00
|
|
|
respBody, err := client.call(storageRESTMethodRenameFile, values, nil, -1)
|
2019-02-06 15:07:03 -05:00
|
|
|
defer http.DrainBody(respBody)
|
2018-10-04 20:44:06 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-09-11 16:49:53 -04:00
|
|
|
func (client *storageRESTClient) VerifyFile(volume, path string, size int64, algo BitrotAlgorithm, sum []byte, shardSize int64) error {
|
2019-07-08 16:51:18 -04:00
|
|
|
values := make(url.Values)
|
|
|
|
values.Set(storageRESTVolume, volume)
|
|
|
|
values.Set(storageRESTFilePath, path)
|
|
|
|
values.Set(storageRESTBitrotAlgo, algo.String())
|
2019-09-11 16:49:53 -04:00
|
|
|
values.Set(storageRESTLength, strconv.FormatInt(size, 10))
|
|
|
|
values.Set(storageRESTShardSize, strconv.Itoa(int(shardSize)))
|
2019-09-12 16:08:02 -04:00
|
|
|
values.Set(storageRESTBitrotHash, hex.EncodeToString(sum))
|
|
|
|
|
2019-07-08 16:51:18 -04:00
|
|
|
respBody, err := client.call(storageRESTMethodVerifyFile, values, nil, -1)
|
|
|
|
defer http.DrainBody(respBody)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-18 19:19:29 -04:00
|
|
|
reader, err := waitForHTTPResponse(respBody)
|
2020-03-11 11:56:36 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-07-08 16:51:18 -04:00
|
|
|
}
|
|
|
|
verifyResp := &VerifyFileResp{}
|
2019-10-01 16:12:15 -04:00
|
|
|
if err = gob.NewDecoder(reader).Decode(verifyResp); err != nil {
|
2019-07-08 16:51:18 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return toStorageErr(verifyResp.Err)
|
|
|
|
}
|
|
|
|
|
2018-10-04 20:44:06 -04:00
|
|
|
// Close - marks the client as closed.
|
|
|
|
func (client *storageRESTClient) Close() error {
|
2019-10-09 02:11:15 -04:00
|
|
|
atomic.StoreInt32(&client.connected, 0)
|
2018-11-20 14:07:19 -05:00
|
|
|
client.restClient.Close()
|
2018-10-04 20:44:06 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a storage rest client.
|
2019-11-13 15:17:45 -05:00
|
|
|
func newStorageRESTClient(endpoint Endpoint) *storageRESTClient {
|
2018-10-04 20:44:06 -04:00
|
|
|
serverURL := &url.URL{
|
2019-11-13 15:17:45 -05:00
|
|
|
Scheme: endpoint.Scheme,
|
2018-10-04 20:44:06 -04:00
|
|
|
Host: endpoint.Host,
|
2019-11-04 12:30:59 -05:00
|
|
|
Path: path.Join(storageRESTPrefix, endpoint.Path, storageRESTVersion),
|
2018-10-04 20:44:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var tlsConfig *tls.Config
|
|
|
|
if globalIsSSL {
|
|
|
|
tlsConfig = &tls.Config{
|
2019-11-13 15:17:45 -05:00
|
|
|
ServerName: endpoint.Hostname(),
|
2018-10-04 20:44:06 -04:00
|
|
|
RootCAs: globalRootCAs,
|
2019-04-18 02:16:27 -04:00
|
|
|
NextProtos: []string{"http/1.1"}, // Force http1.1
|
2018-10-04 20:44:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-17 14:20:56 -04:00
|
|
|
trFn := newCustomHTTPTransport(tlsConfig, rest.DefaultRESTTimeout)
|
2019-11-13 15:17:45 -05:00
|
|
|
restClient, err := rest.NewClient(serverURL, trFn, newAuthToken)
|
2019-02-14 20:53:46 -05:00
|
|
|
if err != nil {
|
2020-04-09 12:30:02 -04:00
|
|
|
logger.LogIf(GlobalContext, err)
|
2019-11-13 15:17:45 -05:00
|
|
|
return &storageRESTClient{endpoint: endpoint, restClient: restClient, connected: 0}
|
2019-02-14 20:53:46 -05:00
|
|
|
}
|
2019-11-13 15:17:45 -05:00
|
|
|
return &storageRESTClient{endpoint: endpoint, restClient: restClient, connected: 1}
|
2018-10-04 20:44:06 -04:00
|
|
|
}
|