From 7d4accc4414a7e06c231d0ae13e906e9e621a20c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B4=94=E7=AB=9E=E5=AE=81?= Date: Thu, 8 Dec 2022 23:38:37 +0800 Subject: [PATCH] fix: linear --- job.go | 23 ++++++++++++++++++++++- opt.go | 4 ++++ prepare.go | 4 ++-- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/job.go b/job.go index 17b922e..8f8331f 100644 --- a/job.go +++ b/job.go @@ -126,9 +126,30 @@ type writeJob struct { ch chan struct{} } +func newWriteJob(job *baseJob, src *mmap.ReaderAt, needWait bool) *writeJob { + j := &writeJob{ + baseJob: job, + src: src, + } + if needWait { + j.ch = make(chan struct{}) + } + return j +} + func (wj *writeJob) done() { wj.src.Close() - close(wj.ch) + + if wj.ch != nil { + close(wj.ch) + } +} + +func (wj *writeJob) wait() { + if wj.ch == nil { + return + } + <-wj.ch } type Job struct { diff --git a/opt.go b/opt.go index e58e4cc..f6aaeb9 100644 --- a/opt.go +++ b/opt.go @@ -91,6 +91,10 @@ func (o *option) check() error { o.fromDevice.check() o.toDevice.check() + if o.fromDevice.linear || o.toDevice.linear { + o.fromDevice.threads = 1 + o.toDevice.threads = 1 + } if o.logger == nil { o.logger = logrus.StandardLogger() } diff --git a/prepare.go b/prepare.go index b4f891d..ff2aee7 100644 --- a/prepare.go +++ b/prepare.go @@ -45,9 +45,9 @@ func (c *Copyer) prepare(ctx context.Context, indexed <-chan *baseJob) <-chan *w return } - wj := &writeJob{baseJob: job, src: file, ch: make(chan struct{})} + wj := newWriteJob(job, file, c.fromDevice.linear) ch <- wj - <-wj.ch + wj.wait() } } })