Skip to content

Commit

Permalink
Add copied and total bytes to progress report
Browse files Browse the repository at this point in the history
  • Loading branch information
pkorotkov committed Mar 14, 2016
1 parent dac6706 commit 542e8b3
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 18 deletions.
19 changes: 12 additions & 7 deletions cloner.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,8 @@ func (cs *CloningSession) Close() error {

func (cs *CloningSession) readSectors(progress chan *ProgressMessage, reports chan *cloningReport, quit chan os.Signal) {
defer close(progress)
lss := int(cs.diskProfile.LogicalSectorSize)
sector := make([]byte, lss)
zeroSector := make([]byte, lss, lss)
sector := make([]byte, cs.diskProfile.LogicalSectorSize)
zeroSector := make([]byte, cs.diskProfile.LogicalSectorSize, cs.diskProfile.LogicalSectorSize)
count, portion := 1.0, 0
md5h, sha1h, sha256h, sha512h := md5.New(), sha1.New(), sha256.New(), sha512.New()
hw := io.MultiWriter(md5h, sha1h, sha256h, sha512h)
Expand All @@ -93,9 +92,9 @@ func (cs *CloningSession) readSectors(progress chan *ProgressMessage, reports ch
if err != io.EOF {
log.Warning("detected unreadable sector at offset %d", cp)
unreadSectors = append(unreadSectors, cp)
sector, n = zeroSector, lss
sector, n = zeroSector, cs.diskProfile.LogicalSectorSize
// Jump to the next sector.
cs.disk.Seek(int64(lss), 1)
cs.disk.Seek(int64(cs.diskProfile.LogicalSectorSize), 1)
} else {
// This is the check of final call with (0, io.EOF) result.
if n == 0 {
Expand Down Expand Up @@ -133,12 +132,18 @@ func (cs *CloningSession) readSectors(progress chan *ProgressMessage, reports ch
}
}
// Report progress.
portion += lss
portion += cs.diskProfile.LogicalSectorSize
if p := float64(portion) / float64(cs.diskProfile.Capacity); p >= count*0.0001 {
count++
// TODO: Use sync.Pool.
// TODO: Take a progress message object from pool.
progress <- &ProgressMessage{cs.name, cs.uuid, int(count)}
progress <- &ProgressMessage{
cs.name,
cs.uuid,
int64(portion),
cs.diskProfile.Capacity,
int(count),
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions cloning-report.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import "time"
type diskProfile struct {
Type string `json:"type"`
SerialNumber string `json:"serial_number"`
PhysicalSectorSize int32 `json:"physical_sector_size"`
LogicalSectorSize int32 `json:"logical_sector_size"`
PhysicalSectorSize int `json:"physical_sector_size"`
LogicalSectorSize int `json:"logical_sector_size"`
Capacity int64 `json:"capacity"`
}

Expand Down
8 changes: 5 additions & 3 deletions internal/progress-message.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package internal

type ProgressMessage struct {
Name string
UUID string
Count int
Name string
UUID string
CopiedBytes int64
TotalBytes int64
Count int
}
6 changes: 3 additions & 3 deletions internal/utils_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func getRHSValue(bs []byte) string {
return string(bytes.TrimSpace(bytes.Split(bs, []byte{61})[1]))
}

func GetDiskProfile(disk *os.File) (dt, sn string, pss, lss int32, c int64) {
func GetDiskProfile(disk *os.File) (dt, sn string, pss, lss int, c int64) {
var (
err error
out []byte
Expand All @@ -101,8 +101,8 @@ func GetDiskProfile(disk *os.File) (dt, sn string, pss, lss int32, c int64) {
}
}
fd := C.int(disk.Fd())
pss = int32(C.get_disk_physical_sector_size(fd))
lss = int32(C.get_disk_logical_sector_size(fd))
pss = int(C.get_disk_physical_sector_size(fd))
lss = int(C.get_disk_logical_sector_size(fd))
c = int64(C.get_disk_capacity_in_bytes(fd))
return
}
Expand Down
7 changes: 4 additions & 3 deletions status-monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ func monitorStatus(quit chan os.Signal) {
if m != nil {
bar, ok := sessions[m.UUID]
if !ok {
sessions[m.UUID] = uip.AddBar(10000).AppendCompleted()
sessions[m.UUID] = uip.AddBar(10000)
bar = sessions[m.UUID]
bar.PrependFunc(func(bar *uip.Bar) string {
return Sprintf("%s[%s...%s]", m.Name, m.UUID[0:6], m.UUID[32:36])
bar.AppendCompleted()
bar.PrependFunc(func(b *uip.Bar) string {
return Sprintf("%s (%s...%s)", m.Name, m.UUID[0:6], m.UUID[32:36])
})
}
bar.Set(m.Count)
Expand Down

0 comments on commit 542e8b3

Please sign in to comment.