@ -17,8 +17,10 @@
package main
package main
import (
import (
"bytes"
"errors"
"errors"
"fmt"
"fmt"
"io"
"net/rpc"
"net/rpc"
"path"
"path"
"strings"
"strings"
@ -162,17 +164,30 @@ func (s *storageServer) ReadAllHandler(args *ReadFileArgs, reply *[]byte) error
}
}
// ReadFileHandler - read file handler is rpc wrapper to read file.
// ReadFileHandler - read file handler is rpc wrapper to read file.
func ( s * storageServer ) ReadFileHandler ( args * ReadFileArgs , reply * int64 ) error {
func ( s * storageServer ) ReadFileHandler ( args * ReadFileArgs , reply * [ ] byte ) ( err error ) {
defer func ( ) {
if r := recover ( ) ; r != nil {
// Recover any panic and return ErrCacheFull.
err = bytes . ErrTooLarge
}
} ( ) // Do not crash the server.
if ! isRPCTokenValid ( args . Token ) {
if ! isRPCTokenValid ( args . Token ) {
return errors . New ( "Invalid token" )
return errors . New ( "Invalid token" )
}
}
n , err := s . storage . ReadFile ( args . Vol , args . Path , args . Offset , args . Buffer )
// Allocate the requested buffer from the client.
if err != nil {
* reply = make ( [ ] byte , args . Size )
var n int64
n , err = s . storage . ReadFile ( args . Vol , args . Path , args . Offset , * reply )
// Sending an error over the rpc layer, would cause unmarshalling to fail. In situations
// when we have short read i.e `io.ErrUnexpectedEOF` treat it as good condition and copy
// the buffer properly.
if err == io . ErrUnexpectedEOF {
// Reset to nil as good condition.
err = nil
}
* reply = ( * reply ) [ 0 : n ]
return err
return err
}
}
* reply = n
return nil
}
// AppendFileHandler - append file handler is rpc wrapper to append file.
// AppendFileHandler - append file handler is rpc wrapper to append file.
func ( s * storageServer ) AppendFileHandler ( args * AppendFileArgs , reply * GenericReply ) error {
func ( s * storageServer ) AppendFileHandler ( args * AppendFileArgs , reply * GenericReply ) error {