diskutil/clone.go

48 lines
1.3 KiB
Go
Raw Normal View History

2021-12-31 06:32:24 +00:00
package main
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <mitch@nerdfortress.dev> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return
* ----------------------------------------------------------------------------
*/
import (
"io"
"os"
)
/**
clone copies the contents of cloneSource to cloneTarget
@param cloneBs int is the block size used for the cloning
@param cloneSource string is the source file / volume for the clone
@param cloneTarget string is the target file / volume for the clone
*/
func clone(cloneBs int, cloneSource string, cloneTarget string) {
p := make([]byte, cloneBs)
fSource, err := os.Open(cloneSource)
defer fSource.Close()
errorHandler(err)
fTarget, err := os.OpenFile(cloneTarget, os.O_WRONLY, 0644)
if os.IsNotExist(err) {
fTarget, err = os.Create(cloneTarget)
}
2021-12-31 06:32:24 +00:00
defer fTarget.Close()
errorHandler(err)
//todo progress indicator
for {
n, err := fSource.Read(p)
if err == io.EOF {
_, err = fTarget.Write(p[:n])
errorHandler(err)
break
}
errorHandler(err)
_, err = fTarget.Write(p[:n])
errorHandler(err)
}
fTarget.Sync()
}