Commit e452f946 authored by ZheNing Hu's avatar ZheNing Hu Committed by Han-Wen Nienhuys

fs: handle directories in zipfs example

Add a test.

Change-Id: I8620e59ce2fc7b19f595c5670839735243fcd1bf
parent 17756c7c
......@@ -9,8 +9,11 @@ import (
"bytes"
"context"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"sort"
"strings"
"syscall"
"testing"
......@@ -19,6 +22,7 @@ import (
var testData = map[string]string{
"file.txt": "content",
"dir/": "",
"dir/subfile1": "content2",
"dir/subdir/subfile": "content3",
}
......@@ -27,9 +31,17 @@ func createZip(data map[string]string) []byte {
buf := &bytes.Buffer{}
zw := zip.NewWriter(buf)
for k, v := range data {
var keys []string
for k := range data {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
fw, _ := zw.Create(k)
fw.Write([]byte(v))
d := []byte(testData[k])
if len(d) > 0 {
fw.Write(d)
}
}
zw.Close()
......@@ -67,6 +79,15 @@ func TestZipFS(t *testing.T) {
defer server.Unmount()
for k, v := range testData {
if strings.HasSuffix(k, "/") {
fi, err := os.Stat(filepath.Join(mntDir, k))
if err != nil {
t.Errorf("stat %s: %v", k, err)
} else if !fi.IsDir() {
t.Errorf("want isdir, got %v", fi)
}
continue
}
c, err := ioutil.ReadFile(filepath.Join(mntDir, k))
if err != nil {
t.Fatal(err)
......
......@@ -107,6 +107,11 @@ func (zr *zipRoot) OnAdd(ctx context.Context) {
p = ch
}
if f.FileInfo().IsDir() {
continue
}
ch := p.NewPersistentInode(ctx, &zipFile{file: f}, fs.StableAttr{})
p.AddChild(base, ch, true)
}
......
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