Skip to content

Commit

Permalink
Refactor log archiving part to mutualize code between compression types
Browse files Browse the repository at this point in the history
  • Loading branch information
mlallaouret committed Apr 18, 2016
1 parent 624f4ec commit ec7e252
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 155 deletions.
2 changes: 1 addition & 1 deletion cfg_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ func getParserTests() []parserTest {
testExpected = new(configForParsing)
testExpected.Constraints, _ = NewMinMaxConstraints(TraceLvl, CriticalLvl)
testExpected.Exceptions = nil
testrollingFileWriter, _ = NewRollingFileWriterSize(testLogFileName, rollingArchiveZip, "old", 100, 5, rollingNameModePostfix , true)
testrollingFileWriter, _ = NewRollingFileWriterSize(testLogFileName, rollingArchiveZip, "old", 100, 5, rollingNameModePostfix, true)
testHeadSplitter, _ = NewSplitDispatcher(DefaultFormatter, []interface{}{testrollingFileWriter})
testExpected.LogType = syncloggerTypeFromString
testExpected.RootDispatcher = testHeadSplitter
Expand Down
21 changes: 13 additions & 8 deletions internals_fsutils.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package seelog

import (
"archive/tar"
"archive/zip"
"bytes"
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"sync"
"time"
"compress/gzip"
"archive/tar"
)

// File and directory permitions.
Expand Down Expand Up @@ -420,9 +420,9 @@ func createTar(files map[string][]byte) ([]byte, error) {
for fpath, fcont := range files {

header := &tar.Header{
Name: fpath,
Size: int64(len(fcont)),
Mode: defaultFilePermissions,
Name: fpath,
Size: int64(len(fcont)),
Mode: defaultFilePermissions,
ModTime: time.Now(),
}

Expand Down Expand Up @@ -459,7 +459,7 @@ func unTar(data []byte) (map[string][]byte, error) {
continue
}
buffer := new(bytes.Buffer)
_, err =io.Copy(buffer, tarReader)
_, err = io.Copy(buffer, tarReader)
files[header.Name] = buffer.Bytes()
if err != nil {
return nil, err
Expand Down Expand Up @@ -509,12 +509,17 @@ func unGzip(filename string) ([]byte, error) {
for {
byteRead, err = reader.Read(byteBuffer)
if err == io.EOF {
break;
break
}
content.Write(byteBuffer[0:byteRead])

}
reader.Close()
return content.Bytes(), nil

}
}

func isTar(data []byte) bool {
tarMagicNumbers := []byte{'\x75', '\x73', '\x74', '\x61', '\x72'}
return bytes.Equal(data[257:262], tarMagicNumbers)
}
36 changes: 25 additions & 11 deletions internals_fsutils_test.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
package seelog

import (
"testing"
"fmt"
"reflect"
"testing"
)


func TestGzip(t *testing.T) {
defer cleanupWriterTest(t)

files := make(map[string][]byte)
files["file1"] = []byte("I am a log")
err := createGzip("./gzip.gz", files["file1"])
if err != nil {
fmt.Println(err)
t.Fail()
t.Fatal(err)
}

decompressedFile, err := unGzip("./gzip.gz")
if err != nil {
fmt.Println(err)
t.Fail()
t.Fatal(err)
}

equal := reflect.DeepEqual(files["file1"], decompressedFile)
if !equal {
t.Fail()
t.Fatal("gzip(ungzip(file)) should be equal to file")
}
}

Expand All @@ -35,11 +31,29 @@ func TestTar(t *testing.T) {
files := make(map[string][]byte)
files["file1"] = []byte("I am a log")
files["file2"] = []byte("I am another log")
tar, _ := createTar(files)
tar, err := createTar(files)
if err != nil {
t.Fatal(err)
}

resultFiles, _ := unTar(tar)
resultFiles, err := unTar(tar)
if err != nil {
t.Fatal(err)
}
equal := reflect.DeepEqual(files, resultFiles)
if !equal {
t.Fail()
t.Fatal("untar(tar(files)) should be equal to files")
}
}

func TestIsTar(t *testing.T) {
defer cleanupWriterTest(t)
files := make(map[string][]byte)
files["file1"] = []byte("I am a log")
files["file2"] = []byte("I am another log")
tar, _ := createTar(files)

if !isTar(tar) {
t.Fatal("tar(files) should be recognized as a tar file")
}
}
Loading

0 comments on commit ec7e252

Please sign in to comment.