Skip to content

Conversation

@bzoz
Copy link
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
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
src/win/fs.c Outdated
++index;
} while (result && index < req->fs.info.nbufs);

if (restore_position) {
Copy link
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

} while (result && index < req->fs.info.nbufs);

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

Choose a reason for hiding this comment

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

ditto

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
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

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
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.

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
Member

Choose a reason for hiding this comment

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

ditto

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
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
Member

saghul commented May 29, 2017

@bzoz
Copy link
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
Member Author

bzoz commented Jun 6, 2017

Rebased, PTAL

@bzoz
Copy link
Member Author

bzoz commented Jun 6, 2017

@bzoz
Copy link
Member Author

bzoz commented Jun 12, 2017

Ping?

test/test-fs.c Outdated
O_RDWR | O_CREAT,
S_IWUSR | S_IRUSR,
NULL);
ASSERT(r >= 0);
Copy link
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
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.

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
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)?

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
Member

Choose a reason for hiding this comment

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

also here?

overlapped_ptr = &overlapped;
if (SetFilePointerEx(handle, zero_offset, &original_position,
FILE_CURRENT)) {
restore_position = 1;
Copy link
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
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
Member Author

bzoz commented Jun 13, 2017

Updated, PTAL

Copy link
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
Member Author

bzoz commented Jun 13, 2017

@bzoz
Copy link
Member Author

bzoz commented Jun 19, 2017

@saghul, can I get +1 from you?

Copy link
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 <[email protected]>
Reviewed-By: Saúl Ibarra Corretgé <[email protected]>
@bzoz
Copy link
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