Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ Tail = (function(_super) {
var next = function() {

if (block.type == 'close') {
fs.close(block.fd);
try {
fs.close(block.fd);
} catch(err) {
console.log("FAILED TO CLOSE FD:", err, err.stack);
}
delete self.bookmarks[block.fd];
};

Expand All @@ -38,10 +42,15 @@ Tail = (function(_super) {
fs.fstat(block.fd, function(err, stat) {

if (err) { return next(); };

var start = self.bookmarks[block.fd];
var end = stat.size;


// self-protection, start may be undefined or NaN, race condition??
if(typeof start === 'undefined' || isNaN(start)) {
return next();
}

if (end < start) {
// file was truncated
debug('file was truncated:', self.filename);
Expand Down Expand Up @@ -161,15 +170,24 @@ Tail = (function(_super) {
};

if (self.fd) {
fs.close(self.fd);
try {
fs.close(self.fd);
} catch(err) {
console.log("FAILED TO CLOSE FD:", err, err.stack);
}

self.fd = null;
};

// close any legacy fds
for (var i in self.queue) {
var item = self.queue[i];
if (item.type == 'close') {
fs.close(item.fd);
try {
fs.close(item.fd);
} catch (err) {
console.log("FAILED TO CLOSE FD:", err, err.stack);
}
};
};

Expand Down Expand Up @@ -222,9 +240,13 @@ Tail = (function(_super) {
if (self.fd == null) {
fs.exists(self.filename, function(exists) {
if (exists) {
self.fd = fs.openSync(self.filename, 'r');
self.inode = curr.ino;
self.bookmarks[self.fd] = 0;
try {
self.fd = fs.openSync(self.filename, 'r');
self.inode = curr.ino;
self.bookmarks[self.fd] = 0;
} catch(err) {
// race condition, file is removed between exists function and openSync function
}
}
callback();
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "always-tail",
"version": "0.2.0",
"version": "0.2.1",
"description": "continuous file tail. robust enough to survive rollovers.",
"main": "index.js",
"scripts": {
Expand Down