15815213711
2024-08-26 67b8b6731811983447e053d4396b3708c14dfe3c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
var fs = require('fs')
 
/**
 * Provides symlink loop detection to directory traversal algorithm.
 */
module.exports = {
    detectLoop: function (entry, symlinkCache) {
        if (entry && entry.isSymlink) {
            var realPath = fs.realpathSync(entry.absolutePath)
            if (symlinkCache[realPath]) {
                return true
            }
        }
        return false
    },
 
    initSymlinkCache: function() {
        return {
            dir1: {},
            dir2: {}
        }
    },
 
    updateSymlinkCache: function(symlinkCache, rootEntry1, rootEntry2, loopDetected1, loopDetected2) {
        var symlinkCachePath1, symlinkCachePath2
        if (rootEntry1 && !loopDetected1) {
            symlinkCachePath1 = rootEntry1.isSymlink ? fs.realpathSync(rootEntry1.absolutePath) : rootEntry1.absolutePath
            symlinkCache.dir1[symlinkCachePath1] = true
        }
        if (rootEntry2 && !loopDetected2) {
            symlinkCachePath2 = rootEntry2.isSymlink ? fs.realpathSync(rootEntry2.absolutePath) : rootEntry2.absolutePath
            symlinkCache.dir2[symlinkCachePath2] = true
        }
    },
 
    cloneSymlinkCache: function (symlinkCache) {
        return {
            dir1: shallowClone(symlinkCache.dir1),
            dir2: shallowClone(symlinkCache.dir2)
        }
    },
}
 
function shallowClone(obj) {
    var cloned = {}
    Object.keys(obj).forEach(function (key) {
        cloned[key] = obj[key]
    })
    return cloned
}