Skip to content

Commit b8bae4c

Browse files
committed
feat: 🎸 add support for file creation
1 parent 98a88f9 commit b8bae4c

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

src/nfs/v4/client/Nfsv4FsClient.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as misc from 'memfs/lib/node/types/misc';
33
import * as opts from 'memfs/lib/node/types/options';
44
import {nfs} from '../builder';
55
import * as msg from '../messages';
6-
import {Nfsv4Stat, Nfsv4OpenAccess, Nfsv4OpenDeny, Nfsv4StableHow, Nfsv4Attr} from '../constants';
6+
import {Nfsv4Stat, Nfsv4OpenAccess, Nfsv4OpenDeny, Nfsv4StableHow, Nfsv4Attr, Nfsv4OpenFlags} from '../constants';
77
import {Writer} from '@jsonjoy.com/buffers/lib/Writer';
88
import {XdrEncoder} from '../../../xdr/XdrEncoder';
99

@@ -99,7 +99,14 @@ export class Nfsv4FsClient implements NfsFsClient {
9999
const openOwner = nfs.OpenOwner(BigInt(1), new Uint8Array([1, 2, 3, 4]));
100100
const claim = nfs.OpenClaimNull(filename);
101101
operations.push(
102-
nfs.OPEN(0, Nfsv4OpenAccess.OPEN4_SHARE_ACCESS_WRITE, Nfsv4OpenDeny.OPEN4_SHARE_DENY_NONE, openOwner, 0, claim),
102+
nfs.OPEN(
103+
0,
104+
Nfsv4OpenAccess.OPEN4_SHARE_ACCESS_WRITE,
105+
Nfsv4OpenDeny.OPEN4_SHARE_DENY_NONE,
106+
openOwner,
107+
Nfsv4OpenFlags.OPEN4_CREATE,
108+
claim,
109+
),
103110
);
104111
const writer = new Writer(16);
105112
const xdr = new XdrEncoder(writer);

src/nfs/v4/server/operations/node/Nfsv4OperationsNode.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
Nfsv4OpenClaimType,
1010
Nfsv4DelegType,
1111
Nfsv4LockType,
12+
Nfsv4OpenFlags,
1213
} from '../../../constants';
1314
import {Nfsv4OperationCtx, Nfsv4Operations} from '../Nfsv4Operations';
1415
import * as msg from '../../../messages';
@@ -535,33 +536,41 @@ export class Nfsv4OperationsNode implements Nfsv4Operations {
535536
const claimNull = request.claim.claim as struct.Nfsv4OpenClaimNull;
536537
const filename = claimNull.file;
537538
const filePath = NodePath.join(currentPathAbsolute, filename);
539+
let fileExists = false;
538540
try {
539541
const stats = await this.promises.lstat(filePath);
540542
if (!stats.isFile()) {
541543
return new msg.Nfsv4OpenResponse(Nfsv4Stat.NFS4ERR_ISDIR);
542544
}
545+
fileExists = true;
543546
} catch (err) {
544-
if (isErrCode(err, 'ENOENT')) {
545-
return new msg.Nfsv4OpenResponse(Nfsv4Stat.NFS4ERR_NOENT);
547+
if (isErrCode('ENOENT', err)) {
548+
if (request.openhow !== Nfsv4OpenFlags.OPEN4_CREATE) {
549+
return new msg.Nfsv4OpenResponse(Nfsv4Stat.NFS4ERR_NOENT);
550+
}
551+
} else {
552+
const status = normalizeNodeFsError(err, ctx.connection.logger);
553+
return new msg.Nfsv4OpenResponse(status);
546554
}
547-
const status = normalizeNodeFsError(err, ctx.connection.logger);
548-
return new msg.Nfsv4OpenResponse(status);
549555
}
550-
if (!this.canAccessFile(filePath, request.shareAccess, request.shareDeny)) {
556+
if (fileExists && !this.canAccessFile(filePath, request.shareAccess, request.shareDeny)) {
551557
return new msg.Nfsv4OpenResponse(Nfsv4Stat.NFS4ERR_SHARE_DENIED);
552558
}
553559
let flags = 0;
554560
const isWrite = (request.shareAccess & Nfsv4OpenAccess.OPEN4_SHARE_ACCESS_WRITE) !== 0;
555561
const isRead = (request.shareAccess & Nfsv4OpenAccess.OPEN4_SHARE_ACCESS_READ) !== 0;
562+
if (request.openhow === 1) {
563+
flags = this.fs.constants.O_CREAT;
564+
}
556565
if (isRead && isWrite) {
557-
flags = this.fs.constants.O_RDWR;
566+
flags |= this.fs.constants.O_RDWR;
558567
} else if (isWrite) {
559-
flags = this.fs.constants.O_WRONLY;
568+
flags |= this.fs.constants.O_WRONLY;
560569
} else {
561-
flags = this.fs.constants.O_RDONLY;
570+
flags |= this.fs.constants.O_RDONLY;
562571
}
563572
try {
564-
const fd = await this.promises.open(filePath, flags);
573+
const fd = await this.promises.open(filePath, flags, 0o644);
565574
const stateid = this.createStateid();
566575
const stateidKey = this.makeStateidKey(stateid);
567576
const openFile = new OpenFileState(

0 commit comments

Comments
 (0)