Use new afero.zipfs

This commit is contained in:
Neale Pickett 2020-08-17 20:33:23 -06:00
parent 2a3826e2b3
commit 80d91fc6c9
4 changed files with 125 additions and 286 deletions

View File

@ -1,43 +1,68 @@
package main package main
import ( import (
"archive/zip"
"bufio" "bufio"
"fmt" "fmt"
"io"
"log" "log"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
"github.com/spf13/afero" "github.com/spf13/afero"
"github.com/spf13/afero/zipfs"
) )
type zipCategory struct {
afero.Fs
io.Closer
}
// Mothballs provides a collection of active mothball files (puzzle categories) // Mothballs provides a collection of active mothball files (puzzle categories)
type Mothballs struct { type Mothballs struct {
categories map[string]*Zipfs
afero.Fs afero.Fs
categories map[string]zipCategory
categoryLock *sync.RWMutex
} }
// NewMothballs returns a new Mothballs structure backed by the provided directory // NewMothballs returns a new Mothballs structure backed by the provided directory
func NewMothballs(fs afero.Fs) *Mothballs { func NewMothballs(fs afero.Fs) *Mothballs {
return &Mothballs{ return &Mothballs{
Fs: fs, Fs: fs,
categories: make(map[string]*Zipfs), categories: make(map[string]zipCategory),
categoryLock: new(sync.RWMutex),
} }
} }
func (m *Mothballs) getCat(cat string) (zipCategory, bool) {
m.categoryLock.RLock()
defer m.categoryLock.RUnlock()
ret, ok := m.categories[cat]
return ret, ok
}
// Open returns a ReadSeekCloser corresponding to the filename in a puzzle's category and points // Open returns a ReadSeekCloser corresponding to the filename in a puzzle's category and points
func (m *Mothballs) Open(cat string, points int, filename string) (ReadSeekCloser, time.Time, error) { func (m *Mothballs) Open(cat string, points int, filename string) (ReadSeekCloser, time.Time, error) {
mb, ok := m.categories[cat] zc, ok := m.getCat(cat)
if !ok { if !ok {
return nil, time.Time{}, fmt.Errorf("No such category: %s", cat) return nil, time.Time{}, fmt.Errorf("No such category: %s", cat)
} }
f, err := mb.Open(fmt.Sprintf("content/%d/%s", points, filename)) f, err := zc.Open(fmt.Sprintf("content/%d/%s", points, filename))
return f, mb.ModTime(), err if err != nil {
return nil, time.Time{}, err
}
fInfo, err := f.Stat()
return f, fInfo.ModTime(), err
} }
// Inventory returns the list of current categories // Inventory returns the list of current categories
func (m *Mothballs) Inventory() []Category { func (m *Mothballs) Inventory() []Category {
m.categoryLock.RLock()
defer m.categoryLock.RUnlock()
categories := make([]Category, 0, 20) categories := make([]Category, 0, 20)
for cat, zfs := range m.categories { for cat, zfs := range m.categories {
pointsList := make([]int, 0, 20) pointsList := make([]int, 0, 20)
@ -62,7 +87,7 @@ func (m *Mothballs) Inventory() []Category {
// CheckAnswer returns an error if the provided answer is in any way incorrect for the given category and points // CheckAnswer returns an error if the provided answer is in any way incorrect for the given category and points
func (m *Mothballs) CheckAnswer(cat string, points int, answer string) error { func (m *Mothballs) CheckAnswer(cat string, points int, answer string) error {
zfs, ok := m.categories[cat] zfs, ok := m.getCat(cat)
if !ok { if !ok {
return fmt.Errorf("No such category: %s", cat) return fmt.Errorf("No such category: %s", cat)
} }
@ -87,27 +112,60 @@ func (m *Mothballs) CheckAnswer(cat string, points int, answer string) error {
// Update refreshes internal state. // Update refreshes internal state.
// It looks for changes to the directory listing, and caches any new mothballs. // It looks for changes to the directory listing, and caches any new mothballs.
func (m *Mothballs) Update() { func (m *Mothballs) Update() {
m.categoryLock.Lock()
defer m.categoryLock.Unlock()
// Any new categories? // Any new categories?
files, err := afero.ReadDir(m.Fs, "/") files, err := afero.ReadDir(m.Fs, "/")
if err != nil { if err != nil {
log.Print("Error listing mothballs: ", err) log.Println("Error listing mothballs:", err)
return return
} }
found := make(map[string]bool)
for _, f := range files { for _, f := range files {
filename := f.Name() filename := f.Name()
if !strings.HasSuffix(filename, ".mb") { if !strings.HasSuffix(filename, ".mb") {
continue continue
} }
categoryName := strings.TrimSuffix(filename, ".mb") categoryName := strings.TrimSuffix(filename, ".mb")
found[categoryName] = true
if _, ok := m.categories[categoryName]; !ok { if _, ok := m.categories[categoryName]; !ok {
zfs, err := OpenZipfs(m.Fs, filename) f, err := m.Fs.Open(filename)
if err != nil { if err != nil {
log.Print("Error opening ", filename, ": ", err) log.Println(err)
continue continue
} }
log.Print("New mothball: ", filename)
m.categories[categoryName] = zfs fi, err := f.Stat()
if err != nil {
f.Close()
log.Println(err)
continue
}
zrc, err := zip.NewReader(f, fi.Size())
if err != nil {
f.Close()
log.Println(err)
continue
}
m.categories[categoryName] = zipCategory{
Fs: zipfs.New(zrc),
Closer: f,
}
log.Println("Adding category:", categoryName)
}
}
// Delete anything in the list that wasn't found
for categoryName, zc := range m.categories {
if !found[categoryName] {
zc.Close()
delete(m.categories, categoryName)
log.Println("Removing category:", categoryName)
} }
} }
} }

View File

@ -1,9 +1,62 @@
package main package main
import ( import (
"archive/zip"
"fmt"
"testing" "testing"
"github.com/spf13/afero"
) )
func TestMothballs(t *testing.T) { var testFiles = []struct {
t.Error("moo") Name, Body string
}{
{"puzzles.txt", "1"},
{"content/1/puzzle.json", `{"name": "moo"}`},
{"content/1/moo.txt", `My cow goes "moo"`},
}
func (m *Mothballs) createMothball(cat string) {
f, _ := m.Create(fmt.Sprintf("%s.mb", cat))
defer f.Close()
w := zip.NewWriter(f)
defer w.Close()
for _, file := range testFiles {
of, _ := w.Create(file.Name)
of.Write([]byte(file.Body))
}
}
func TestMothballs(t *testing.T) {
m := NewMothballs(new(afero.MemMapFs))
m.createMothball("test1")
m.Update()
if _, ok := m.categories["test1"]; !ok {
t.Error("Didn't create a new category")
}
inv := m.Inventory()
if len(inv) != 1 {
t.Error("Wrong inventory size:", inv)
}
for _, cat := range inv {
for _, points := range cat.Puzzles {
f, _, err := m.Open(cat.Name, points, "puzzle.json")
if err != nil {
t.Error(cat.Name, err)
continue
}
f.Close()
}
}
m.createMothball("test2")
m.Fs.Remove("test1.mb")
m.Update()
inv = m.Inventory()
if len(inv) != 1 {
t.Error("Deleted mothball is still around", inv)
}
} }

View File

@ -1,208 +0,0 @@
package main
import (
"archive/zip"
"fmt"
"io"
"io/ioutil"
"strings"
"time"
"github.com/spf13/afero"
)
// Zipfs defines a Zip Filesystem structure
type Zipfs struct {
f io.Closer
zf *zip.Reader
filename string
mtime time.Time
fs afero.Fs
}
type ZipfsFile struct {
f io.ReadCloser
pos int64
zf *zip.File
io.Reader
io.Seeker
io.Closer
}
func NewZipfsFile(zf *zip.File) (*ZipfsFile, error) {
zfsf := &ZipfsFile{
zf: zf,
pos: 0,
f: nil,
}
if err := zfsf.reopen(); err != nil {
return nil, err
}
return zfsf, nil
}
func (zfsf *ZipfsFile) reopen() error {
if zfsf.f != nil {
if err := zfsf.f.Close(); err != nil {
return err
}
}
f, err := zfsf.zf.Open()
if err != nil {
return err
}
zfsf.f = f
zfsf.pos = 0
return nil
}
func (zfsf *ZipfsFile) ModTime() time.Time {
return zfsf.zf.Modified
}
func (zfsf *ZipfsFile) Read(p []byte) (int, error) {
n, err := zfsf.f.Read(p)
zfsf.pos += int64(n)
return n, err
}
func (zfsf *ZipfsFile) Seek(offset int64, whence int) (int64, error) {
var pos int64
switch whence {
case io.SeekStart:
pos = offset
case io.SeekCurrent:
pos = zfsf.pos + int64(offset)
case io.SeekEnd:
pos = int64(zfsf.zf.UncompressedSize64) - int64(offset)
}
if pos < 0 {
return zfsf.pos, fmt.Errorf("Tried to seek %d before start of file", pos)
}
if pos >= int64(zfsf.zf.UncompressedSize64) {
// We don't need to decompress anything, we're at the end of the file
zfsf.f.Close()
zfsf.f = ioutil.NopCloser(strings.NewReader(""))
zfsf.pos = int64(zfsf.zf.UncompressedSize64)
return zfsf.pos, nil
}
if pos < zfsf.pos {
if err := zfsf.reopen(); err != nil {
return zfsf.pos, err
}
}
buf := make([]byte, 32*1024)
for pos > zfsf.pos {
l := pos - zfsf.pos
if l > int64(cap(buf)) {
l = int64(cap(buf)) - 1
}
p := buf[0:int(l)]
n, err := zfsf.Read(p)
if err != nil {
return zfsf.pos, err
} else if n <= 0 {
return zfsf.pos, fmt.Errorf("Short read (%d bytes)", n)
}
}
return zfsf.pos, nil
}
func (zfsf *ZipfsFile) Close() error {
return zfsf.f.Close()
}
func OpenZipfs(fs afero.Fs, filename string) (*Zipfs, error) {
var zfs Zipfs
zfs.fs = fs
zfs.filename = filename
err := zfs.Refresh()
if err != nil {
return nil, err
}
return &zfs, nil
}
func (zfs *Zipfs) Close() error {
return zfs.f.Close()
}
func (zfs *Zipfs) Refresh() error {
info, err := zfs.fs.Stat(zfs.filename)
if err != nil {
return err
}
mtime := info.ModTime()
if !mtime.After(zfs.mtime) {
return nil
}
f, err := zfs.fs.Open(zfs.filename)
if err != nil {
return err
}
zf, err := zip.NewReader(f, info.Size())
if err != nil {
f.Close()
return err
}
// Clean up the last one
if zfs.zf != nil {
zfs.f.Close()
}
zfs.zf = zf
zfs.f = f
zfs.mtime = mtime
return nil
}
func (zfs *Zipfs) ModTime() time.Time {
return zfs.mtime
}
func (zfs *Zipfs) get(filename string) (*zip.File, error) {
for _, f := range zfs.zf.File {
if filename == f.Name {
return f, nil
}
}
return nil, fmt.Errorf("File not found: %s %s", zfs.filename, filename)
}
func (zfs *Zipfs) Header(filename string) (*zip.FileHeader, error) {
f, err := zfs.get(filename)
if err != nil {
return nil, err
}
return &f.FileHeader, nil
}
func (zfs *Zipfs) Open(filename string) (*ZipfsFile, error) {
f, err := zfs.get(filename)
if err != nil {
return nil, err
}
zfsf, err := NewZipfsFile(f)
return zfsf, err
}
func (zfs *Zipfs) ReadFile(filename string) ([]byte, error) {
f, err := zfs.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
bytes, err := ioutil.ReadAll(f)
return bytes, err
}

View File

@ -1,64 +0,0 @@
package main
import (
"archive/zip"
"fmt"
"github.com/spf13/afero"
"io"
"testing"
)
func TestZipfs(t *testing.T) {
fs := new(afero.MemMapFs)
tf, err := fs.Create("/test.zip")
if err != nil {
t.Error(err)
return
}
defer fs.Remove(tf.Name())
w := zip.NewWriter(tf)
f, err := w.Create("moo.txt")
if err != nil {
t.Error(err)
return
}
// no Close method
_, err = fmt.Fprintln(f, "The cow goes moo")
//.Write([]byte("The cow goes moo"))
if err != nil {
t.Error(err)
return
}
w.Close()
tf.Close()
// Now read it in
mb, err := OpenZipfs(fs, tf.Name())
if err != nil {
t.Error(err)
return
}
cow, err := mb.Open("moo.txt")
if err != nil {
t.Error(err)
return
}
line := make([]byte, 200)
n, err := cow.Read(line)
if (err != nil) && (err != io.EOF) {
t.Error(err)
return
}
if string(line[:n]) != "The cow goes moo\n" {
t.Log(line)
t.Error("Contents didn't match")
return
}
}