示例#1
0
文件: indexed.go 项目: sombr/ccat
// readIndex calls x.Read with the index's backing file.
func readIndex(fs rwvfs.FileSystem, name string, x persistedIndex) (err error) {
	vlog.Printf("%s: reading index...", name)
	var f vfs.ReadSeekCloser
	f, err = fs.Open(fmt.Sprintf(indexFilename, name))
	if err != nil {
		vlog.Printf("%s: failed to read index: %s.", name, err)
		if os.IsNotExist(err) {
			return &errIndexNotExist{name: name, err: err}
		}
		return err
	}
	defer func() {
		err2 := f.Close()
		if err == nil {
			err = err2
		}
	}()

	r, err := gzip.NewReader(f)
	if err != nil {
		return err
	}

	if err := x.Read(r); err != nil {
		return err
	}
	if err := r.Close(); err != nil {
		return err
	}
	vlog.Printf("%s: done reading index.", name)
	return nil
}
示例#2
0
文件: vfs_test.go 项目: sombr/ccat
func testWrite(t *testing.T, fs rwvfs.FileSystem, path string) {
	label := fmt.Sprintf("%T", fs)

	w, err := fs.Create(path)
	if err != nil {
		t.Fatalf("%s: WriterOpen: %s", label, err)
	}

	input := []byte("qux")
	_, err = w.Write(input)
	if err != nil {
		t.Fatalf("%s: Write: %s", label, err)
	}

	err = w.Close()
	if err != nil {
		t.Fatalf("%s: w.Close: %s", label, err)
	}

	var r io.ReadCloser
	r, err = fs.Open(path)
	if err != nil {
		t.Fatalf("%s: Open: %s", label, err)
	}
	var output []byte
	output, err = ioutil.ReadAll(r)
	if err != nil {
		t.Fatalf("%s: ReadAll: %s", label, err)
	}
	if !bytes.Equal(output, input) {
		t.Errorf("%s: got output %q, want %q", label, output, input)
	}

	r, err = fs.Open(path)
	if err != nil {
		t.Fatalf("%s: Open: %s", label, err)
	}
	output, err = ioutil.ReadAll(r)
	if err != nil {
		t.Fatalf("%s: ReadAll: %s", label, err)
	}
	if !bytes.Equal(output, input) {
		t.Errorf("%s: got output %q, want %q", label, output, input)
	}

	err = fs.Remove(path)
	if err != nil {
		t.Errorf("%s: Remove(%q): %s", label, path, err)
	}
	testPathDoesNotExist(t, label, fs, path)
}
示例#3
0
文件: fs_store.go 项目: sombr/ccat
// openFetcher calls fs.OpenFetcher if it implemented the
// FetcherOpener interface; otherwise it calls fs.Open.
func openFetcherOrOpen(fs rwvfs.FileSystem, name string) (vfs.ReadSeekCloser, error) {
	if fo, ok := fs.(rwvfs.FetcherOpener); ok {
		return fo.OpenFetcher(name)
	}
	return fs.Open(name)
}