Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: consider NaN/Infinity in toUnixTimestamp #2387

Closed
wants to merge 1 commit into from

Conversation

yorkie
Copy link
Contributor

@yorkie yorkie commented Aug 15, 2015

We might should follow the rule of linux:

If times is NULL, then the access and modification times of the file are set to the current time.

ref: http://linux.die.net/man/2/utime

@mscdex mscdex added the fs Issues and PRs related to the fs subsystem / file system. label Aug 15, 2015
@mscdex
Copy link
Contributor

mscdex commented Aug 15, 2015

Why not replace both checks with !isFinite()?

@yorkie
Copy link
Contributor Author

yorkie commented Aug 15, 2015

Oh, I absolutely this function, let me use and test it, thanks for the tip

@mscdex mscdex added the semver-major PRs that contain breaking changes and should be released in the next major version. label Aug 15, 2015
@yorkie
Copy link
Contributor Author

yorkie commented Aug 15, 2015

Fixed, thank you Brian

@mscdex
Copy link
Contributor

mscdex commented Aug 15, 2015

I meant replace both conditions, like simply:

if (!isFinite(time)) {

since !isFinite(NaN) === true

@yorkie
Copy link
Contributor Author

yorkie commented Aug 15, 2015

Fixed, thank you

@@ -126,7 +126,9 @@ runTest(new Date('1982-09-10 13:37'), new Date('1982-09-10 13:37'), function() {
runTest(new Date(), new Date(), function() {
runTest(123456.789, 123456.789, function() {
runTest(stats.mtime, stats.mtime, function() {
// done
runTest(NaN, Infinity, function() {
// TODO
Copy link
Contributor

Choose a reason for hiding this comment

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

What exactly to do here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, it should be "done"

@yorkie
Copy link
Contributor Author

yorkie commented Aug 16, 2015

Corrected, thanks @thefourtheye

@@ -1046,6 +1046,9 @@ fs.chownSync = function(path, uid, gid) {
// converts Date or number to a fractional UNIX timestamp
function toUnixTimestamp(time) {
if (typeof time === 'number') {
if (!isFinite(time)) {

Choose a reason for hiding this comment

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

Should use Number.isFinite() instead of isFinite():

In comparison to the global isFinite() function, this method doesn't forcibly convert the parameter to a number. This means only values of the type number, that are also finite, return true.

Doesn't matter here, but good for consistency.

@yorkie
Copy link
Contributor Author

yorkie commented Aug 16, 2015

After reading some docs about the difference at MDN and ECMA spec, I changed it to use the Number.isFinite is better than that global function. Thanks @sindresorhus

@yorkie
Copy link
Contributor Author

yorkie commented Aug 19, 2015

Any feedback from core team?

@trevnorris
Copy link
Contributor

LGTM

@thefourtheye
Copy link
Contributor

Hmmm.

  1. what about the negative numbers?
  2. Also, we should mention this behaviour in the docs as well, I think.

@yorkie
Copy link
Contributor Author

yorkie commented Aug 22, 2015

what about the negative numbers?

I have no idea about the negative, because the linux manual didn't mention this, so I have the following proposals:

  • Treat it as NULL and set the date to Date.now()
  • Date.now() + time means minus the absolute value
  • Do nothing

Also, we should mention this behaviour in the docs as well, I think.

I will document this either after getting the answer of the above from @thefourtheye or other Node.js team member. Thank you :)

@yorkie
Copy link
Contributor Author

yorkie commented Aug 26, 2015

any words? @thefourtheye

@yorkie
Copy link
Contributor Author

yorkie commented Aug 26, 2015

How is this pr going? @thefourtheye did ignore my questions and proposals again, I don't know what happened :(

@trevnorris
Copy link
Contributor

@yorkie Might be outside the scope of this PR, but is there utility in accepting strings? The check would basically be:

if (typeof time === 'string' && +time == time) {
  return +time;
}

I'm cool w/ the change if CI is happy.

@yorkie
Copy link
Contributor Author

yorkie commented Aug 27, 2015

Hey @trevnorris I Added your suggestion at this PR, but I still care about strict mode and source code confusion in using the operator ==.

Mainly, the unit test is pass on my side :)

@yorkie
Copy link
Contributor Author

yorkie commented Aug 27, 2015

Added few document as well, but I have not yet got any response about minus value, so I will just keep the current logic.

@thefourtheye
Copy link
Contributor

@yorkie I am really sorry about the delay. I finally got some time to test this. The value of -1 for atime and mtime seems to reset the times to UNIX Epoch time, in my Ubuntu box.


- if the value is a numberable string like "123456789", the value would get converted to
corresponding number.
- if the value is `NaN` or `Infinity`, the value would get converted to `Date.now()`.
Copy link
Contributor

Choose a reason for hiding this comment

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

If

@yorkie
Copy link
Contributor Author

yorkie commented Aug 27, 2015

It's fine @thefourtheye

reset the times to UNIX Epoch time

Do you have some document about this or someone from nodejs team tell me what should I do, or I think set to Unix Epoch is not still the best solution.

@trevnorris
Copy link
Contributor

@yorkie Thanks. Only reason I suggest using +val == val is because it's the only single operation to check for both stringified hex and floating point values.

@thefourtheye
Copy link
Contributor

@yorkie I searched for documentations but I couldn't find any. I found that by running

const fs = require('fs');
console.error(fs.statSync('Test.py'));
fs.utimesSync('Test.py', -1, -1);
console.error(fs.statSync('Test.py'));

@yorkie
Copy link
Contributor Author

yorkie commented Aug 27, 2015

Thanks for your work @thefourtheye, I will follow your running result at first :)

@yorkie
Copy link
Contributor Author

yorkie commented Aug 27, 2015

Updated, now the minus number and other Date.now() has been set to corresponding Epoch timestamp, @thefourtheye could you please review again, thank you :)

@thefourtheye
Copy link
Contributor

@yorkie Ah, lets get some confirmation about the negative values. @bnoordhuis any idea?

@yorkie
Copy link
Contributor Author

yorkie commented Aug 27, 2015

Of course, and I will document something after that :)

@yorkie
Copy link
Contributor Author

yorkie commented Aug 28, 2015

Ping @bnoordhuis

@yorkie
Copy link
Contributor Author

yorkie commented Sep 14, 2015

@orangemocha so the thing that I should or can do is reword the title and push again?

runTest(new Date('1982-09-10 13:37'), new Date('1982-09-10 13:37'), function() {
runTest(new Date(), new Date(), function() {
runTest(123456.789, 123456.789, function() {
runTest(stats.mtime, stats.mtime, function() {
// done
runTest(NaN, Infinity, function() {
runTest('123456', -1, function () {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Linter would break at this line? @Fishrock123

@orangemocha
Copy link
Contributor

so the thing that I should or can do is reword the title and push again?

@yorke : yes!

@yorkie
Copy link
Contributor Author

yorkie commented Sep 14, 2015

Yes, sir. I will fix the line 131 as well :)

@orangemocha
Copy link
Contributor

@yorkie : or, if you added those tests in the first place, you could also do an interactive rebase and change your commits to simply not add them.

@yorkie
Copy link
Contributor Author

yorkie commented Sep 14, 2015

Ok, I did squash all commits then could someone run the CI again, thanks for your helps :)

@orangemocha
Copy link
Contributor

@yorkie
Copy link
Contributor Author

yorkie commented Sep 14, 2015

The test seems to be suspended by ARM at https://ci.nodejs.org/job/node-test-commit-arm/634/console, anything that I can help to fix?

@trevnorris
Copy link
Contributor

@yorkie Just took a really long time to finish. Everything looks green now.

trevnorris pushed a commit that referenced this pull request Sep 14, 2015
PR-URL: #2387
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
@trevnorris
Copy link
Contributor

Landed on 1f842c2 with column change in doc.

@trevnorris trevnorris closed this Sep 14, 2015
@Fishrock123
Copy link
Contributor

To clarify, this is indeed a breaking change?

@yorkie yorkie deleted the fix/fs branch September 15, 2015 04:27
@yorkie
Copy link
Contributor Author

yorkie commented Sep 15, 2015

Thanks @trevnorris

To clarify, this is indeed a breaking change?

Shouldn't, I guess few modules use Nan or Infinity internally, and in my opinion this seems to be a bug fix?

@rvagg rvagg mentioned this pull request Sep 15, 2015
@trevnorris
Copy link
Contributor

@Fishrock123 I'd say this is a bug-fix. This simply makes previously undefined behavior defined.

@Fishrock123 Fishrock123 removed the semver-major PRs that contain breaking changes and should be released in the next major version. label Sep 15, 2015
Fishrock123 pushed a commit that referenced this pull request Sep 15, 2015
PR-URL: #2387
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
@Fishrock123 Fishrock123 mentioned this pull request Sep 15, 2015
7 tasks
Fishrock123 added a commit to Fishrock123/node that referenced this pull request Sep 17, 2015
Notable changes:

* buffer:
  - Buffers are now created in JavaScript, rather than C++. This increases the speed of buffer creation (Trevor Norris) nodejs#2866.
  - `Buffer#slice()` now uses `Uint8Array#subarray()` internally, increasing `slice()` performance (Karl Skomski) nodejs#2777.
* fs:
  - `fs.utimes()` now properly converts numeric strings, `NaN`, and `Infinity` (Yazhong Liu) nodejs#2387.
  - `fs.WriteStream` now implements `_writev`, allowing for super-fast bulk writes (Ron Korving) nodejs#2167.
* http: Fixed an issue with certain `write()` sizes causing errors when using `http.request()` (Fedor Indutny) nodejs#2824.
* npm: Upgrade to version 2.14.3, see https://github.com/npm/npm/releases/tag/v2.14.3 for more details (Kat Marchán) nodejs#2822.
* src: V8 cpu profiling no longer erroneously shows idle time (Oleksandr Chekhovskyi) nodejs#2324.
* v8: Lateral upgrade to 4.5.103.33 from 4.5.103.30, contains minor fixes (Ali Ijaz Sheikh) nodejs#2870.
  - This fixes a previously known bug where some computed object shorthand properties did not work correctly (nodejs#2507).

Refs: nodejs#2844
PR-URL: nodejs#2889
Fishrock123 added a commit that referenced this pull request Sep 17, 2015
Notable changes:

* buffer:
  - Buffers are now created in JavaScript, rather than C++. This increases the speed of buffer creation (Trevor Norris) #2866.
  - `Buffer#slice()` now uses `Uint8Array#subarray()` internally, increasing `slice()` performance (Karl Skomski) #2777.
* fs:
  - `fs.utimes()` now properly converts numeric strings, `NaN`, and `Infinity` (Yazhong Liu) #2387.
  - `fs.WriteStream` now implements `_writev`, allowing for super-fast bulk writes (Ron Korving) #2167.
* http: Fixed an issue with certain `write()` sizes causing errors when using `http.request()` (Fedor Indutny) #2824.
* npm: Upgrade to version 2.14.3, see https://github.com/npm/npm/releases/tag/v2.14.3 for more details (Kat Marchán) #2822.
* src: V8 cpu profiling no longer erroneously shows idle time (Oleksandr Chekhovskyi) #2324.
* v8: Lateral upgrade to 4.5.103.33 from 4.5.103.30, contains minor fixes (Ali Ijaz Sheikh) #2870.
  - This fixes a previously known bug where some computed object shorthand properties did not work correctly (#2507).

Refs: #2844
PR-URL: #2889
@rvagg rvagg mentioned this pull request Sep 22, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fs Issues and PRs related to the fs subsystem / file system.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants