예제 #1
0
func (w *testWriter) Sync() error {
	p := w.p
	desc := p.desc
	desc.Lock()
	defer desc.Unlock()
	for desc.emuDelaySync&p.t != 0 {
		desc.Unlock()
		desc.emuCh <- struct{}{}
		desc.Lock()
	}
	if desc.emuSyncErr&p.t != 0 {
		return errors.New("emulated sync error")
	}
	return nil
}
예제 #2
0
func (p *testFilePtr) Exist() bool {
	desc := p.desc

	desc.Lock()
	defer desc.Unlock()

	_, exist := desc.files[p.id()]
	return exist
}
예제 #3
0
func (w *testWriter) Write(b []byte) (n int, err error) {
	p := w.p
	desc := p.desc
	desc.Lock()
	defer desc.Unlock()
	if desc.emuWriteErr&p.t != 0 {
		return 0, errors.New("emulated write error")
	}
	return w.p.buf.Write(b)
}
예제 #4
0
func (r *testReader) Close() error {
	p := r.p
	desc := p.desc

	desc.Lock()
	p.desc.print(fmt.Sprintf("testDesc: closing reader, num=%d type=%s", p.num, p.t))
	p.opened = false
	desc.Unlock()

	return nil
}
예제 #5
0
func (w *testWriter) Close() error {
	p := w.p
	desc := p.desc

	desc.Lock()
	desc.print(fmt.Sprintf("testDesc: closing writer, num=%d type=%s", p.num, p.t))
	p.opened = false
	desc.Unlock()

	return nil
}
예제 #6
0
func (p *testFilePtr) Size() (size uint64, err error) {
	desc := p.desc

	desc.Lock()
	defer desc.Unlock()

	if f, exist := desc.files[p.id()]; exist {
		return uint64(f.buf.Len()), nil
	}

	return 0, os.ErrNotExist
}
예제 #7
0
func (p *testFilePtr) Remove() error {
	desc := p.desc

	desc.Lock()
	defer desc.Unlock()

	desc.print(fmt.Sprintf("testDesc: removing file, num=%d type=%s", p.num, p.t))

	if f, exist := desc.files[p.id()]; exist {
		if f.opened {
			return errFileOpen
		}
		f.buf.Reset()
		delete(desc.files, p.id())
	}

	return nil
}
예제 #8
0
func (p *testFilePtr) Open() (r desc.Reader, err error) {
	desc := p.desc

	desc.Lock()
	defer desc.Unlock()

	desc.print(fmt.Sprintf("testDesc: open file, num=%d type=%s", p.num, p.t))

	f, exist := desc.files[p.id()]
	if !exist {
		return nil, os.ErrNotExist
	}

	if f.opened {
		return nil, errFileOpen
	}

	f.opened = true
	r = &testReader{f, bytes.NewReader(f.buf.Bytes())}
	return
}
예제 #9
0
func (p *testFilePtr) Create() (w desc.Writer, err error) {
	desc := p.desc

	desc.Lock()
	defer desc.Unlock()

	desc.print(fmt.Sprintf("testDesc: create file, num=%d type=%s", p.num, p.t))

	f, exist := desc.files[p.id()]
	if exist {
		if f.opened {
			return nil, errFileOpen
		}
	} else {
		f = &testFile{desc: desc, num: p.num, t: p.t}
		desc.files[p.id()] = f
	}

	f.opened = true
	f.buf.Reset()
	return &testWriter{f}, nil
}
예제 #10
0
func (p *testFilePtr) Rename(num uint64, t desc.FileType) error {
	desc := p.desc

	desc.Lock()
	defer desc.Unlock()

	desc.print(fmt.Sprintf("testDesc: rename file, from num=%d type=%s, to num=%d type=%d", p.num, p.t, num, t))

	oid := p.id()
	p.num = num
	p.t = t

	if f, exist := desc.files[oid]; exist {
		if f.opened {
			return errFileOpen
		}
		delete(desc.files, oid)
		f.num = num
		f.t = t
		desc.files[p.id()] = f
	}

	return nil
}
예제 #11
0
func (p *testFilePtr) Num() uint64 {
	desc := p.desc
	desc.Lock()
	defer desc.Unlock()
	return p.num
}
예제 #12
0
func (p *testFilePtr) Type() desc.FileType {
	desc := p.desc
	desc.Lock()
	defer desc.Unlock()
	return p.t
}