Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const path = require('path')
const filter = require('interface-datastore').utils.filter
const take = require('interface-datastore').utils.take
const map = require('interface-datastore').utils.map
const sortAll = require('interface-datastore').utils.sortAll
const IDatastore = require('interface-datastore')
const noop = () => {}
const asyncMkdirp = promisify(require('mkdirp'))
const fsAccess = promisify(fs.access || noop)
const fsReadFile = promisify(fs.readFile || noop)
const fsUnlink = promisify(fs.unlink || noop)
const Key = IDatastore.Key
const Errors = IDatastore.Errors
async function writeFile (path, contents) {
try {
await writeAtomic(path, contents)
} catch (err) {
if (err.code === 'EPERM' && err.syscall === 'rename') {
// fast-write-atomic writes a file to a temp location before renaming it.
// On Windows, if the final file already exists this error is thrown.
// No such error is thrown on Linux/Mac
// Make sure we can read & write to this file
await fsAccess(path, fs.constants.F_OK | fs.constants.W_OK)
// The file was created by another context - this means there were
// attempts to write the same block by two different function calls
return
}