2016-04-12 15:45:15 -04:00
|
|
|
/*
|
|
|
|
* Minio Cloud Storage, (C) 2016 Minio, Inc.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2016-08-18 17:50:50 -04:00
|
|
|
package cmd
|
2016-04-12 15:45:15 -04:00
|
|
|
|
|
|
|
import (
|
2016-12-08 23:35:07 -05:00
|
|
|
"bytes"
|
2018-06-06 04:51:56 -04:00
|
|
|
"crypto/tls"
|
2016-08-11 18:55:36 -04:00
|
|
|
"io"
|
2016-09-01 05:49:06 -04:00
|
|
|
"net"
|
2018-06-06 04:51:56 -04:00
|
|
|
"net/url"
|
2016-07-31 17:11:14 -04:00
|
|
|
"path"
|
2017-02-15 06:47:47 -05:00
|
|
|
"strings"
|
2016-04-12 15:45:15 -04:00
|
|
|
|
2018-06-06 04:51:56 -04:00
|
|
|
"github.com/minio/minio/cmd/logger"
|
|
|
|
xnet "github.com/minio/minio/pkg/net"
|
2016-04-12 15:45:15 -04:00
|
|
|
)
|
|
|
|
|
2018-06-06 04:51:56 -04:00
|
|
|
func isNetworkDisconnectError(err error) bool {
|
2018-02-15 20:45:57 -05:00
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
}
|
2018-06-06 04:51:56 -04:00
|
|
|
|
|
|
|
if uerr, isURLError := err.(*url.Error); isURLError {
|
|
|
|
if uerr.Timeout() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
err = uerr.Err
|
2018-02-15 20:45:57 -05:00
|
|
|
}
|
2018-06-06 04:51:56 -04:00
|
|
|
|
|
|
|
_, isNetOpError := err.(*net.OpError)
|
|
|
|
return isNetOpError
|
2018-02-15 20:45:57 -05:00
|
|
|
}
|
|
|
|
|
2016-04-16 15:48:41 -04:00
|
|
|
// Converts rpc.ServerError to underlying error. This function is
|
|
|
|
// written so that the storageAPI errors are consistent across network
|
|
|
|
// disks as well.
|
|
|
|
func toStorageErr(err error) error {
|
2016-08-11 22:32:04 -04:00
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2016-09-01 05:49:06 -04:00
|
|
|
|
2018-06-06 04:51:56 -04:00
|
|
|
if isNetworkDisconnectError(err) {
|
2018-02-15 20:45:57 -05:00
|
|
|
return errDiskNotFound
|
2016-12-29 06:13:51 -05:00
|
|
|
}
|
|
|
|
|
2016-04-16 15:48:41 -04:00
|
|
|
switch err.Error() {
|
2016-08-11 18:55:36 -04:00
|
|
|
case io.EOF.Error():
|
|
|
|
return io.EOF
|
|
|
|
case io.ErrUnexpectedEOF.Error():
|
|
|
|
return io.ErrUnexpectedEOF
|
|
|
|
case errUnexpected.Error():
|
|
|
|
return errUnexpected
|
2016-04-19 05:42:10 -04:00
|
|
|
case errDiskFull.Error():
|
|
|
|
return errDiskFull
|
2016-04-16 15:48:41 -04:00
|
|
|
case errVolumeNotFound.Error():
|
|
|
|
return errVolumeNotFound
|
|
|
|
case errVolumeExists.Error():
|
|
|
|
return errVolumeExists
|
|
|
|
case errFileNotFound.Error():
|
|
|
|
return errFileNotFound
|
2016-08-11 18:55:36 -04:00
|
|
|
case errFileNameTooLong.Error():
|
|
|
|
return errFileNameTooLong
|
|
|
|
case errFileAccessDenied.Error():
|
|
|
|
return errFileAccessDenied
|
2016-04-16 15:48:41 -04:00
|
|
|
case errIsNotRegular.Error():
|
|
|
|
return errIsNotRegular
|
|
|
|
case errVolumeNotEmpty.Error():
|
|
|
|
return errVolumeNotEmpty
|
|
|
|
case errVolumeAccessDenied.Error():
|
|
|
|
return errVolumeAccessDenied
|
2016-08-11 18:55:36 -04:00
|
|
|
case errCorruptedFormat.Error():
|
|
|
|
return errCorruptedFormat
|
|
|
|
case errUnformattedDisk.Error():
|
|
|
|
return errUnformattedDisk
|
2016-10-07 14:15:55 -04:00
|
|
|
case errInvalidAccessKeyID.Error():
|
|
|
|
return errInvalidAccessKeyID
|
|
|
|
case errAuthentication.Error():
|
|
|
|
return errAuthentication
|
2018-02-06 18:07:17 -05:00
|
|
|
case errRPCAPIVersionUnsupported.Error():
|
|
|
|
return errRPCAPIVersionUnsupported
|
2016-10-07 14:15:55 -04:00
|
|
|
case errServerTimeMismatch.Error():
|
|
|
|
return errServerTimeMismatch
|
2016-04-16 15:48:41 -04:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-06-06 04:51:56 -04:00
|
|
|
// StorageRPCClient - storage RPC client.
|
|
|
|
type StorageRPCClient struct {
|
|
|
|
*RPCClient
|
|
|
|
connected bool
|
2018-09-10 19:21:59 -04:00
|
|
|
// Plain error of the last RPC call
|
|
|
|
lastRPCError error
|
2016-04-12 15:45:15 -04:00
|
|
|
}
|
|
|
|
|
2017-04-18 13:35:17 -04:00
|
|
|
// Stringer provides a canonicalized representation of network device.
|
2018-06-06 04:51:56 -04:00
|
|
|
func (client *StorageRPCClient) String() string {
|
|
|
|
url := client.ServiceURL()
|
|
|
|
// Remove the storage RPC path prefix, internal paths are meaningless. why?
|
|
|
|
url.Path = strings.TrimPrefix(url.Path, storageServicePath)
|
|
|
|
return url.String()
|
2016-10-05 15:48:07 -04:00
|
|
|
}
|
|
|
|
|
2018-09-10 19:21:59 -04:00
|
|
|
// LastError - returns the last RPC call result, nil or error if any
|
|
|
|
func (client *StorageRPCClient) LastError() error {
|
|
|
|
return client.lastRPCError
|
|
|
|
}
|
|
|
|
|
2018-06-06 04:51:56 -04:00
|
|
|
// Close - closes underneath RPC client.
|
|
|
|
func (client *StorageRPCClient) Close() error {
|
|
|
|
client.connected = false
|
|
|
|
return toStorageErr(client.RPCClient.Close())
|
2016-11-23 18:48:10 -05:00
|
|
|
}
|
|
|
|
|
2018-06-06 04:51:56 -04:00
|
|
|
// IsOnline - returns whether RPC client failed to connect or not.
|
|
|
|
func (client *StorageRPCClient) IsOnline() bool {
|
|
|
|
return client.connected
|
2018-02-15 20:45:57 -05:00
|
|
|
}
|
|
|
|
|
2018-09-10 19:21:59 -04:00
|
|
|
func (client *StorageRPCClient) connect() {
|
|
|
|
err := client.Call(storageServiceName+".Connect", &AuthArgs{}, &VoidReply{})
|
|
|
|
client.lastRPCError = err
|
|
|
|
client.connected = err == nil
|
|
|
|
}
|
|
|
|
|
2018-06-06 04:51:56 -04:00
|
|
|
func (client *StorageRPCClient) call(handler string, args interface {
|
|
|
|
SetAuthArgs(args AuthArgs)
|
2018-02-15 20:45:57 -05:00
|
|
|
}, reply interface{}) error {
|
2018-09-10 19:21:59 -04:00
|
|
|
|
2018-06-06 04:51:56 -04:00
|
|
|
if !client.connected {
|
2018-02-15 20:45:57 -05:00
|
|
|
return errDiskNotFound
|
|
|
|
}
|
2018-06-06 04:51:56 -04:00
|
|
|
|
|
|
|
err := client.Call(handler, args, reply)
|
2018-09-10 19:21:59 -04:00
|
|
|
client.lastRPCError = err
|
2018-06-06 04:51:56 -04:00
|
|
|
if err == nil {
|
|
|
|
return nil
|
2018-02-15 20:45:57 -05:00
|
|
|
}
|
2018-06-06 04:51:56 -04:00
|
|
|
|
|
|
|
if isNetworkDisconnectError(err) {
|
|
|
|
client.connected = false
|
|
|
|
}
|
|
|
|
|
|
|
|
return toStorageErr(err)
|
2018-02-15 20:45:57 -05:00
|
|
|
}
|
|
|
|
|
2016-08-25 20:16:34 -04:00
|
|
|
// DiskInfo - fetch disk information for a remote disk.
|
2018-06-06 04:51:56 -04:00
|
|
|
func (client *StorageRPCClient) DiskInfo() (info DiskInfo, err error) {
|
|
|
|
err = client.call(storageServiceName+".DiskInfo", &AuthArgs{}, &info)
|
|
|
|
return info, err
|
2016-08-25 20:16:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// MakeVol - create a volume on a remote disk.
|
2018-06-06 04:51:56 -04:00
|
|
|
func (client *StorageRPCClient) MakeVol(volume string) (err error) {
|
|
|
|
return client.call(storageServiceName+".MakeVol", &VolArgs{Vol: volume}, &VoidReply{})
|
2016-04-12 15:45:15 -04:00
|
|
|
}
|
|
|
|
|
2016-08-25 20:16:34 -04:00
|
|
|
// ListVols - List all volumes on a remote disk.
|
2018-06-06 04:51:56 -04:00
|
|
|
func (client *StorageRPCClient) ListVols() ([]VolInfo, error) {
|
|
|
|
var reply []VolInfo
|
|
|
|
err := client.call(storageServiceName+".ListVols", &AuthArgs{}, &reply)
|
|
|
|
return reply, err
|
2016-04-12 15:45:15 -04:00
|
|
|
}
|
|
|
|
|
2016-11-23 18:48:10 -05:00
|
|
|
// StatVol - get volume info over the network.
|
2018-06-06 04:51:56 -04:00
|
|
|
func (client *StorageRPCClient) StatVol(volume string) (volInfo VolInfo, err error) {
|
|
|
|
err = client.call(storageServiceName+".StatVol", &VolArgs{Vol: volume}, &volInfo)
|
|
|
|
return volInfo, err
|
2016-04-12 15:45:15 -04:00
|
|
|
}
|
|
|
|
|
2016-11-23 18:48:10 -05:00
|
|
|
// DeleteVol - Deletes a volume over the network.
|
2018-06-06 04:51:56 -04:00
|
|
|
func (client *StorageRPCClient) DeleteVol(volume string) (err error) {
|
|
|
|
return client.call(storageServiceName+".DeleteVol", &VolArgs{Vol: volume}, &VoidReply{})
|
2016-04-12 15:45:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// File operations.
|
|
|
|
|
2018-06-06 04:51:56 -04:00
|
|
|
// PrepareFile - calls PrepareFile RPC.
|
|
|
|
func (client *StorageRPCClient) PrepareFile(volume, path string, length int64) (err error) {
|
|
|
|
args := PrepareFileArgs{
|
2016-10-29 15:44:44 -04:00
|
|
|
Vol: volume,
|
|
|
|
Path: path,
|
|
|
|
Size: length,
|
2018-06-06 04:51:56 -04:00
|
|
|
}
|
|
|
|
reply := VoidReply{}
|
|
|
|
|
|
|
|
return client.call(storageServiceName+".PrepareFile", &args, &reply)
|
2016-10-29 15:44:44 -04:00
|
|
|
}
|
|
|
|
|
2016-11-23 18:48:10 -05:00
|
|
|
// AppendFile - append file writes buffer to a remote network path.
|
2018-06-06 04:51:56 -04:00
|
|
|
func (client *StorageRPCClient) AppendFile(volume, path string, buffer []byte) (err error) {
|
|
|
|
args := AppendFileArgs{
|
2016-08-23 22:19:24 -04:00
|
|
|
Vol: volume,
|
|
|
|
Path: path,
|
|
|
|
Buffer: buffer,
|
2018-06-06 04:51:56 -04:00
|
|
|
}
|
|
|
|
reply := VoidReply{}
|
|
|
|
|
|
|
|
return client.call(storageServiceName+".AppendFile", &args, &reply)
|
2016-04-12 15:45:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// StatFile - get latest Stat information for a file at path.
|
2018-06-06 04:51:56 -04:00
|
|
|
func (client *StorageRPCClient) StatFile(volume, path string) (fileInfo FileInfo, err error) {
|
|
|
|
err = client.call(storageServiceName+".StatFile", &StatFileArgs{Vol: volume, Path: path}, &fileInfo)
|
|
|
|
return fileInfo, err
|
2016-04-12 15:45:15 -04:00
|
|
|
}
|
|
|
|
|
2016-06-27 01:05:48 -04:00
|
|
|
// ReadAll - reads entire contents of the file at path until EOF, returns the
|
2016-06-25 17:51:06 -04:00
|
|
|
// contents in a byte slice. Returns buf == nil if err != nil.
|
|
|
|
// This API is meant to be used on files which have small memory footprint, do
|
|
|
|
// not use this on large files as it would cause server to crash.
|
2018-06-06 04:51:56 -04:00
|
|
|
func (client *StorageRPCClient) ReadAll(volume, path string) (buf []byte, err error) {
|
|
|
|
err = client.call(storageServiceName+".ReadAll", &ReadAllArgs{Vol: volume, Path: path}, &buf)
|
|
|
|
return buf, err
|
2016-06-25 17:51:06 -04:00
|
|
|
}
|
|
|
|
|
2016-11-23 18:48:10 -05:00
|
|
|
// ReadFile - reads a file at remote path and fills the buffer.
|
2018-06-06 04:51:56 -04:00
|
|
|
func (client *StorageRPCClient) ReadFile(volume string, path string, offset int64, buffer []byte, verifier *BitrotVerifier) (m int64, err error) {
|
|
|
|
// Recover from any panic and return error.
|
2016-12-08 23:35:07 -05:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
err = bytes.ErrTooLarge
|
|
|
|
}
|
2018-06-06 04:51:56 -04:00
|
|
|
}()
|
2016-12-08 23:35:07 -05:00
|
|
|
|
2017-09-25 14:32:56 -04:00
|
|
|
args := ReadFileArgs{
|
|
|
|
Vol: volume,
|
|
|
|
Path: path,
|
|
|
|
Offset: offset,
|
2018-09-05 02:22:05 -04:00
|
|
|
Length: int64(len(buffer)),
|
2017-09-25 14:32:56 -04:00
|
|
|
Verified: true, // mark read as verified by default
|
|
|
|
}
|
|
|
|
if verifier != nil {
|
|
|
|
args.Algo = verifier.algorithm
|
|
|
|
args.ExpectedHash = verifier.sum
|
|
|
|
}
|
2018-06-06 04:51:56 -04:00
|
|
|
var reply []byte
|
2017-05-16 17:21:52 -04:00
|
|
|
|
2018-06-06 04:51:56 -04:00
|
|
|
err = client.call(storageServiceName+".ReadFile", &args, &reply)
|
2017-05-16 17:21:52 -04:00
|
|
|
|
2018-06-06 04:51:56 -04:00
|
|
|
// Copy reply to buffer.
|
|
|
|
copy(buffer, reply)
|
2017-05-16 17:21:52 -04:00
|
|
|
|
|
|
|
// Return length of result, err if any.
|
2018-06-06 04:51:56 -04:00
|
|
|
return int64(len(reply)), err
|
2017-05-16 17:21:52 -04:00
|
|
|
}
|
|
|
|
|
2016-05-05 15:51:56 -04:00
|
|
|
// ListDir - list all entries at prefix.
|
2018-06-06 04:51:56 -04:00
|
|
|
func (client *StorageRPCClient) ListDir(volume, path string, count int) (entries []string, err error) {
|
|
|
|
err = client.call(storageServiceName+".ListDir", &ListDirArgs{Vol: volume, Path: path, Count: count}, &entries)
|
|
|
|
return entries, err
|
2016-04-12 15:45:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteFile - Delete a file at path.
|
2018-06-06 04:51:56 -04:00
|
|
|
func (client *StorageRPCClient) DeleteFile(volume, path string) (err error) {
|
|
|
|
args := DeleteFileArgs{
|
2016-08-23 22:19:24 -04:00
|
|
|
Vol: volume,
|
|
|
|
Path: path,
|
2018-06-06 04:51:56 -04:00
|
|
|
}
|
|
|
|
reply := VoidReply{}
|
|
|
|
|
|
|
|
return client.call(storageServiceName+".DeleteFile", &args, &reply)
|
2016-04-12 15:45:15 -04:00
|
|
|
}
|
2016-04-29 15:17:48 -04:00
|
|
|
|
2016-11-23 18:48:10 -05:00
|
|
|
// RenameFile - rename a remote file from source to destination.
|
2018-06-06 04:51:56 -04:00
|
|
|
func (client *StorageRPCClient) RenameFile(srcVolume, srcPath, dstVolume, dstPath string) (err error) {
|
|
|
|
args := RenameFileArgs{
|
2016-08-23 22:19:24 -04:00
|
|
|
SrcVol: srcVolume,
|
|
|
|
SrcPath: srcPath,
|
|
|
|
DstVol: dstVolume,
|
|
|
|
DstPath: dstPath,
|
2018-06-06 04:51:56 -04:00
|
|
|
}
|
|
|
|
reply := VoidReply{}
|
|
|
|
|
|
|
|
return client.call(storageServiceName+".RenameFile", &args, &reply)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewStorageRPCClient - returns new storage RPC client.
|
|
|
|
func NewStorageRPCClient(host *xnet.Host, endpointPath string) (*StorageRPCClient, error) {
|
|
|
|
scheme := "http"
|
|
|
|
if globalIsSSL {
|
|
|
|
scheme = "https"
|
|
|
|
}
|
|
|
|
|
|
|
|
serviceURL := &xnet.URL{
|
|
|
|
Scheme: scheme,
|
|
|
|
Host: host.String(),
|
|
|
|
Path: path.Join(storageServicePath, endpointPath),
|
|
|
|
}
|
|
|
|
|
|
|
|
var tlsConfig *tls.Config
|
|
|
|
if globalIsSSL {
|
|
|
|
tlsConfig = &tls.Config{
|
|
|
|
ServerName: host.Name,
|
|
|
|
RootCAs: globalRootCAs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rpcClient, err := NewRPCClient(
|
|
|
|
RPCClientArgs{
|
|
|
|
NewAuthTokenFunc: newAuthToken,
|
|
|
|
RPCVersion: globalRPCAPIVersion,
|
|
|
|
ServiceName: storageServiceName,
|
|
|
|
ServiceURL: serviceURL,
|
|
|
|
TLSConfig: tlsConfig,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &StorageRPCClient{RPCClient: rpcClient}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize new storage rpc client.
|
|
|
|
func newStorageRPC(endpoint Endpoint) *StorageRPCClient {
|
|
|
|
host, err := xnet.ParseHost(endpoint.Host)
|
2018-09-21 19:04:11 -04:00
|
|
|
logger.FatalIf(err, "Unable to parse storage RPC Host")
|
2018-06-06 04:51:56 -04:00
|
|
|
rpcClient, err := NewStorageRPCClient(host, endpoint.Path)
|
2018-09-21 19:04:11 -04:00
|
|
|
logger.FatalIf(err, "Unable to initialize storage RPC client")
|
2018-09-10 19:21:59 -04:00
|
|
|
// Attempt first try connection and save error if any.
|
|
|
|
rpcClient.connect()
|
2018-06-06 04:51:56 -04:00
|
|
|
return rpcClient
|
2016-04-29 15:17:48 -04:00
|
|
|
}
|