From 60cb509ff9c496aca56aea06158bf5600ea8b57a Mon Sep 17 00:00:00 2001 From: h3yEllex Date: Tue, 24 Oct 2017 12:56:50 +0300 Subject: [PATCH] SplitFile will not create an empty file anymore. Also files created by tests will be removed. --- utils.go | 9 ++++++--- utils_test.go | 1 - 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/utils.go b/utils.go index 9c238365..ccc5c46d 100644 --- a/utils.go +++ b/utils.go @@ -36,7 +36,7 @@ func CatFiles(files []string, outfile string, remove bool) error { return nil } -// SplitFile +// SplitFile creates a bunch of smaller files with the data from source splited into them func SplitFile(fileName string, numPieces int) (err error) { file, err := os.Open(fileName) if err != nil { @@ -62,10 +62,13 @@ func SplitFile(fileName string, numPieces int) (err error) { for { n, err := file.Read(buf) out.Write(buf[:n]) - bytesRead += n - if err == io.EOF { + // If written bytes count is smaller than lenght of buffer + // then we don't create one more empty file + if err == io.EOF || n < len(buf) { break } + bytesRead += n + if bytesRead >= bytesPerPiece { // Close file and open a new one out.Close() diff --git a/utils_test.go b/utils_test.go index 6fb486aa..8dcab25a 100644 --- a/utils_test.go +++ b/utils_test.go @@ -12,5 +12,4 @@ func TestSplitFile(t *testing.T) { } os.Remove("README.md.0") os.Remove("README.md.1") - os.Remove("README.md.2") }