Skip to content
This repository has been archived by the owner on Oct 29, 2024. It is now read-only.

Commit

Permalink
Merge pull request #63 from docker/hotfix/import-any-tar-gz
Browse files Browse the repository at this point in the history
Allow to import from any .tar.gz
  • Loading branch information
benja-M-1 authored Sep 15, 2022
2 parents ffdd808 + 4550ef6 commit e9f1c16
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
8 changes: 5 additions & 3 deletions vm/internal/handler/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ func (h *Handler) ExportVolume(ctx echo.Context) error {
// Export
cmd := []string{
"tar",
"-zcvf",
"/vackup" + "/" + filepath.Base(fileName),
"/vackup-volume",
"zcvf",
"/vackup" + "/" + filepath.Base(fileName), // the .tar.gz file
"-C", // -C is used to not include the parent directory
"/vackup-volume", // the directory where the files to compress are
".",
}

cmdJoined := strings.Join(cmd, " ")
Expand Down
22 changes: 14 additions & 8 deletions vm/internal/handler/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,24 @@ func TestExportVolume(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if err := extractTarGz(t, r); err != nil {
t.Fatal(err)
}

dst := filepath.Join(tmpDir, "export-destination")
defer func() {
// the folder that is exported from the volume.tar.gz
if err = os.RemoveAll("vackup-volume"); err != nil {
if err = os.RemoveAll(dst); err != nil {
t.Fatal(err)
}
}()

if err := extractTarGz(t, dst, r); err != nil {
t.Fatal(err)
}

exportedFiles := []string{"50x.html", "index.html"}

actual := make(map[string][]byte, len(exportedFiles))
for _, f := range exportedFiles {
actual[f] = readFile(t, "vackup-volume", f)
actual[f] = readFile(t, dst, f)
}
require.Len(t, actual, 2)

Expand All @@ -128,7 +131,7 @@ func TestExportVolume(t *testing.T) {
}
}

func extractTarGz(t *testing.T, gzipStream io.Reader) error {
func extractTarGz(t *testing.T, dst string, gzipStream io.Reader) error {
t.Helper()

uncompressedStream, err := gzip.NewReader(gzipStream)
Expand All @@ -149,13 +152,16 @@ func extractTarGz(t *testing.T, gzipStream io.Reader) error {
return err
}

// the target location where the dir/file should be created
target := filepath.Join(dst, header.Name)

switch header.Typeflag {
case tar.TypeDir:
if err := os.Mkdir(header.Name, 0755); err != nil {
if err := os.Mkdir(target, 0755); err != nil {
return err
}
case tar.TypeReg:
outFile, err := os.Create(header.Name)
outFile, err := os.Create(target)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion vm/internal/handler/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (h *Handler) ImportTarGzFile(ctx echo.Context) error {
// remove hidden and not-hidden files and folders:
// ..?* matches all dot-dot files except '..'
// .[!.]* matches all dot files except '.' and files whose name begins with '..'
Cmd: []string{"/bin/sh", "-c", "rm -rf /vackup-volume/..?* /vackup-volume/.[!.]* /vackup-volume/* && tar -xvzf /vackup"},
Cmd: []string{"/bin/sh", "-c", "rm -rf /vackup-volume/..?* /vackup-volume/.[!.]* /vackup-volume/* && tar -xvzf /vackup -C /vackup-volume"},
Labels: map[string]string{
"com.docker.desktop.extension": "true",
"com.docker.desktop.extension.name": "Volumes Backup & Share",
Expand Down
Binary file modified vm/internal/handler/testdata/import/nginx.tar.gz
Binary file not shown.

0 comments on commit e9f1c16

Please sign in to comment.