Skip to content

Commit

Permalink
Added syncronous cleanup method
Browse files Browse the repository at this point in the history
  • Loading branch information
JrMasterModelBuilder committed Oct 3, 2023
1 parent 86b85fa commit 94b9e6d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/archive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,15 @@ export abstract class Archive {
afters.delete(resolve(path));
}

/**
* Syncronously cleanup temporary resources.
* Can be called on shutdown/abort/signals to free resources.
* Not applicable to most archive formats.
*/
public cleanup() {
// Do nothing.
}

/**
* Read archive.
* If the itter callback returns false, reading ends.
Expand Down
33 changes: 31 additions & 2 deletions src/archive/hdi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import {Stats, createReadStream} from 'node:fs';
import {basename, join as pathJoin} from 'node:path';

import {Mounter} from '@shockpkg/hdi-mac';
import {IMounterAttachInfo, Mounter} from '@shockpkg/hdi-mac';

import {Archive, Entry, IEntryInfo} from '../archive';
import {PathType} from '../types';
import {
IFsWalkSignal,
fsLstatExists,
fsReadlinkRaw,
fsWalk,
Expand Down Expand Up @@ -196,6 +197,11 @@ export class ArchiveHdi extends Archive {
*/
public nobrowse = false;

protected _active = new Set<{
info: IMounterAttachInfo;
signal: IFsWalkSignal;
}>();

/**
* ArchiveHdi constructor.
*
Expand All @@ -205,6 +211,23 @@ export class ArchiveHdi extends Archive {
super(path);
}

/**
* Call this to eject actively open disk images on shutdown.
*
* @inheritdoc
*/
public cleanup() {
const active = this._active;
for (const a of this._active) {
a.signal.abort = true;
// eslint-disable-next-line no-sync
a.info.ejectSync({
force: true
});
active.delete(a);
}
}

/**
* Read archive.
* If the itter callback returns false, reading ends.
Expand Down Expand Up @@ -323,6 +346,10 @@ export class ArchiveHdi extends Archive {
readonly: true
});

const signal = {};
const active = {info, signal};
this._active.add(active);

// Eject device when done.
try {
for (const device of info.devices) {
Expand All @@ -340,11 +367,13 @@ export class ArchiveHdi extends Archive {
const pathRaw = pathJoin(volumeName, pathRel);
return each(pathFull, pathRaw, stat);
},
walkOpts
walkOpts,
signal
);
}
} finally {
await info.eject();
this._active.delete(active);
}
}
}

0 comments on commit 94b9e6d

Please sign in to comment.