syscopy windows

This commit is contained in:
Josh Yan 2024-07-05 16:09:10 -07:00
parent 72314bf4b5
commit 8048ce0816

View File

@ -1,34 +1,39 @@
//go:build windows
// +build windows
package cmd package cmd
import "errors" import (
"fmt"
"os"
"path/filepath"
"syscall"
"unsafe"
)
func localCopy(src, target string) error { func localCopy(src, target string) error {
return errors.New("no local copy implementation for windows") // Create target directory if it doesn't exist
}
/* func localCopy(src, target string) error {
dirPath := filepath.Dir(target) dirPath := filepath.Dir(target)
if err := os.MkdirAll(dirPath, 0o755); err != nil { if err := os.MkdirAll(dirPath, 0o755); err != nil {
return err return err
} }
// Open source file
sourceFile, err := os.Open(src) sourceFile, err := os.Open(src)
if err != nil { if err != nil {
return err return err
} }
defer sourceFile.Close() defer sourceFile.Close()
// Create target file
targetFile, err := os.Create(target) targetFile, err := os.Create(target)
if err != nil { if err != nil {
return err return err
} }
defer targetFile.Close() defer targetFile.Close()
sourceHandle := syscall.Handle(sourceFile.Fd()) // Use CopyFileExW to copy the file
targetHandle := syscall.Handle(targetFile.Fd()) err = copyFileEx(src, target)
err = copyFileEx(sourceHandle, targetHandle)
if err != nil { if err != nil {
return err return err
} }
@ -36,13 +41,24 @@ func localCopy(src, target string) error {
return nil return nil
} }
func copyFileEx(srcHandle, dstHandle syscall.Handle) error { func copyFileEx(src, dst string) error {
fmt.Println("HELELELLEEOEOEOEO")
kernel32 := syscall.NewLazyDLL("kernel32.dll") kernel32 := syscall.NewLazyDLL("kernel32.dll")
copyFileEx := kernel32.NewProc("CopyFileExW") copyFileEx := kernel32.NewProc("CopyFileExW")
srcPtr, err := syscall.UTF16PtrFromString(src)
if err != nil {
return err
}
dstPtr, err := syscall.UTF16PtrFromString(dst)
if err != nil {
return err
}
r1, _, err := copyFileEx.Call( r1, _, err := copyFileEx.Call(
uintptr(srcHandle), uintptr(unsafe.Pointer(srcPtr)),
uintptr(dstHandle), uintptr(unsafe.Pointer(dstPtr)),
0, 0, 0, 0) 0, 0, 0, 0)
if r1 == 0 { if r1 == 0 {
@ -51,4 +67,3 @@ func copyFileEx(srcHandle, dstHandle syscall.Handle) error {
return nil return nil
} }
*/