-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisk.mjs
More file actions
92 lines (84 loc) · 2.1 KB
/
Copy pathdisk.mjs
File metadata and controls
92 lines (84 loc) · 2.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
import { SZ } from "./const.mjs";
import { loadPartitionTable } from "./dao.mjs";
import { createFileSystem } from "./fs.mjs";
import { createIO } from "./io.mjs";
import { Driver } from "./types.mjs";
/**
* @implements {libmount.Disk}
*/
class Disk {
/**
* @param {!Driver} driver
* @param {!libmount.Codepage} cp
*/
constructor(driver, cp) {
this.driver = driver;
this.cp = cp;
}
// ns.Disk
/**
* @override
* @return {number}
*/
// @ts-expect-error
capacity() {
return this.driver.len();
}
/**
* @override
* @return {?libmount.FileSystem}
*/
// @ts-expect-error
getFileSystem() {
return createFileSystem(this.driver, this.cp);
}
/**
* @override
* @return {!Array<!libmount.Partition>}
*/
// @ts-expect-error
getPartitions() {
if (this.driver.len() < SZ) {
return [];
}
const array = this.driver.readUint8Array(0, SZ);
const io = createIO(array);
try {
return loadPartitionTable(io).map(({ BootIndicator, SystemID, RelativeSectors, TotalSectors }) => ({
active: BootIndicator === 0x80,
type: SystemID,
relativeSectors: RelativeSectors,
totalSectors: TotalSectors,
}));
// @ts-expect-error
} catch (/** @type {!Error} */ e) {
if (e.name === "ValidationError") {
// console.warn("No partition table", e);
return [];
}
throw e;
}
}
/**
* @override
* @param {!libmount.DiskSectors} diskSectors
*/
// @ts-expect-error
write(diskSectors) {
const { driver } = this;
const { bytsPerSec, zeroRegions, dataSectors } = diskSectors;
for (const { /** @type {number} */ i, /** @type {number} */ count } of zeroRegions) {
driver.writeBytes(bytsPerSec * i, 0, bytsPerSec * count);
}
for (const { /** @type {number} */ i, /** @type {!Uint8Array} */ data } of dataSectors) {
driver.writeUint8Array(bytsPerSec * i, data);
}
}
}
// Export
/**
* @param {!Driver} driver
* @param {!libmount.Codepage} cp
* @return {!libmount.Disk}
*/
export const createDisk = (driver, cp) => new Disk(driver, cp);