Skip to content

Commit

Permalink
version 0.9.0
Browse files Browse the repository at this point in the history
  • Loading branch information
cuhsat committed May 17, 2024
1 parent 53ecb51 commit de26545
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 13 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ go.work
bin/
*.dd
*.zip
!internal/testdata/*
!internal/testdata/windows.zip
!internal/testdata/windows.dd.zip
2 changes: 1 addition & 1 deletion internal/sys/sys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func TestCall(t *testing.T) {
t.Run("Test layered call", func(t *testing.T) {
t.Run("Test call", func(t *testing.T) {
stdout := new(strings.Builder)
stderr := new(strings.Builder)

Expand Down
Binary file added internal/testdata/windows.dd.zip
Binary file not shown.
41 changes: 41 additions & 0 deletions internal/zip/zip_test.go
Original file line number Diff line number Diff line change
@@ -1,2 +1,43 @@
// Zip archive implementation tests.
package zip

import (
"os"
"path/filepath"
"slices"
"testing"

"github.com/cuhsat/fact/internal/test"
)

func TestIndex(t *testing.T) {
t.Run("Test index", func(t *testing.T) {
idx, err := Index(test.Testdata("windows.zip"))

if err != nil {
t.Fatal(err)
}

if !slices.Contains(idx, "Users/Test/NTUSER.DAT") {
t.Fatal("file not found")
}
})
}

func TestUnzip(t *testing.T) {
t.Run("Test unzip", func(t *testing.T) {
tmp, _ := os.MkdirTemp(os.TempDir(), "zip")

err := Unzip(test.Testdata("windows.zip"), tmp)

if err != nil {
t.Fatal(err)
}

_, err = os.Stat(filepath.Join(tmp, "Windows"))

if os.IsNotExist(err) {
t.Fatal("folder not found")
}
})
}
10 changes: 7 additions & 3 deletions pkg/fmount/dd/dd.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ func Is(img string) (is bool, err error) {

func Mount(img, dir string, so bool) (sysroot string, err error) {
if len(dir) == 0 {
b := filepath.Base(img)

dir = strings.TrimSuffix(b, filepath.Ext(b))
dir = baseFile(img)
}

if err = os.MkdirAll(dir, mode); err != nil {
Expand Down Expand Up @@ -151,6 +149,12 @@ func Unmount(img string) (err error) {
return
}

func baseFile(name string) string {
b := filepath.Base(name)

return strings.TrimSuffix(b, filepath.Ext(b))
}

func detectMagic(name string) (has bool, err error) {
f, err := os.Open(name)

Expand Down
51 changes: 51 additions & 0 deletions pkg/fmount/dd/dd_test.go
Original file line number Diff line number Diff line change
@@ -1,2 +1,53 @@
// DD implementation tests.
package dd

import (
"os"
"path/filepath"
"testing"

"github.com/cuhsat/fact/internal/test"
"github.com/cuhsat/fact/internal/zip"
)

func TestDD(t *testing.T) {
cases := []struct {
name, file string
}{
{
name: "Test mount for Windows",
file: test.Testdata("windows.dd.zip"),
},
}

for _, tt := range cases {
tmp, _ := os.MkdirTemp(os.TempDir(), "dd")
mnt, _ := os.MkdirTemp(os.TempDir(), "dd-mnt")

err := zip.Unzip(tt.file, tmp)

if err != nil {
t.Fatal(err)
}

t.Run(tt.name, func(t *testing.T) {
img := filepath.Join(tmp, baseFile(tt.file))

p, err := Mount(img, mnt, true)

if err != nil {
t.Fatal(err)
}

if p != filepath.Join(mnt, "p1") {
t.Fatal("sysroot unexpected", p)
}

err = Unmount(img)

if err != nil {
t.Fatal(err)
}
})
}
}
17 changes: 9 additions & 8 deletions pkg/fmount/fmount.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ func DetectType(img, t string) (string, error) {
}

func Extract(img string) (p string, err error) {
dir, err := os.Getwd()

if err != nil {
return
}

i, err := zip.Index(img)

if err != nil {
Expand All @@ -45,11 +39,18 @@ func Extract(img string) (p string, err error) {
return
}

if err = zip.Unzip(img, dir); err != nil {
dir := filepath.Dir(img)

p = path.Join(dir, i[0])

if _, err = os.Stat(p); !os.IsNotExist(err) {
err = errors.New("file already exists")
return
}

p = path.Join(dir, i[0])
if err = zip.Unzip(img, dir); err != nil {
return
}

return
}
Expand Down

0 comments on commit de26545

Please sign in to comment.