fixes based on comments + minor test added
diff --git a/client.go b/client.go
index b6b7201..0d09d2a 100644
--- a/client.go
+++ b/client.go
@@ -51,16 +51,23 @@
}
}
-// UseFstat sets whether to use Fstat or Stat when File.WriteTo is called (usually when copying files).
-// Some servers limit the amount of open files and calling Stat after opening the file will throw an error
-// From the server. Setting this flag will call Fstat instead of Stat which is suppose to be called on an
-// open file handle.
+// UseFstat sets whether to use Fstat or Stat when File.WriteTo is called
+// (usually when copying files).
+// Some servers limit the amount of open files and calling Stat after opening
+// the file will throw an error From the server. Setting this flag will call
+// Fstat instead of Stat which is suppose to be called on an open file handle.
//
-// From our experience it happens often with IBM Sterling SFTP servers that have "extractability" level
-// set to 1 which means only 1 file can be open at any given time.
+// It has been found that that with IBM Sterling SFTP servers which have
+// "extractability" level set to 1 which means only 1 file can be opened at
+// any given time.
//
-// If the server you are working with still has an issue with both Stat and Fstat calls you can always
-// open a file and just loop through multiple Read calls until the file is completely read.
+// If the server you are working with still has an issue with both Stat and
+// Fstat calls you can always open a file and read it until the end.
+//
+// Another reason to read the file until its end and Fstat doesn't work is
+// that in some servers, reading a full file will automatically delete the
+// file as some of these mainframes map the file to a message in a queue.
+// Once the file has been read it will get deleted.
func UseFstat(value bool) ClientOption {
return func(c *Client) error {
c.useFstat = value
@@ -931,18 +938,17 @@
// maximise throughput for transferring the entire file (especially
// over high latency links).
func (f *File) WriteTo(w io.Writer) (int64, error) {
- var err error
- fileSize := uint64(0)
+ var fileSize uint64
if f.c.useFstat {
- var fileStat *FileStat
- if fileStat, err = f.c.fstat(f.handle); err != nil {
+ fileStat, err := f.c.fstat(f.handle)
+ if err != nil {
return 0, err
}
- fileSize = uint64(fileStat.Size)
+ fileSize = fileStat.Size
} else {
- var fi os.FileInfo
- if fi, err = f.c.Stat(f.path); err != nil {
+ fi, err := f.c.Stat(f.path)
+ if err != nil {
return 0, err
}
fileSize = uint64(fi.Size())
diff --git a/client_test.go b/client_test.go
index c0d620d..05107c5 100644
--- a/client_test.go
+++ b/client_test.go
@@ -194,3 +194,17 @@
t.Errorf("MaxPacketChecked(%v)\n- want: %v\n- got: %v", tt.size, tt.size, c.maxPacket)
}
}
+
+func testFstatOption(t *testing.T, o ClientOption, value bool) {
+ var c Client
+
+ err := o(&c)
+ if err == nil && c.useFstat != value {
+ t.Errorf("UseFStat(%v)\n- want: %v\n- got: %v", value, value, c.useFstat)
+ }
+}
+
+func TestUseFstatChecked(t *testing.T) {
+ testFstatOption(t, UseFstat(true), true)
+ testFstatOption(t, UseFstat(false), false)
+}