Ejemplo n.º 1
0
// shareFiles writes the specified files to w. First a header is written,
// followed by the gzipped concatenation of each file.
func shareFiles(files []*file, w io.Writer) error {
	// Write header.
	err := encoding.NewEncoder(w).EncodeAll(
		shareHeader,
		shareVersion,
		uint64(len(files)),
	)
	if err != nil {
		return err
	}

	// Create compressor.
	zip, _ := gzip.NewWriterLevel(w, gzip.BestCompression)
	enc := encoding.NewEncoder(zip)

	// Encode each file.
	for _, f := range files {
		err = enc.Encode(f)
		if err != nil {
			return err
		}
	}

	return zip.Close()
}
Ejemplo n.º 2
0
// saveFile saves a file to the renter directory.
func (r *Renter) saveFile(f *file) error {
	handle, err := persist.NewSafeFile(filepath.Join(r.persistDir, f.name+ShareExtension))
	if err != nil {
		return err
	}
	defer handle.Close()

	// Write header with length of 1.
	err = encoding.NewEncoder(handle).EncodeAll(
		shareHeader,
		shareVersion,
		uint64(1),
	)
	if err != nil {
		return err
	}

	// Write file.
	err = f.save(handle)
	if err != nil {
		return err
	}

	// Commit the SafeFile.
	return handle.Commit()
}
Ejemplo n.º 3
0
// shareFiles writes the specified files to w.
func (r *Renter) shareFiles(nicknames []string, w io.Writer) error {
	// Write header.
	err := encoding.NewEncoder(w).EncodeAll(
		shareHeader,
		shareVersion,
		uint64(len(nicknames)),
	)
	if err != nil {
		return err
	}

	// Write each file.
	for _, name := range nicknames {
		file, exists := r.files[name]
		if !exists {
			return ErrUnknownNickname
		}
		err := file.save(w)
		if err != nil {
			return err
		}
	}

	return nil
}
Ejemplo n.º 4
0
Archivo: persist.go Proyecto: dlmac/Sia
// save saves a file to w in shareable form. Files are stored in binary format
// and gzipped to reduce size.
func (f *file) save(w io.Writer) error {
	// TODO: error checking
	zip, _ := gzip.NewWriterLevel(w, gzip.BestCompression)
	defer zip.Close()
	enc := encoding.NewEncoder(zip)

	// encode easy fields
	enc.Encode(f.name)
	enc.Encode(f.size)
	enc.Encode(f.masterKey)
	enc.Encode(f.pieceSize)
	enc.Encode(f.mode)
	enc.Encode(f.bytesUploaded)
	enc.Encode(f.chunksUploaded)

	// encode erasureCode
	switch code := f.erasureCode.(type) {
	case *rsCode:
		enc.Encode("Reed-Solomon")
		enc.Encode(uint64(code.dataPieces))
		enc.Encode(uint64(code.numPieces - code.dataPieces))
	default:
		if build.DEBUG {
			panic("unknown erasure code")
		}
		return errors.New("unknown erasure code")
	}
	// encode contracts
	enc.Encode(uint64(len(f.contracts)))
	for _, c := range f.contracts {
		enc.Encode(c)
	}
	return nil
}
Ejemplo n.º 5
0
Archivo: persist.go Proyecto: dlmac/Sia
// saveFile saves a file to the renter directory.
func (r *Renter) saveFile(f *file) error {
	handle, err := persist.NewSafeFile(filepath.Join(r.persistDir, f.name+ShareExtension))
	if err != nil {
		return err
	}
	defer handle.Close()

	enc := encoding.NewEncoder(handle)

	// Write header.
	enc.Encode(shareHeader)
	enc.Encode(shareVersion)

	// Write length of 1.
	err = enc.Encode(uint64(1))
	if err != nil {
		return err
	}

	// Write file.
	err = f.save(handle)
	if err != nil {
		return err
	}

	return nil
}
Ejemplo n.º 6
0
// saveFile saves a file to the renter directory.
func (r *Renter) saveFile(f *file) error {
	// Create directory structure specified in nickname.
	fullPath := filepath.Join(r.persistDir, f.name+ShareExtension)
	err := os.MkdirAll(filepath.Dir(fullPath), 0700)
	if err != nil {
		return err
	}

	handle, err := persist.NewSafeFile(filepath.Join(r.persistDir, f.name+ShareExtension))
	if err != nil {
		return err
	}
	defer handle.Close()

	// Write header with length of 1.
	err = encoding.NewEncoder(handle).EncodeAll(
		shareHeader,
		shareVersion,
		uint64(1),
	)
	if err != nil {
		return err
	}

	// Write file.
	err = f.save(handle)
	if err != nil {
		return err
	}

	// Commit the SafeFile.
	return handle.Commit()
}
Ejemplo n.º 7
0
// WriteSignedObject writes a length-prefixed object prefixed by its signature.
func WriteSignedObject(w io.Writer, obj interface{}, sk SecretKey) error {
	objBytes := encoding.Marshal(obj)
	sig, err := SignHash(HashBytes(objBytes), sk)
	if err != nil {
		return err
	}
	return encoding.NewEncoder(w).EncodeAll(sig, objBytes)
}
Ejemplo n.º 8
0
// save saves a file to w in shareable form. Files are stored in binary format
// and gzipped to reduce size.
func (f *file) save(w io.Writer) error {
	zip, _ := gzip.NewWriterLevel(w, gzip.BestCompression)
	defer zip.Close()
	enc := encoding.NewEncoder(zip)

	// encode easy fields
	err := enc.EncodeAll(
		f.name,
		f.size,
		f.masterKey,
		f.pieceSize,
		f.mode,
	)
	if err != nil {
		return err
	}
	// COMPATv0.4.3 - encode the bytesUploaded and chunksUploaded fields
	// TODO: the resulting .sia file may confuse old clients.
	err = enc.EncodeAll(f.pieceSize*f.numChunks()*uint64(f.erasureCode.NumPieces()), f.numChunks())
	if err != nil {
		return err
	}

	// encode erasureCode
	switch code := f.erasureCode.(type) {
	case *rsCode:
		err = enc.EncodeAll(
			"Reed-Solomon",
			uint64(code.dataPieces),
			uint64(code.numPieces-code.dataPieces),
		)
		if err != nil {
			return err
		}
	default:
		if build.DEBUG {
			panic("unknown erasure code")
		}
		return errors.New("unknown erasure code")
	}
	// encode contracts
	if err := enc.Encode(uint64(len(f.contracts))); err != nil {
		return err
	}
	for _, c := range f.contracts {
		if err := enc.Encode(c); err != nil {
			return err
		}
	}
	return nil
}
Ejemplo n.º 9
0
// MarshalSia implements the encoding.SiaMarshaler interface.
func (b Block) MarshalSia(w io.Writer) error {
	w.Write(b.ParentID[:])
	w.Write(b.Nonce[:])
	w.Write(encoding.EncUint64(uint64(b.Timestamp)))
	return encoding.NewEncoder(w).EncodeAll(b.MinerPayouts, b.Transactions)
}