Skip to content

Commit

Permalink
Fixed internal config flags
Browse files Browse the repository at this point in the history
  • Loading branch information
james-pre committed Oct 31, 2024
1 parent 6975b73 commit e019a81
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ export async function configure<T extends ConfigMounts>(configuration: Partial<C

cache.setEnabled(configuration.cacheStats ?? false);
config.checkAccess = !configuration.disableAccessChecks;
config.syncOnRead = !configuration.onlySyncOnClose || !configuration.disableUpdateOnRead;
config.syncOnWrite = !configuration.onlySyncOnClose;
config.updateOnRead = !configuration.disableUpdateOnRead;
config.syncImmediately = !configuration.onlySyncOnClose;

if (configuration.addDevices) {
const devfs = new DeviceFS();
Expand Down
8 changes: 4 additions & 4 deletions src/emulation/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ export const config = {
checkAccess: true,

/**
* Whether to sync atime updates immediately when reading from a file
* Whether to mark a file as dirty after updating its `atime` when read from
*/
syncOnRead: true,
updateOnRead: true,

/**
* Whether to immediately sync when files are written to
* Whether to immediately sync when files are changed
*/
syncOnWrite: true,
syncImmediately: true,

/**
* If a file's buffer is not large enough to store content when writing and the buffer can't be resized, reuse the buffer passed to write()
Expand Down
32 changes: 19 additions & 13 deletions src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,12 +441,12 @@ export class PreloadFile<FS extends FileSystem> extends File {

public async truncate(length: number): Promise<void> {
this._truncate(length);
if (config.syncOnWrite) await this.sync();
if (config.syncImmediately) await this.sync();
}

public truncateSync(length: number): void {
this._truncate(length);
if (config.syncOnWrite) this.syncSync();
if (config.syncImmediately) this.syncSync();
}

protected _write(buffer: Uint8Array, offset: number = 0, length: number = this.stats.size, position: number = this.position): number {
Expand Down Expand Up @@ -494,7 +494,7 @@ export class PreloadFile<FS extends FileSystem> extends File {
*/
public async write(buffer: Uint8Array, offset?: number, length?: number, position?: number): Promise<number> {
const bytesWritten = this._write(buffer, offset, length, position);
if (config.syncOnWrite) await this.sync();
if (config.syncImmediately) await this.sync();
return bytesWritten;
}

Expand All @@ -509,19 +509,25 @@ export class PreloadFile<FS extends FileSystem> extends File {
*/
public writeSync(buffer: Uint8Array, offset: number = 0, length: number = this.stats.size, position: number = this.position): number {
const bytesWritten = this._write(buffer, offset, length, position);
if (config.syncOnWrite) this.syncSync();
if (config.syncImmediately) this.syncSync();
return bytesWritten;
}

protected _read(buffer: ArrayBufferView, offset: number = 0, length: number = this.stats.size, position?: number): number {
if (this.closed) {
throw ErrnoError.With('EBADF', this.path, 'File.read');
}

if (!isReadable(this.flag)) {
throw new ErrnoError(Errno.EPERM, 'File not opened with a readable mode.');
}
this.dirty = true;

if (config.updateOnRead) {
this.dirty = true;
}

this.stats.atimeMs = Date.now();

position ??= this.position;
let end = position + length;
if (end > this.stats.size) {
Expand All @@ -547,7 +553,7 @@ export class PreloadFile<FS extends FileSystem> extends File {
*/
public async read<TBuffer extends ArrayBufferView>(buffer: TBuffer, offset?: number, length?: number, position?: number): Promise<{ bytesRead: number; buffer: TBuffer }> {
const bytesRead = this._read(buffer, offset, length, position);
if (config.syncOnRead) await this.sync();
if (config.syncImmediately) await this.sync();
return { bytesRead, buffer };
}

Expand All @@ -562,7 +568,7 @@ export class PreloadFile<FS extends FileSystem> extends File {
*/
public readSync(buffer: ArrayBufferView, offset?: number, length?: number, position?: number): number {
const bytesRead = this._read(buffer, offset, length, position);
if (config.syncOnRead) this.syncSync();
if (config.syncImmediately) this.syncSync();
return bytesRead;
}

Expand All @@ -572,7 +578,7 @@ export class PreloadFile<FS extends FileSystem> extends File {
}
this.dirty = true;
this.stats.chmod(mode);
if (config.syncOnWrite) await this.sync();
if (config.syncImmediately) await this.sync();
}

public chmodSync(mode: number): void {
Expand All @@ -581,7 +587,7 @@ export class PreloadFile<FS extends FileSystem> extends File {
}
this.dirty = true;
this.stats.chmod(mode);
if (config.syncOnWrite) this.syncSync();
if (config.syncImmediately) this.syncSync();
}

public async chown(uid: number, gid: number): Promise<void> {
Expand All @@ -590,7 +596,7 @@ export class PreloadFile<FS extends FileSystem> extends File {
}
this.dirty = true;
this.stats.chown(uid, gid);
if (config.syncOnWrite) await this.sync();
if (config.syncImmediately) await this.sync();
}

public chownSync(uid: number, gid: number): void {
Expand All @@ -599,7 +605,7 @@ export class PreloadFile<FS extends FileSystem> extends File {
}
this.dirty = true;
this.stats.chown(uid, gid);
if (config.syncOnWrite) this.syncSync();
if (config.syncImmediately) this.syncSync();
}

public async utimes(atime: Date, mtime: Date): Promise<void> {
Expand All @@ -609,7 +615,7 @@ export class PreloadFile<FS extends FileSystem> extends File {
this.dirty = true;
this.stats.atime = atime;
this.stats.mtime = mtime;
if (config.syncOnWrite) await this.sync();
if (config.syncImmediately) await this.sync();
}

public utimesSync(atime: Date, mtime: Date): void {
Expand All @@ -619,7 +625,7 @@ export class PreloadFile<FS extends FileSystem> extends File {
this.dirty = true;
this.stats.atime = atime;
this.stats.mtime = mtime;
if (config.syncOnWrite) this.syncSync();
if (config.syncImmediately) this.syncSync();
}

public async _setType(type: FileType): Promise<void> {
Expand Down

0 comments on commit e019a81

Please sign in to comment.