Commit d1deed27 authored by Han-Wen Nienhuys's avatar Han-Wen Nienhuys

zipfs: fix and test tarfs

parent 74a78660
......@@ -11,6 +11,7 @@ import (
"compress/gzip"
"context"
"io"
"log"
"os"
"path/filepath"
"strings"
......@@ -30,20 +31,6 @@ func HeaderToFileInfo(out *fuse.Attr, h *tar.Header) {
out.SetTimes(&h.AccessTime, &h.ModTime, &h.ChangeTime)
}
type TarFile struct {
data []byte
tar.Header
}
func (f *TarFile) Stat(out *fuse.Attr) {
HeaderToFileInfo(out, &f.Header)
out.Mode |= syscall.S_IFREG
}
func (f *TarFile) Data() []byte {
return f.data
}
type tarRoot struct {
nodefs.Inode
rc io.ReadCloser
......@@ -61,7 +48,9 @@ func (r *tarRoot) OnAdd(ctx context.Context) {
break
}
if err != nil {
// handle error
log.Printf("Add: %v", err)
// XXX handle error
break
}
if hdr.Typeflag == 'L' {
......@@ -83,9 +72,6 @@ func (r *tarRoot) OnAdd(ctx context.Context) {
buf := bytes.NewBuffer(make([]byte, 0, hdr.Size))
io.Copy(buf, tr)
df := &nodefs.MemRegularFile{
Data: buf.Bytes(),
}
dir, base := filepath.Split(filepath.Clean(hdr.Name))
p := r.EmbeddedInode()
......@@ -95,16 +81,26 @@ func (r *tarRoot) OnAdd(ctx context.Context) {
}
ch := p.GetChild(comp)
if ch == nil {
p.AddChild(comp, p.NewPersistentInode(ctx,
ch = p.NewPersistentInode(ctx,
&nodefs.Inode{},
nodefs.NodeAttr{Mode: syscall.S_IFDIR}), false)
nodefs.NodeAttr{Mode: syscall.S_IFDIR})
p.AddChild(comp, ch, false)
}
p = ch
}
if hdr.Typeflag == tar.TypeSymlink {
p.AddChild(base, r.NewPersistentInode(ctx, &nodefs.MemSymlink{
Data: []byte(hdr.Linkname),
}, nodefs.NodeAttr{Mode: syscall.S_IFLNK}), false)
} else {
df := &nodefs.MemRegularFile{
Data: buf.Bytes(),
}
HeaderToFileInfo(&df.Attr, hdr)
p.AddChild(base, r.NewPersistentInode(ctx, df, nodefs.NodeAttr{}), false)
}
}
}
type readCloser struct {
......@@ -121,7 +117,6 @@ func NewTarCompressedTree(name string, format string) (nodefs.InodeEmbedder, err
if err != nil {
return nil, err
}
defer f.Close()
var stream io.ReadCloser
switch format {
......
// Copyright 2016 the Go-FUSE Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package zipfs
import (
"archive/tar"
"bytes"
"io"
"io/ioutil"
"os"
"path/filepath"
"syscall"
"testing"
"time"
"github.com/hanwen/go-fuse/internal/testutil"
"github.com/hanwen/go-fuse/nodefs"
)
var tarContents = map[string]string{
"file.txt": "content",
"dir/subfile.txt": "other content",
}
type addClose struct {
io.Reader
}
func (c *addClose) Close() error {
return nil
}
func TestTar(t *testing.T) {
buf := &bytes.Buffer{}
w := tar.NewWriter(buf)
now := time.Now()
for k, v := range tarContents {
h := &tar.Header{
Name: k,
Size: int64(len(v)),
Mode: 0464,
Uid: 42,
Gid: 42,
ModTime: now,
}
isLink := filepath.Base(k) == "link"
if isLink {
h.Typeflag = tar.TypeSymlink
h.Linkname = v
}
w.WriteHeader(h)
if !isLink {
w.Write([]byte(v))
}
}
w.Close()
root := &tarRoot{rc: &addClose{buf}}
mnt := testutil.TempDir()
defer os.Remove(mnt)
opts := &nodefs.Options{}
opts.Debug = true
s, err := nodefs.Mount(mnt, root, opts)
if err != nil {
t.Errorf("Mount: %v", err)
}
defer s.Unmount()
for k, want := range tarContents {
p := filepath.Join(mnt, k)
var st syscall.Stat_t
if err := syscall.Lstat(p, &st); err != nil {
t.Fatalf("Stat %q: %v", p, err)
}
if filepath.Base(k) == "link" {
got, err := os.Readlink(p)
if err != nil {
t.Fatalf("Readlink: %v", err)
}
if got != want {
t.Errorf("Readlink: got %q want %q", got, want)
}
} else {
if got, want := st.Mode, uint32(syscall.S_IFREG|0464); got != want {
t.Errorf("got mode %o, want %o", got, want)
}
c, err := ioutil.ReadFile(p)
if err != nil {
t.Errorf("read %q: %v", k, err)
got := string(c)
if got != want {
t.Errorf("file %q: got %q, want %q", k, got, want)
}
}
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment