Skip to content

Commit

Permalink
Merge pull request #96 from CodeKingdomsTeam/empty-city-data
Browse files Browse the repository at this point in the history
Avoid trying to read from empty city data files (fixes #83)
  • Loading branch information
bluesmoon committed Dec 2, 2015
2 parents 86e04a6 + e1ad5b9 commit 09b6480
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions lib/geoip.js
Expand Up @@ -303,14 +303,20 @@ function preload(callback) {
datFile = fs.openSync(dataFiles.cityNames, 'r');
datSize = fs.fstatSync(datFile).size;

if (datSize === 0) {
throw {
code: 'EMPTY_FILE'
};
}

cache4.locationBuffer = new Buffer(datSize);
fs.readSync(datFile, cache4.locationBuffer, 0, datSize, 0);
fs.closeSync(datFile);

datFile = fs.openSync(dataFiles.city, 'r');
datSize = fs.fstatSync(datFile).size;
} catch(err) {
if (err.code !== 'ENOENT' && err.code !== 'EBADF') {
if (err.code !== 'ENOENT' && err.code !== 'EBADF' && err.code !== 'EMPTY_FILE') {
throw err;
}

Expand Down Expand Up @@ -407,8 +413,14 @@ function preload6(callback) {
try {
datFile = fs.openSync(dataFiles.city6, 'r');
datSize = fs.fstatSync(datFile).size;

if (datSize === 0) {
throw {
code: 'EMPTY_FILE'
};
}
} catch(err) {
if (err.code !== 'ENOENT' && err.code !== 'EBADF') {
if (err.code !== 'ENOENT' && err.code !== 'EBADF' && err.code !== 'EMPTY_FILE') {
throw err;
}

Expand Down

0 comments on commit 09b6480

Please sign in to comment.