// An Archive represents an open archive file. It is always scanned sequentially
// from start to end, without backing up.
type Archive struct {
- fd *os.File // Open file descriptor.
- files []string // Explicit list of files to be processed.
- pad int // Padding bytes required at end of current archive file
+ fd *os.File // Open file descriptor.
+ files []string // Explicit list of files to be processed.
+ pad int // Padding bytes required at end of current archive file
+ matchAll bool // match all files in archive
}
// archive opens (or if necessary creates) the named archive.
}
mustBeArchive(fd)
return &Archive{
- fd: fd,
- files: files,
+ fd: fd,
+ files: files,
+ matchAll: len(files) == 0,
}
}
// match reports whether the entry matches the argument list.
// If it does, it also drops the file from the to-be-processed list.
func (ar *Archive) match(entry *Entry) bool {
- if len(ar.files) == 0 {
+ if ar.matchAll {
return true
}
for i, name := range ar.files {
defer os.RemoveAll(dir)
name := filepath.Join(dir, "pack.a")
ar := archive(name, os.O_RDWR, nil)
+
// Add some entries by hand.
ar.addFile(helloFile.Reset())
ar.addFile(goodbyeFile.Reset())
ar.fd.Close()
+
// Now print it.
ar = archive(name, os.O_RDONLY, nil)
var buf bytes.Buffer
if result != expect {
t.Fatalf("expected %q got %q", expect, result)
}
+
// Do it again without verbose.
verbose = false
buf.Reset()
if result != expect {
t.Fatalf("expected %q got %q", expect, result)
}
+
+ // Do it again with file list arguments.
+ verbose = false
+ buf.Reset()
+ ar = archive(name, os.O_RDONLY, []string{helloFile.name})
+ ar.scan(ar.tableOfContents)
+ ar.fd.Close()
+ result = buf.String()
+ // Expect only helloFile.
+ expect = fmt.Sprintf("%s\n", helloFile.name)
+ if result != expect {
+ t.Fatalf("expected %q got %q", expect, result)
+ }
}
// Test that we can create an archive, put some files in it, and get back a file.