Skip to content

fs.stat returns a size of 0 for /proc/* files #43669

Closed as not planned
Closed as not planned
@mleblebici

Description

@mleblebici

Version

v16.15.1

Platform

Linux ubuntu 5.13.0-48-generic #54~20.04.1-Ubuntu

Subsystem

fs

What steps will reproduce the bug?

You can see the result by simply running the following code.

var fs = require('fs')
 
fs.stat('/proc/meminfo', (error, stats) => {
   console.log(stats)
})

How often does it reproduce? Is there a required condition?

There is no required condition, it always returns the same result.

What is the expected behavior?

It was expected to return actual file size.

What do you see instead?

Instead of actual file size, it shows size as 0.

Additional information

This is valid for files under /proc directory. fs.stat returns size as 0 for /proc/meminfo, /proc/cpuinfo, /proc/version and other files.
Below code from nodejs-procfs with a small modification, is able to return actual file size.

const getSize = path => {
	const fd = openSync(path, 'r', 0o666);
	let pos = 0;
	let bytesRead;
	let buf = tmpBuf;
	let length = buf.length;
	do {
		bytesRead = readSync(fd, buf, pos, buf.length - pos, null);
		pos += bytesRead;
		if (pos === tmpBuf.length) {
			length = length << 1;
			let newBuf = Buffer.allocUnsafeSlow(length);

			if (length <= tmpBufMaxLen) {
				tmpBuf = newBuf;
			}

			buf.copy(newBuf);
			buf = newBuf;
		}
	} while (bytesRead !== 0);
	closeSync(fd);
	return buf.toString('utf8', 0, pos).length;
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    fsIssues and PRs related to the fs subsystem / file system.invalidIssues and PRs that are invalid.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions