Skip to content

Commit

Permalink
opendir dosent play well with symlinks on rehl8
Browse files Browse the repository at this point in the history
  • Loading branch information
MoLow committed Mar 3, 2024
1 parent ec113cf commit 30a8f10
Showing 1 changed file with 11 additions and 26 deletions.
37 changes: 11 additions & 26 deletions lib/internal/fs/glob.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
'use strict';
const { lstatSync, readdirSync } = require('fs');
const { lstat, opendir } = require('fs/promises');
const { lstat, readdir } = require('fs/promises');
const { join, resolve } = require('path');

const {
createDeferredPromise,
kEmptyObject,
} = require('internal/util');
const {
Expand All @@ -23,7 +22,6 @@ const {
ArrayPrototypePush,
ArrayPrototypeSome,
PromisePrototypeThen,
SafeArrayIterator,
SafeMap,
SafeSet,
StringPrototypeEndsWith,
Expand All @@ -42,7 +40,6 @@ class Cache {
#cache = new SafeMap();
#statsCache = new SafeMap();
#readdirCache = new SafeMap();
#opendirCache = new SafeMap();

stat(path) {
const cached = this.#statsCache.get(path);
Expand Down Expand Up @@ -70,27 +67,14 @@ class Cache {
addToStatCache(path, val) {
this.#statsCache.set(path, val);
}
async* opendir(path) {
const cached = this.#opendirCache.get(path);
async readdir(path) {
const cached = this.#readdirCache.get(path);
if (cached) {
yield* await cached;
return;
}
const { promise, resolve } = createDeferredPromise();
this.#opendirCache.set(path, promise);
let val;
try {
val = await opendir(path);
} catch {
resolve(new SafeArrayIterator([]));
return;
}
const cache = [];
for await (const item of val) {
yield item;
ArrayPrototypePush(cache, item);
return cached;
}
resolve(cache);
const promise = PromisePrototypeThen(readdir(path, { __proto__: null, withFileTypes: true }), null, () => null);
this.#readdirCache.set(path, promise);
return promise;
}
readdirSync(path) {
const cached = this.#readdirCache.get(path);
Expand Down Expand Up @@ -501,15 +485,16 @@ class Glob {
const stat = await this.#cache.stat(join(fullpath, firstPattern));
if (stat) {
stat.name = firstPattern;
children = new SafeArrayIterator([stat]);
children = [stat];
} else {
return;
}
} else {
children = this.#cache.opendir(fullpath);
children = await this.#cache.readdir(fullpath);
}

for await (const entry of children) {
for (let i = 0; i < children.length; i++) {
const entry = children[i];
const entryPath = join(path, entry.name);
this.#cache.addToStatCache(join(fullpath, entry.name), entry);

Expand Down

0 comments on commit 30a8f10

Please sign in to comment.