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.
235 lines
6.3 KiB
235 lines
6.3 KiB
9 years ago
|
/*
|
||
|
* 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.
|
||
|
*/
|
||
|
|
||
8 years ago
|
package cmd
|
||
9 years ago
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
"net/rpc"
|
||
|
"strings"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
9 years ago
|
type networkStorage struct {
|
||
9 years ago
|
netScheme string
|
||
9 years ago
|
netAddr string
|
||
|
netPath string
|
||
|
rpcClient *rpc.Client
|
||
|
httpClient *http.Client
|
||
|
}
|
||
|
|
||
|
const (
|
||
9 years ago
|
storageRPCPath = reservedBucket + "/storage"
|
||
9 years ago
|
)
|
||
|
|
||
|
// splits network path into its components Address and Path.
|
||
|
func splitNetPath(networkPath string) (netAddr, netPath string) {
|
||
|
index := strings.LastIndex(networkPath, ":")
|
||
|
netAddr = networkPath[:index]
|
||
|
netPath = networkPath[index+1:]
|
||
|
return netAddr, netPath
|
||
|
}
|
||
|
|
||
9 years ago
|
// 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 {
|
||
|
switch err.Error() {
|
||
9 years ago
|
case errDiskFull.Error():
|
||
|
return errDiskFull
|
||
9 years ago
|
case errVolumeNotFound.Error():
|
||
|
return errVolumeNotFound
|
||
|
case errVolumeExists.Error():
|
||
|
return errVolumeExists
|
||
|
case errFileNotFound.Error():
|
||
|
return errFileNotFound
|
||
|
case errIsNotRegular.Error():
|
||
|
return errIsNotRegular
|
||
|
case errVolumeNotEmpty.Error():
|
||
|
return errVolumeNotEmpty
|
||
|
case errFileAccessDenied.Error():
|
||
|
return errFileAccessDenied
|
||
|
case errVolumeAccessDenied.Error():
|
||
|
return errVolumeAccessDenied
|
||
|
}
|
||
|
return err
|
||
|
}
|
||
|
|
||
9 years ago
|
// Initialize new rpc client.
|
||
|
func newRPCClient(networkPath string) (StorageAPI, error) {
|
||
9 years ago
|
// Input validation.
|
||
9 years ago
|
if networkPath == "" || strings.LastIndex(networkPath, ":") == -1 {
|
||
9 years ago
|
return nil, errInvalidArgument
|
||
|
}
|
||
|
|
||
|
// TODO validate netAddr and netPath.
|
||
|
netAddr, netPath := splitNetPath(networkPath)
|
||
|
|
||
|
// Dial minio rpc storage http path.
|
||
9 years ago
|
rpcClient, err := rpc.DialHTTPPath("tcp", netAddr, storageRPCPath)
|
||
9 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
// Initialize http client.
|
||
|
httpClient := &http.Client{
|
||
|
// Setting a sensible time out of 6minutes to wait for
|
||
|
// response headers. Request is pro-actively cancelled
|
||
|
// after 6minutes if no response was received from server.
|
||
|
Timeout: 6 * time.Minute,
|
||
|
Transport: http.DefaultTransport,
|
||
|
}
|
||
|
|
||
|
// Initialize network storage.
|
||
9 years ago
|
ndisk := &networkStorage{
|
||
9 years ago
|
netScheme: "http", // TODO: fix for ssl rpc support.
|
||
9 years ago
|
netAddr: netAddr,
|
||
|
netPath: netPath,
|
||
|
rpcClient: rpcClient,
|
||
|
httpClient: httpClient,
|
||
|
}
|
||
|
|
||
|
// Returns successfully here.
|
||
|
return ndisk, nil
|
||
|
}
|
||
|
|
||
|
// MakeVol - make a volume.
|
||
9 years ago
|
func (n networkStorage) MakeVol(volume string) error {
|
||
9 years ago
|
reply := GenericReply{}
|
||
|
if err := n.rpcClient.Call("Storage.MakeVolHandler", volume, &reply); err != nil {
|
||
9 years ago
|
return toStorageErr(err)
|
||
9 years ago
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// ListVols - List all volumes.
|
||
9 years ago
|
func (n networkStorage) ListVols() (vols []VolInfo, err error) {
|
||
9 years ago
|
ListVols := ListVolsReply{}
|
||
|
err = n.rpcClient.Call("Storage.ListVolsHandler", "", &ListVols)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return ListVols.Vols, nil
|
||
|
}
|
||
|
|
||
|
// StatVol - get current Stat volume info.
|
||
9 years ago
|
func (n networkStorage) StatVol(volume string) (volInfo VolInfo, err error) {
|
||
9 years ago
|
if err = n.rpcClient.Call("Storage.StatVolHandler", volume, &volInfo); err != nil {
|
||
9 years ago
|
return VolInfo{}, toStorageErr(err)
|
||
9 years ago
|
}
|
||
|
return volInfo, nil
|
||
|
}
|
||
|
|
||
|
// DeleteVol - Delete a volume.
|
||
9 years ago
|
func (n networkStorage) DeleteVol(volume string) error {
|
||
9 years ago
|
reply := GenericReply{}
|
||
|
if err := n.rpcClient.Call("Storage.DeleteVolHandler", volume, &reply); err != nil {
|
||
9 years ago
|
return toStorageErr(err)
|
||
9 years ago
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// File operations.
|
||
|
|
||
|
// CreateFile - create file.
|
||
9 years ago
|
func (n networkStorage) AppendFile(volume, path string, buffer []byte) (err error) {
|
||
|
reply := GenericReply{}
|
||
9 years ago
|
if err = n.rpcClient.Call("Storage.AppendFileHandler", AppendFileArgs{
|
||
|
Vol: volume,
|
||
|
Path: path,
|
||
|
Buffer: buffer,
|
||
9 years ago
|
}, &reply); err != nil {
|
||
|
return toStorageErr(err)
|
||
9 years ago
|
}
|
||
9 years ago
|
return nil
|
||
9 years ago
|
}
|
||
|
|
||
|
// StatFile - get latest Stat information for a file at path.
|
||
9 years ago
|
func (n networkStorage) StatFile(volume, path string) (fileInfo FileInfo, err error) {
|
||
9 years ago
|
if err = n.rpcClient.Call("Storage.StatFileHandler", StatFileArgs{
|
||
|
Vol: volume,
|
||
|
Path: path,
|
||
|
}, &fileInfo); err != nil {
|
||
9 years ago
|
return FileInfo{}, toStorageErr(err)
|
||
9 years ago
|
}
|
||
|
return fileInfo, nil
|
||
|
}
|
||
|
|
||
9 years ago
|
// ReadAll - reads entire contents of the file at path until EOF, returns the
|
||
9 years ago
|
// 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) {
|
||
|
if err = n.rpcClient.Call("Storage.ReadAllHandler", ReadAllArgs{
|
||
|
Vol: volume,
|
||
|
Path: path,
|
||
|
}, &buf); err != nil {
|
||
|
return nil, toStorageErr(err)
|
||
|
}
|
||
|
return buf, nil
|
||
|
}
|
||
|
|
||
9 years ago
|
// ReadFile - reads a file.
|
||
9 years ago
|
func (n networkStorage) ReadFile(volume string, path string, offset int64, buffer []byte) (m int64, err error) {
|
||
|
if err = n.rpcClient.Call("Storage.ReadFileHandler", ReadFileArgs{
|
||
|
Vol: volume,
|
||
|
Path: path,
|
||
|
Offset: offset,
|
||
|
Buffer: buffer,
|
||
|
}, &m); err != nil {
|
||
|
return 0, toStorageErr(err)
|
||
9 years ago
|
}
|
||
9 years ago
|
return m, nil
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// ListDir - list all entries at prefix.
|
||
|
func (n networkStorage) ListDir(volume, path string) (entries []string, err error) {
|
||
|
if err = n.rpcClient.Call("Storage.ListDirHandler", ListDirArgs{
|
||
|
Vol: volume,
|
||
|
Path: path,
|
||
|
}, &entries); err != nil {
|
||
|
return nil, toStorageErr(err)
|
||
9 years ago
|
}
|
||
9 years ago
|
// Return successfully unmarshalled results.
|
||
9 years ago
|
return entries, nil
|
||
9 years ago
|
}
|
||
|
|
||
|
// DeleteFile - Delete a file at path.
|
||
9 years ago
|
func (n networkStorage) DeleteFile(volume, path string) (err error) {
|
||
9 years ago
|
reply := GenericReply{}
|
||
|
if err = n.rpcClient.Call("Storage.DeleteFileHandler", DeleteFileArgs{
|
||
|
Vol: volume,
|
||
|
Path: path,
|
||
|
}, &reply); err != nil {
|
||
9 years ago
|
return toStorageErr(err)
|
||
9 years ago
|
}
|
||
|
return nil
|
||
|
}
|
||
9 years ago
|
|
||
|
// RenameFile - Rename file.
|
||
9 years ago
|
func (n networkStorage) RenameFile(srcVolume, srcPath, dstVolume, dstPath string) (err error) {
|
||
9 years ago
|
reply := GenericReply{}
|
||
|
if err = n.rpcClient.Call("Storage.RenameFileHandler", RenameFileArgs{
|
||
|
SrcVol: srcVolume,
|
||
|
SrcPath: srcPath,
|
||
|
DstVol: dstVolume,
|
||
|
DstPath: dstPath,
|
||
|
}, &reply); err != nil {
|
||
|
return toStorageErr(err)
|
||
|
}
|
||
|
return nil
|
||
9 years ago
|
}
|