Skip to content

win: restore file position after positional read/write operation#1357

Closed
bzoz wants to merge 2 commits intolibuv:v1.xfrom
JaneaSystems:bartek-fd-read
Closed

win: restore file position after positional read/write operation#1357
bzoz wants to merge 2 commits intolibuv:v1.xfrom
JaneaSystems:bartek-fd-read

Conversation

@bzoz
Copy link
Copy Markdown
Member

@bzoz bzoz commented May 23, 2017

File read or write from specified position will move file pointer on Windows but not on POSIX. This makes Windows behave as other supported platforms: for writes/reads with provided offset, file position before operation is saved, and then restored once the operation completes.

Ref: nodejs/node#9671

@sam-github
Copy link
Copy Markdown
Contributor

I've noticed this before, it surfaces as an undocumented aspect of the node fs.read/write calls. The windows implementation can't be atomic, as pread()/pwrite() are, but I don't think that matters, and it would be good to bring Unix/Windows into alignment here.

@saghul saghul added the windows label May 23, 2017
Comment thread src/win/fs.c Outdated
++index;
} while (result && index < req->fs.info.nbufs);

if (restore_position) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: drop braces here

Comment thread src/win/fs.c
} while (result && index < req->fs.info.nbufs);

if (restore_position) {
SetFilePointerEx(handle, original_position, NULL, FILE_BEGIN);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Comment thread test/test-fs.c Outdated
unlink("test_file");
loop = uv_default_loop();

r = uv_fs_open(loop, &open_req1, "test_file", O_RDWR | O_CREAT,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: please line arguments vertically

Comment thread test/test-fs.c
r = uv_fs_open(loop, &open_req1, "test_file", O_RDWR | O_CREAT,
S_IWUSR | S_IRUSR, NULL);
ASSERT(r >= 0);
ASSERT(open_req1.result >= 0);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this assert is kinda redundant, since the result of a sync open is the same as the return value.

Comment thread test/test-fs.c Outdated
iov = uv_buf_init(test_buf, sizeof(test_buf));
r = uv_fs_write(NULL, &write_req, open_req1.result, &iov, 1, 0, NULL);
ASSERT(r >= 0);
ASSERT(write_req.result >= 0);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Comment thread test/test-fs.c Outdated
iov = uv_buf_init(buf, sizeof(buf));
r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, 0, NULL);
ASSERT(r >= 0);
ASSERT(read_req.result >= 0);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

@saghul
Copy link
Copy Markdown
Member

saghul commented May 29, 2017

@bzoz
Copy link
Copy Markdown
Member Author

bzoz commented May 30, 2017

Updated, PTAL

File read or write from specified position will move file pointer on
Windows but not on POSIX. This makes Windows behave as other
supported platforms.

Ref: nodejs/node#9671
@bzoz bzoz force-pushed the bartek-fd-read branch from 102aac1 to c71d154 Compare June 6, 2017 09:49
@bzoz
Copy link
Copy Markdown
Member Author

bzoz commented Jun 6, 2017

Rebased, PTAL

@bzoz
Copy link
Copy Markdown
Member Author

bzoz commented Jun 6, 2017

@bzoz
Copy link
Copy Markdown
Member Author

bzoz commented Jun 12, 2017

Ping?

Comment thread test/test-fs.c Outdated
O_RDWR | O_CREAT,
S_IWUSR | S_IRUSR,
NULL);
ASSERT(r >= 0);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't r be greater than 0?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My test is modified fs_file_open_append test, didn't give a second though. I'll update the tests.

Comment thread test/test-fs.c Outdated

iov = uv_buf_init(test_buf, sizeof(test_buf));
r = uv_fs_write(NULL, &write_req, open_req1.result, &iov, 1, 0, NULL);
ASSERT(r >= 0);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we check the number r == sizeof(test_buf)?

Comment thread test/test-fs.c Outdated

iov = uv_buf_init(buf, sizeof(buf));
r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, 0, NULL);
ASSERT(r >= 0);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also here?

Comment thread src/win/fs.c
overlapped_ptr = &overlapped;
if (SetFilePointerEx(handle, zero_offset, &original_position,
FILE_CURRENT)) {
restore_position = 1;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On which cases can this fail? Should we note in the documentation that on Windows the offset could actually be modified after calling uv_fs_write() / uv_fs_read()?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per MSDN SetFilePointerEx doc this call can fail if the handle is not a file or if it was opened with FILE_FLAG_NO_BUFFERING. We test handle type and libuv does not uses that flag, so I guess we are safe.

@bzoz
Copy link
Copy Markdown
Member Author

bzoz commented Jun 13, 2017

Updated, PTAL

Copy link
Copy Markdown
Member

@santigimeno santigimeno left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM though I can't test it locally.

@bzoz
Copy link
Copy Markdown
Member Author

bzoz commented Jun 13, 2017

@bzoz
Copy link
Copy Markdown
Member Author

bzoz commented Jun 19, 2017

@saghul, can I get +1 from you?

Copy link
Copy Markdown
Member

@saghul saghul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

bzoz added a commit that referenced this pull request Jun 21, 2017
File read or write from specified position will move file pointer on
Windows but not on POSIX. This makes Windows behave as other
supported platforms.

Ref: nodejs/node#9671

PR-URL: #1357
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: Saúl Ibarra Corretgé <saghul@gmail.com>
@bzoz
Copy link
Copy Markdown
Member Author

bzoz commented Jun 21, 2017

Landed in 0bd8f5b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants