func StoreData(uri string, dataReader io.Reader, dataLength uint32) (int64, common.SyncServiceError) {
	if trace.IsLogging(logger.TRACE) {
		trace.Trace("Storing data at %s", uri)
	}
	dataURI, err := url.Parse(uri)
	if err != nil || !strings.EqualFold(dataURI.Scheme, "file") {
		return 0, &Error{"Invalid data URI"}
	}
	filePath := dataURI.Path + ".tmp"
	file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0600)
	if err != nil {
		return 0, common.CreateError(err, fmt.Sprintf("Failed to open file %s to write data. Error: ", dataURI.Path))
	}
	defer file.Close()
	if _, err = file.Seek(0, io.SeekStart); err != nil {
		return 0, &common.IOError{Message: "Failed to seek to the start of a file. Error: " + err.Error()}
	}
	written, err := io.Copy(file, dataReader)
	if err != nil && err != io.EOF {
		return 0, &common.IOError{Message: "Failed to write to file. Error: " + err.Error()}
	}
	if written != int64(dataLength) && dataLength != 0 {
		return 0, &common.IOError{Message: "Failed to write all the data to file."}
	}
	if err := os.Rename(filePath, dataURI.Path); err != nil {
		return 0, &common.IOError{Message: "Failed to rename data file. Error: " + err.Error()}
	}
	return written, nil
}
StoreData writes the data to the file stored at the given URI
package dataURI
import (
	"fmt"
	"io"
	"net/url"
	"os"
	"strings"
	"github.com/open-horizon/edge-sync-service/common"
	"github.com/open-horizon/edge-utilities/logger"
	"github.com/open-horizon/edge-utilities/logger/trace"
)
type Error struct {
	message string
}
func (e *Error) Error() string {
	return e.message
}
func AppendData(uri string, dataReader io.Reader, dataLength uint32, offset int64, total int64, isFirstChunk bool, isLastChunk bool) common.SyncServiceError {
	if trace.IsLogging(logger.TRACE) {
		trace.Trace("Storing data chunk at %s", uri)
	}
	dataURI, err := url.Parse(uri)
	if err != nil || !strings.EqualFold(dataURI.Scheme, "file") {
		return &Error{"Invalid data URI"}
	}
	filePath := dataURI.Path + ".tmp"
	file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0600)
	if err != nil {
		return common.CreateError(err, fmt.Sprintf("Failed to open file %s to append data. Error: ", dataURI.Path))
	}
	defer file.Close()
	if _, err = file.Seek(offset, io.SeekStart); err != nil {
		return &common.IOError{Message: fmt.Sprintf("Failed to seek to the offset %d of a file. Error: %s", offset, err.Error())}
	}
	written, err := io.Copy(file, dataReader)
	if err != nil && err != io.EOF {
		return &common.IOError{Message: "Failed to write to file. Error: " + err.Error()}
	}
	if written != int64(dataLength) {
		return &common.IOError{Message: "Failed to write all the data to file."}
	}
	if isLastChunk {
		if err := os.Rename(filePath, dataURI.Path); err != nil {
			return &common.IOError{Message: "Failed to rename data file. Error: " + err.Error()}
		}
	}
	return nil
}