Beispiel #1
0
func (s *localAPIServer) PutBlock(putBlockServer drive.API_PutBlockServer) (retErr error) {
	result := &drive.BlockRefs{}
	defer func(start time.Time) { s.Log(nil, result, retErr, time.Since(start)) }(time.Now())
	scanner := bufio.NewScanner(protostream.NewStreamingBytesReader(putBlockServer))
	for {
		blockRef, err := s.putOneBlock(scanner)
		if err != nil {
			return err
		}
		result.BlockRef = append(result.BlockRef, blockRef)
		if (blockRef.Range.Upper - blockRef.Range.Lower) < uint64(blockSize) {
			break
		}
	}
	return putBlockServer.SendAndClose(result)
}
Beispiel #2
0
func (s *localAPIServer) PutBlock(putBlockServer drive.API_PutBlockServer) (retErr error) {
	var result *drive.Block
	defer func(start time.Time) { s.Log(nil, result, retErr, time.Since(start)) }(time.Now())
	hash := newHash()
	tmp, err := ioutil.TempFile(s.tmpDir(), "block")
	if err != nil {
		return err
	}
	defer func() {
		if err := tmp.Close(); err != nil && retErr == nil {
			retErr = err
			return
		}
		if result == nil {
			return
		}
		// Check if it's a new block, if so rename it, otherwise remove.
		if _, err := os.Stat(s.blockPath(result)); !os.IsNotExist(err) {
			if err := os.Remove(tmp.Name()); err != nil && retErr == nil {
				retErr = err
				return
			}
			return
		}
		if err := os.Rename(tmp.Name(), s.blockPath(result)); err != nil && retErr == nil {
			retErr = err
			return
		}
	}()
	r := io.TeeReader(protostream.NewStreamingBytesReader(putBlockServer), hash)
	if _, err := io.Copy(tmp, r); err != nil {
		return err
	}
	result = getBlock(hash)
	return putBlockServer.SendAndClose(result)
}