1// A path exclusive reservation system 2// reserve([list, of, paths], fn) 3// When the fn is first in line for all its paths, it 4// is called with a cb that clears the reservation. 5// 6// Used by async unpack to avoid clobbering paths in use, 7// while still allowing maximal safe parallelization. 8 9const assert = require('assert') 10const normPath = require('./normalize-windows-path.js') 11const stripSlashes = require('./strip-trailing-slashes.js') 12const { join } = require('path') 13 14const platform = process.env.TESTING_TAR_FAKE_PLATFORM || process.platform 15const isWindows = platform === 'win32' 16 17module.exports = () => { 18 // path => [function or Set] 19 // A Set object means a directory reservation 20 // A fn is a direct reservation on that path 21 const queues = new Map() 22 23 // fn => {paths:[path,...], dirs:[path, ...]} 24 const reservations = new Map() 25 26 // return a set of parent dirs for a given path 27 // '/a/b/c/d' -> ['/', '/a', '/a/b', '/a/b/c', '/a/b/c/d'] 28 const getDirs = path => { 29 const dirs = path.split('/').slice(0, -1).reduce((set, path) => { 30 if (set.length) 31 path = normPath(join(set[set.length - 1], path)) 32 set.push(path || '/') 33 return set 34 }, []) 35 return dirs 36 } 37 38 // functions currently running 39 const running = new Set() 40 41 // return the queues for each path the function cares about 42 // fn => {paths, dirs} 43 const getQueues = fn => { 44 const res = reservations.get(fn) 45 /* istanbul ignore if - unpossible */ 46 if (!res) 47 throw new Error('function does not have any path reservations') 48 return { 49 paths: res.paths.map(path => queues.get(path)), 50 dirs: [...res.dirs].map(path => queues.get(path)), 51 } 52 } 53 54 // check if fn is first in line for all its paths, and is 55 // included in the first set for all its dir queues 56 const check = fn => { 57 const {paths, dirs} = getQueues(fn) 58 return paths.every(q => q[0] === fn) && 59 dirs.every(q => q[0] instanceof Set && q[0].has(fn)) 60 } 61 62 // run the function if it's first in line and not already running 63 const run = fn => { 64 if (running.has(fn) || !check(fn)) 65 return false 66 running.add(fn) 67 fn(() => clear(fn)) 68 return true 69 } 70 71 const clear = fn => { 72 if (!running.has(fn)) 73 return false 74 75 const { paths, dirs } = reservations.get(fn) 76 const next = new Set() 77 78 paths.forEach(path => { 79 const q = queues.get(path) 80 assert.equal(q[0], fn) 81 if (q.length === 1) 82 queues.delete(path) 83 else { 84 q.shift() 85 if (typeof q[0] === 'function') 86 next.add(q[0]) 87 else 88 q[0].forEach(fn => next.add(fn)) 89 } 90 }) 91 92 dirs.forEach(dir => { 93 const q = queues.get(dir) 94 assert(q[0] instanceof Set) 95 if (q[0].size === 1 && q.length === 1) { 96 queues.delete(dir) 97 } else if (q[0].size === 1) { 98 q.shift() 99 100 // must be a function or else the Set would've been reused 101 next.add(q[0]) 102 } else 103 q[0].delete(fn) 104 }) 105 running.delete(fn) 106 107 next.forEach(fn => run(fn)) 108 return true 109 } 110 111 const reserve = (paths, fn) => { 112 // collide on matches across case and unicode normalization 113 // On windows, thanks to the magic of 8.3 shortnames, it is fundamentally 114 // impossible to determine whether two paths refer to the same thing on 115 // disk, without asking the kernel for a shortname. 116 // So, we just pretend that every path matches every other path here, 117 // effectively removing all parallelization on windows. 118 paths = isWindows ? ['win32 parallelization disabled'] : paths.map(p => { 119 return stripSlashes(normPath(join(p))) 120 .normalize('NFKD') 121 .toLowerCase() 122 }) 123 124 const dirs = new Set( 125 paths.map(path => getDirs(path)).reduce((a, b) => a.concat(b)) 126 ) 127 reservations.set(fn, {dirs, paths}) 128 paths.forEach(path => { 129 const q = queues.get(path) 130 if (!q) 131 queues.set(path, [fn]) 132 else 133 q.push(fn) 134 }) 135 dirs.forEach(dir => { 136 const q = queues.get(dir) 137 if (!q) 138 queues.set(dir, [new Set([fn])]) 139 else if (q[q.length-1] instanceof Set) 140 q[q.length-1].add(fn) 141 else 142 q.push(new Set([fn])) 143 }) 144 145 return run(fn) 146 } 147 148 return { check, reserve } 149} 150