Skip to content

Commit

Permalink
Cleaner solution
Browse files Browse the repository at this point in the history
  • Loading branch information
JrMasterModelBuilder committed Oct 3, 2023
1 parent ebb58f4 commit 20b4657
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 68 deletions.
10 changes: 0 additions & 10 deletions src/archive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -860,16 +860,6 @@ export abstract class Archive {
afters.delete(resolve(path));
}

/**
* Syncronously cleanup temporary resources on shutdown.
* Can be called on shutdown/abort to free resources.
* Should not be called at any other time.
* Not applicable to most archive formats.
*/
public shutdown() {
// Do nothing.
}

/**
* Read archive.
* If the itter callback returns false, reading ends.
Expand Down
62 changes: 24 additions & 38 deletions src/archive/hdi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {IMounterAttachInfo, Mounter} from '@shockpkg/hdi-mac';
import {Archive, Entry, IEntryInfo} from '../archive';
import {PathType} from '../types';
import {
IFsWalkSignal,
fsLstatExists,
fsReadlinkRaw,
fsWalk,
Expand All @@ -21,6 +20,10 @@ const walkOpts = {
ignoreUnreadableDirectories: true
};

const ejectOptions = {
force: true
};

export interface IEntryInfoHdi extends IEntryInfo {
/**
* @inheritdoc
Expand Down Expand Up @@ -196,14 +199,6 @@ export class ArchiveHdi extends Archive {
*/
public nobrowse = false;

/**
* Actively attached info objects.
*/
protected _active = new Set<{
info: IMounterAttachInfo;
signal: IFsWalkSignal;
}>();

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

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

/**
* @inheritdoc
*/
Expand All @@ -241,6 +219,8 @@ export class ArchiveHdi extends Archive {
* @inheritdoc
*/
protected async _read(itter: (entry: EntryHdi) => Promise<unknown>) {
const {mounterMac, nobrowse} = this;

/**
* Each itterator.
*
Expand Down Expand Up @@ -332,18 +312,24 @@ export class ArchiveHdi extends Archive {
return true;
};

// Attach disk image, using automatic eject on shutdown (3rd arg).
// Just in case process shutdown without reaching finally.
const {mounterMac, nobrowse} = this;
const info = await mounterMac.attach(this.path, {
let info: IMounterAttachInfo | null = null;

/**
* Attempt to auto-eject on normal shutdown.
* Does not catch signals (no clean way in a library).
* Users can explicitly call process.exit() on signals to invoke this.
*/
const shutdown = () => {
// eslint-disable-next-line no-sync
info?.ejectSync(ejectOptions);
};
process.once('exit', shutdown);

info = await mounterMac.attach(this.path, {
nobrowse,
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 @@ -361,13 +347,13 @@ export class ArchiveHdi extends Archive {
const pathRaw = pathJoin(volumeName, pathRel);
return each(pathFull, pathRaw, stat);
},
walkOpts,
signal
walkOpts
);
}
} finally {
await info.eject();
this._active.delete(active);
await info.eject(ejectOptions);
info = null;
process.off('exit', shutdown);
}
}
}
23 changes: 3 additions & 20 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,6 @@ export interface IFsWalkOptions {
ignoreUnreadableDirectories?: boolean;
}

export interface IFsWalkSignal {
/**
* Option to abort walker while walking.
*
* @default false
*/
abort?: boolean;
}

const {O_WRONLY, O_SYMLINK} = fsConstants;
export const fsLchmodSupported = !!O_SYMLINK;
export const fsLutimesSupported = !!O_SYMLINK;
Expand Down Expand Up @@ -288,29 +279,24 @@ export async function fsLstatExists(path: string) {
* @param base Directory path.
* @param itter Callback for each entry.
* @param options Walk options.
* @param signal Walk signal.
*/
export async function fsWalk(
base: string,
itter: (path: string, stat: Stats) => Promise<boolean | null | void>,
options: Readonly<IFsWalkOptions> = {},
signal: IFsWalkSignal = {}
options: Readonly<IFsWalkOptions> = {}
) {
const {ignoreUnreadableDirectories} = options;
const stack = (await fsReaddir(base)).reverse();
while (!signal.abort && stack.length) {
while (stack.length) {
const entry = stack.pop() as string;
const fullPath = pathJoin(base, entry);
// eslint-disable-next-line no-await-in-loop
const stat = await fsLstat(fullPath);
if (signal.abort) {
break;
}

// Callback, possibly stop recursion on directory.
// eslint-disable-next-line no-await-in-loop
const recurse = await itter(entry, stat);
if (signal.abort || recurse === null) {
if (recurse === null) {
break;
}
if (recurse === false || !stat.isDirectory()) {
Expand All @@ -333,9 +319,6 @@ export async function fsWalk(
throw err;
}
}
if (signal.abort) {
break;
}
for (let i = subs.length; i--; ) {
stack.push(pathJoin(entry, subs[i]));
}
Expand Down

0 comments on commit 20b4657

Please sign in to comment.