Skip to content

Commit

Permalink
Removed File._setType (+Sync) (folded into chmod)
Browse files Browse the repository at this point in the history
  • Loading branch information
james-pre committed Nov 20, 2024
1 parent e2c696c commit 14cd605
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 45 deletions.
6 changes: 1 addition & 5 deletions src/backends/port/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Errno, ErrnoError } from '../../error.js';
import { File } from '../../file.js';
import { FileSystem, type FileSystemMetadata } from '../../filesystem.js';
import { Async } from '../../mixins/async.js';
import { Stats, type FileType } from '../../stats.js';
import { Stats } from '../../stats.js';
import type { Backend, FilesystemOf } from '../backend.js';
import { InMemory } from '../memory.js';
import * as RPC from './rpc.js';
Expand Down Expand Up @@ -104,10 +104,6 @@ export class PortFile extends File {
this._throwNoSync('utimes');
}

public _setType(type: FileType): Promise<void> {
return this.rpc('_setType', type);
}

public _setTypeSync(): void {
this._throwNoSync('_setType');
}
Expand Down
2 changes: 1 addition & 1 deletion src/emulation/promises.ts
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ export async function symlink(target: fs.PathLike, path: fs.PathLike, type: fs.s

await using handle = await _open(path, 'w+', 0o644, false);
await handle.writeFile(target.toString());
await handle.file._setType(constants.S_IFLNK);
await handle.file.chmod(constants.S_IFLNK);
}
symlink satisfies typeof promises.symlink;

Expand Down
2 changes: 1 addition & 1 deletion src/emulation/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ export function symlinkSync(target: fs.PathLike, path: fs.PathLike, type: fs.sym

writeFileSync(path, target.toString());
const file = _openSync(path, 'r+', 0o644, false);
file._setTypeSync(constants.S_IFLNK);
file.chmodSync(constants.S_IFLNK);
}
symlinkSync satisfies typeof fs.symlinkSync;

Expand Down
44 changes: 7 additions & 37 deletions src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { O_APPEND, O_CREAT, O_EXCL, O_RDONLY, O_RDWR, O_SYNC, O_TRUNC, O_WRONLY,
import { Errno, ErrnoError } from './error.js';
import type { FileSystem } from './filesystem.js';
import './polyfills.js';
import { Stats, type FileType } from './stats.js';
import { _chown, Stats } from './stats.js';

/**
Typescript does not include a type declaration for resizable array buffers.
Expand Down Expand Up @@ -251,18 +251,6 @@ export abstract class File<FS extends FileSystem = FileSystem> {
* Change the file timestamps of the file.
*/
public abstract utimesSync(atime: Date, mtime: Date): void;

/**
* Set the file type
* @internal
*/
public abstract _setType(type: FileType): Promise<void>;

/**
* Set the file type
* @internal
*/
public abstract _setTypeSync(type: FileType): void;
}

/**
Expand Down Expand Up @@ -573,25 +561,25 @@ export class PreloadFile<FS extends FileSystem> extends File<FS> {
throw ErrnoError.With('EBADF', this.path, 'File.chmod');
}
this.dirty = true;
this.stats.chmod(mode);
if (config.syncImmediately) await this.sync();
this.stats.mode = (this.stats.mode & (mode > S_IFMT ? ~S_IFMT : S_IFMT)) | mode;
if (config.syncImmediately || mode > S_IFMT) await this.sync();
}

public chmodSync(mode: number): void {
if (this.closed) {
throw ErrnoError.With('EBADF', this.path, 'File.chmod');
}
this.dirty = true;
this.stats.chmod(mode);
if (config.syncImmediately) this.syncSync();
this.stats.mode = (this.stats.mode & (mode > S_IFMT ? ~S_IFMT : S_IFMT)) | mode;
if (config.syncImmediately || mode > S_IFMT) this.syncSync();
}

public async chown(uid: number, gid: number): Promise<void> {
if (this.closed) {
throw ErrnoError.With('EBADF', this.path, 'File.chown');
}
this.dirty = true;
this.stats.chown(uid, gid);
_chown(this.stats, uid, gid);
if (config.syncImmediately) await this.sync();
}

Expand All @@ -600,7 +588,7 @@ export class PreloadFile<FS extends FileSystem> extends File<FS> {
throw ErrnoError.With('EBADF', this.path, 'File.chown');
}
this.dirty = true;
this.stats.chown(uid, gid);
_chown(this.stats, uid, gid);
if (config.syncImmediately) this.syncSync();
}

Expand All @@ -623,24 +611,6 @@ export class PreloadFile<FS extends FileSystem> extends File<FS> {
this.stats.mtime = mtime;
if (config.syncImmediately) this.syncSync();
}

public async _setType(type: FileType): Promise<void> {
if (this.closed) {
throw ErrnoError.With('EBADF', this.path, 'File._setType');
}
this.dirty = true;
this.stats.mode = (this.stats.mode & ~S_IFMT) | type;
await this.sync();
}

public _setTypeSync(type: FileType): void {
if (this.closed) {
throw ErrnoError.With('EBADF', this.path, 'File._setType');
}
this.dirty = true;
this.stats.mode = (this.stats.mode & ~S_IFMT) | type;
this.syncSync();
}
}

/**
Expand Down
20 changes: 19 additions & 1 deletion src/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ export interface StatsLike<T extends number | bigint = number | bigint> {
* Inode number
*/
ino: T;
/**
* Number of hard links
*/
nlink: T;
}

/**
Expand Down Expand Up @@ -277,6 +281,7 @@ export abstract class StatsCommon<T extends number | bigint> implements Node.Sta
* Change the mode of the file.
* We use this helper function to prevent messing up the type of the file.
* @internal
* @deprecated This will be removed in the next minor release since it is internal
*/
public chmod(mode: number): void {
this.mode = this._convert((this.mode & S_IFMT) | mode);
Expand All @@ -286,8 +291,9 @@ export abstract class StatsCommon<T extends number | bigint> implements Node.Sta
* Change the owner user/group of the file.
* This function makes sure it is a valid UID/GID (that is, a 32 unsigned int)
* @internal
* @deprecated This will be removed in the next minor release since it is internal
*/
public chown(uid: number | bigint, gid: number | bigint): void {
public chown(uid: number, gid: number): void {
uid = Number(uid);
gid = Number(gid);
if (!isNaN(uid) && 0 <= uid && uid < 2 ** 32) {
Expand Down Expand Up @@ -315,6 +321,18 @@ export abstract class StatsCommon<T extends number | bigint> implements Node.Sta
}
}

/**
* @hidden @internal
*/
export function _chown(stats: Partial<StatsLike<number>>, uid: number, gid: number) {
if (!isNaN(uid) && 0 <= uid && uid < 2 ** 32) {
stats.uid = uid;
}
if (!isNaN(gid) && 0 <= gid && gid < 2 ** 32) {
stats.gid = gid;
}
}

/**
* Implementation of Node's `Stats`.
*
Expand Down

0 comments on commit 14cd605

Please sign in to comment.