Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
fix: windows EPERM due to leaving file descriptor open
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmurdoch committed Jan 16, 2020
1 parent a7aa9be commit 7199264
Show file tree
Hide file tree
Showing 7 changed files with 964 additions and 520 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
version: ~> 1.0
language: node_js
node_js:
- "8"
Expand All @@ -23,6 +24,7 @@ before_install:
export LINK="gcc-5";
export LINKXX="g++-5";
fi
- git config --global core.autocrlf false

after_success:
npm run coverage
32 changes: 26 additions & 6 deletions lib/database/filedown.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,46 @@ FileDown.prototype._put = function(key, value, options, callback) {
// leveldb implementation that doesn't use a separate file for every key Soon(TM).
accessQueue.execute(lKey, () => {
// get a tmp file to write the contents to...
tmp.file((err, path, fd) => {
tmp.file((err, path, fd, cleanupTmpFile) => {
if (err) {
callback(err);
accessQueue.next(lKey);
return;
}

// write the contents to that temp file
// write the value to our temporary file
fs.writeFile(fd, value, "utf8", (err) => {
if (err) {
cleanupTmpFile();
callback(err);
accessQueue.next(lKey);
return;
}
// move the temp file to its final destination

// It worked! Move the temporary file to its final destination
fs.rename(path, lKey, (err) => {
callback(err);
if (err) {
cleanupTmpFile();
callback(err);
accessQueue.next(lKey);
return;
}

// if there is more work to be done on this key, do it.
accessQueue.next(lKey);
// make sure we close this file descriptor now that the file is no
// longer "temporary" (because we successfully moved it)
fs.close(fd, (err) => {
if (err) {
// at this point things seem to have worked, but juuuussttt in
// case `fs.close` fails, let's log some info so we can try to
// debug it
console.warn("An unexpected file descriptor close error occured: ", err);
cleanupTmpFile();
}
callback();

// if there is more work to be done on this key, do it.
accessQueue.next(lKey);
});
});
});
});
Expand Down
Loading

0 comments on commit 7199264

Please sign in to comment.