Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support direct filesystem #2

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,30 @@ package disk
import (
"io"

"github.com/masahiro331/go-disk/fs"
"github.com/masahiro331/go-disk/gpt"
"github.com/masahiro331/go-disk/mbr"
"github.com/masahiro331/go-disk/types"
"golang.org/x/xerrors"
)

type Driver interface {
Next() (types.Partition, error)
}

func NewDriver(sr *io.SectionReader) (Driver, error) {
func NewDriver(
sr *io.SectionReader,
checkFsFuncs ...func(
r io.Reader,
) bool,
) (types.Driver, error) {
m, err := mbr.NewMasterBootRecord(sr)
if err != nil {
if xerrors.Is(mbr.InvalidSignature, err) {
masahiro331 marked this conversation as resolved.
Show resolved Hide resolved
ok, err := fs.CheckFileSystem(sr, checkFsFuncs)
if err != nil {
return nil, xerrors.Errorf("failed to check filesystem: %w", err)
}
if ok {
return fs.NewDirectFileSystem(sr)
}
}
return nil, xerrors.Errorf("failed to new MBR: %w", err)
}

Expand All @@ -28,4 +39,4 @@ func NewDriver(sr *io.SectionReader) (Driver, error) {
}

return g, nil
}
}
89 changes: 89 additions & 0 deletions fs/fs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package fs
masahiro331 marked this conversation as resolved.
Show resolved Hide resolved

import (
"io"

"github.com/masahiro331/go-disk/types"
"golang.org/x/xerrors"
)

var (
_ types.Driver = &DirectFileSystem{}
_ types.Partition = &DirectFileSystemPartition{}
)

type DirectFileSystem struct {
Partition *DirectFileSystemPartition
}

func (d *DirectFileSystem) Next() (types.Partition, error) {
if d.Partition == nil {
return nil, io.EOF
}
partition := d.Partition
d.Partition = nil

return partition, nil
}

type DirectFileSystemPartition struct {
sectionReader *io.SectionReader
}

func (p DirectFileSystemPartition) Name() string {
return "0"
}

func (p DirectFileSystemPartition) GetType() []byte {
return []byte{}
}

func (p DirectFileSystemPartition) GetStartSector() uint64 {
return uint64(0)
}

func (p DirectFileSystemPartition) Bootable() bool {
return false
}

func (p DirectFileSystemPartition) GetSize() uint64 {
return uint64(p.sectionReader.Size())
}

func (p DirectFileSystemPartition) GetSectionReader() io.SectionReader {
return *p.sectionReader
}

func (p DirectFileSystemPartition) IsSupported() bool {
return true
}

func NewDirectFileSystem(sr *io.SectionReader) (*DirectFileSystem, error) {
_, err := sr.Seek(0, io.SeekStart)
if err != nil {
return nil, xerrors.Errorf("failed to DirectFileSystem seek offset error: %w", err)
}
return &DirectFileSystem{
Partition: &DirectFileSystemPartition{
sectionReader: sr,
},
}, nil
}

func CheckFileSystem(
masahiro331 marked this conversation as resolved.
Show resolved Hide resolved
r *io.SectionReader,
checkFsFuncs []func(
masahiro331 marked this conversation as resolved.
Show resolved Hide resolved
r io.Reader,
) bool,
) (bool, error) {
for _, checkFsFunc := range checkFsFuncs {
_, err := r.Seek(0, io.SeekStart)
masahiro331 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return false, xerrors.Errorf("failed to seek offset error: %w", err)
}
if checkFsFunc(r) {
return true, nil
}
}
return false, nil
}
4 changes: 4 additions & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package types

import "io"

type Driver interface {
Next() (Partition, error)
}

type Partition interface {
Bootable() bool
GetStartSector() uint64
Expand Down