简体   繁体   English

如何知道 Node.js 中的文件 position? - lseek 的返回值

[英]How to know file position in Node.js? - return value of lseek

Reading fs.read and fs.write , it seems that in Node.js no interface to the C function lseek is directly exposed;读取fs.readfs.write ,似乎在 Node.js 中没有 C function lseek的接口直接暴露; the current file descriptor position can be changed right before any fs.read or fs.write by the argument position .当前文件描述符 position 可以在任何fs.readfs.write之前通过参数position

I strongly suspect that, at low level, the argument position is handled with an lseek call before the read or write operation, but what about the return value of lseek ?我强烈怀疑,在低级别,参数position在读取或写入操作之前通过lseek调用处理,但是lseek的返回值呢?

To be more specific, to get the current file descriptor position in C, I can write:更具体地说,要获取 C 中的当前文件描述符 position,我可以这样写:

int position = lseek(fd, 0, SEEK_CUR);

Is there a way in Node.js to get the same information? Node.js 有没有办法获得相同的信息?

It seems there's no "native" way for this.似乎没有“本机”方式。 Probably because of大概是因为

It is unsafe to use fs.write***() multiple times on the same file without waiting for the callback.在同一个文件上多次使用 fs.write***()而不等待回调是不安全的。

It would be even more so with something like getCurrentPosition for a buffered IO.对于缓冲的 IO,使用getCurrentPosition之类的东西更是如此。 What it should return after write*** or read*** but before the callback is called?write***read***之后但在调用回调之前它应该返回什么?

But you can try and keep track of the current position yourself with the help of bytesRead and bytesWritten .但是您可以尝试在bytesReadbytesWritten的帮助下自己跟踪当前的 position。 And in case of streams there is chunk.length , but don't forget about encodings in case of strings.如果是流,则有chunk.length ,但不要忘记字符串的编码。


As a side note... you've wrote:作为旁注......你写道:

thinking to new features I thought this could be an useful info考虑新功能我认为这可能是一个有用的信息

If no one requested it, and you yourself is not sure what it can be used for, then I think it's a good idea to exercise YAGNI principle.如果没有人要求它,而您自己不确定它可以用于什么,那么我认为练习 YAGNI 原则是一个好主意。

I strongly suspect that, at low level, the argument position is handled with an lseek call before the read or write operation, but what about the return value of lseek?我强烈怀疑,在低级别,参数 position 在读取或写入操作之前通过 lseek 调用处理,但是 lseek 的返回值呢?

It is not used under the hood as said by Ben Noordhuis (libuv maintainer):正如Ben Noordhuis (libuv 维护者)所说,它没有在后台使用:

lseek() is unpredictable with concurrent readers or writers. lseek() 对于并发读取器或写入器是不可预测的。 The libuv way is to use positional reads and writes (the offset argument to uv_fs_read() and uv_fs_write().) libuv 的方式是使用位置读写(uv_fs_read() 和 uv_fs_write() 的偏移量参数。)

This issue on node.js GitHub talk about how they manage the position argument to read and write data into the file.本期关于 node.js GitHub谈论他们如何管理 position 参数以将数据读取和写入文件。

Is there a way in Node.js to get the same information? Node.js 有没有办法获得相同的信息?

The accepted answer explain well how to do that: you need to track the read and written bytes.接受的答案很好地解释了如何做到这一点:您需要跟踪读取和写入的字节。

In Node.js you can use fs.read() and fs.write() for same.在 Node.js 中,您可以使用fs.read()fs.write()

Node can tell you where in the file system it is working by using the _filename and _dirname variables. Node 可以通过使用_filename_dirname变量告诉您它在文件系统中的哪个位置工作。 The filename variable provides the absolute path to the file that is currently executing; filename 变量提供当前正在执行的文件的绝对路径; dirname provides the absolute path to the working directory where the file being executed is located. dirname 提供正在执行的文件所在的工作目录的绝对路径。 Neither variable has to be imported from any modules because each is provided standard.两个变量都不必从任何模块导入,因为每个模块都是标准提供的。

A simple example using both variables appears below:下面是一个使用这两个变量的简单示例:

console.log("This file is " +__filename ); console.log("这个文件是" +__filename ); console.log("It's located in " +__dirname ); console.log("它位于 " +__dirname );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM