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

fix copy sym links #135

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions cmd/xgo/runtime_gen/core/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
)

const VERSION = "1.0.36"
const REVISION = "110daef2be989ffe0f7a2111e4a8e75272a4b6d3+1"
const NUMBER = 227
const REVISION = "fce3dfc3c3587abde57944d6b16030437a9d8ce9+1"
const NUMBER = 228

// these fields will be filled by compiler
const XGO_VERSION = ""
Expand Down
4 changes: 2 additions & 2 deletions cmd/xgo/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package main
import "fmt"

const VERSION = "1.0.36"
const REVISION = "110daef2be989ffe0f7a2111e4a8e75272a4b6d3+1"
const NUMBER = 227
const REVISION = "fce3dfc3c3587abde57944d6b16030437a9d8ce9+1"
const NUMBER = 228

func getRevision() string {
revSuffix := ""
Expand Down
4 changes: 2 additions & 2 deletions runtime/core/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
)

const VERSION = "1.0.36"
const REVISION = "110daef2be989ffe0f7a2111e4a8e75272a4b6d3+1"
const NUMBER = 227
const REVISION = "fce3dfc3c3587abde57944d6b16030437a9d8ce9+1"
const NUMBER = 228

// these fields will be filled by compiler
const XGO_VERSION = ""
Expand Down
15 changes: 15 additions & 0 deletions support/filecopy/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,21 @@ func copyDirHandle(srcDir string, targetAbsDir string, opts *copyOptions, handle
if isDir {
return os.MkdirAll(dstPath, 0755)
}

// handle symlink and other files
typ := d.Type()
if !typ.IsRegular() {
if (typ & fs.ModeSymlink) == 0 {
return nil
}
// create symlink
target, err := os.Readlink(path)
if err != nil {
return err
}
return os.Symlink(target, dstPath)
}

return handler(path, dstPath)
})
}
Expand Down
119 changes: 119 additions & 0 deletions support/filecopy/copy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package filecopy

import (
"fmt"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"testing"
)

func testCopyReplace(prepare func(rootDir string, srcDir string, dstDir string) error, check func(rootDir string, srcDir string, dstDir string) error) error {
tmpDir, err := os.MkdirTemp("", "copy-with-link")
if err != nil {
return err
}
defer os.RemoveAll(tmpDir)

srcDir := filepath.Join(tmpDir, "src")
err = os.MkdirAll(srcDir, 0755)
if err != nil {
return err
}
dstDir := filepath.Join(tmpDir, "dst")

err = prepare(tmpDir, srcDir, dstDir)
if err != nil {
return err
}
err = CopyReplaceDir(srcDir, dstDir, false)
if err != nil {
return err
}
if check == nil {
return nil
}
return check(tmpDir, srcDir, dstDir)
}

func TestCopyWithSymLinkFiles(t *testing.T) {
// doc.txt
// src/
// a.txt
// doc.txt -> ../doc.txt
err := testCopyReplace(func(rootDir, srcDir, dstDir string) error {
docTxt := filepath.Join(rootDir, "doc.txt")
err := ioutil.WriteFile(docTxt, []byte("doc"), 0755)
if err != nil {
return err
}

err = ioutil.WriteFile(filepath.Join(srcDir, "a.txt"), []byte("test"), 0755)
if err != nil {
return err
}
err = os.Symlink(docTxt, filepath.Join(srcDir, "doc.txt"))
if err != nil {
return err
}
return nil
}, func(rootDir, srcDir, dstDir string) error {
ok, err := checkIsSymLink(filepath.Join(dstDir, "doc.txt"))
if err != nil {
return err
}
if !ok {
return fmt.Errorf("expect dst/doc.txt to be sym link, actually not")
}
return nil
})
if err != nil {
t.Fatal(err)
}
}

func checkIsSymLink(file string) (bool, error) {
finfo, err := os.Lstat(file)
if err != nil {
return false, err
}
if finfo.Mode()&fs.ModeSymlink != 0 {
return true, nil
}
return false, nil
}
func TestCopyWithSymLinkDirs(t *testing.T) {
// doc.txt
// src/
// a.txt
// doc.txt -> ../doc.txt
err := testCopyReplace(func(rootDir, srcDir, dstDir string) error {
docDir := filepath.Join(rootDir, "doc")
err := os.MkdirAll(docDir, 0755)
if err != nil {
return err
}
err = ioutil.WriteFile(filepath.Join(docDir, "doc.txt"), []byte("doc"), 0755)
if err != nil {
return err
}
err = os.Symlink(docDir, filepath.Join(srcDir, "doc"))
if err != nil {
return err
}
return nil
}, func(rootDir, srcDir, dstDir string) error {
ok, err := checkIsSymLink(filepath.Join(dstDir, "doc"))
if err != nil {
return err
}
if !ok {
return fmt.Errorf("expect dst/doc to be sym link, actually not")
}
return nil
})
if err != nil {
t.Fatal(err)
}
}
Loading