package main

import (
	"errors"
	"fmt"
	"io/ioutil"
	"os"
	"path"
	"strconv"
)

type RawAction struct {
	BaseAction `yaml:",inline"`
	Offset     string
	Source     string
	Path       string
}

func (raw *RawAction) Verify(context *YaibContext) error {
	if raw.Source != "rootdir" {
		return errors.New("Only suppport sourcing from filesystem")
	}

	return nil
}

func (raw *RawAction) Run(context *YaibContext) error {
	raw.LogStart()
	s := path.Join(context.rootdir, raw.Path)
	content, err := ioutil.ReadFile(s)

	if err != nil {
		return fmt.Errorf("Failed to read %s", s)
	}

	target, err := os.OpenFile(context.image, os.O_WRONLY, 0)
	if err != nil {
		return fmt.Errorf("Failed to open image file %v", err)
	}

	offset, err := strconv.ParseInt(raw.Offset, 0, 64)
	if err != nil {
		return fmt.Errorf("Couldn't parse offset %v", err)
	}
	bytes, err := target.WriteAt(content, offset)
	if bytes != len(content) {
		return errors.New("Couldn't write complete data")
	}

	return nil
}

package main

import (
	"errors"
	"fmt"
	"io/ioutil"
	"os"
	"path"
	"strconv"
)

type RawAction struct {
	BaseAction `yaml:",inline"`
	Offset     string
	Source     string
	Path       string
}

func (raw *RawAction) Verify(context *YaibContext) error {
	if raw.Source != "filesystem" {
		return errors.New("Only suppport sourcing from filesystem")
	}

	return nil
}

func (raw *RawAction) Run(context *YaibContext) error {
	raw.LogStart()
	s := path.Join(context.rootdir, raw.Path)
	content, err := ioutil.ReadFile(s)

	if err != nil {
		return fmt.Errorf("Failed to read %s", s)
	}

	target, err := os.OpenFile(context.image, os.O_WRONLY, 0)
	if err != nil {
		return fmt.Errorf("Failed to open image file %v", err)
	}

	offset, err := strconv.ParseInt(raw.Offset, 0, 64)
	if err != nil {
		return fmt.Errorf("Couldn't parse offset %v", err)
	}
	bytes, err := target.WriteAt(content, offset)
	if bytes != len(content) {
		return errors.New("Couldn't write complete data")
	}

	return nil
}

Change source naming in the raw action
Change source naming in the raw action

To be consistent with the filesystem deploy action, change the source
identifier from rootdir to filesystem. In general the intended jargon is
that the "filesystem" is the filesystem being created by the tool
(by unpacking a tarball or debootstrap or deploying an ostree or
whatever)

Signed-off-by: Sjoerd Simons <1371fd10dd05a897e5b55dc6ae4fbafa1f84c6ec@collabora.co.uk>
