Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ab68f39

Browse files
committedJul 8, 2011
Fix fs can't handle large file on 64bit platform
fs.read() and fs.write() can't handle more than 2GB files on 64bit platform. Also fs.truncate() can't handle more than 4GB files. Fixes #1199.
1 parent e81a89b commit ab68f39

File tree

2 files changed

+92
-2
lines changed

2 files changed

+92
-2
lines changed
 

‎src/node_file.cc

+37-2
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ static inline bool SetCloseOnExec(int fd) {
8484
#endif
8585
}
8686

87+
#ifdef _LARGEFILE_SOURCE
88+
static inline int IsInt64(double x) {
89+
return x == static_cast<double>(static_cast<int64_t>(x));
90+
}
91+
#endif
92+
8793

8894
static int After(eio_req *req) {
8995
HandleScope scope;
@@ -460,6 +466,20 @@ static Handle<Value> Rename(const Arguments& args) {
460466
}
461467
}
462468

469+
#ifndef _LARGEFILE_SOURCE
470+
#define ASSERT_TRUNCATE_LENGTH(a) \
471+
if (!(a)->IsUndefined() && !(a)->IsNull() && !(a)->IsUInt32()) { \
472+
return ThrowException(Exception::TypeError(String::New("Not an integer"))); \
473+
}
474+
#define GET_TRUNCATE_LENGTH(a) ((a)->UInt32Value())
475+
#else
476+
#define ASSERT_TRUNCATE_LENGTH(a) \
477+
if (!(a)->IsUndefined() && !(a)->IsNull() && !IsInt64((a)->NumberValue())) { \
478+
return ThrowException(Exception::TypeError(String::New("Not an integer"))); \
479+
}
480+
#define GET_TRUNCATE_LENGTH(a) ((a)->IntegerValue())
481+
#endif
482+
463483
static Handle<Value> Truncate(const Arguments& args) {
464484
HandleScope scope;
465485

@@ -468,7 +488,9 @@ static Handle<Value> Truncate(const Arguments& args) {
468488
}
469489

470490
int fd = args[0]->Int32Value();
471-
off_t len = args[1]->Uint32Value();
491+
492+
ASSERT_TRUNCATE_LENGTH(args[1]);
493+
off_t len = GET_TRUNCATE_LENGTH(args[1]);
472494

473495
if (args[2]->IsFunction()) {
474496
ASYNC_CALL(ftruncate, args[2], fd, len)
@@ -670,7 +692,19 @@ static Handle<Value> Open(const Arguments& args) {
670692
}
671693
}
672694

673-
#define GET_OFFSET(a) (a)->IsInt32() ? (a)->IntegerValue() : -1;
695+
#ifndef _LARGEFILE_SOURCE
696+
#define ASSERT_OFFSET(a) \
697+
if (!(a)->IsUndefined() && !(a)->IsNull() && !(a)->IsInt32()) { \
698+
return ThrowException(Exception::TypeError(String::New("Not an integer"))); \
699+
}
700+
#define GET_OFFSET(a) ((a)->IsNumber() ? (a)->Int32Value() : -1)
701+
#else
702+
#define ASSERT_OFFSET(a) \
703+
if (!(a)->IsUndefined() && !(a)->IsNull() && !IsInt64((a)->NumberValue())) { \
704+
return ThrowException(Exception::TypeError(String::New("Not an integer"))); \
705+
}
706+
#define GET_OFFSET(a) ((a)->IsNumber() ? (a)->IntegerValue() : -1)
707+
#endif
674708

675709
// bytesWritten = write(fd, data, position, enc, callback)
676710
// Wrapper for write(2).
@@ -711,6 +745,7 @@ static Handle<Value> Write(const Arguments& args) {
711745
String::New("Length is extends beyond buffer")));
712746
}
713747

748+
ASSERT_OFFSET(args[4]);
714749
off_t pos = GET_OFFSET(args[4]);
715750

716751
char * buf = (char*)buffer_data + off;

‎test/disabled/test-fs-largefile.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
var path = require('path'),
25+
fs = require('fs'),
26+
filepath = path.join(common.tmpDir, 'large.txt'),
27+
fd = fs.openSync(filepath, 'w+'),
28+
offset = 5 * 1024 * 1024 * 1024, // 5GB
29+
message = 'Large File';
30+
31+
fs.truncateSync(fd, offset);
32+
assert.equal(fs.statSync(filepath).size, offset);
33+
var writeBuf = new Buffer(message);
34+
fs.writeSync(fd, writeBuf, 0, writeBuf.length, offset);
35+
var readBuf = new Buffer(writeBuf.length);
36+
fs.readSync(fd, readBuf, 0, readBuf.length, offset);
37+
assert.equal(readBuf.toString(), message);
38+
fs.readSync(fd, readBuf, 0, 1, 0);
39+
assert.equal(readBuf[0], 0);
40+
41+
var exceptionRaised = false;
42+
try {
43+
fs.writeSync(fd, writeBuf, 0, writeBuf.length, 42.000001);
44+
} catch (err) {
45+
console.log(err);
46+
exceptionRaised = true;
47+
assert.equal(err.message, 'Not an integer');
48+
}
49+
assert.ok(exceptionRaised);
50+
fs.close(fd);
51+
52+
process.on('exit', function() {
53+
fs.unlink(filepath);
54+
});
55+

0 commit comments

Comments
 (0)
This repository has been archived.