11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst fs = require('fs');
61cb0ef41Sopenharmony_ciconst fsPromises = fs.promises;
71cb0ef41Sopenharmony_ciconst pathModule = require('path');
81cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst testDir = tmpdir.path;
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciconst fileStructure = [
131cb0ef41Sopenharmony_ci  [ 'a', [ 'foo', 'bar' ] ],
141cb0ef41Sopenharmony_ci  [ 'b', [ 'foo', 'bar' ] ],
151cb0ef41Sopenharmony_ci  [ 'c', [ 'foo', 'bar' ] ],
161cb0ef41Sopenharmony_ci  [ 'd', [ 'foo', 'bar' ] ],
171cb0ef41Sopenharmony_ci  [ 'e', [ 'foo', 'bar' ] ],
181cb0ef41Sopenharmony_ci  [ 'f', [ 'foo', 'bar' ] ],
191cb0ef41Sopenharmony_ci  [ 'g', [ 'foo', 'bar' ] ],
201cb0ef41Sopenharmony_ci  [ 'h', [ 'foo', 'bar' ] ],
211cb0ef41Sopenharmony_ci  [ 'i', [ 'foo', 'bar' ] ],
221cb0ef41Sopenharmony_ci  [ 'j', [ 'foo', 'bar' ] ],
231cb0ef41Sopenharmony_ci  [ 'k', [ 'foo', 'bar' ] ],
241cb0ef41Sopenharmony_ci  [ 'l', [ 'foo', 'bar' ] ],
251cb0ef41Sopenharmony_ci  [ 'm', [ 'foo', 'bar' ] ],
261cb0ef41Sopenharmony_ci  [ 'n', [ 'foo', 'bar' ] ],
271cb0ef41Sopenharmony_ci  [ 'o', [ 'foo', 'bar' ] ],
281cb0ef41Sopenharmony_ci  [ 'p', [ 'foo', 'bar' ] ],
291cb0ef41Sopenharmony_ci  [ 'q', [ 'foo', 'bar' ] ],
301cb0ef41Sopenharmony_ci  [ 'r', [ 'foo', 'bar' ] ],
311cb0ef41Sopenharmony_ci  [ 's', [ 'foo', 'bar' ] ],
321cb0ef41Sopenharmony_ci  [ 't', [ 'foo', 'bar' ] ],
331cb0ef41Sopenharmony_ci  [ 'u', [ 'foo', 'bar' ] ],
341cb0ef41Sopenharmony_ci  [ 'v', [ 'foo', 'bar' ] ],
351cb0ef41Sopenharmony_ci  [ 'w', [ 'foo', 'bar' ] ],
361cb0ef41Sopenharmony_ci  [ 'x', [ 'foo', 'bar' ] ],
371cb0ef41Sopenharmony_ci  [ 'y', [ 'foo', 'bar' ] ],
381cb0ef41Sopenharmony_ci  [ 'z', [ 'foo', 'bar' ] ],
391cb0ef41Sopenharmony_ci  [ 'aa', [ 'foo', 'bar' ] ],
401cb0ef41Sopenharmony_ci  [ 'bb', [ 'foo', 'bar' ] ],
411cb0ef41Sopenharmony_ci  [ 'cc', [ 'foo', 'bar' ] ],
421cb0ef41Sopenharmony_ci  [ 'dd', [ 'foo', 'bar' ] ],
431cb0ef41Sopenharmony_ci  [ 'ee', [ 'foo', 'bar' ] ],
441cb0ef41Sopenharmony_ci  [ 'ff', [ 'foo', 'bar' ] ],
451cb0ef41Sopenharmony_ci  [ 'gg', [ 'foo', 'bar' ] ],
461cb0ef41Sopenharmony_ci  [ 'hh', [ 'foo', 'bar' ] ],
471cb0ef41Sopenharmony_ci  [ 'ii', [ 'foo', 'bar' ] ],
481cb0ef41Sopenharmony_ci  [ 'jj', [ 'foo', 'bar' ] ],
491cb0ef41Sopenharmony_ci  [ 'kk', [ 'foo', 'bar' ] ],
501cb0ef41Sopenharmony_ci  [ 'll', [ 'foo', 'bar' ] ],
511cb0ef41Sopenharmony_ci  [ 'mm', [ 'foo', 'bar' ] ],
521cb0ef41Sopenharmony_ci  [ 'nn', [ 'foo', 'bar' ] ],
531cb0ef41Sopenharmony_ci  [ 'oo', [ 'foo', 'bar' ] ],
541cb0ef41Sopenharmony_ci  [ 'pp', [ 'foo', 'bar' ] ],
551cb0ef41Sopenharmony_ci  [ 'qq', [ 'foo', 'bar' ] ],
561cb0ef41Sopenharmony_ci  [ 'rr', [ 'foo', 'bar' ] ],
571cb0ef41Sopenharmony_ci  [ 'ss', [ 'foo', 'bar' ] ],
581cb0ef41Sopenharmony_ci  [ 'tt', [ 'foo', 'bar' ] ],
591cb0ef41Sopenharmony_ci  [ 'uu', [ 'foo', 'bar' ] ],
601cb0ef41Sopenharmony_ci  [ 'vv', [ 'foo', 'bar' ] ],
611cb0ef41Sopenharmony_ci  [ 'ww', [ 'foo', 'bar' ] ],
621cb0ef41Sopenharmony_ci  [ 'xx', [ 'foo', 'bar' ] ],
631cb0ef41Sopenharmony_ci  [ 'yy', [ 'foo', 'bar' ] ],
641cb0ef41Sopenharmony_ci  [ 'zz', [ 'foo', 'bar' ] ],
651cb0ef41Sopenharmony_ci  [ 'abc', [ ['def', [ 'foo', 'bar' ] ], ['ghi', [ 'foo', 'bar' ] ] ] ],
661cb0ef41Sopenharmony_ci];
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_cifunction createFiles(path, fileStructure) {
691cb0ef41Sopenharmony_ci  for (const fileOrDir of fileStructure) {
701cb0ef41Sopenharmony_ci    if (typeof fileOrDir === 'string') {
711cb0ef41Sopenharmony_ci      fs.writeFileSync(pathModule.join(path, fileOrDir), '');
721cb0ef41Sopenharmony_ci    } else {
731cb0ef41Sopenharmony_ci      const dirPath = pathModule.join(path, fileOrDir[0]);
741cb0ef41Sopenharmony_ci      fs.mkdirSync(dirPath);
751cb0ef41Sopenharmony_ci      createFiles(dirPath, fileOrDir[1]);
761cb0ef41Sopenharmony_ci    }
771cb0ef41Sopenharmony_ci  }
781cb0ef41Sopenharmony_ci}
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci// Make sure tmp directory is clean
811cb0ef41Sopenharmony_citmpdir.refresh();
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_cicreateFiles(testDir, fileStructure);
841cb0ef41Sopenharmony_ciconst symlinksRootPath = pathModule.join(testDir, 'symlinks');
851cb0ef41Sopenharmony_ciconst symlinkTargetFile = pathModule.join(symlinksRootPath, 'symlink-target-file');
861cb0ef41Sopenharmony_ciconst symlinkTargetDir = pathModule.join(symlinksRootPath, 'symlink-target-dir');
871cb0ef41Sopenharmony_cifs.mkdirSync(symlinksRootPath);
881cb0ef41Sopenharmony_cifs.writeFileSync(symlinkTargetFile, '');
891cb0ef41Sopenharmony_cifs.mkdirSync(symlinkTargetDir);
901cb0ef41Sopenharmony_cifs.symlinkSync(symlinkTargetFile, pathModule.join(symlinksRootPath, 'symlink-src-file'));
911cb0ef41Sopenharmony_cifs.symlinkSync(symlinkTargetDir, pathModule.join(symlinksRootPath, 'symlink-src-dir'));
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ciconst expected = [
941cb0ef41Sopenharmony_ci  'a', 'a/bar', 'a/foo', 'aa', 'aa/bar', 'aa/foo',
951cb0ef41Sopenharmony_ci  'abc', 'abc/def', 'abc/def/bar', 'abc/def/foo', 'abc/ghi', 'abc/ghi/bar', 'abc/ghi/foo',
961cb0ef41Sopenharmony_ci  'b', 'b/bar', 'b/foo', 'bb', 'bb/bar', 'bb/foo',
971cb0ef41Sopenharmony_ci  'c', 'c/bar', 'c/foo', 'cc', 'cc/bar', 'cc/foo',
981cb0ef41Sopenharmony_ci  'd', 'd/bar', 'd/foo', 'dd', 'dd/bar', 'dd/foo',
991cb0ef41Sopenharmony_ci  'e', 'e/bar', 'e/foo', 'ee', 'ee/bar', 'ee/foo',
1001cb0ef41Sopenharmony_ci  'f', 'f/bar', 'f/foo', 'ff', 'ff/bar', 'ff/foo',
1011cb0ef41Sopenharmony_ci  'g', 'g/bar', 'g/foo', 'gg', 'gg/bar', 'gg/foo',
1021cb0ef41Sopenharmony_ci  'h', 'h/bar', 'h/foo', 'hh', 'hh/bar', 'hh/foo',
1031cb0ef41Sopenharmony_ci  'i', 'i/bar', 'i/foo', 'ii', 'ii/bar', 'ii/foo',
1041cb0ef41Sopenharmony_ci  'j', 'j/bar', 'j/foo', 'jj', 'jj/bar', 'jj/foo',
1051cb0ef41Sopenharmony_ci  'k', 'k/bar', 'k/foo', 'kk', 'kk/bar', 'kk/foo',
1061cb0ef41Sopenharmony_ci  'l', 'l/bar', 'l/foo', 'll', 'll/bar', 'll/foo',
1071cb0ef41Sopenharmony_ci  'm', 'm/bar', 'm/foo', 'mm', 'mm/bar', 'mm/foo',
1081cb0ef41Sopenharmony_ci  'n', 'n/bar', 'n/foo', 'nn', 'nn/bar', 'nn/foo',
1091cb0ef41Sopenharmony_ci  'o', 'o/bar', 'o/foo', 'oo', 'oo/bar', 'oo/foo',
1101cb0ef41Sopenharmony_ci  'p', 'p/bar', 'p/foo', 'pp', 'pp/bar', 'pp/foo',
1111cb0ef41Sopenharmony_ci  'q', 'q/bar', 'q/foo', 'qq', 'qq/bar', 'qq/foo',
1121cb0ef41Sopenharmony_ci  'r', 'r/bar', 'r/foo', 'rr', 'rr/bar', 'rr/foo',
1131cb0ef41Sopenharmony_ci  's', 's/bar', 's/foo', 'ss', 'ss/bar', 'ss/foo',
1141cb0ef41Sopenharmony_ci  'symlinks', 'symlinks/symlink-src-dir', 'symlinks/symlink-src-file',
1151cb0ef41Sopenharmony_ci  'symlinks/symlink-target-dir', 'symlinks/symlink-target-file',
1161cb0ef41Sopenharmony_ci  't', 't/bar', 't/foo', 'tt', 'tt/bar', 'tt/foo',
1171cb0ef41Sopenharmony_ci  'u', 'u/bar', 'u/foo', 'uu', 'uu/bar', 'uu/foo',
1181cb0ef41Sopenharmony_ci  'v', 'v/bar', 'v/foo', 'vv', 'vv/bar', 'vv/foo',
1191cb0ef41Sopenharmony_ci  'w', 'w/bar', 'w/foo', 'ww', 'ww/bar', 'ww/foo',
1201cb0ef41Sopenharmony_ci  'x', 'x/bar', 'x/foo', 'xx', 'xx/bar', 'xx/foo',
1211cb0ef41Sopenharmony_ci  'y', 'y/bar', 'y/foo', 'yy', 'yy/bar', 'yy/foo',
1221cb0ef41Sopenharmony_ci  'z', 'z/bar', 'z/foo', 'zz', 'zz/bar', 'zz/foo',
1231cb0ef41Sopenharmony_ci];
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ci// Normalize paths once for non POSIX platforms
1261cb0ef41Sopenharmony_cifor (let i = 0; i < expected.length; i++) {
1271cb0ef41Sopenharmony_ci  expected[i] = pathModule.normalize(expected[i]);
1281cb0ef41Sopenharmony_ci}
1291cb0ef41Sopenharmony_ci
1301cb0ef41Sopenharmony_cifunction getDirentPath(dirent) {
1311cb0ef41Sopenharmony_ci  return pathModule.relative(testDir, dirent.path);
1321cb0ef41Sopenharmony_ci}
1331cb0ef41Sopenharmony_ci
1341cb0ef41Sopenharmony_cifunction assertDirents(dirents) {
1351cb0ef41Sopenharmony_ci  dirents.sort((a, b) => (getDirentPath(a) < getDirentPath(b) ? -1 : 1));
1361cb0ef41Sopenharmony_ci  for (const [i, dirent] of dirents.entries()) {
1371cb0ef41Sopenharmony_ci    assert(dirent instanceof fs.Dirent);
1381cb0ef41Sopenharmony_ci    assert.strictEqual(getDirentPath(dirent), expected[i]);
1391cb0ef41Sopenharmony_ci  }
1401cb0ef41Sopenharmony_ci}
1411cb0ef41Sopenharmony_ci
1421cb0ef41Sopenharmony_cifunction processDirSync(dir) {
1431cb0ef41Sopenharmony_ci  const dirents = [];
1441cb0ef41Sopenharmony_ci  let dirent = dir.readSync();
1451cb0ef41Sopenharmony_ci  while (dirent !== null) {
1461cb0ef41Sopenharmony_ci    dirents.push(dirent);
1471cb0ef41Sopenharmony_ci    dirent = dir.readSync();
1481cb0ef41Sopenharmony_ci  }
1491cb0ef41Sopenharmony_ci  assertDirents(dirents);
1501cb0ef41Sopenharmony_ci}
1511cb0ef41Sopenharmony_ci
1521cb0ef41Sopenharmony_ci// Opendir read results sync
1531cb0ef41Sopenharmony_ci
1541cb0ef41Sopenharmony_ci{
1551cb0ef41Sopenharmony_ci  const dir = fs.opendirSync(testDir, { recursive: true });
1561cb0ef41Sopenharmony_ci  processDirSync(dir);
1571cb0ef41Sopenharmony_ci  dir.closeSync();
1581cb0ef41Sopenharmony_ci}
1591cb0ef41Sopenharmony_ci
1601cb0ef41Sopenharmony_ci{
1611cb0ef41Sopenharmony_ci  fs.opendir(testDir, { recursive: true }, common.mustSucceed((dir) => {
1621cb0ef41Sopenharmony_ci    processDirSync(dir);
1631cb0ef41Sopenharmony_ci    dir.close(common.mustSucceed());
1641cb0ef41Sopenharmony_ci  }));
1651cb0ef41Sopenharmony_ci}
1661cb0ef41Sopenharmony_ci
1671cb0ef41Sopenharmony_ci// Opendir read result using callback
1681cb0ef41Sopenharmony_ci
1691cb0ef41Sopenharmony_cifunction processDirCb(dir, cb) {
1701cb0ef41Sopenharmony_ci  const acc = [];
1711cb0ef41Sopenharmony_ci
1721cb0ef41Sopenharmony_ci  function _process(dir, acc, cb) {
1731cb0ef41Sopenharmony_ci    dir.read((err, dirent) => {
1741cb0ef41Sopenharmony_ci      if (err) {
1751cb0ef41Sopenharmony_ci        return cb(err);
1761cb0ef41Sopenharmony_ci      }
1771cb0ef41Sopenharmony_ci
1781cb0ef41Sopenharmony_ci      if (dirent !== null) {
1791cb0ef41Sopenharmony_ci        acc.push(dirent);
1801cb0ef41Sopenharmony_ci        _process(dir, acc, cb);
1811cb0ef41Sopenharmony_ci      } else {
1821cb0ef41Sopenharmony_ci        cb(null, acc);
1831cb0ef41Sopenharmony_ci      }
1841cb0ef41Sopenharmony_ci    });
1851cb0ef41Sopenharmony_ci  }
1861cb0ef41Sopenharmony_ci
1871cb0ef41Sopenharmony_ci  _process(dir, acc, cb);
1881cb0ef41Sopenharmony_ci}
1891cb0ef41Sopenharmony_ci
1901cb0ef41Sopenharmony_ci{
1911cb0ef41Sopenharmony_ci  const dir = fs.opendirSync(testDir, { recursive: true });
1921cb0ef41Sopenharmony_ci  processDirCb(dir, common.mustSucceed((dirents) => {
1931cb0ef41Sopenharmony_ci    assertDirents(dirents);
1941cb0ef41Sopenharmony_ci    dir.close(common.mustSucceed());
1951cb0ef41Sopenharmony_ci  }));
1961cb0ef41Sopenharmony_ci}
1971cb0ef41Sopenharmony_ci
1981cb0ef41Sopenharmony_ci{
1991cb0ef41Sopenharmony_ci  fs.opendir(testDir, { recursive: true }, common.mustSucceed((dir) => {
2001cb0ef41Sopenharmony_ci    processDirCb(dir, common.mustSucceed((dirents) => {
2011cb0ef41Sopenharmony_ci      assertDirents(dirents);
2021cb0ef41Sopenharmony_ci      dir.close(common.mustSucceed());
2031cb0ef41Sopenharmony_ci    }));
2041cb0ef41Sopenharmony_ci  }));
2051cb0ef41Sopenharmony_ci}
2061cb0ef41Sopenharmony_ci
2071cb0ef41Sopenharmony_ci// Opendir read result using AsyncIterator
2081cb0ef41Sopenharmony_ci
2091cb0ef41Sopenharmony_ci{
2101cb0ef41Sopenharmony_ci  async function test() {
2111cb0ef41Sopenharmony_ci    const dir = await fsPromises.opendir(testDir, { recursive: true });
2121cb0ef41Sopenharmony_ci    const dirents = [];
2131cb0ef41Sopenharmony_ci    for await (const dirent of dir) {
2141cb0ef41Sopenharmony_ci      dirents.push(dirent);
2151cb0ef41Sopenharmony_ci    }
2161cb0ef41Sopenharmony_ci    assertDirents(dirents);
2171cb0ef41Sopenharmony_ci  }
2181cb0ef41Sopenharmony_ci
2191cb0ef41Sopenharmony_ci  test().then(common.mustCall());
2201cb0ef41Sopenharmony_ci}
221