mirror of https://github.com/lukechilds/node.git
Browse Source
This reverts commit c86c1eeab5
.
original commit message:
This patch
1. moves the basic validation of arguments to `truncate` family
of functions to the JavaScript layer from the C++ layer.
2. makes sure that the File Descriptors are validated strictly.
PR-URL: #2498
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/7950
Reviewed-By: Julien Gilli <jgilli@nodejs.org>
Reviewed-By: Rod Vagg <rod@vagg.org>
Reviewed-By: Minwoo Jung <jmwsoft@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
v7.x
Myles Borins
9 years ago
committed by
Myles Borins
6 changed files with 118 additions and 301 deletions
@ -1,71 +1,21 @@ |
|||
'use strict'; |
|||
const common = require('../common'); |
|||
const assert = require('assert'); |
|||
const fs = require('fs'); |
|||
const path = require('path'); |
|||
|
|||
var common = require('../common'); |
|||
var assert = require('assert'); |
|||
var path = require('path'); |
|||
var fs = require('fs'); |
|||
var tmp = common.tmpDir; |
|||
common.refreshTmpDir(); |
|||
const fds = []; |
|||
var filename = path.resolve(tmp, 'truncate-file.txt'); |
|||
|
|||
const filename = path.resolve(common.tmpDir, 'truncate-file.txt'); |
|||
fs.writeFileSync(filename, 'hello world', 'utf8'); |
|||
const fd = fs.openSync(filename, 'r+'); |
|||
fds.push(fd); |
|||
var fd = fs.openSync(filename, 'r+'); |
|||
fs.truncate(fd, 5, common.mustCall(function(err) { |
|||
assert.ifError(err); |
|||
assert.ok(!err); |
|||
assert.equal(fs.readFileSync(filename, 'utf8'), 'hello'); |
|||
})); |
|||
|
|||
{ |
|||
// test partial truncation of a file
|
|||
const fileName = path.resolve(common.tmpDir, 'truncate-file-1.txt'); |
|||
console.log(fileName); |
|||
fs.writeFileSync(fileName, 'hello world', 'utf8'); |
|||
const fd = fs.openSync(fileName, 'r+'); |
|||
fds.push(fd); |
|||
|
|||
fs.truncate(fd, 5, common.mustCall(function(err) { |
|||
assert.ifError(err); |
|||
assert.strictEqual(fs.readFileSync(fileName, 'utf8'), 'hello'); |
|||
})); |
|||
} |
|||
|
|||
{ |
|||
// make sure numbers as strings are not treated as fds with sync version
|
|||
const fileName = path.resolve(common.tmpDir, 'truncate-file-2.txt'); |
|||
console.log(fileName); |
|||
fs.writeFileSync(fileName, 'One'); |
|||
const fd = fs.openSync(fileName, 'r'); |
|||
fds.push(fd); |
|||
|
|||
const fdFileName = path.resolve(common.tmpDir, '' + fd); |
|||
fs.writeFileSync(fdFileName, 'Two'); |
|||
assert.strictEqual(fs.readFileSync(fileName).toString(), 'One'); |
|||
assert.strictEqual(fs.readFileSync(fdFileName).toString(), 'Two'); |
|||
|
|||
fs.truncateSync(fdFileName); |
|||
assert.strictEqual(fs.readFileSync(fileName).toString(), 'One'); |
|||
assert.strictEqual(fs.readFileSync(fdFileName).toString(), ''); |
|||
} |
|||
|
|||
{ |
|||
// make sure numbers as strings are not treated as fds with async version
|
|||
const fileName = path.resolve(common.tmpDir, 'truncate-file-3.txt'); |
|||
console.log(fileName); |
|||
fs.writeFileSync(fileName, 'One'); |
|||
const fd = fs.openSync(fileName, 'r'); |
|||
fds.push(fd); |
|||
|
|||
const fdFileName = path.resolve(common.tmpDir, '' + fd); |
|||
fs.writeFileSync(fdFileName, 'Two'); |
|||
assert.strictEqual(fs.readFileSync(fileName).toString(), 'One'); |
|||
assert.strictEqual(fs.readFileSync(fdFileName).toString(), 'Two'); |
|||
|
|||
fs.truncate(fdFileName, common.mustCall(function(err) { |
|||
assert.ifError(err); |
|||
assert.strictEqual(fs.readFileSync(fileName).toString(), 'One'); |
|||
assert.strictEqual(fs.readFileSync(fdFileName).toString(), ''); |
|||
})); |
|||
} |
|||
|
|||
process.on('exit', () => fds.forEach((fd) => fs.closeSync(fd))); |
|||
process.on('exit', function() { |
|||
fs.closeSync(fd); |
|||
fs.unlinkSync(filename); |
|||
console.log('ok'); |
|||
}); |
|||
|
@ -1,13 +1,12 @@ |
|||
'use strict'; |
|||
|
|||
require('../common'); |
|||
const fs = require('fs'); |
|||
const assert = require('assert'); |
|||
|
|||
assert.throws(function() { |
|||
fs.write(null, Buffer.allocUnsafe(1), 0, 1, () => {}); |
|||
}, /TypeError: file descriptor must be a unsigned 32-bit integer/); |
|||
fs.write(null, Buffer.allocUnsafe(1), 0, 1); |
|||
}, /TypeError/); |
|||
|
|||
assert.throws(function() { |
|||
fs.write(undefined, '1', 0, 1, () => {}); |
|||
}, /TypeError: file descriptor must be a unsigned 32-bit integer/); |
|||
fs.write(null, '1', 0, 1); |
|||
}, /TypeError/); |
|||
|
Loading…
Reference in new issue