Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
Refactored Directory to extend Map
Browse files Browse the repository at this point in the history
  • Loading branch information
james-pre committed Oct 3, 2024
1 parent 753dec8 commit a2d68e5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 64 deletions.
19 changes: 0 additions & 19 deletions jest.config.json

This file was deleted.

61 changes: 21 additions & 40 deletions src/Directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,40 @@ import { DirectoryRecord, ISODirectoryRecord, JolietDirectoryRecord } from './Di
import { FileFlags } from './constants.js';
import { CLEntry, REEntry } from './entries.js';

export abstract class Directory<T extends DirectoryRecord> {
protected _record: T;
private _fileList: string[] = [];
private _fileMap: { [name: string]: T } = {};
export abstract class Directory<T extends DirectoryRecord> extends Map<string, T> {
//public readonly files: string[] = [];
//private fileMap = new Map<string, T>();

public constructor(record: T, isoData: Uint8Array) {
this._record = record;
public constructor(
protected record: T,
isoData: Uint8Array
) {
super();
let i = record.lba;
let limit = i + record.dataLength;
if (!(record.fileFlags & FileFlags.Directory)) {
// Must have a CL entry.
const cl = record.getSUEntries(isoData).filter(e => e instanceof CLEntry)[0] as CLEntry;
const cl = record.getSUEntries(isoData).find(e => e instanceof CLEntry);
if (!cl) {
throw new ReferenceError('No CL entry');
}
i = cl.childDirectoryLba * 2048;
limit = Infinity;
}

while (i < limit) {
const len = isoData[i];
const length = isoData[i];
// Zero-padding between sectors.
// TODO: Could optimize this to seek to nearest-sector upon
// seeing a 0.
if (len === 0) {
// Could optimize this to seek to nearest-sector upon seeing a 0.
if (!length) {
i++;
continue;
}
const r = this._constructDirectoryRecord(isoData.slice(i));
const fname = r.fileName(isoData);
// Skip '.' and '..' entries.
if (fname !== '\u0000' && fname !== '\u0001') {
// Skip relocated entries.
if (!r.hasRockRidge || r.getSUEntries(isoData).filter(e => e instanceof REEntry).length === 0) {
this._fileMap[fname] = r;
this._fileList.push(fname);
}
if (fname !== '\u0000' && fname !== '\u0001' && (!r.hasRockRidge || !r.getSUEntries(isoData).filter(e => e instanceof REEntry).length)) {
this.set(fname, r);
} else if (limit === Infinity) {
// First entry contains needed data.
limit = i + r.dataLength;
Expand All @@ -44,40 +44,21 @@ export abstract class Directory<T extends DirectoryRecord> {
}
}

/**
* Get the record with the given name.
* Returns undefined if not present.
*/
public getRecord(name: string): DirectoryRecord {
return this._fileMap[name];
}

public get fileList(): string[] {
return this._fileList;
}

public getDotEntry(isoData: Uint8Array): T {
return this._constructDirectoryRecord(isoData.slice(this._record.lba));
return this._constructDirectoryRecord(isoData.slice(this.record.lba));
}

protected abstract _constructDirectoryRecord(data: Uint8Array): T;
}
export class ISODirectory extends Directory<ISODirectoryRecord> {
public constructor(record: ISODirectoryRecord, isoData: Uint8Array) {
super(record, isoData);
}

export class ISODirectory extends Directory<ISODirectoryRecord> {
protected _constructDirectoryRecord(data: Uint8Array): ISODirectoryRecord {
return new ISODirectoryRecord(data, this._record.rockRidgeOffset);
return new ISODirectoryRecord(data, this.record.rockRidgeOffset);
}
}

export class JolietDirectory extends Directory<JolietDirectoryRecord> {
public constructor(record: JolietDirectoryRecord, isoData: Uint8Array) {
super(record, isoData);
}

protected _constructDirectoryRecord(data: Uint8Array): JolietDirectoryRecord {
return new JolietDirectoryRecord(data, this._record.rockRidgeOffset);
return new JolietDirectoryRecord(data, this.record.rockRidgeOffset);
}
}
10 changes: 5 additions & 5 deletions src/IsoFS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export class IsoFS extends Readonly(Sync(FileSystem)) {
}

if (record.isDirectory(this.data)) {
return record.getDirectory(this.data).fileList.slice(0);
return Array.from(record.getDirectory(this.data).keys());
}

throw ErrnoError.With('ENOTDIR', path, 'readdir');
Expand All @@ -146,13 +146,13 @@ export class IsoFS extends Readonly(Sync(FileSystem)) {
if (path === '/') {
return this._root;
}
const components = path.split('/').slice(1);
let dir = this._root;
for (const component of components) {
const parts = path.split('/').slice(1);
let dir: DirectoryRecord | undefined = this._root;
for (const part of parts) {
if (!dir.isDirectory(this.data)) {
return;
}
dir = dir.getDirectory(this.data).getRecord(component);
dir = dir.getDirectory(this.data).get(part);
if (!dir) {
return;
}
Expand Down

0 comments on commit a2d68e5

Please sign in to comment.