-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource_client.go
More file actions
162 lines (130 loc) · 3.54 KB
/
source_client.go
File metadata and controls
162 lines (130 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"fmt"
"io"
"os"
"time"
"github.com/u-root/u-root/pkg/mount/block"
"net"
)
const (
BlockSize = 1024 * 1024
)
type Device struct{
Details *block.BlockDev
MountPoint *string
DeviceBlockSize uint64 //Size of the block device in bytes
DeviceName string
}
type DeviceReplicator struct {
DeviceDetails Device
Blocksize int
}
func (device *Device) GetDetails() error{
// Get device details
// Check for the device
var err error
device.Details, err = block.Device(device.DeviceName)
if err != nil{
fmt.Println("Device not found")
return err
}
fmt.Println("Device Found Details: ", device.Details)
// Get Device Path
devicePath := device.Details.DevicePath()
fmt.Println("Device Path: ", devicePath)
// Get Device Mount Point
device.MountPoint, err= block.GetMountpointByDevice(devicePath)
if(err != nil){
fmt.Println("Mount point ot found for device: ",device.DeviceName)
return err
}
fmt.Println("Device Mount : ", *device.MountPoint)
device.DeviceBlockSize, err = device.Details.Size()
if(err!=nil){
fmt.Println("Error in finding out size: ", err)
return err
}
fmt.Println("Device Size in bytes: ", device.DeviceBlockSize)
return nil
}
func (dr *DeviceReplicator) ReplicateToDestination() error{
//Open Source Device for reading
source, err := os.OpenFile(dr.DeviceDetails.DeviceName,os.O_RDONLY,0)
if (err != nil){
return err
}
defer source.Close()
/// Connect to server
conn,err := net.Dial("tcp","localhost:4000")
if err != nil{
return err
}
defer conn.Close()
fmt.Printf("Connected to the server")
fmt.Printf("Replicating Device %s (%d bytes) ...\n",dr.DeviceDetails.Details,dr.DeviceDetails.DeviceBlockSize)
/// Read the source and write to network
clientWriter := &ClientWriter{
Writer: conn,
Total: dr.DeviceDetails.DeviceBlockSize,
StartTime: time.Now(),
BlockSize: dr.Blocksize,
}
buffer := make([]byte,dr.Blocksize)
fmt.Println("Starting Replication")
// start writing to network
copied, err := io.CopyBuffer(clientWriter,source,buffer)
if err != nil {
fmt.Printf("Error during replication: %v\n", err)
return err
}
fmt.Println("Replication Completed")
fmt.Printf("Total copied: %d bytes (%.2f GB)\n", copied, float64(copied)/(1024*1024*1024))
return nil
}
// ClientWriter to write the data from soirce to netweork
type ClientWriter struct{
Writer io.Writer
Total uint64
Current uint64
StartTime time.Time
lastReport uint64
BlockSize uint64
}
// Client Writer that will write to network
func (cw *ClientWriter) Write(p []byte) (int,error){
n,err := cw.Writer.Write(p)
cw.Current += uint64(n)
reportInterval := uint64(100*1024*1024)
blockInterval := cw.BlockSize * 1000
if(blockInterval<reportInterval){
reportInterval=blockInterval
}
if(cw.Current-cw.lastReport>=reportInterval || err==io.EOF){
percentage := float64(cw.Current)/float64(cw.Total) * 100
elapsed := time.Since(cw.StartTime)
speed := float64(cw.Current)/elapsed.Seconds() / (1024*1024) //MB/S
blocksProcessed := cw.Current/cw.BlockSize
fmt.Printf("\rProgress: %.1f%% (%d/%d bytes, %d blocks) - Speed: %.1f MB/s",
percentage, cw.Current, cw.Total, blocksProcessed, speed)
cw.lastReport=cw.Current
}
return n,err
}
func main(){
// Get a device details
//Source device
device := Device{
DeviceName: "/dev/nvme0n1",
}
if err:=device.GetDetails(); err!=nil{
fmt.Println("Error",err)
}
dr := DeviceReplicator{
DeviceDetails: device,
BlockSize: 1024,
}
if err:=dr.ReplicateToDestination(); err!=nil{
fmt.Println("Replication Function Error: ",err)
}
}