Skip to content

Instantly share code, notes, and snippets.

@gaurav-gogia
Created March 20, 2019 17:10
Show Gist options
  • Save gaurav-gogia/0d3b26c35d4c6e75defdfc60996d84fb to your computer and use it in GitHub Desktop.
Save gaurav-gogia/0d3b26c35d4c6e75defdfc60996d84fb to your computer and use it in GitHub Desktop.
copying from src to dst big file
func (dd *opts) run() error {
var thread int64
var names []string
done := make(chan bool)
size := getsize(*dd.src)
for i := int64(0); i < size; i += *dd.buffersize {
dstname := partfile + strconv.FormatInt(i, 10)
names = append(names, dstname)
go parts(*dd.buffersize, *dd.src, dstname, done)
thread++
if thread > 16 {
<-done
thread--
}
fmt.Printf("\rSpawning %d threads", thread)
}
fmt.Println()
for thread > 0 {
<-done
thread--
}
file, err := os.OpenFile(*dd.dst, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0777)
defer file.Close()
handle(err)
for _, name := range names {
data, err := ioutil.ReadFile(name)
handle(err)
_, err = file.Write(data)
handle(err)
//os.Remove(name)
fmt.Printf("\rCompiling .... %s", name)
}
fmt.Println()
return nil
}
func parts(buffersize int64, src, dst string, done chan bool) {
destination, err := create(dst)
handle(err)
destination.Close()
buff := make([]byte, buffersize)
read, err := unix.Open(src, unix.O_RDONLY, 0777)
defer unix.Close(read)
handle(err)
write, err := unix.Open(dst, unix.O_WRONLY, 0777)
defer unix.Close(write)
handle(err)
_, err = unix.Read(read, buff)
handle(err)
_, err = unix.Write(write, buff)
handle(err)
done <- true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment