node.js 无法理解 fs.stat() 的工作原理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8582105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Having trouble understanding how fs.stat() works
提问by Asher Saban
I'm trying to write a function that tells me is a certain path is a directory.
我正在尝试编写一个函数,告诉我某个路径是一个目录。
var fs = require('fs');
console.log("+++++++++++++++++++++++++++++++++++++++");
fs.statSync(pathname, function(err, stats) {
console.log(stats.isDirectory());
});
console.log("+++++++++++++++++++++++++++++++++++++++");
However, it never prints the answer.
但是,它永远不会打印答案。
If pathname exists - it doesn't call the function.
If it doesn't exists, it generates an exception: ENOENT not a file or directory.
I don't want to know it pathname exists, but I want to know if it's a directory.
如果路径名存在 - 它不会调用该函数。如果它不存在,它会生成一个异常:ENOENT not a file or directory. 我不想知道它路径名存在,但我想知道它是否是一个目录。
Can anyone help me fix it?
任何人都可以帮我修复它吗?
回答by Alex Wayne
You are using the synchronous version, which doesn't use a callback. It simply returns the result instead. So either use the async form fs.stat(path, callback)or use the sync form like this:
您正在使用不使用回调的同步版本。它只是简单地返回结果。所以要么使用异步表单,fs.stat(path, callback)要么像这样使用同步表单:
var fs = require('fs');
console.log("+++++++++++++++++++++++++++++++++++++++");
var stats = fs.statSync(pathname);
console.log(stats.isDirectory());
console.log("+++++++++++++++++++++++++++++++++++++++");
回答by Arthur Lacoste
How fs.stat() works ?
fs.stat() 如何工作?
If you want to use a callback/async fs function, don't use the synchronous version, use fs.stat() :
如果要使用回调/异步 fs 函数,请不要使用同步版本,请使用 fs.stat() :
var fs = require('fs');
console.log("+++++++++++++++++++++++++++++++++++++++");
fs.stat(pathname, function(err, stats) {
console.log(stats.isDirectory());
});
console.log("+++++++++++++++++++++++++++++++++++++++");
There is more information about fs.stat(). You can get a lot of information about the main object :
有更多关于fs.stat() 的信息。您可以获得有关主要对象的大量信息:
fs.stat(path, function(err, stats) {
console.log(stats)
}
Output :
输出 :
{ dev: 2049,
ino: 305352,
mode: 16877,
nlink: 12,
uid: 1000,
gid: 1000,
rdev: 0,
size: 4096,
blksize: 4096,
blocks: 8,
atime: '2009-06-29T11:11:55Z',
mtime: '2009-06-29T11:11:40Z',
ctime: '2009-06-29T11:11:40Z' }
Lots of elements is often useless for us, yes. But here is the signification of all of these variables, according to this article:
很多元素对我们来说通常是无用的,是的。但根据本文,这是所有这些变量的含义:
- dev:ID of the device containing the file
- mode:file protection
- nlink:number of hard links to the file
- uid:user ID of the file's owner.
- gid:group ID of the file's owner.
- rdev:device ID if the file is a special file.
- blksize:block size for file system I/O.
- ino:File inode number. An inode is a file system data structure that -
- storesinformation about a file.
- size:file total size in bytes.
- blocks:number of blocks allocated for the file.
- atime:date object representing the file's last access time.
- mtime:date object representing the file's last modification time.
- ctime:date object representing the last time the file's inode was changed.
- dev:包含文件的设备ID
- 模式:文件保护
- nlink:文件的硬链接数
- uid:文件所有者的用户 ID。
- gid:文件所有者的组 ID。
- rdev:如果文件是特殊文件,则为设备 ID。
- blksize:文件系统 I/O 的块大小。
- ino:文件 inode 编号。inode 是一种文件系统数据结构,它 -
- 存储有关文件的信息。
- size:文件总大小(以字节为单位)。
- blocks:为文件分配的块数。
- atime:表示文件上次访问时间的日期对象。
- mtime:表示文件最后修改时间的日期对象。
- ctime:表示上次更改文件 inode 的日期对象。
You can also, like nodeJS documentation says, get more information like :
您还可以像nodeJS 文档所说的那样,获取更多信息,例如:
stats.isFile()
stats.isDirectory()
stats.isBlockDevice()
stats.isSymbolicLink() (only valid with fs.lstat())
stats.isCharacterDevice()
stats.isFIFO()
stats.isSocket()
About stats.isSymbolicLink(), there is another function than fs.stat, called fs.lstat(), and here is the difference between them :
关于stats.isSymbolicLink(),除了fs.stat还有一个函数,叫做fs.lstat(),下面是它们的区别:
statfollows symlinks. When given a path that is a symlink, it returns the stat of the target of the symlink.lstatdoesn't follow symlinks. When given a path that is a symlink it returns the stat of the symlink and not its target.
stat遵循符号链接。当给定一个符号链接路径时,它返回符号链接目标的统计信息。lstat不遵循符号链接。当给定一个符号链接路径时,它返回符号链接的统计信息而不是其目标。

