forked from microsoft/react-native-code-push
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateManager.cs
More file actions
330 lines (288 loc) · 15.1 KB
/
UpdateManager.cs
File metadata and controls
330 lines (288 loc) · 15.1 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
using CodePush.Net46.Adapters.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using PCLStorage;
using System;
using System.IO;
using System.IO.Compression;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
namespace CodePush.ReactNative
{
internal class UpdateManager
{
#region Internal methods
internal async Task ClearUpdatesAsync()
{
await (await UpdateUtils.GetCodePushFolderAsync().ConfigureAwait(false)).DeleteAsync().ConfigureAwait(false);
}
internal async Task DownloadPackageAsync(JObject updatePackage, string expectedBundleFileName, Progress<HttpProgress> downloadProgress)
{
// Using its hash, get the folder where the new update will be saved
var codePushFolder = await UpdateUtils.GetCodePushFolderAsync().ConfigureAwait(false);
var newUpdateHash = (string)updatePackage[CodePushConstants.PackageHashKey];
var newUpdateFolder = await GetPackageFolderAsync(newUpdateHash, false).ConfigureAwait(false);
if (newUpdateFolder != null)
{
// This removes any stale data in newUpdateFolder that could have been left
// uncleared due to a crash or error during the download or install process.
await newUpdateFolder.DeleteAsync().ConfigureAwait(false);
}
newUpdateFolder = await GetPackageFolderAsync(newUpdateHash, true).ConfigureAwait(false);
var newUpdateMetadataFile = await newUpdateFolder.CreateFileAsync(CodePushConstants.PackageFileName, CreationCollisionOption.ReplaceExisting).ConfigureAwait(false);
var downloadUrlString = (string)updatePackage[CodePushConstants.DownloadUrlKey];
var downloadFile = await GetDownloadFileAsync().ConfigureAwait(false);
await UpdateUtils.DownloadBundleAsync(downloadUrlString, downloadFile.Path, downloadProgress);
try
{
// Unzip the downloaded file and then delete the zip
var unzippedFolder = await CreateUnzippedFolderAsync().ConfigureAwait(false);
/**
* TODO:
* 1) ZipFile.ExtractToDirectory is not reliable and throws exception if:
* - path is too long (> 250 chars)
*
* 2) Un-zipping is quite long operation. Does it make sense for async?
* await UpdateUtils.UnzipBundleAsync(downloadFile.Path, unzippedFolder.Path);
*
* Possible implementation
*
* internal async static Task UnzipBundleAsync(string zipFileName, string targetDir)
* {
* await Task.Run(() =>
* {
* ZipFile.ExtractToDirectory(zipFileName, targetDir)
* return Task.CompletedTask;
* });
* }
*/
ZipFile.ExtractToDirectory(downloadFile.Path, unzippedFolder.Path);
await downloadFile.DeleteAsync().ConfigureAwait(false);
// Merge contents with current update based on the manifest
IFile diffManifestFile = null;
try
{
diffManifestFile = await unzippedFolder.GetFileAsync(CodePushConstants.DiffManifestFileName).ConfigureAwait(false);
}
catch (FileNotFoundException)
{
//file may not be present in folder just skip it
}
if (diffManifestFile != null)
{
var currentPackageFolder = await GetCurrentPackageFolderAsync().ConfigureAwait(false);
if (currentPackageFolder == null)
{
throw new InvalidDataException("Received a diff update, but there is no current version to diff against.");
}
await UpdateUtils.CopyNecessaryFilesFromCurrentPackageAsync(diffManifestFile, currentPackageFolder, newUpdateFolder).ConfigureAwait(false);
await diffManifestFile.DeleteAsync().ConfigureAwait(false);
}
await FileUtils.MergeFoldersAsync(unzippedFolder, newUpdateFolder).ConfigureAwait(false);
await unzippedFolder.DeleteAsync().ConfigureAwait(false);
// For zip updates, we need to find the relative path to the jsBundle and save it in the
// metadata so that we can find and run it easily the next time.
var relativeBundlePath = await UpdateUtils.FindJSBundleInUpdateContentsAsync(newUpdateFolder, expectedBundleFileName).ConfigureAwait(false);
if (relativeBundlePath == null)
{
throw new InvalidDataException("Update is invalid - A JS bundle file named \"" + expectedBundleFileName + "\" could not be found within the downloaded contents. Please check that you are releasing your CodePush updates using the exact same JS bundle file name that was shipped with your app's binary.");
}
else
{
if (diffManifestFile != null)
{
// TODO verify hash for diff update
// CodePushUpdateUtils.verifyHashForDiffUpdate(newUpdateFolderPath, newUpdateHash);
}
updatePackage[CodePushConstants.RelativeBundlePathKey] = relativeBundlePath;
}
}
catch (InvalidDataException)
{
// Downloaded file is not a zip, assume it is a jsbundle
await downloadFile.RenameAsync(expectedBundleFileName).ConfigureAwait(false);
await downloadFile.MoveAsync(newUpdateFolder.Path, NameCollisionOption.ReplaceExisting).ConfigureAwait(false);
}
// Save metadata to the folder
await newUpdateMetadataFile.WriteAllTextAsync(JsonConvert.SerializeObject(updatePackage)).ConfigureAwait(false);
}
internal async Task<JObject> GetCurrentPackageAsync()
{
var packageHash = await GetCurrentPackageHashAsync().ConfigureAwait(false);
return packageHash == null ? null : await GetPackageAsync(packageHash).ConfigureAwait(false);
}
internal async Task<IFile> GetCurrentPackageBundleAsync(string bundleFileName)
{
var packageFolder = await GetCurrentPackageFolderAsync().ConfigureAwait(false);
if (packageFolder == null)
{
return null;
}
var currentPackage = await GetCurrentPackageAsync().ConfigureAwait(false);
var relativeBundlePath = (string)currentPackage[CodePushConstants.RelativeBundlePathKey];
return relativeBundlePath == null
? await packageFolder.GetFileAsync(bundleFileName).ConfigureAwait(false)
: await packageFolder.GetFileAsync(relativeBundlePath).ConfigureAwait(false);
}
internal async Task<string> GetCurrentPackageHashAsync()
{
var info = await GetCurrentPackageInfoAsync().ConfigureAwait(false);
var currentPackageShortHash = (string)info[CodePushConstants.CurrentPackageKey];
if (currentPackageShortHash == null)
{
return null;
}
var currentPackageMetadata = await GetPackageAsync(currentPackageShortHash).ConfigureAwait(false);
return currentPackageMetadata == null ? null : (string)currentPackageMetadata[CodePushConstants.PackageHashKey];
}
internal async Task<JObject> GetPackageAsync(string packageHash)
{
var packageFolder = await GetPackageFolderAsync(packageHash, false).ConfigureAwait(false);
if (packageFolder == null)
{
return null;
}
try
{
var packageFile = await packageFolder.GetFileAsync(CodePushConstants.PackageFileName).ConfigureAwait(false);
return await CodePushUtils.GetJObjectFromFileAsync(packageFile).ConfigureAwait(false);
}
catch (IOException)
{
return null;
}
}
internal async Task<IFolder> GetPackageFolderAsync(string packageHash, bool createIfNotExists)
{
var codePushFolder = await UpdateUtils.GetCodePushFolderAsync().ConfigureAwait(false);
try
{
packageHash = ShortenPackageHash(packageHash);
return createIfNotExists
? await codePushFolder.CreateFolderAsync(packageHash, CreationCollisionOption.OpenIfExists).ConfigureAwait(false)
: await codePushFolder.GetFolderAsync(packageHash).ConfigureAwait(false);
}
catch (FileNotFoundException)
{
return null;
}
catch (DirectoryNotFoundException)
{
return null;
}
}
internal async Task<JObject> GetPreviousPackageAsync()
{
var packageHash = await GetPreviousPackageHashAsync().ConfigureAwait(false);
return packageHash == null ? null : await GetPackageAsync(packageHash).ConfigureAwait(false);
}
internal async Task<string> GetPreviousPackageHashAsync()
{
var info = await GetCurrentPackageInfoAsync().ConfigureAwait(false);
var previousPackageShortHash = (string)info[CodePushConstants.PreviousPackageKey];
if (previousPackageShortHash == null)
{
return null;
}
var previousPackageMetadata = await GetPackageAsync(previousPackageShortHash).ConfigureAwait(false);
return previousPackageMetadata == null ? null : (string)previousPackageMetadata[CodePushConstants.PackageHashKey];
}
internal async Task InstallPackageAsync(JObject updatePackage, bool currentUpdateIsPending)
{
var packageHash = (string)updatePackage[CodePushConstants.PackageHashKey];
var info = await GetCurrentPackageInfoAsync().ConfigureAwait(false);
if (currentUpdateIsPending)
{
// Don't back up current update to the "previous" position because
// it is an unverified update which should not be rolled back to.
var currentPackageFolder = await GetCurrentPackageFolderAsync().ConfigureAwait(false);
if (currentPackageFolder != null)
{
await currentPackageFolder.DeleteAsync().ConfigureAwait(false);
}
}
else
{
var previousPackageHash = await GetPreviousPackageHashAsync().ConfigureAwait(false);
if (previousPackageHash != null && !previousPackageHash.Equals(packageHash))
{
var previousPackageFolder = await GetPackageFolderAsync(previousPackageHash, false).ConfigureAwait(false);
if (previousPackageFolder != null)
{
await previousPackageFolder.DeleteAsync().ConfigureAwait(false);
}
}
info[CodePushConstants.PreviousPackageKey] = info[CodePushConstants.CurrentPackageKey];
}
info[CodePushConstants.CurrentPackageKey] = packageHash;
await UpdateCurrentPackageInfoAsync(info).ConfigureAwait(false);
}
internal async Task RollbackPackageAsync()
{
var info = await GetCurrentPackageInfoAsync().ConfigureAwait(false);
var currentPackageFolder = await GetCurrentPackageFolderAsync().ConfigureAwait(false);
if (currentPackageFolder != null)
{
await currentPackageFolder.DeleteAsync().ConfigureAwait(false);
}
info[CodePushConstants.CurrentPackageKey] = info[CodePushConstants.PreviousPackageKey];
info[CodePushConstants.PreviousPackageKey] = null;
await UpdateCurrentPackageInfoAsync(info).ConfigureAwait(false);
}
#endregion
#region Private methods
private async Task<IFolder> GetCurrentPackageFolderAsync()
{
var info = await GetCurrentPackageInfoAsync().ConfigureAwait(false);
if (info == null)
{
return null;
}
var packageHash = (string)info[CodePushConstants.CurrentPackageKey];
return packageHash == null ? null : await GetPackageFolderAsync(packageHash, false).ConfigureAwait(false);
}
private async Task<JObject> GetCurrentPackageInfoAsync()
{
var statusFile = await GetStatusFileAsync().ConfigureAwait(false);
var info = await CodePushUtils.GetJObjectFromFileAsync(statusFile).ConfigureAwait(false);
if (info != null)
{
return info;
}
// info file has been corrupted - re-create it
await statusFile.DeleteAsync().ConfigureAwait(false);
statusFile = await GetStatusFileAsync().ConfigureAwait(false);
return await CodePushUtils.GetJObjectFromFileAsync(statusFile).ConfigureAwait(false);
}
private async Task<IFile> GetDownloadFileAsync()
{
var codePushFolder = await UpdateUtils.GetCodePushFolderAsync().ConfigureAwait(false);
return await codePushFolder.CreateFileAsync(CodePushConstants.DownloadFileName, CreationCollisionOption.OpenIfExists).ConfigureAwait(false);
}
private async Task<IFile> GetStatusFileAsync()
{
var codePushFolder = await UpdateUtils.GetCodePushFolderAsync().ConfigureAwait(false);
return await codePushFolder.CreateFileAsync(CodePushConstants.StatusFileName, CreationCollisionOption.OpenIfExists).ConfigureAwait(false);
}
private async Task<IFolder> CreateUnzippedFolderAsync()
{
var codePushFolder = await UpdateUtils.GetCodePushFolderAsync().ConfigureAwait(false);
var isUnzippedFolderExists = await codePushFolder.CheckExistsAsync(CodePushConstants.UnzippedFolderName).ConfigureAwait(false);
if (isUnzippedFolderExists != ExistenceCheckResult.NotFound)
{
await codePushFolder.GetFolderAsync(CodePushConstants.UnzippedFolderName).ContinueWith((existingFolder) => existingFolder.Result.DeleteAsync());
}
return await codePushFolder.CreateFolderAsync(CodePushConstants.UnzippedFolderName, CreationCollisionOption.OpenIfExists).ConfigureAwait(false);
}
private string ShortenPackageHash(string longPackageHash)
{
return longPackageHash.Substring(0, 8);
}
private async Task UpdateCurrentPackageInfoAsync(JObject packageInfo)
{
var file = await GetStatusFileAsync().ConfigureAwait(false);
await file.WriteAllTextAsync(JsonConvert.SerializeObject(packageInfo)).ConfigureAwait(false);
}
#endregion
}
}