@@ -51,6 +51,12 @@ pub struct SnapshotUploader {
51
51
ebs_client : EbsClient ,
52
52
}
53
53
54
+ struct SnapshotPrep {
55
+ snapshot_id : String ,
56
+ file_size : i64 ,
57
+ block_size : i32 ,
58
+ }
59
+
54
60
impl SnapshotUploader {
55
61
pub fn new ( ebs_client : EbsClient ) -> Self {
56
62
SnapshotUploader { ebs_client }
@@ -70,7 +76,36 @@ impl SnapshotUploader {
70
76
description : Option < & str > ,
71
77
progress_bar : Option < ProgressBar > ,
72
78
) -> Result < String > {
73
- let path = path. as_ref ( ) ;
79
+ let SnapshotPrep {
80
+ snapshot_id,
81
+ file_size,
82
+ block_size,
83
+ } = self
84
+ . prepare_snapshot_upload ( path. as_ref ( ) , volume_size, description)
85
+ . await ?;
86
+
87
+ self . upload_blocks_from_file (
88
+ path. as_ref ( ) ,
89
+ progress_bar,
90
+ file_size,
91
+ block_size,
92
+ & snapshot_id,
93
+ )
94
+ . await
95
+ . map_err ( |e| {
96
+ eprintln ! ( "Error during upload of {}" , snapshot_id) ;
97
+ e
98
+ } ) ?;
99
+
100
+ Ok ( snapshot_id)
101
+ }
102
+
103
+ async fn prepare_snapshot_upload (
104
+ & self ,
105
+ path : & Path ,
106
+ volume_size : Option < i64 > ,
107
+ description : Option < & str > ,
108
+ ) -> Result < SnapshotPrep > {
74
109
let description = description. map ( |s| s. to_string ( ) ) . unwrap_or_else ( || {
75
110
path. file_name ( )
76
111
. unwrap_or_else ( || OsStr :: new ( "" ) )
@@ -104,6 +139,20 @@ impl SnapshotUploader {
104
139
// Start the snapshot, which gives us the ID and block size we need.
105
140
debug ! ( "Uploading {}G to snapshot..." , volume_size) ;
106
141
let ( snapshot_id, block_size) = self . start_snapshot ( volume_size, description) . await ?;
142
+ Ok ( SnapshotPrep {
143
+ snapshot_id,
144
+ file_size,
145
+ block_size,
146
+ } )
147
+ }
148
+ async fn upload_blocks_from_file (
149
+ & self ,
150
+ path : & Path ,
151
+ progress_bar : Option < ProgressBar > ,
152
+ file_size : i64 ,
153
+ block_size : i32 ,
154
+ snapshot_id : & str ,
155
+ ) -> Result < ( ) > {
107
156
let file_blocks = ( file_size + i64:: from ( block_size - 1 ) ) / i64:: from ( block_size) ;
108
157
let file_blocks =
109
158
i32:: try_from ( file_blocks) . with_context ( |_| error:: ConvertNumberSnafu {
@@ -158,7 +207,7 @@ impl SnapshotUploader {
158
207
data_length,
159
208
block_index : i,
160
209
block_size,
161
- snapshot_id : snapshot_id. clone ( ) ,
210
+ snapshot_id : snapshot_id. to_string ( ) ,
162
211
changed_blocks_count : Arc :: clone ( & changed_blocks_count) ,
163
212
block_digests : Arc :: clone ( & block_digests) ,
164
213
block_errors : Arc :: clone ( & block_errors) ,
@@ -212,7 +261,7 @@ impl SnapshotUploader {
212
261
let error_report: String = block_errors. values ( ) . map ( |e| e. to_string ( ) ) . collect ( ) ;
213
262
error:: PutSnapshotBlocksSnafu {
214
263
error_count : block_errors_count,
215
- snapshot_id : snapshot_id . clone ( ) ,
264
+ snapshot_id,
216
265
error_report,
217
266
}
218
267
. fail ( ) ?;
@@ -236,7 +285,7 @@ impl SnapshotUploader {
236
285
self . complete_snapshot ( & snapshot_id, changed_blocks_count, & full_hash)
237
286
. await ?;
238
287
239
- Ok ( snapshot_id )
288
+ Ok ( ( ) )
240
289
}
241
290
242
291
/// Find the size of a file.
0 commit comments