source: trip-planner-front/node_modules/tar/lib/path-reservations.js@ e29cc2e

Last change on this file since e29cc2e was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 4.2 KB
Line 
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 normalize = require('./normalize-unicode.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 = 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 // don't need normPath, because we skip this entirely for windows
120 return normalize(stripSlashes(join(p))).toLowerCase()
121 })
122
123 const dirs = new Set(
124 paths.map(path => getDirs(path)).reduce((a, b) => a.concat(b))
125 )
126 reservations.set(fn, {dirs, paths})
127 paths.forEach(path => {
128 const q = queues.get(path)
129 if (!q)
130 queues.set(path, [fn])
131 else
132 q.push(fn)
133 })
134 dirs.forEach(dir => {
135 const q = queues.get(dir)
136 if (!q)
137 queues.set(dir, [new Set([fn])])
138 else if (q[q.length - 1] instanceof Set)
139 q[q.length - 1].add(fn)
140 else
141 q.push(new Set([fn]))
142 })
143
144 return run(fn)
145 }
146
147 return { check, reserve }
148}
Note: See TracBrowser for help on using the repository browser.