Skip to content
This repository was archived by the owner on Jul 3, 2020. It is now read-only.

Commit 1048bfc

Browse files
committed
The disk API makes more sense now
1 parent 103fa21 commit 1048bfc

5 files changed

Lines changed: 98 additions & 97 deletions

File tree

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,34 @@
1414

1515
'use strict';
1616

17-
const nameHandle = Symbol('name');
17+
const privates = new WeakMap();
1818

1919
class DiskDriver {
20-
constructor(name = '') {
21-
this[nameHandle] = name;
20+
constructor(name = '', init = {}) {
21+
privates.set(this, {
22+
name,
23+
});
2224
this.onread = null;
2325
this.onwrite = null;
2426
this.ongetformatinfo = null;
27+
this.ongetonline = null;
28+
if (typeof init === 'object') {
29+
if (typeof init.read === 'function') {
30+
this.onread = init.read;
31+
}
32+
if (typeof init.write === 'function') {
33+
this.write = init.write;
34+
}
35+
if (typeof init.formatInfo === 'object') {
36+
this.ongetformatinfo = () => init.formatInfo;
37+
}
38+
if (typeof init.isOnline === 'function') {
39+
this.ongetonline = init.isOnline;
40+
}
41+
}
2542
}
2643
get name() {
27-
return this[nameHandle];
44+
return privates.get(this).name;
2845
}
2946
read(sector, u8) {
3047
if (!this.onread) {
@@ -44,6 +61,12 @@ class DiskDriver {
4461
}
4562
return this.ongetformatinfo();
4663
}
64+
get isOnline() {
65+
if (!this.ongetonline) {
66+
throw new Error('driver was not initialized');
67+
}
68+
return this.ongetonline();
69+
}
4770
}
4871

4972
module.exports = DiskDriver;

js/core/disk/disks.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2016-present runtime.js project authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
const availableDisks = Object.create(null);
17+
18+
module.exports = {
19+
registerDisk(disk) {
20+
availableDisks[disk.name] = disk;
21+
22+
console.log(`[disk] registered disk ${disk.name}`);
23+
},
24+
getDisks() {
25+
return availableDisks;
26+
},
27+
};

js/core/disk/drivers.js

Lines changed: 0 additions & 42 deletions
This file was deleted.

js/core/disk/index.js

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,13 @@
1313
// limitations under the License.
1414

1515
'use strict';
16-
const DiskDriver = require('./disk-driver');
17-
const { addDriver, getDefaultDriver, getCopyOfDrivers } = require('./drivers');
16+
const DiskInterface = require('./disk-interface');
17+
const { registerDisk, getDisks } = require('./disks');
1818

1919
module.exports = {
20-
DiskDriver,
21-
addDriver,
22-
read(sector, data) {
23-
return getDefaultDriver().read(sector, data);
24-
},
25-
write(sector, data) {
26-
return getDefaultDriver().write(sector, data);
27-
},
28-
get formatInfo() {
29-
return getDefaultDriver().formatInfo;
30-
},
31-
// we expose drivers because the user may want
32-
// to access different drives with different drivers
33-
get drivers() {
34-
return getCopyOfDrivers();
20+
DiskInterface,
21+
registerDisk,
22+
get disks() {
23+
return getDisks();
3524
}
3625
};

js/driver/virtio/blk.js

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -72,42 +72,46 @@ function initializeBlockDevice(pciDevice) {
7272
return u8;
7373
}
7474

75-
const diskDriver = new runtime.disk.DiskDriver('blk');
76-
diskDriver.onread = (sector, data) => {
77-
return new Promise((resolve, reject) => {
78-
if (sector > totalSectorCount) {
79-
setImmediate(() => {
80-
reject(new RangeError(`sector ${sector} out of bounds (max ${totalSectorCount}, non-inclusive)`));
81-
});
82-
return;
83-
}
84-
const status = new Uint8Array(1);
85-
promiseQueue.push([resolve, reject, VIRTIO_BLK_T_IN, data, status]);
86-
reqQueue.placeBuffers([buildHeader(VIRTIO_BLK_T_IN, sector), data, status], [false, true, true]);
87-
88-
if (reqQueue.isNotificationNeeded()) {
89-
dev.queueNotify(QUEUE_ID_REQ);
90-
}
91-
});
92-
};
93-
diskDriver.onwrite = (sector, data) => {
94-
return new Promise((resolve, reject) => {
95-
const status = new Uint8Array(1);
96-
promiseQueue.push([resolve, reject, VIRTIO_BLK_T_OUT, data, status]);
97-
reqQueue.placeBuffers([buildHeader(VIRTIO_BLK_T_OUT, sector), data, status], [false, false, true]);
98-
99-
if (reqQueue.isNotificationNeeded()) {
100-
dev.queueNotify(QUEUE_ID_REQ);
101-
}
102-
});
103-
};
104-
diskDriver.ongetformatinfo = () => ({
105-
sectorSize,
106-
sectorCount,
107-
totalSectorCount,
75+
const diskDriver = new runtime.disk.DiskInterface('hda', {
76+
read(sector, data) {
77+
return new Promise((resolve, reject) => {
78+
if (sector > totalSectorCount) {
79+
setImmediate(() => {
80+
reject(new RangeError(`sector ${sector} out of bounds (max ${totalSectorCount}, non-inclusive)`));
81+
});
82+
return;
83+
}
84+
const status = new Uint8Array(1);
85+
promiseQueue.push([resolve, reject, VIRTIO_BLK_T_IN, data, status]);
86+
reqQueue.placeBuffers([buildHeader(VIRTIO_BLK_T_IN, sector), data, status], [false, true, true]);
87+
88+
if (reqQueue.isNotificationNeeded()) {
89+
dev.queueNotify(QUEUE_ID_REQ);
90+
}
91+
});
92+
},
93+
write(sector, data) {
94+
return new Promise((resolve, reject) => {
95+
const status = new Uint8Array(1);
96+
promiseQueue.push([resolve, reject, VIRTIO_BLK_T_OUT, data, status]);
97+
reqQueue.placeBuffers([buildHeader(VIRTIO_BLK_T_OUT, sector), data, status], [false, false, true]);
98+
99+
if (reqQueue.isNotificationNeeded()) {
100+
dev.queueNotify(QUEUE_ID_REQ);
101+
}
102+
});
103+
},
104+
formatInfo: {
105+
sectorSize,
106+
sectorCount,
107+
totalSectorCount,
108+
},
109+
isOnline() {
110+
return true; // TODO: actually check if the disk is online or not
111+
}
108112
});
109113

110-
runtime.disk.addDriver(diskDriver);
114+
runtime.disk.registerDisk(diskDriver);
111115

112116
function recvBuffer() {
113117
if (promiseQueue.length === 0) {

0 commit comments

Comments
 (0)