-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move open files to its own module, properly close fds in tests
- Loading branch information
Showing
14 changed files
with
192 additions
and
139 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
const { FIRST_DESCRIPTOR } = require('./constants'); | ||
const openFiles = {}; | ||
|
||
/** | ||
* Start at FIRST_DESCRIPTOR and go until we find | ||
* an empty file descriptor, then return it. | ||
*/ | ||
const getEmptyDescriptor = () => { | ||
let fd = FIRST_DESCRIPTOR; | ||
|
||
while(getOpenFileDescription(fd)) { | ||
fd++; | ||
} | ||
|
||
return fd; | ||
}; | ||
|
||
/** | ||
* Look up the open file description object for a given | ||
* file descriptor. | ||
*/ | ||
const getOpenFileDescription = ofd => openFiles[ofd]; | ||
|
||
/** | ||
* Allocate a new file descriptor for the given | ||
* open file description. | ||
*/ | ||
const allocDescriptor = openFileDescription => { | ||
const ofd = getEmptyDescriptor(); | ||
openFiles[ofd] = openFileDescription; | ||
return ofd; | ||
}; | ||
|
||
/** | ||
* Release the given existing file descriptor created | ||
* with allocDescriptor(). | ||
*/ | ||
const releaseDescriptor = ofd => delete openFiles[ofd]; | ||
|
||
module.exports = { | ||
allocDescriptor, | ||
releaseDescriptor, | ||
getOpenFileDescription | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.