2016-04-12 21:45:15 +02: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 23:50:50 +02:00
|
|
|
package cmd
|
2016-04-12 21:45:15 +02:00
|
|
|
|
|
|
|
import (
|
2016-08-12 00:55:36 +02:00
|
|
|
"io"
|
2016-09-01 11:49:06 +02:00
|
|
|
"net"
|
|
|
|
"net/rpc"
|
2016-10-27 12:30:52 +02:00
|
|
|
"net/url"
|
2016-07-31 23:11:14 +02:00
|
|
|
"path"
|
2016-08-26 02:16:34 +02:00
|
|
|
|
|
|
|
"github.com/minio/minio/pkg/disk"
|
2016-04-12 21:45:15 +02:00
|
|
|
)
|
|
|
|
|
2016-05-05 21:51:56 +02:00
|
|
|
type networkStorage struct {
|
2016-08-11 06:09:31 +02:00
|
|
|
netAddr string
|
|
|
|
netPath string
|
2016-08-22 20:01:21 +02:00
|
|
|
rpcClient *AuthRPCClient
|
2016-04-12 21:45:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2016-04-24 09:36:00 +02:00
|
|
|
storageRPCPath = reservedBucket + "/storage"
|
2016-04-12 21:45:15 +02:00
|
|
|
)
|
|
|
|
|
2016-04-16 21:48:41 +02: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-12 04:32:04 +02:00
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2016-09-01 11:49:06 +02:00
|
|
|
|
|
|
|
switch err.(type) {
|
|
|
|
case *net.OpError:
|
|
|
|
return errDiskNotFound
|
|
|
|
}
|
|
|
|
|
2016-04-16 21:48:41 +02:00
|
|
|
switch err.Error() {
|
2016-08-12 00:55:36 +02:00
|
|
|
case io.EOF.Error():
|
|
|
|
return io.EOF
|
|
|
|
case io.ErrUnexpectedEOF.Error():
|
|
|
|
return io.ErrUnexpectedEOF
|
2016-09-01 11:49:06 +02:00
|
|
|
case rpc.ErrShutdown.Error():
|
|
|
|
return errDiskNotFound
|
2016-08-12 00:55:36 +02:00
|
|
|
case errUnexpected.Error():
|
|
|
|
return errUnexpected
|
2016-04-19 11:42:10 +02:00
|
|
|
case errDiskFull.Error():
|
|
|
|
return errDiskFull
|
2016-04-16 21:48:41 +02:00
|
|
|
case errVolumeNotFound.Error():
|
|
|
|
return errVolumeNotFound
|
|
|
|
case errVolumeExists.Error():
|
|
|
|
return errVolumeExists
|
|
|
|
case errFileNotFound.Error():
|
|
|
|
return errFileNotFound
|
2016-08-12 00:55:36 +02:00
|
|
|
case errFileNameTooLong.Error():
|
|
|
|
return errFileNameTooLong
|
|
|
|
case errFileAccessDenied.Error():
|
|
|
|
return errFileAccessDenied
|
2016-04-16 21:48:41 +02:00
|
|
|
case errIsNotRegular.Error():
|
|
|
|
return errIsNotRegular
|
|
|
|
case errVolumeNotEmpty.Error():
|
|
|
|
return errVolumeNotEmpty
|
|
|
|
case errVolumeAccessDenied.Error():
|
|
|
|
return errVolumeAccessDenied
|
2016-08-12 00:55:36 +02:00
|
|
|
case errCorruptedFormat.Error():
|
|
|
|
return errCorruptedFormat
|
|
|
|
case errUnformattedDisk.Error():
|
|
|
|
return errUnformattedDisk
|
2016-10-07 20:15:55 +02:00
|
|
|
case errInvalidAccessKeyID.Error():
|
|
|
|
return errInvalidAccessKeyID
|
|
|
|
case errAuthentication.Error():
|
|
|
|
return errAuthentication
|
|
|
|
case errServerVersionMismatch.Error():
|
|
|
|
return errServerVersionMismatch
|
|
|
|
case errServerTimeMismatch.Error():
|
|
|
|
return errServerTimeMismatch
|
2016-04-16 21:48:41 +02:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-27 12:30:52 +02:00
|
|
|
// Initialize new storage rpc client.
|
|
|
|
func newStorageRPC(ep *url.URL) (StorageAPI, error) {
|
|
|
|
if ep == nil {
|
2016-04-12 21:45:15 +02:00
|
|
|
return nil, errInvalidArgument
|
|
|
|
}
|
|
|
|
|
2016-07-31 23:11:14 +02:00
|
|
|
// Dial minio rpc storage http path.
|
2016-10-27 12:30:52 +02:00
|
|
|
rpcPath := path.Join(storageRPCPath, getPath(ep))
|
|
|
|
rpcAddr := ep.Host
|
2016-10-18 21:49:24 +02:00
|
|
|
|
2016-08-15 09:00:18 +02:00
|
|
|
// Initialize rpc client with network address and rpc path.
|
2016-10-27 12:30:52 +02:00
|
|
|
accessKeyID := serverConfig.GetCredential().AccessKeyID
|
|
|
|
secretAccessKey := serverConfig.GetCredential().SecretAccessKey
|
|
|
|
if ep.User != nil {
|
|
|
|
accessKeyID = ep.User.Username()
|
|
|
|
if key, set := ep.User.Password(); set {
|
|
|
|
secretAccessKey = key
|
|
|
|
}
|
|
|
|
}
|
2016-08-24 19:14:14 +02:00
|
|
|
rpcClient := newAuthClient(&authConfig{
|
2016-10-27 12:30:52 +02:00
|
|
|
accessKey: accessKeyID,
|
|
|
|
secretKey: secretAccessKey,
|
2016-09-30 08:42:37 +02:00
|
|
|
secureConn: isSSL(),
|
2016-08-24 19:14:14 +02:00
|
|
|
address: rpcAddr,
|
|
|
|
path: rpcPath,
|
|
|
|
loginMethod: "Storage.LoginHandler",
|
|
|
|
})
|
2016-10-06 11:30:54 +02:00
|
|
|
|
2016-04-12 21:45:15 +02:00
|
|
|
// Initialize network storage.
|
2016-05-05 21:51:56 +02:00
|
|
|
ndisk := &networkStorage{
|
2016-10-27 12:30:52 +02:00
|
|
|
netAddr: ep.Host,
|
|
|
|
netPath: getPath(ep),
|
2016-08-11 06:09:31 +02:00
|
|
|
rpcClient: rpcClient,
|
2016-04-12 21:45:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns successfully here.
|
|
|
|
return ndisk, nil
|
|
|
|
}
|
|
|
|
|
2016-10-05 21:48:07 +02:00
|
|
|
// Stringer interface compatible representation of network device.
|
|
|
|
func (n networkStorage) String() string {
|
|
|
|
return n.netAddr + ":" + n.netPath
|
|
|
|
}
|
|
|
|
|
2016-08-26 02:16:34 +02:00
|
|
|
// DiskInfo - fetch disk information for a remote disk.
|
|
|
|
func (n networkStorage) DiskInfo() (info disk.Info, err error) {
|
|
|
|
args := GenericArgs{}
|
|
|
|
if err = n.rpcClient.Call("Storage.DiskInfoHandler", &args, &info); err != nil {
|
|
|
|
return disk.Info{}, err
|
|
|
|
}
|
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MakeVol - create a volume on a remote disk.
|
2016-05-05 21:51:56 +02:00
|
|
|
func (n networkStorage) MakeVol(volume string) error {
|
2016-04-12 21:45:15 +02:00
|
|
|
reply := GenericReply{}
|
2016-08-24 04:19:24 +02:00
|
|
|
args := GenericVolArgs{Vol: volume}
|
2016-08-22 20:01:21 +02:00
|
|
|
if err := n.rpcClient.Call("Storage.MakeVolHandler", &args, &reply); err != nil {
|
2016-04-16 21:48:41 +02:00
|
|
|
return toStorageErr(err)
|
2016-04-12 21:45:15 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-26 02:16:34 +02:00
|
|
|
// ListVols - List all volumes on a remote disk.
|
2016-05-05 21:51:56 +02:00
|
|
|
func (n networkStorage) ListVols() (vols []VolInfo, err error) {
|
2016-04-12 21:45:15 +02:00
|
|
|
ListVols := ListVolsReply{}
|
2016-08-24 04:19:24 +02:00
|
|
|
err = n.rpcClient.Call("Storage.ListVolsHandler", &GenericArgs{}, &ListVols)
|
2016-04-12 21:45:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ListVols.Vols, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// StatVol - get current Stat volume info.
|
2016-05-05 21:51:56 +02:00
|
|
|
func (n networkStorage) StatVol(volume string) (volInfo VolInfo, err error) {
|
2016-08-24 04:19:24 +02:00
|
|
|
args := GenericVolArgs{Vol: volume}
|
2016-08-22 20:01:21 +02:00
|
|
|
if err = n.rpcClient.Call("Storage.StatVolHandler", &args, &volInfo); err != nil {
|
2016-04-16 21:48:41 +02:00
|
|
|
return VolInfo{}, toStorageErr(err)
|
2016-04-12 21:45:15 +02:00
|
|
|
}
|
|
|
|
return volInfo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteVol - Delete a volume.
|
2016-05-05 21:51:56 +02:00
|
|
|
func (n networkStorage) DeleteVol(volume string) error {
|
2016-04-12 21:45:15 +02:00
|
|
|
reply := GenericReply{}
|
2016-08-24 04:19:24 +02:00
|
|
|
args := GenericVolArgs{Vol: volume}
|
2016-08-22 20:01:21 +02:00
|
|
|
if err := n.rpcClient.Call("Storage.DeleteVolHandler", &args, &reply); err != nil {
|
2016-04-16 21:48:41 +02:00
|
|
|
return toStorageErr(err)
|
2016-04-12 21:45:15 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// File operations.
|
|
|
|
|
|
|
|
// CreateFile - create file.
|
2016-06-20 00:31:13 +02:00
|
|
|
func (n networkStorage) AppendFile(volume, path string, buffer []byte) (err error) {
|
|
|
|
reply := GenericReply{}
|
2016-08-22 20:01:21 +02:00
|
|
|
if err = n.rpcClient.Call("Storage.AppendFileHandler", &AppendFileArgs{
|
2016-08-24 04:19:24 +02:00
|
|
|
Vol: volume,
|
|
|
|
Path: path,
|
|
|
|
Buffer: buffer,
|
2016-06-20 00:31:13 +02:00
|
|
|
}, &reply); err != nil {
|
|
|
|
return toStorageErr(err)
|
2016-05-29 00:13:15 +02:00
|
|
|
}
|
2016-06-20 00:31:13 +02:00
|
|
|
return nil
|
2016-04-12 21:45:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// StatFile - get latest Stat information for a file at path.
|
2016-05-05 21:51:56 +02:00
|
|
|
func (n networkStorage) StatFile(volume, path string) (fileInfo FileInfo, err error) {
|
2016-08-22 20:01:21 +02:00
|
|
|
if err = n.rpcClient.Call("Storage.StatFileHandler", &StatFileArgs{
|
2016-08-24 04:19:24 +02:00
|
|
|
Vol: volume,
|
|
|
|
Path: path,
|
2016-04-12 21:45:15 +02:00
|
|
|
}, &fileInfo); err != nil {
|
2016-04-16 21:48:41 +02:00
|
|
|
return FileInfo{}, toStorageErr(err)
|
2016-04-12 21:45:15 +02:00
|
|
|
}
|
|
|
|
return fileInfo, nil
|
|
|
|
}
|
|
|
|
|
2016-06-27 07:05:48 +02:00
|
|
|
// ReadAll - reads entire contents of the file at path until EOF, returns the
|
2016-06-25 23:51:06 +02: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.
|
|
|
|
func (n networkStorage) ReadAll(volume, path string) (buf []byte, err error) {
|
2016-08-22 20:01:21 +02:00
|
|
|
if err = n.rpcClient.Call("Storage.ReadAllHandler", &ReadAllArgs{
|
2016-08-24 04:19:24 +02:00
|
|
|
Vol: volume,
|
|
|
|
Path: path,
|
2016-06-25 23:51:06 +02:00
|
|
|
}, &buf); err != nil {
|
|
|
|
return nil, toStorageErr(err)
|
|
|
|
}
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
2016-04-12 21:45:15 +02:00
|
|
|
// ReadFile - reads a file.
|
2016-05-29 00:13:15 +02:00
|
|
|
func (n networkStorage) ReadFile(volume string, path string, offset int64, buffer []byte) (m int64, err error) {
|
2016-08-12 04:32:04 +02:00
|
|
|
var result []byte
|
2016-08-22 20:01:21 +02:00
|
|
|
err = n.rpcClient.Call("Storage.ReadFileHandler", &ReadFileArgs{
|
2016-08-24 04:19:24 +02:00
|
|
|
Vol: volume,
|
|
|
|
Path: path,
|
|
|
|
Offset: offset,
|
|
|
|
Size: len(buffer),
|
2016-08-12 04:32:04 +02:00
|
|
|
}, &result)
|
|
|
|
// Copy results to buffer.
|
|
|
|
copy(buffer, result)
|
|
|
|
// Return length of result, err if any.
|
|
|
|
return int64(len(result)), toStorageErr(err)
|
2016-04-12 21:45:15 +02:00
|
|
|
}
|
|
|
|
|
2016-05-05 21:51:56 +02:00
|
|
|
// ListDir - list all entries at prefix.
|
|
|
|
func (n networkStorage) ListDir(volume, path string) (entries []string, err error) {
|
2016-08-22 20:01:21 +02:00
|
|
|
if err = n.rpcClient.Call("Storage.ListDirHandler", &ListDirArgs{
|
2016-08-24 04:19:24 +02:00
|
|
|
Vol: volume,
|
|
|
|
Path: path,
|
2016-05-05 21:51:56 +02:00
|
|
|
}, &entries); err != nil {
|
|
|
|
return nil, toStorageErr(err)
|
2016-04-12 21:45:15 +02:00
|
|
|
}
|
2016-04-16 21:48:41 +02:00
|
|
|
// Return successfully unmarshalled results.
|
2016-05-05 21:51:56 +02:00
|
|
|
return entries, nil
|
2016-04-12 21:45:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteFile - Delete a file at path.
|
2016-05-05 21:51:56 +02:00
|
|
|
func (n networkStorage) DeleteFile(volume, path string) (err error) {
|
2016-04-12 21:45:15 +02:00
|
|
|
reply := GenericReply{}
|
2016-08-22 20:01:21 +02:00
|
|
|
if err = n.rpcClient.Call("Storage.DeleteFileHandler", &DeleteFileArgs{
|
2016-08-24 04:19:24 +02:00
|
|
|
Vol: volume,
|
|
|
|
Path: path,
|
2016-04-12 21:45:15 +02:00
|
|
|
}, &reply); err != nil {
|
2016-04-16 21:48:41 +02:00
|
|
|
return toStorageErr(err)
|
2016-04-12 21:45:15 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-04-29 21:17:48 +02:00
|
|
|
|
|
|
|
// RenameFile - Rename file.
|
2016-05-05 21:51:56 +02:00
|
|
|
func (n networkStorage) RenameFile(srcVolume, srcPath, dstVolume, dstPath string) (err error) {
|
2016-05-02 12:12:18 +02:00
|
|
|
reply := GenericReply{}
|
2016-08-22 20:01:21 +02:00
|
|
|
if err = n.rpcClient.Call("Storage.RenameFileHandler", &RenameFileArgs{
|
2016-08-24 04:19:24 +02:00
|
|
|
SrcVol: srcVolume,
|
|
|
|
SrcPath: srcPath,
|
|
|
|
DstVol: dstVolume,
|
|
|
|
DstPath: dstPath,
|
2016-05-02 12:12:18 +02:00
|
|
|
}, &reply); err != nil {
|
|
|
|
return toStorageErr(err)
|
|
|
|
}
|
|
|
|
return nil
|
2016-04-29 21:17:48 +02:00
|
|
|
}
|