@@ -28,7 +28,6 @@ const {
2828 ArrayFromAsync,
2929 ArrayPrototypePush,
3030 BigIntPrototypeToString,
31- Boolean,
3231 FunctionPrototypeCall,
3332 MathMax,
3433 Number,
@@ -57,9 +56,6 @@ const {
5756 F_OK ,
5857 O_WRONLY ,
5958 O_SYMLINK ,
60- UV_DIRENT_DIR ,
61- UV_DIRENT_LINK ,
62- UV_DIRENT_UNKNOWN ,
6359} = constants ;
6460
6561const pathModule = require ( 'path' ) ;
@@ -103,10 +99,11 @@ const {
10399 kReadFileBufferLength,
104100 kMaxUserId,
105101 } ,
102+ collectRecursiveReaddirResult,
106103 copyObject,
107104 Dirent,
108- getDirent,
109105 getDirents,
106+ getRecursiveDirents,
110107 getOptions,
111108 getValidatedFd,
112109 getValidatedPath,
@@ -1743,57 +1740,40 @@ function mkdirSync(path, options) {
17431740}
17441741
17451742/**
1746- * Appends one directory's entries to `context.results` and the subdirectories
1747- * still to visit to `context.dirs` (with the prefix their entries get in
1748- * string results in `context.prefixes`). `result` is a `binding.readdir()`
1749- * result with file types, so only symbolic links and entries of unknown type
1750- * need a stat() to find out whether they lead to a directory.
1751- * @param {string } dir
1752- * @param {string } prefix
1753- * @param {[string[], number[]] } result
1754- * @param {{ withFileTypes: boolean, results: (string | Dirent)[], dirs: string[], prefixes: string[] } } context
1755- */
1756- function collectRecursiveReaddirResult ( dir , prefix , { 0 : names , 1 : types } , context ) {
1757- const { length } = names ;
1758- for ( let i = 0 ; i < length ; i ++ ) {
1759- const name = names [ i ] ;
1760- const relative = prefix === '' ? name : `${ prefix } ${ pathModule . sep } ${ name } ` ;
1761- let isDirectory ;
1762- if ( context . withFileTypes ) {
1763- const dirent = getDirent ( dir , name , types [ i ] ) ;
1764- ArrayPrototypePush ( context . results , dirent ) ;
1765- // Follow symbolic links to directories, see https://github.com/nodejs/node/issues/52663
1766- isDirectory = dirent . isDirectory ( ) ||
1767- ( dirent . isSymbolicLink ( ) && binding . internalModuleStat ( pathModule . join ( dir , name ) ) === 1 ) ;
1768- } else {
1769- ArrayPrototypePush ( context . results , relative ) ;
1770- const type = types [ i ] ;
1771- isDirectory = type === UV_DIRENT_DIR ||
1772- ( ( type === UV_DIRENT_LINK || type === UV_DIRENT_UNKNOWN ) &&
1773- binding . internalModuleStat ( pathModule . join ( dir , name ) ) === 1 ) ;
1774- }
1775- if ( isDirectory ) {
1776- ArrayPrototypePush ( context . dirs , pathModule . join ( dir , name ) ) ;
1777- ArrayPrototypePush ( context . prefixes , relative ) ;
1778- }
1779- }
1780- }
1781-
1782- /*
1783- * An recursive algorithm for reading the entire contents of the `basePath` directory.
1784- * This function does not validate `basePath` as a directory. It is passed directly to
1785- * `binding.readdir`.
1786- * @param {string } basePath
1743+ * Reads the entire contents of the `basePath` directory. This function does
1744+ * not validate `basePath` as a directory. It is passed directly to
1745+ * `binding.readdirRecursive`, or to `binding.readdir` for each directory when
1746+ * the permission model is enabled, since that checks every directory on the
1747+ * main thread where the native walk can not.
1748+ * @param {string | Buffer } basePath
17871749 * @param {{ encoding: string, withFileTypes: boolean } } options
17881750 * @param {(
17891751 * err?: Error,
17901752 * files?: string[] | Buffer[] | Dirent[]
17911753 * ) => any} callback
17921754 * @returns {void }
1793- */
1755+ */
17941756function readdirRecursive ( basePath , options , callback ) {
1757+ if ( permission . isEnabled ( ) ) {
1758+ readdirRecursiveWithPermissionModel ( basePath , options , callback ) ;
1759+ return ;
1760+ }
1761+ const withFileTypes = ! ! options . withFileTypes ;
1762+ const req = new FSReqCallback ( ) ;
1763+ req . oncomplete = ( err , result ) => {
1764+ if ( err ) {
1765+ callback ( err ) ;
1766+ return ;
1767+ }
1768+ callback ( null , withFileTypes ? getRecursiveDirents ( basePath , result ) : result ) ;
1769+ } ;
1770+ binding . readdirRecursive ( basePath , options . encoding , withFileTypes , req ) ;
1771+ }
1772+
1773+ function readdirRecursiveWithPermissionModel ( basePath , options , callback ) {
1774+ const { encoding } = options ;
17951775 const context = {
1796- withFileTypes : Boolean ( options . withFileTypes ) ,
1776+ withFileTypes : ! ! options . withFileTypes ,
17971777 results : [ ] ,
17981778 dirs : [ basePath ] ,
17991779 prefixes : [ '' ] ,
@@ -1815,11 +1795,6 @@ function readdirRecursive(basePath, options, callback) {
18151795 return ;
18161796 }
18171797
1818- if ( result === undefined ) {
1819- callback ( null , context . results ) ;
1820- return ;
1821- }
1822-
18231798 try {
18241799 collectRecursiveReaddirResult ( path , prefix , result , context ) ;
18251800 } catch ( err ) {
@@ -1834,37 +1809,37 @@ function readdirRecursive(basePath, options, callback) {
18341809 }
18351810 } ;
18361811
1837- binding . readdir ( path , options . encoding , true , req ) ;
1812+ binding . readdir ( path , encoding , true , req ) ;
18381813 }
18391814
18401815 read ( context . dirs [ i ] , context . prefixes [ i ++ ] ) ;
18411816}
18421817
18431818/**
1844- * An iterative algorithm for reading the entire contents of the `basePath` directory.
1845- * This function does not validate `basePath` as a directory. It is passed directly to
1846- * `binding.readdir`.
1847- * @param {string } basePath
1819+ * Synchronously reads the entire contents of the `basePath` directory, see
1820+ * `readdirRecursive`.
1821+ * @param {string | Buffer } basePath
18481822 * @param {{ encoding: string, withFileTypes: boolean } } options
1849- * @returns {string[] | Dirent[] }
1823+ * @returns {string[] | Buffer[] | Dirent[] }
18501824 */
18511825function readdirSyncRecursive ( basePath , options ) {
1852- const context = {
1853- withFileTypes : Boolean ( options . withFileTypes ) ,
1854- results : [ ] ,
1855- dirs : [ basePath ] ,
1856- prefixes : [ '' ] ,
1857- } ;
1858-
1859- for ( let i = 0 ; i < context . dirs . length ; i ++ ) {
1860- const dir = context . dirs [ i ] ;
1861- const result = binding . readdir ( dir , options . encoding , true ) ;
1862- if ( result !== undefined ) {
1826+ const withFileTypes = ! ! options . withFileTypes ;
1827+ if ( permission . isEnabled ( ) ) {
1828+ const context = {
1829+ withFileTypes ,
1830+ results : [ ] ,
1831+ dirs : [ basePath ] ,
1832+ prefixes : [ '' ] ,
1833+ } ;
1834+ for ( let i = 0 ; i < context . dirs . length ; i ++ ) {
1835+ const dir = context . dirs [ i ] ;
1836+ const result = binding . readdir ( dir , options . encoding , true ) ;
18631837 collectRecursiveReaddirResult ( dir , context . prefixes [ i ] , result , context ) ;
18641838 }
1839+ return context . results ;
18651840 }
1866-
1867- return context . results ;
1841+ const result = binding . readdirRecursive ( basePath , options . encoding , withFileTypes ) ;
1842+ return result !== undefined && withFileTypes ? getRecursiveDirents ( basePath , result ) : result ;
18681843}
18691844
18701845/**
@@ -1898,9 +1873,6 @@ function readdir(path, options, callback) {
18981873 }
18991874
19001875 if ( options . recursive ) {
1901- // Make shallow copy to prevent mutating options from affecting results
1902- options = copyObject ( options ) ;
1903-
19041876 readdirRecursive ( path , options , callback ) ;
19051877 return ;
19061878 }
0 commit comments