Skip to content

Commit

Permalink
Make a better unarchiver API, remove all global config variables
Browse files Browse the repository at this point in the history
  • Loading branch information
mfenniak committed Sep 2, 2012
1 parent a42ad5c commit 860b1dc
Show file tree
Hide file tree
Showing 7 changed files with 263 additions and 250 deletions.
39 changes: 13 additions & 26 deletions falib/archiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package falib
import (
"bufio"
"encoding/binary"
"errors"
"hash"
"hash/crc64"
"io"
Expand All @@ -14,20 +13,10 @@ import (
"syscall"
)

var (
ErrAbsoluteDirectoryPath = errors.New("unable to process archive with absolute path reference")
)

// Will rename to "Logger" when Logger global var is removed
type MultiLevelLogger interface {
Verbose(v ...interface{})
Warning(v ...interface{})
}

type Archiver struct {
directoryScanQueue chan string
fileReadQueue chan string
blockQueue chan Block
blockQueue chan block
workInProgress sync.WaitGroup
excludePatterns []string
output *bufio.Writer
Expand All @@ -37,7 +26,8 @@ type Archiver struct {
FileReadQueueSize int
BlockQueueSize int
ExcludePatterns []string
Logger MultiLevelLogger
Logger Logger
BlockSize uint16
error error
}

Expand All @@ -50,6 +40,7 @@ func NewArchiver(output io.Writer) *Archiver {
retval.DirScanQueueSize = 128
retval.FileReadQueueSize = 128
retval.BlockQueueSize = 128
retval.BlockSize = 4096
return retval
}

Expand All @@ -66,7 +57,7 @@ func (a *Archiver) Run() error {
a.directoryScanQueue = make(chan string, a.DirScanQueueSize)
}
a.fileReadQueue = make(chan string, a.FileReadQueueSize)
a.blockQueue = make(chan Block, a.BlockQueueSize)
a.blockQueue = make(chan block, a.BlockQueueSize)
a.error = nil

for i := 0; i < a.DirReaderCount; i++ {
Expand Down Expand Up @@ -99,9 +90,7 @@ func (a *Archiver) directoryScanner() {
a.workInProgress.Done()
continue
}
if Verbose {
a.Logger.Verbose(directoryPath)
}
a.Logger.Verbose(directoryPath)

directory, err := os.Open(directoryPath)
if err != nil {
Expand All @@ -111,7 +100,7 @@ func (a *Archiver) directoryScanner() {
}

uid, gid, mode := a.getModeOwnership(directory)
a.blockQueue <- Block{directoryPath, 0, nil, blockTypeDirectory, uid, gid, mode}
a.blockQueue <- block{directoryPath, 0, nil, blockTypeDirectory, uid, gid, mode}

for fileName := range a.readdirnames(directory) {
filePath := filepath.Join(directoryPath, fileName)
Expand Down Expand Up @@ -182,20 +171,18 @@ func (a *Archiver) getModeOwnership(file *os.File) (int, int, os.FileMode) {

func (a *Archiver) fileReader() {
for filePath := range a.fileReadQueue {
if Verbose {
a.Logger.Warning(filePath)
}
a.Logger.Verbose(filePath)

file, err := os.Open(filePath)
if err == nil {

uid, gid, mode := a.getModeOwnership(file)
a.blockQueue <- Block{filePath, 0, nil, blockTypeStartOfFile, uid, gid, mode}
a.blockQueue <- block{filePath, 0, nil, blockTypeStartOfFile, uid, gid, mode}

bufferedFile := bufio.NewReader(file)

for {
buffer := make([]byte, BlockSize)
buffer := make([]byte, a.BlockSize)
bytesRead, err := bufferedFile.Read(buffer)
if err == io.EOF {
break
Expand All @@ -204,10 +191,10 @@ func (a *Archiver) fileReader() {
break
}

a.blockQueue <- Block{filePath, uint16(bytesRead), buffer, blockTypeData, 0, 0, 0}
a.blockQueue <- block{filePath, uint16(bytesRead), buffer, blockTypeData, 0, 0, 0}
}

a.blockQueue <- Block{filePath, 0, nil, blockTypeEndOfFile, 0, 0, 0}
a.blockQueue <- block{filePath, 0, nil, blockTypeEndOfFile, 0, 0, 0}
file.Close()
} else {
a.Logger.Warning("file open error:", err.Error())
Expand All @@ -217,7 +204,7 @@ func (a *Archiver) fileReader() {
}
}

func (b *Block) writeBlock(output io.Writer) error {
func (b *block) writeBlock(output io.Writer) error {
filePath := []byte(b.filePath)
err := binary.Write(output, binary.BigEndian, uint16(len(filePath)))
if err == nil {
Expand Down
13 changes: 2 additions & 11 deletions falib/block.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package falib

import (
"log"
"os"
)
import "os"

type blockType byte

Expand All @@ -15,7 +12,7 @@ const (
blockTypeChecksum
)

type Block struct {
type block struct {
filePath string
numBytes uint16
buffer []byte
Expand All @@ -28,9 +25,3 @@ type Block struct {
// Archive header: stole ideas from the PNG file header here, but replaced
// 'PNG' with 'FA1' to identify the fast-archive format (version 1).
var fastArchiverHeader = []byte{0x89, 0x46, 0x41, 0x31, 0x0D, 0x0A, 0x1A, 0x0A}

var BlockSize uint16
var Logger *log.Logger
var Verbose bool
var IgnorePerms bool
var IgnoreOwners bool
10 changes: 10 additions & 0 deletions falib/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package falib

import "errors"

var (
ErrAbsoluteDirectoryPath = errors.New("unable to process archive with absolute path reference")
ErrFileHeaderMismatch = errors.New("unexpected file header")
ErrCrcMismatch = errors.New("crc64 mismatch")
ErrUnrecognizedBlockType = errors.New("unrecognized block type")
)
202 changes: 0 additions & 202 deletions falib/extract-archive.go

This file was deleted.

6 changes: 6 additions & 0 deletions falib/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package falib

type Logger interface {
Verbose(v ...interface{})
Warning(v ...interface{})
}
Loading

0 comments on commit 860b1dc

Please sign in to comment.