Exemplo n.º 1
0
func (c *checkPoint) Restore(r *bitmap.File) (err error) {
	size := r.Size() // make sure is blksize-round
	off := size - (size & int64(c.blkSize-1))
	buf := make([]byte, 2)
	for {
		if off == 0 {
			break
		}
		off -= int64(c.blkSize)
		_, err = r.ReadAt(buf, off)
		if err != nil {
			return
		}
		if !bytes.Equal(buf, MagicBytes) {
			continue
		}

		bio := utils.NewBufio(utils.NewReader(r, off+2))
		if err = gob.NewDecoder(bio).Decode(&c.Data); err != nil {
			logex.Error(err)
			return
		}

		off = bio.Offset(-1)
		break
	}

	// move offset to next blk
	off = c.calFloor(off)

	if c.Data == nil {
		c.Data = make(map[string]int64)
	}
	updated := false
	if off < size {
		logex.Info("trying to resore ino into checkpoint")
	}
	bio := utils.NewBufio(utils.NewReader(r, off))
	for off < size {
		bio.Offset(off)
		ino, err := ReadInode(bio, c.blkBit)
		if err != nil {
			if logex.Equal(err, io.EOF) {
				break
			}
			off += int64(c.blkSize)
			continue
		}

		updated = true
		c.Data[ino.Name.String()] = off
		off = c.calFloor(bio.Offset(-1))
	}

	if updated {
		// write down anything which checkpoint is missing
		err = logex.Trace(c.Save(utils.NewWriter(r, r.Size())))
	}
	return
}
Exemplo n.º 2
0
func TestFileSize(t *testing.T) {
	var err error
	defer utils.TDefer(t, &err)

	lfs, err := newIns()
	if err != nil {
		return
	}

	f, err := lfs.Open("/hello")
	if err != nil {
		return
	}
	w := utils.NewWriter(f, f.Size())
	_, err = w.Write([]byte("hello"))
	if err != nil {
		return
	}
	if f.Size() != 5 {
		err = logex.NewError("fileSize not expected: ", f.Size())
		return
	}
	w.Close()

	// test restore
	lfs, err = New(cfg)
	if err != nil {
		return
	}
	f, err = lfs.Open("/hello")
	if err != nil {
		return
	}
	if f.Size() != 5 {
		err = logex.NewError("fileSize not expected: ", f.Size())
	}
}