diskutil/clone.go
mitch 624c112176 Improved notifications
Added progress indicator in clone output
2021-12-31 02:17:42 -05:00

52 lines
1.4 KiB
Go

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 (
"fmt"
"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)
}
defer fTarget.Close()
errorHandler(err)
progress := cloneBs
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)
fmt.Println(progress/1048576, "MB written...")
progress += cloneBs
}
fmt.Println("Performing sync operations...")
fTarget.Sync()
}