Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

fix fs.truncate when first arg is an int fd #9161

Closed
wants to merge 1 commit into from

Conversation

bjouhier
Copy link

@bjouhier bjouhier commented Feb 7, 2015

Bug is rather obvious and fix is trivial (see diff below).

@cjihrig
Copy link

cjihrig commented Feb 7, 2015

Can you add a regression test please.

@bjouhier
Copy link
Author

bjouhier commented Feb 8, 2015

@cjihrig Getting late I'll do it tomorrow. Where should I put it? I guess it is test/simple/test-regress-GH-9161.js but I am not sure.

bjouhier added a commit to bjouhier/node that referenced this pull request Feb 8, 2015
@bjouhier
Copy link
Author

bjouhier commented Feb 8, 2015

@cjihrig I added the test.

var path = require('path');
var fs = require('fs');
var tmp = common.tmpDir;
if (!fs.existsSync(tmp)) fs.mkdirSync(tmp);
Copy link

Choose a reason for hiding this comment

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

Can you put the fs.mkdirSync(tmp); on a new line.

Copy link
Author

Choose a reason for hiding this comment

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

done

@cjihrig
Copy link

cjihrig commented Feb 8, 2015

I notice that this behavior doesn't seem to be documented. Perhaps it should be added there.

@bjouhier
Copy link
Author

bjouhier commented Feb 8, 2015

Regarding the doc issue, I have the impression that this is compatibility code. People should use ftruncate instead. Do you document compat hooks?

fs.writeFileSync(filename, 'hello world', 'utf8');
var fd = fs.openSync(filename, 'r+');
fs.truncate(fd, 5, function(err) {
if (err) throw err;
Copy link

Choose a reason for hiding this comment

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

This can just be assert.equal(err, null); (or undefined depending what the success case is).

@cjihrig
Copy link

cjihrig commented Feb 8, 2015

@tjfontaine should this behavior be documented?

@tjfontaine
Copy link

Yes, I think we should document this behavior, for v0.10 and v0.12. We can decide if we want to deprecate/remove this fallback in master at a later date.

I agree that this fix is the the best way to handle the bug for v0.12.

For master though, I think we want to fix makeCallback:

  • the first check should be ifNullOrUndefined because the caller explicitly didn't pass in an argument
  • then the next check should be !isFunction which imo should outright throw, because invalid input was provided.

This would have made there result much more crisp and clear about what went wrong.

@bjouhier
Copy link
Author

bjouhier commented Feb 9, 2015

@tjfontaine I agree. I can handle the doc issue if you want but makeCallback should probably be handled as a separate issue.

@cjihrig
Copy link

cjihrig commented Feb 13, 2015

@bjouhier can you address the last code nit, update the docs, and squash your commits.

added unit test
added note to the doc
improved makeCallback arg checking
@bjouhier
Copy link
Author

@cjihrig I simplified the unit test, improved makeCallback, added a note to the doc and squashed the commits.

cjihrig pushed a commit that referenced this pull request Feb 17, 2015
Currently, fs.truncate() silently fails when a file descriptor
is passed as the first argument. This commit changes this
behavior to properly call fs.ftruncate(). This commit also
adds proper type checking to the callback provided to
makeCallback().

PR-URL: #9161
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Timothy J Fontaine <tjfontaine@gmail.com>
@cjihrig
Copy link

cjihrig commented Feb 17, 2015

Thanks! Landed in master in 4c31cda with a few tweaks.

@cjihrig cjihrig closed this Feb 17, 2015
cjihrig pushed a commit that referenced this pull request Feb 17, 2015
Currently, fs.truncate() silently fails when a file descriptor
is passed as the first argument. This commit changes this
behavior to properly call fs.ftruncate().

PR-URL: #9161
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Timothy J Fontaine <tjfontaine@gmail.com>
@cjihrig
Copy link

cjihrig commented Feb 17, 2015

And landed in 0.12 in b3aa876 without the change to makeCallback().

cjihrig pushed a commit to nodejs/node that referenced this pull request Feb 21, 2015
Currently, fs.truncate() silently fails when a file descriptor
is passed as the first argument. This commit changes this
behavior to properly call fs.ftruncate().

PR-URL: nodejs/node-v0.x-archive#9161
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Timothy J Fontaine <tjfontaine@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>

Conflicts:
	lib/fs.js
jasnell pushed a commit to jasnell/node-joyent that referenced this pull request Mar 14, 2015
Currently, fs.truncate() silently fails when a file descriptor
is passed as the first argument. This commit changes this
behavior to properly call fs.ftruncate(). This commit also
adds proper type checking to the callback provided to
makeCallback().

PR-URL: nodejs#9161
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Timothy J Fontaine <tjfontaine@gmail.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants