Exemple #1
0
func (s *BlockStorage) WriteChunk(blobID, chunkID proto.ID, size int64, r io.Reader) (err error) {
	lock, err := s.FDLocks.Take()
	if err != nil {
		return
	}
	defer lock.Release()

	n := filepath.Join(s.idPath(upload_ns, blobID), chunkID.String())
	w, err := s.getCAFile(n)
	if err != nil {
		return
	}
	defer w.Close()

	written, err := io.Copy(w, r)
	if err != nil {
		return
	}

	if written != size {
		err = fmt.Errorf("bad chunk size for %s:%s : %d != %d",
			blobID, chunkID, size, written)
		return
	}
	err = w.Accept()
	return
}
Exemple #2
0
// Store chunk in assemble
func (a *Assembler) StoreChunk(r io.Reader, id proto.ID) (err error) {
	lock, err := a.model.FdLocks.Take()
	if err != nil {
		return
	}
	defer lock.Release()

	caOpts := contentaddressable.DefaultOptions()
	caOpts.Hasher = sha3.New256()

	w, err := contentaddressable.NewFileWithOptions(
		filepath.Join(a.Where, id.String()), caOpts)
	if os.IsExist(err) {
		err = nil
		return
	}
	if err != nil {
		return
	}
	defer w.Close()

	if _, err = io.Copy(w, r); err != nil {
		return
	}
	err = w.Accept()
	return
}
Exemple #3
0
func (a *Assembler) commitBlob(name string, id proto.ID) (err error) {
	dst := filepath.Join(a.model.WD, name)
	src := dst + id.String()
	bak := dst + ".bak"

	os.Rename(dst, bak)
	if err = os.Rename(src, dst); err != nil {
		os.Remove(dst)
		os.Rename(bak, dst)
		return
	}
	defer os.Remove(src)
	defer os.Remove(bak)
	return
}
Exemple #4
0
func (a *Assembler) commitBlob(name string, id proto.ID) (err error) {
	dst := lists.OSFromSlash(lists.OSJoin(a.model.WD, name))
	src := dst + "-" + id.String()
	bak := dst + "-bak"

	os.Rename(dst, bak)
	if err = os.Rename(src, dst); err != nil {
		os.Remove(dst)
		os.Rename(bak, dst)
		return
	}
	os.Remove(src)
	os.Remove(bak)
	return
}
Exemple #5
0
func (a *Assembler) writeChunkTo(w io.Writer, id proto.ID) (err error) {
	lock, err := a.model.FdLocks.Take()
	if err != nil {
		return
	}
	defer lock.Release()

	name := filepath.Join(a.Where, id.String())
	r, err := os.Open(name)
	if err != nil {
		return
	}
	defer r.Close()

	_, err = io.Copy(w, r)
	return
}
Exemple #6
0
func (s *BlockStorage) UploadChunk(uploadID uuid.UUID, chunkID proto.ID, r io.Reader) (err error) {
	lock, err := s.FDLocks.Take()
	if err != nil {
		return
	}
	defer lock.Release()
	hexid := proto.ID(hex.EncodeToString(uploadID[:]))

	n := filepath.Join(s.idPath(upload_ns, hexid), chunkID.String())
	w, err := s.getCAFile(n)
	if err != nil {
		return
	}
	defer w.Close()

	if _, err = io.Copy(w, r); err != nil {
		return
	}

	err = w.Accept()
	return
}
Exemple #7
0
func (s *BlockStorage) idPath(ns string, id proto.ID) string {
	ids := id.String()
	return filepath.Join(s.Root, ns, ids[:s.Split], ids)
}