2016-04-08 13:37:38 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/rpc"
|
|
|
|
"strconv"
|
|
|
|
|
2016-04-25 02:12:54 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2016-04-08 13:37:38 -04:00
|
|
|
router "github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Storage server implements rpc primitives to facilitate exporting a
|
|
|
|
// disk over a network.
|
|
|
|
type storageServer struct {
|
|
|
|
storage StorageAPI
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Volume operations handlers
|
|
|
|
|
|
|
|
// MakeVolHandler - make vol handler is rpc wrapper for MakeVol operation.
|
|
|
|
func (s *storageServer) MakeVolHandler(arg *string, reply *GenericReply) error {
|
2016-04-24 03:36:00 -04:00
|
|
|
err := s.storage.MakeVol(*arg)
|
|
|
|
if err != nil {
|
2016-04-25 02:12:54 -04:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": *arg,
|
|
|
|
}).Debugf("MakeVol failed with error %s", err)
|
2016-04-24 03:36:00 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListVolsHandler - list vols handler is rpc wrapper for ListVols operation.
|
|
|
|
func (s *storageServer) ListVolsHandler(arg *string, reply *ListVolsReply) error {
|
|
|
|
vols, err := s.storage.ListVols()
|
|
|
|
if err != nil {
|
2016-04-24 03:36:00 -04:00
|
|
|
log.Debugf("Listsvols failed with error %s", err)
|
2016-04-08 13:37:38 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
reply.Vols = vols
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// StatVolHandler - stat vol handler is a rpc wrapper for StatVol operation.
|
|
|
|
func (s *storageServer) StatVolHandler(arg *string, reply *VolInfo) error {
|
|
|
|
volInfo, err := s.storage.StatVol(*arg)
|
|
|
|
if err != nil {
|
2016-04-25 02:12:54 -04:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": *arg,
|
|
|
|
}).Debugf("StatVol failed with error %s", err)
|
2016-04-08 13:37:38 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
*reply = volInfo
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteVolHandler - delete vol handler is a rpc wrapper for
|
|
|
|
// DeleteVol operation.
|
|
|
|
func (s *storageServer) DeleteVolHandler(arg *string, reply *GenericReply) error {
|
2016-04-24 03:36:00 -04:00
|
|
|
err := s.storage.DeleteVol(*arg)
|
|
|
|
if err != nil {
|
2016-04-25 02:12:54 -04:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": *arg,
|
|
|
|
}).Debugf("DeleteVol failed with error %s", err)
|
2016-04-24 03:36:00 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// File operations
|
|
|
|
|
|
|
|
// ListFilesHandler - list files handler.
|
|
|
|
func (s *storageServer) ListFilesHandler(arg *ListFilesArgs, reply *ListFilesReply) error {
|
|
|
|
files, eof, err := s.storage.ListFiles(arg.Vol, arg.Prefix, arg.Marker, arg.Recursive, arg.Count)
|
|
|
|
if err != nil {
|
2016-04-25 02:12:54 -04:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": arg.Vol,
|
|
|
|
"prefix": arg.Prefix,
|
|
|
|
"marker": arg.Marker,
|
|
|
|
"recursive": arg.Recursive,
|
|
|
|
"count": arg.Count,
|
|
|
|
}).Debugf("ListFiles failed with error %s", err)
|
2016-04-08 13:37:38 -04:00
|
|
|
return err
|
|
|
|
}
|
2016-04-16 15:48:41 -04:00
|
|
|
|
|
|
|
// Fill reply structure.
|
2016-04-08 13:37:38 -04:00
|
|
|
reply.Files = files
|
|
|
|
reply.EOF = eof
|
2016-04-16 15:48:41 -04:00
|
|
|
|
|
|
|
// Return success.
|
2016-04-08 13:37:38 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// StatFileHandler - stat file handler is rpc wrapper to stat file.
|
|
|
|
func (s *storageServer) StatFileHandler(arg *StatFileArgs, reply *FileInfo) error {
|
|
|
|
fileInfo, err := s.storage.StatFile(arg.Vol, arg.Path)
|
|
|
|
if err != nil {
|
2016-04-25 02:12:54 -04:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": arg.Vol,
|
|
|
|
"path": arg.Path,
|
|
|
|
}).Debugf("StatFile failed with error %s", err)
|
2016-04-08 13:37:38 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
*reply = fileInfo
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteFileHandler - delete file handler is rpc wrapper to delete file.
|
|
|
|
func (s *storageServer) DeleteFileHandler(arg *DeleteFileArgs, reply *GenericReply) error {
|
2016-04-24 03:36:00 -04:00
|
|
|
err := s.storage.DeleteFile(arg.Vol, arg.Path)
|
|
|
|
if err != nil {
|
2016-04-25 02:12:54 -04:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": arg.Vol,
|
|
|
|
"path": arg.Path,
|
|
|
|
}).Debugf("DeleteFile failed with error %s", err)
|
2016-04-24 03:36:00 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|
|
|
|
|
2016-04-19 23:05:38 -04:00
|
|
|
// Initialize new storage rpc.
|
2016-04-29 17:24:10 -04:00
|
|
|
func newRPCServer(exportPath string) (*storageServer, error) {
|
|
|
|
// Initialize posix storage API.
|
|
|
|
storage, err := newPosix(exportPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|
2016-04-29 17:24:10 -04:00
|
|
|
return &storageServer{
|
|
|
|
storage: storage,
|
|
|
|
}, nil
|
2016-04-19 23:05:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// registerStorageRPCRouter - register storage rpc router.
|
|
|
|
func registerStorageRPCRouter(mux *router.Router, stServer *storageServer) {
|
2016-04-12 15:45:15 -04:00
|
|
|
storageRPCServer := rpc.NewServer()
|
2016-04-08 13:37:38 -04:00
|
|
|
storageRPCServer.RegisterName("Storage", stServer)
|
|
|
|
storageRouter := mux.NewRoute().PathPrefix(reservedBucket).Subrouter()
|
2016-04-12 15:45:15 -04:00
|
|
|
// Add minio storage routes.
|
2016-04-24 03:36:00 -04:00
|
|
|
storageRouter.Path("/storage").Handler(storageRPCServer)
|
2016-04-12 15:45:15 -04:00
|
|
|
// StreamUpload - stream upload handler.
|
2016-04-24 03:36:00 -04:00
|
|
|
storageRouter.Methods("POST").Path("/storage/upload/{volume}/{path:.+}").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2016-04-12 15:45:15 -04:00
|
|
|
vars := router.Vars(r)
|
|
|
|
volume := vars["volume"]
|
|
|
|
path := vars["path"]
|
|
|
|
writeCloser, err := stServer.storage.CreateFile(volume, path)
|
|
|
|
if err != nil {
|
2016-04-25 02:12:54 -04:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
|
|
|
"path": path,
|
|
|
|
}).Debugf("CreateFile failed with error %s", err)
|
2016-04-16 15:48:41 -04:00
|
|
|
httpErr := http.StatusInternalServerError
|
|
|
|
if err == errVolumeNotFound {
|
|
|
|
httpErr = http.StatusNotFound
|
|
|
|
} else if err == errIsNotRegular {
|
|
|
|
httpErr = http.StatusConflict
|
|
|
|
}
|
|
|
|
http.Error(w, err.Error(), httpErr)
|
2016-04-12 15:45:15 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
reader := r.Body
|
|
|
|
if _, err = io.Copy(writeCloser, reader); err != nil {
|
2016-04-25 02:12:54 -04:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
|
|
|
"path": path,
|
|
|
|
}).Debugf("Copying incoming reader to writer failed %s", err)
|
2016-04-16 15:48:41 -04:00
|
|
|
safeCloseAndRemove(writeCloser)
|
2016-04-12 15:45:15 -04:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
writeCloser.Close()
|
|
|
|
reader.Close()
|
|
|
|
})
|
|
|
|
// StreamDownloadHandler - stream download handler.
|
2016-04-24 03:36:00 -04:00
|
|
|
storageRouter.Methods("GET").Path("/storage/download/{volume}/{path:.+}").Queries("offset", "{offset:.*}").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2016-04-12 15:45:15 -04:00
|
|
|
vars := router.Vars(r)
|
|
|
|
volume := vars["volume"]
|
|
|
|
path := vars["path"]
|
|
|
|
offset, err := strconv.ParseInt(r.URL.Query().Get("offset"), 10, 64)
|
|
|
|
if err != nil {
|
2016-04-25 02:12:54 -04:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
|
|
|
"path": path,
|
|
|
|
}).Debugf("Parse offset failure with error %s", err)
|
2016-04-12 15:45:15 -04:00
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
readCloser, err := stServer.storage.ReadFile(volume, path, offset)
|
|
|
|
if err != nil {
|
2016-04-25 02:12:54 -04:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
|
|
|
"path": path,
|
|
|
|
}).Debugf("ReadFile failed with error %s", err)
|
2016-04-12 15:45:15 -04:00
|
|
|
httpErr := http.StatusBadRequest
|
2016-04-16 15:48:41 -04:00
|
|
|
if err == errVolumeNotFound {
|
|
|
|
httpErr = http.StatusNotFound
|
|
|
|
} else if err == errFileNotFound {
|
2016-04-12 15:45:15 -04:00
|
|
|
httpErr = http.StatusNotFound
|
|
|
|
}
|
|
|
|
http.Error(w, err.Error(), httpErr)
|
|
|
|
return
|
|
|
|
}
|
2016-04-16 15:48:41 -04:00
|
|
|
|
|
|
|
// Copy reader to writer.
|
2016-04-12 15:45:15 -04:00
|
|
|
io.Copy(w, readCloser)
|
2016-04-16 15:48:41 -04:00
|
|
|
|
|
|
|
// Flush out any remaining buffers to client.
|
2016-04-12 15:45:15 -04:00
|
|
|
w.(http.Flusher).Flush()
|
2016-04-16 15:48:41 -04:00
|
|
|
|
|
|
|
// Close the reader.
|
2016-04-12 15:45:15 -04:00
|
|
|
readCloser.Close()
|
|
|
|
})
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|