moth/src/mothball.go

192 lines
3.2 KiB
Go
Raw Normal View History

2018-09-14 18:24:48 -06:00
package main
2018-05-09 21:52:52 -06:00
import (
"archive/zip"
"fmt"
"io"
2018-09-14 18:24:48 -06:00
"io/ioutil"
2018-05-09 21:52:52 -06:00
"os"
2018-09-19 17:56:26 -06:00
"strings"
2018-05-09 21:52:52 -06:00
"time"
)
type Mothball struct {
2018-09-17 17:00:08 -06:00
zf *zip.ReadCloser
2018-05-09 21:52:52 -06:00
filename string
2018-09-17 17:00:08 -06:00
mtime time.Time
2018-05-09 21:52:52 -06:00
}
2018-09-19 17:56:26 -06:00
type MothballFile struct {
f io.ReadCloser
pos int64
zf *zip.File
io.Reader
io.Seeker
io.Closer
}
func NewMothballFile(zf *zip.File) (*MothballFile, error) {
mf := &MothballFile{
zf: zf,
pos: 0,
f: nil,
}
if err := mf.reopen(); err != nil {
return nil, err
}
return mf, nil
}
func (mf *MothballFile) reopen() error {
if mf.f != nil {
if err := mf.f.Close(); err != nil {
return err
}
}
f, err := mf.zf.Open()
if err != nil {
return err
}
mf.f = f
mf.pos = 0
return nil
}
func (mf *MothballFile) ModTime() time.Time {
return mf.zf.Modified
}
func (mf *MothballFile) Read(p []byte) (int, error) {
n, err := mf.f.Read(p)
mf.pos += int64(n)
return n, err
}
func (mf *MothballFile) Seek(offset int64, whence int) (int64, error) {
var pos int64
switch whence {
case io.SeekStart:
pos = offset
case io.SeekCurrent:
pos = mf.pos + int64(offset)
case io.SeekEnd:
pos = int64(mf.zf.UncompressedSize64) - int64(offset)
}
if pos < 0 {
return mf.pos, fmt.Errorf("Tried to seek %d before start of file", pos)
}
if pos >= int64(mf.zf.UncompressedSize64) {
// We don't need to decompress anything, we're at the end of the file
mf.f.Close()
mf.f = ioutil.NopCloser(strings.NewReader(""))
mf.pos = int64(mf.zf.UncompressedSize64)
return mf.pos, nil
}
if pos < mf.pos {
if err := mf.reopen(); err != nil {
return mf.pos, err
}
}
buf := make([]byte, 32 * 1024)
for pos > mf.pos {
l := pos - mf.pos
if l > int64(cap(buf)) {
l = int64(cap(buf)) - 1
}
p := buf[0:int(l)]
n, err := mf.Read(p)
if err != nil {
return mf.pos, err
} else if n <= 0 {
return mf.pos, fmt.Errorf("Short read (%d bytes)", n)
}
}
return mf.pos, nil
}
func (mf *MothballFile) Close() error {
return mf.f.Close()
}
2018-09-14 18:24:48 -06:00
func OpenMothball(filename string) (*Mothball, error) {
2018-05-09 21:52:52 -06:00
var m Mothball
2018-09-17 17:00:08 -06:00
2018-05-09 21:52:52 -06:00
m.filename = filename
2018-09-17 17:00:08 -06:00
2018-05-09 21:52:52 -06:00
err := m.Refresh()
if err != nil {
return nil, err
}
return &m, nil
}
2018-09-17 17:00:08 -06:00
func (m *Mothball) Close() error {
2018-05-09 21:52:52 -06:00
return m.zf.Close()
}
2018-09-17 17:00:08 -06:00
func (m *Mothball) Refresh() error {
2018-05-09 21:52:52 -06:00
info, err := os.Stat(m.filename)
if err != nil {
return err
}
mtime := info.ModTime()
2018-09-17 17:00:08 -06:00
if !mtime.After(m.mtime) {
2018-05-09 21:52:52 -06:00
return nil
}
2018-09-17 17:00:08 -06:00
2018-05-09 21:52:52 -06:00
zf, err := zip.OpenReader(m.filename)
if err != nil {
return err
}
if m.zf != nil {
m.zf.Close()
}
m.zf = zf
m.mtime = mtime
2018-09-17 17:00:08 -06:00
2018-05-09 21:52:52 -06:00
return nil
}
2018-09-19 17:56:26 -06:00
func (m *Mothball) get(filename string) (*zip.File, error) {
2018-05-09 21:52:52 -06:00
for _, f := range m.zf.File {
if filename == f.Name {
2018-09-19 17:56:26 -06:00
return f, nil
2018-05-09 21:52:52 -06:00
}
}
2018-09-19 17:56:26 -06:00
return nil, fmt.Errorf("File not found: %s %s", m.filename, filename)
}
func (m *Mothball) Header(filename string) (*zip.FileHeader, error) {
f, err := m.get(filename)
if err != nil {
return nil, err
}
return &f.FileHeader, nil
}
func (m *Mothball) Open(filename string) (*MothballFile, error) {
f, err := m.get(filename)
if err != nil {
return nil, err
}
mf, err := NewMothballFile(f)
return mf, err
2018-05-09 21:52:52 -06:00
}
2018-09-14 18:24:48 -06:00
func (m *Mothball) ReadFile(filename string) ([]byte, error) {
f, err := m.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
2018-09-17 17:00:08 -06:00
2018-09-14 18:24:48 -06:00
bytes, err := ioutil.ReadAll(f)
return bytes, err
}