Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | 26x 26x 26x 26x 26x 147x 147x 147x 147x 147x 238x 238x 238x 238x 7616x 497x 497x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 16x 8x 8x 60x 60x 22x 3x 60x 60x 60x 107x 107x 107x 107x 37x 37x 37x 37x 98x 98x 98x 98x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 38x 38x 38x 38x 38x 38x 38x 38x 1x 1x 1x 147x 147x | /**
* Attribute encoding utilities for NFSv4 server operations.
*/
import type {Stats} from 'node:fs';
import {Writer} from '@jsonjoy.com/buffers/lib/Writer';
import {XdrEncoder} from '../../../../../xdr/XdrEncoder';
import {Nfsv4Attr, Nfsv4FType, Nfsv4FhExpireType, Nfsv4Stat} from '../../../constants';
import * as struct from '../../../structs';
import {SET_ONLY_ATTRS, setBit} from '../../../attributes';
import type {FilesystemStats} from '../FilesystemStats';
/**
* Encodes file attributes based on the requested bitmap.
* Returns the attributes as a Nfsv4Fattr structure.
* @param requestedAttrs Bitmap of requested attributes
* @param stats Optional file stats (required only if stat-based attributes are requested)
* @param path File path (for context)
* @param fh Optional file handle (required only if FATTR4_FILEHANDLE is requested)
* @param leaseTime Optional lease time in seconds (required only if FATTR4_LEASE_TIME is requested)
* @param fsStats Optional filesystem statistics (required for space/files attributes)
*/
export const encodeAttrs = (
requestedAttrs: struct.Nfsv4Bitmap,
stats: Stats | undefined,
path: string,
fh?: Uint8Array,
leaseTime?: number,
fsStats?: FilesystemStats,
): struct.Nfsv4Fattr => {
const writer = new Writer(512);
const xdr = new XdrEncoder(writer);
const supportedMask: number[] = [];
const requested = requestedAttrs.mask;
for (let i = 0; i < requested.length; i++) {
const word = requested[i];
Iif (!word) continue;
const wordIndex = i;
for (let bit = 0; bit < 32; bit++) {
if (!(word & (1 << bit))) continue;
const attrNum = wordIndex * 32 + bit;
switch (attrNum) {
case Nfsv4Attr.FATTR4_SUPPORTED_ATTRS: {
const implementedAttrs: number[] = [];
setBit(implementedAttrs, Nfsv4Attr.FATTR4_SUPPORTED_ATTRS);
setBit(implementedAttrs, Nfsv4Attr.FATTR4_TYPE);
setBit(implementedAttrs, Nfsv4Attr.FATTR4_FH_EXPIRE_TYPE);
setBit(implementedAttrs, Nfsv4Attr.FATTR4_CHANGE);
setBit(implementedAttrs, Nfsv4Attr.FATTR4_SIZE);
setBit(implementedAttrs, Nfsv4Attr.FATTR4_LINK_SUPPORT);
setBit(implementedAttrs, Nfsv4Attr.FATTR4_SYMLINK_SUPPORT);
setBit(implementedAttrs, Nfsv4Attr.FATTR4_NAMED_ATTR);
setBit(implementedAttrs, Nfsv4Attr.FATTR4_FSID);
setBit(implementedAttrs, Nfsv4Attr.FATTR4_UNIQUE_HANDLES);
setBit(implementedAttrs, Nfsv4Attr.FATTR4_LEASE_TIME);
setBit(implementedAttrs, Nfsv4Attr.FATTR4_RDATTR_ERROR);
setBit(implementedAttrs, Nfsv4Attr.FATTR4_FILEHANDLE);
setBit(implementedAttrs, Nfsv4Attr.FATTR4_FILEID);
setBit(implementedAttrs, Nfsv4Attr.FATTR4_MODE);
setBit(implementedAttrs, Nfsv4Attr.FATTR4_NUMLINKS);
setBit(implementedAttrs, Nfsv4Attr.FATTR4_SPACE_USED);
setBit(implementedAttrs, Nfsv4Attr.FATTR4_SPACE_AVAIL);
setBit(implementedAttrs, Nfsv4Attr.FATTR4_SPACE_FREE);
setBit(implementedAttrs, Nfsv4Attr.FATTR4_SPACE_TOTAL);
setBit(implementedAttrs, Nfsv4Attr.FATTR4_FILES_AVAIL);
setBit(implementedAttrs, Nfsv4Attr.FATTR4_FILES_FREE);
setBit(implementedAttrs, Nfsv4Attr.FATTR4_FILES_TOTAL);
setBit(implementedAttrs, Nfsv4Attr.FATTR4_TIME_ACCESS);
setBit(implementedAttrs, Nfsv4Attr.FATTR4_TIME_METADATA);
setBit(implementedAttrs, Nfsv4Attr.FATTR4_TIME_MODIFY);
xdr.writeUnsignedInt(implementedAttrs.length);
for (let j = 0; j < implementedAttrs.length; j++) {
xdr.writeUnsignedInt(implementedAttrs[j]);
}
setBit(supportedMask, attrNum);
break;
}
case Nfsv4Attr.FATTR4_TYPE: {
Iif (!stats) break;
let type: Nfsv4FType;
if (stats.isFile()) type = Nfsv4FType.NF4REG;
else if (stats.isDirectory()) type = Nfsv4FType.NF4DIR;
else if (stats.isSymbolicLink()) type = Nfsv4FType.NF4LNK;
else Eif (stats.isBlockDevice()) type = Nfsv4FType.NF4BLK;
else if (stats.isCharacterDevice()) type = Nfsv4FType.NF4CHR;
else if (stats.isFIFO()) type = Nfsv4FType.NF4FIFO;
else if (stats.isSocket()) type = Nfsv4FType.NF4SOCK;
else type = Nfsv4FType.NF4REG;
xdr.writeUnsignedInt(type);
setBit(supportedMask, attrNum);
break;
}
case Nfsv4Attr.FATTR4_SIZE: {
Iif (!stats) break;
xdr.writeUnsignedHyper(BigInt(stats.size));
setBit(supportedMask, attrNum);
break;
}
case Nfsv4Attr.FATTR4_FILEID: {
Iif (!stats) break;
xdr.writeUnsignedHyper(BigInt(stats.ino));
setBit(supportedMask, attrNum);
break;
}
case Nfsv4Attr.FATTR4_MODE: {
Iif (!stats) break;
xdr.writeUnsignedInt(stats.mode & 0o7777);
setBit(supportedMask, attrNum);
break;
}
case Nfsv4Attr.FATTR4_NUMLINKS: {
Iif (!stats) break;
xdr.writeUnsignedInt(stats.nlink);
setBit(supportedMask, attrNum);
break;
}
case Nfsv4Attr.FATTR4_SPACE_USED: {
Iif (!stats) break;
xdr.writeUnsignedHyper(BigInt(stats.blocks * 512));
setBit(supportedMask, attrNum);
break;
}
case Nfsv4Attr.FATTR4_SPACE_AVAIL: {
Iif (!fsStats) break;
xdr.writeUnsignedHyper(fsStats.spaceAvail);
setBit(supportedMask, attrNum);
break;
}
case Nfsv4Attr.FATTR4_SPACE_FREE: {
Iif (!fsStats) break;
xdr.writeUnsignedHyper(fsStats.spaceFree);
setBit(supportedMask, attrNum);
break;
}
case Nfsv4Attr.FATTR4_SPACE_TOTAL: {
Iif (!fsStats) break;
xdr.writeUnsignedHyper(fsStats.spaceTotal);
setBit(supportedMask, attrNum);
break;
}
case Nfsv4Attr.FATTR4_FILES_AVAIL: {
Iif (!fsStats) break;
xdr.writeUnsignedHyper(fsStats.filesAvail);
setBit(supportedMask, attrNum);
break;
}
case Nfsv4Attr.FATTR4_FILES_FREE: {
Iif (!fsStats) break;
xdr.writeUnsignedHyper(fsStats.filesFree);
setBit(supportedMask, attrNum);
break;
}
case Nfsv4Attr.FATTR4_FILES_TOTAL: {
Iif (!fsStats) break;
xdr.writeUnsignedHyper(fsStats.filesTotal);
setBit(supportedMask, attrNum);
break;
}
case Nfsv4Attr.FATTR4_TIME_ACCESS: {
Iif (!stats) break;
const atime = stats.atimeMs;
const seconds = Math.floor(atime / 1000);
const nseconds = Math.floor((atime % 1000) * 1000000);
xdr.writeHyper(BigInt(seconds));
xdr.writeUnsignedInt(nseconds);
setBit(supportedMask, attrNum);
break;
}
case Nfsv4Attr.FATTR4_TIME_MODIFY: {
Iif (!stats) break;
const mtime = stats.mtimeMs;
const seconds = Math.floor(mtime / 1000);
const nseconds = Math.floor((mtime % 1000) * 1000000);
xdr.writeHyper(BigInt(seconds));
xdr.writeUnsignedInt(nseconds);
setBit(supportedMask, attrNum);
break;
}
case Nfsv4Attr.FATTR4_TIME_METADATA: {
Iif (!stats) break;
const ctime = stats.ctimeMs;
const seconds = Math.floor(ctime / 1000);
const nseconds = Math.floor((ctime % 1000) * 1000000);
xdr.writeHyper(BigInt(seconds));
xdr.writeUnsignedInt(nseconds);
setBit(supportedMask, attrNum);
break;
}
case Nfsv4Attr.FATTR4_CHANGE: {
Iif (!stats) break;
const changeTime = BigInt(Math.floor(stats.mtimeMs * 1000000));
xdr.writeUnsignedHyper(changeTime);
setBit(supportedMask, attrNum);
break;
}
case Nfsv4Attr.FATTR4_LEASE_TIME: {
Iif (leaseTime !== undefined) {
xdr.writeUnsignedInt(leaseTime);
setBit(supportedMask, attrNum);
}
break;
}
case Nfsv4Attr.FATTR4_FH_EXPIRE_TYPE: {
xdr.writeUnsignedInt(Nfsv4FhExpireType.FH4_VOLATILE_ANY);
setBit(supportedMask, attrNum);
break;
}
case Nfsv4Attr.FATTR4_LINK_SUPPORT: {
xdr.writeUnsignedInt(1);
setBit(supportedMask, attrNum);
break;
}
case Nfsv4Attr.FATTR4_SYMLINK_SUPPORT: {
xdr.writeUnsignedInt(1);
setBit(supportedMask, attrNum);
break;
}
case Nfsv4Attr.FATTR4_NAMED_ATTR: {
xdr.writeUnsignedInt(0);
setBit(supportedMask, attrNum);
break;
}
case Nfsv4Attr.FATTR4_FSID: {
xdr.writeUnsignedHyper(BigInt(0));
xdr.writeUnsignedHyper(BigInt(0));
setBit(supportedMask, attrNum);
break;
}
case Nfsv4Attr.FATTR4_UNIQUE_HANDLES: {
xdr.writeUnsignedInt(1);
setBit(supportedMask, attrNum);
break;
}
case Nfsv4Attr.FATTR4_RDATTR_ERROR: {
xdr.writeUnsignedInt(0);
setBit(supportedMask, attrNum);
break;
}
case Nfsv4Attr.FATTR4_FILEHANDLE: {
Iif (fh) {
xdr.writeVarlenOpaque(fh);
setBit(supportedMask, attrNum);
}
break;
}
default: {
Iif (SET_ONLY_ATTRS.has(attrNum)) throw Nfsv4Stat.NFS4ERR_INVAL;
}
}
}
}
const attrVals = writer.flush();
return new struct.Nfsv4Fattr(new struct.Nfsv4Bitmap(supportedMask), attrVals);
};
|