-
Notifications
You must be signed in to change notification settings - Fork 16
/
multi-destination.ts
179 lines (171 loc) · 4.77 KB
/
multi-destination.ts
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
/*
* Copyright 2018 balena.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Disk } from 'file-disk';
import { Stats, promises as fs } from 'fs';
import { platform } from 'os';
import { Argv } from 'yargs';
import { scanner, sourceDestination } from '../lib';
import { configure as legacyConfigure } from '../lib/source-destination/configured-source/configure';
import { ConfigureFunction } from '../lib/source-destination/configured-source/configured-source';
import {
pipeSourceToDestinationsWithProgressBar,
readJsonFile,
wrapper,
} from './utils';
const FILE_PROTOCOL = 'file://';
async function openUrl(
url: string,
deviceScanner: scanner.Scanner,
write: boolean,
direct = true,
): Promise<
| sourceDestination.Http
| sourceDestination.File
| sourceDestination.BlockDevice
> {
if (url.startsWith('http://') || url.startsWith('https://')) {
return new sourceDestination.Http({ url });
}
if (url.startsWith(FILE_PROTOCOL)) {
url = url.slice(FILE_PROTOCOL.length);
}
const plat = platform();
const lowercaseUrl = url.toLowerCase();
if (plat === 'win32' && lowercaseUrl.startsWith('\\\\.\\physicaldrive')) {
// fs.stat will fail on \\.\PhysicalDriveN on windows
const device = Array.from(deviceScanner.drives.values()).find((d) => {
return d.device !== null && d.device.toLowerCase() === lowercaseUrl;
});
if (
device !== undefined &&
device instanceof sourceDestination.BlockDevice
) {
device.oWrite = write;
device.oDirect = direct;
return device;
}
throw new Error(`Could not open ${url}`);
}
let stats: Stats | undefined;
try {
stats = await fs.stat(url);
} catch (error) {
if (error.code !== 'ENOENT') {
throw error;
}
}
if (stats !== undefined) {
if (
stats.isBlockDevice() ||
(stats.isCharacterDevice() && plat === 'darwin')
) {
const device = Array.from(deviceScanner.drives.values()).find((d) => {
return d.device === url || d.devicePath === url || d.raw === url;
});
if (
device !== undefined &&
device instanceof sourceDestination.BlockDevice
) {
device.oWrite = write;
device.oDirect = direct;
return device;
}
}
} else if (!write) {
// Non existing file
throw new Error(`Invalid url for reading: ${url}`);
}
return new sourceDestination.File({ path: url, write });
}
async function main({
sourceImage,
devices,
verify,
trim,
decompressFirst,
config,
numBuffers,
}: {
sourceImage: string;
devices: string[];
verify: boolean;
trim: boolean;
decompressFirst: boolean;
config: string;
numBuffers: number;
}) {
const adapters = [
new scanner.adapters.BlockDeviceAdapter({
includeSystemDrives: () => false,
unmountOnSuccess: false,
write: true,
direct: true,
}),
];
const deviceScanner = new scanner.Scanner(adapters);
deviceScanner.on('error', console.error);
void deviceScanner.start();
// Wait for the deviceScanner to be ready
await new Promise((resolve) => {
deviceScanner.on('ready', resolve);
});
let configure: ConfigureFunction | undefined;
if (config !== undefined) {
configure = async (disk: Disk) => {
await legacyConfigure(disk, await readJsonFile(config));
};
}
const destinationDrives = await Promise.all(
devices.map((d) => openUrl(d, deviceScanner, true)),
);
const source = await openUrl(
sourceImage,
deviceScanner,
false,
// ConfiguredSource will not work with O_DIRECT
!trim && configure === undefined,
);
deviceScanner.stop();
await pipeSourceToDestinationsWithProgressBar({
source,
destinations: destinationDrives,
verify,
numBuffers,
trim,
decompressFirst,
configure,
});
}
// eslint-disable-next-line @typescript-eslint/no-var-requires
const argv = require('yargs').command(
'$0 <sourceImage> [devices..]',
'Write the sourceImage on all devices.',
(yargs: Argv) => {
yargs.positional('sourceImage', { describe: 'Source image' });
yargs.positional('devices', {
describe: 'Devices to write to',
});
yargs.option('verify', { type: 'boolean', default: false });
yargs.option('trim', { type: 'boolean', default: false });
yargs.option('decompressFirst', { type: 'boolean', default: false });
yargs.option('numBuffers', {
default: 16,
description: 'Number of 1MiB buffers used to buffer data',
});
yargs.describe('config', 'json configuration file');
},
).argv;
wrapper(main, argv);