11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../common'); 31cb0ef41Sopenharmony_ciconst assert = require('assert'); 41cb0ef41Sopenharmony_ciconst cp = require('child_process'); 51cb0ef41Sopenharmony_ciconst fs = require('fs'); 61cb0ef41Sopenharmony_ciconst path = require('path'); 71cb0ef41Sopenharmony_ciconst util = require('util'); 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ciconst tests = Object.create(null); 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_cilet gid = 1; 121cb0ef41Sopenharmony_cilet uid = 1; 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ciif (!common.isWindows) { 151cb0ef41Sopenharmony_ci gid = process.getgid(); 161cb0ef41Sopenharmony_ci uid = process.getuid(); 171cb0ef41Sopenharmony_ci} 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_cifunction wrapper(func, args) { 201cb0ef41Sopenharmony_ci return `(${func.toString()})(${JSON.stringify(args)})`; 211cb0ef41Sopenharmony_ci} 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_cifunction access() { 241cb0ef41Sopenharmony_ci const fs = require('fs'); 251cb0ef41Sopenharmony_ci fs.writeFileSync('fs0.txt', '123', 'utf8'); 261cb0ef41Sopenharmony_ci fs.access('fs0.txt', () => { 271cb0ef41Sopenharmony_ci fs.unlinkSync('fs0.txt'); 281cb0ef41Sopenharmony_ci }); 291cb0ef41Sopenharmony_ci} 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_cifunction chmod() { 321cb0ef41Sopenharmony_ci const fs = require('fs'); 331cb0ef41Sopenharmony_ci fs.writeFileSync('fs1.txt', '123', 'utf8'); 341cb0ef41Sopenharmony_ci fs.chmod('fs1.txt', 100, () => { 351cb0ef41Sopenharmony_ci fs.unlinkSync('fs1.txt'); 361cb0ef41Sopenharmony_ci }); 371cb0ef41Sopenharmony_ci} 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_cifunction chown({ uid, gid }) { 401cb0ef41Sopenharmony_ci const fs = require('fs'); 411cb0ef41Sopenharmony_ci fs.writeFileSync('fs2.txt', '123', 'utf8'); 421cb0ef41Sopenharmony_ci fs.chown('fs2.txt', uid, gid, () => { 431cb0ef41Sopenharmony_ci fs.unlinkSync('fs2.txt'); 441cb0ef41Sopenharmony_ci }); 451cb0ef41Sopenharmony_ci} 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_cifunction close() { 481cb0ef41Sopenharmony_ci const fs = require('fs'); 491cb0ef41Sopenharmony_ci fs.writeFile('fs3.txt', '123', 'utf8', () => { 501cb0ef41Sopenharmony_ci fs.unlinkSync('fs3.txt'); 511cb0ef41Sopenharmony_ci }); 521cb0ef41Sopenharmony_ci} 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_cifunction copyfile() { 551cb0ef41Sopenharmony_ci const fs = require('fs'); 561cb0ef41Sopenharmony_ci fs.writeFileSync('fs4.txt', '123', 'utf8'); 571cb0ef41Sopenharmony_ci fs.copyFile('fs4.txt', 'a.txt', () => { 581cb0ef41Sopenharmony_ci fs.unlinkSync('fs4.txt'); 591cb0ef41Sopenharmony_ci fs.unlinkSync('a.txt'); 601cb0ef41Sopenharmony_ci }); 611cb0ef41Sopenharmony_ci} 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_cifunction fchmod() { 641cb0ef41Sopenharmony_ci const fs = require('fs'); 651cb0ef41Sopenharmony_ci fs.writeFileSync('fs5.txt', '123', 'utf8'); 661cb0ef41Sopenharmony_ci const fd = fs.openSync('fs5.txt', 'r+'); 671cb0ef41Sopenharmony_ci fs.fchmod(fd, 100, () => { 681cb0ef41Sopenharmony_ci fs.unlinkSync('fs5.txt'); 691cb0ef41Sopenharmony_ci }); 701cb0ef41Sopenharmony_ci} 711cb0ef41Sopenharmony_ci 721cb0ef41Sopenharmony_cifunction fchown({ uid, gid }) { 731cb0ef41Sopenharmony_ci const fs = require('fs'); 741cb0ef41Sopenharmony_ci fs.writeFileSync('fs6.txt', '123', 'utf8'); 751cb0ef41Sopenharmony_ci const fd = fs.openSync('fs6.txt', 'r+'); 761cb0ef41Sopenharmony_ci fs.fchown(fd, uid, gid, () => { 771cb0ef41Sopenharmony_ci fs.unlinkSync('fs6.txt'); 781cb0ef41Sopenharmony_ci }); 791cb0ef41Sopenharmony_ci} 801cb0ef41Sopenharmony_ci 811cb0ef41Sopenharmony_cifunction fdatasync() { 821cb0ef41Sopenharmony_ci const fs = require('fs'); 831cb0ef41Sopenharmony_ci fs.writeFileSync('fs7.txt', '123', 'utf8'); 841cb0ef41Sopenharmony_ci const fd = fs.openSync('fs7.txt', 'r+'); 851cb0ef41Sopenharmony_ci fs.fdatasync(fd, () => { 861cb0ef41Sopenharmony_ci fs.unlinkSync('fs7.txt'); 871cb0ef41Sopenharmony_ci }); 881cb0ef41Sopenharmony_ci} 891cb0ef41Sopenharmony_ci 901cb0ef41Sopenharmony_cifunction fstat() { 911cb0ef41Sopenharmony_ci const fs = require('fs'); 921cb0ef41Sopenharmony_ci fs.writeFileSync('fs8.txt', '123', 'utf8'); 931cb0ef41Sopenharmony_ci fs.readFile('fs8.txt', () => { 941cb0ef41Sopenharmony_ci fs.unlinkSync('fs8.txt'); 951cb0ef41Sopenharmony_ci }); 961cb0ef41Sopenharmony_ci} 971cb0ef41Sopenharmony_ci 981cb0ef41Sopenharmony_cifunction fsync() { 991cb0ef41Sopenharmony_ci const fs = require('fs'); 1001cb0ef41Sopenharmony_ci fs.writeFileSync('fs9.txt', '123', 'utf8'); 1011cb0ef41Sopenharmony_ci const fd = fs.openSync('fs9.txt', 'r+'); 1021cb0ef41Sopenharmony_ci fs.fsync(fd, () => { 1031cb0ef41Sopenharmony_ci fs.unlinkSync('fs9.txt'); 1041cb0ef41Sopenharmony_ci }); 1051cb0ef41Sopenharmony_ci} 1061cb0ef41Sopenharmony_ci 1071cb0ef41Sopenharmony_cifunction ftruncate() { 1081cb0ef41Sopenharmony_ci const fs = require('fs'); 1091cb0ef41Sopenharmony_ci fs.writeFileSync('fs10.txt', '123', 'utf8'); 1101cb0ef41Sopenharmony_ci const fd = fs.openSync('fs10.txt', 'r+'); 1111cb0ef41Sopenharmony_ci fs.ftruncate(fd, 1, () => { 1121cb0ef41Sopenharmony_ci fs.unlinkSync('fs10.txt'); 1131cb0ef41Sopenharmony_ci }); 1141cb0ef41Sopenharmony_ci} 1151cb0ef41Sopenharmony_ci 1161cb0ef41Sopenharmony_cifunction futime() { 1171cb0ef41Sopenharmony_ci const fs = require('fs'); 1181cb0ef41Sopenharmony_ci fs.writeFileSync('fs11.txt', '123', 'utf8'); 1191cb0ef41Sopenharmony_ci const fd = fs.openSync('fs11.txt', 'r+'); 1201cb0ef41Sopenharmony_ci fs.futimes(fd, 1, 1, () => { 1211cb0ef41Sopenharmony_ci fs.unlinkSync('fs11.txt'); 1221cb0ef41Sopenharmony_ci }); 1231cb0ef41Sopenharmony_ci} 1241cb0ef41Sopenharmony_ci 1251cb0ef41Sopenharmony_cifunction lutime() { 1261cb0ef41Sopenharmony_ci const fs = require('fs'); 1271cb0ef41Sopenharmony_ci fs.writeFileSync('fs111.txt', '123', 'utf8'); 1281cb0ef41Sopenharmony_ci fs.lutimes('fs111.txt', 1, 1, () => { 1291cb0ef41Sopenharmony_ci fs.unlinkSync('fs111.txt'); 1301cb0ef41Sopenharmony_ci }); 1311cb0ef41Sopenharmony_ci} 1321cb0ef41Sopenharmony_ci 1331cb0ef41Sopenharmony_cifunction lchown({ uid, gid }) { 1341cb0ef41Sopenharmony_ci const fs = require('fs'); 1351cb0ef41Sopenharmony_ci fs.writeFileSync('fs12.txt', '123', 'utf8'); 1361cb0ef41Sopenharmony_ci fs.lchown('fs12.txt', uid, gid, () => { 1371cb0ef41Sopenharmony_ci fs.unlinkSync('fs12.txt'); 1381cb0ef41Sopenharmony_ci }); 1391cb0ef41Sopenharmony_ci} 1401cb0ef41Sopenharmony_ci 1411cb0ef41Sopenharmony_cifunction link() { 1421cb0ef41Sopenharmony_ci const fs = require('fs'); 1431cb0ef41Sopenharmony_ci fs.writeFileSync('fs13.txt', '123', 'utf8'); 1441cb0ef41Sopenharmony_ci fs.link('fs13.txt', 'fs14.txt', () => { 1451cb0ef41Sopenharmony_ci fs.unlinkSync('fs13.txt'); 1461cb0ef41Sopenharmony_ci fs.unlinkSync('fs14.txt'); 1471cb0ef41Sopenharmony_ci }); 1481cb0ef41Sopenharmony_ci} 1491cb0ef41Sopenharmony_ci 1501cb0ef41Sopenharmony_cifunction lstat() { 1511cb0ef41Sopenharmony_ci const fs = require('fs'); 1521cb0ef41Sopenharmony_ci fs.writeFileSync('fs15.txt', '123', 'utf8'); 1531cb0ef41Sopenharmony_ci fs.lstat('fs15.txt', () => { 1541cb0ef41Sopenharmony_ci fs.unlinkSync('fs15.txt'); 1551cb0ef41Sopenharmony_ci }); 1561cb0ef41Sopenharmony_ci} 1571cb0ef41Sopenharmony_ci 1581cb0ef41Sopenharmony_cifunction mkdir() { 1591cb0ef41Sopenharmony_ci const fs = require('fs'); 1601cb0ef41Sopenharmony_ci fs.mkdir('fstemp0', () => { 1611cb0ef41Sopenharmony_ci fs.rmdir('fstemp0', () => {}); 1621cb0ef41Sopenharmony_ci }); 1631cb0ef41Sopenharmony_ci} 1641cb0ef41Sopenharmony_ci 1651cb0ef41Sopenharmony_cifunction mktmp() { 1661cb0ef41Sopenharmony_ci fs.mkdtemp('fstemp1', (err, fp) => { 1671cb0ef41Sopenharmony_ci fs.rmdir(fp, () => {}); 1681cb0ef41Sopenharmony_ci }); 1691cb0ef41Sopenharmony_ci} 1701cb0ef41Sopenharmony_ci 1711cb0ef41Sopenharmony_cifunction open() { 1721cb0ef41Sopenharmony_ci const fs = require('fs'); 1731cb0ef41Sopenharmony_ci fs.writeFile('fs16.txt', '123', 'utf8', () => { 1741cb0ef41Sopenharmony_ci fs.unlinkSync('fs16.txt'); 1751cb0ef41Sopenharmony_ci }); 1761cb0ef41Sopenharmony_ci} 1771cb0ef41Sopenharmony_ci 1781cb0ef41Sopenharmony_cifunction read() { 1791cb0ef41Sopenharmony_ci const fs = require('fs'); 1801cb0ef41Sopenharmony_ci fs.writeFileSync('fs17.txt', '123', 'utf8'); 1811cb0ef41Sopenharmony_ci fs.readFile('fs17.txt', () => { 1821cb0ef41Sopenharmony_ci fs.unlinkSync('fs17.txt'); 1831cb0ef41Sopenharmony_ci }); 1841cb0ef41Sopenharmony_ci} 1851cb0ef41Sopenharmony_ci 1861cb0ef41Sopenharmony_cifunction readdir() { 1871cb0ef41Sopenharmony_ci const fs = require('fs'); 1881cb0ef41Sopenharmony_ci fs.readdir('./', () => {}); 1891cb0ef41Sopenharmony_ci} 1901cb0ef41Sopenharmony_ci 1911cb0ef41Sopenharmony_cifunction opendir() { 1921cb0ef41Sopenharmony_ci const fs = require('fs'); 1931cb0ef41Sopenharmony_ci fs.opendir('./', () => {}); 1941cb0ef41Sopenharmony_ci} 1951cb0ef41Sopenharmony_ci 1961cb0ef41Sopenharmony_cifunction realpath() { 1971cb0ef41Sopenharmony_ci const fs = require('fs'); 1981cb0ef41Sopenharmony_ci fs.writeFileSync('fs18.txt', '123', 'utf8'); 1991cb0ef41Sopenharmony_ci fs.linkSync('fs18.txt', 'fs19.txt'); 2001cb0ef41Sopenharmony_ci fs.realpath.native('fs19.txt', () => { 2011cb0ef41Sopenharmony_ci fs.unlinkSync('fs18.txt'); 2021cb0ef41Sopenharmony_ci fs.unlinkSync('fs19.txt'); 2031cb0ef41Sopenharmony_ci }); 2041cb0ef41Sopenharmony_ci} 2051cb0ef41Sopenharmony_ci 2061cb0ef41Sopenharmony_cifunction rename() { 2071cb0ef41Sopenharmony_ci const fs = require('fs'); 2081cb0ef41Sopenharmony_ci fs.writeFileSync('fs20.txt', '123', 'utf8'); 2091cb0ef41Sopenharmony_ci fs.rename('fs20.txt', 'fs21.txt', () => { 2101cb0ef41Sopenharmony_ci fs.unlinkSync('fs21.txt'); 2111cb0ef41Sopenharmony_ci }); 2121cb0ef41Sopenharmony_ci} 2131cb0ef41Sopenharmony_ci 2141cb0ef41Sopenharmony_cifunction rmdir() { 2151cb0ef41Sopenharmony_ci const fs = require('fs'); 2161cb0ef41Sopenharmony_ci fs.mkdirSync('fstemp2'); 2171cb0ef41Sopenharmony_ci fs.rmdir('fstemp2', () => {}); 2181cb0ef41Sopenharmony_ci} 2191cb0ef41Sopenharmony_ci 2201cb0ef41Sopenharmony_cifunction stat() { 2211cb0ef41Sopenharmony_ci const fs = require('fs'); 2221cb0ef41Sopenharmony_ci fs.writeFileSync('fs22.txt', '123', 'utf8'); 2231cb0ef41Sopenharmony_ci fs.stat('fs22.txt', () => { 2241cb0ef41Sopenharmony_ci fs.unlinkSync('fs22.txt'); 2251cb0ef41Sopenharmony_ci }); 2261cb0ef41Sopenharmony_ci} 2271cb0ef41Sopenharmony_ci 2281cb0ef41Sopenharmony_cifunction unlink() { 2291cb0ef41Sopenharmony_ci const fs = require('fs'); 2301cb0ef41Sopenharmony_ci fs.writeFileSync('fs23.txt', '123', 'utf8'); 2311cb0ef41Sopenharmony_ci fs.linkSync('fs23.txt', 'fs24.txt'); 2321cb0ef41Sopenharmony_ci fs.unlink('fs23.txt', () => {}); 2331cb0ef41Sopenharmony_ci fs.unlink('fs24.txt', () => {}); 2341cb0ef41Sopenharmony_ci} 2351cb0ef41Sopenharmony_ci 2361cb0ef41Sopenharmony_cifunction utime() { 2371cb0ef41Sopenharmony_ci const fs = require('fs'); 2381cb0ef41Sopenharmony_ci fs.writeFileSync('fs25.txt', '123', 'utf8'); 2391cb0ef41Sopenharmony_ci fs.utimes('fs25.txt', 1, 1, () => { 2401cb0ef41Sopenharmony_ci fs.unlinkSync('fs25.txt'); 2411cb0ef41Sopenharmony_ci }); 2421cb0ef41Sopenharmony_ci} 2431cb0ef41Sopenharmony_ci 2441cb0ef41Sopenharmony_cifunction write() { 2451cb0ef41Sopenharmony_ci const fs = require('fs'); 2461cb0ef41Sopenharmony_ci fs.writeFile('fs26.txt', '123', 'utf8', () => { 2471cb0ef41Sopenharmony_ci fs.unlinkSync('fs26.txt'); 2481cb0ef41Sopenharmony_ci }); 2491cb0ef41Sopenharmony_ci} 2501cb0ef41Sopenharmony_ci 2511cb0ef41Sopenharmony_cifunction symlink() { 2521cb0ef41Sopenharmony_ci const fs = require('fs'); 2531cb0ef41Sopenharmony_ci fs.writeFileSync('fs27.txt', '123', 'utf8'); 2541cb0ef41Sopenharmony_ci fs.symlink('fs27.txt', 'fs28.txt', () => { 2551cb0ef41Sopenharmony_ci fs.unlinkSync('fs27.txt'); 2561cb0ef41Sopenharmony_ci fs.unlinkSync('fs28.txt'); 2571cb0ef41Sopenharmony_ci }); 2581cb0ef41Sopenharmony_ci} 2591cb0ef41Sopenharmony_ci 2601cb0ef41Sopenharmony_cifunction readlink() { 2611cb0ef41Sopenharmony_ci const fs = require('fs'); 2621cb0ef41Sopenharmony_ci fs.writeFileSync('fs29.txt', '123', 'utf8'); 2631cb0ef41Sopenharmony_ci fs.symlinkSync('fs29.txt', 'fs30.txt'); 2641cb0ef41Sopenharmony_ci fs.readlink('fs30.txt', () => { 2651cb0ef41Sopenharmony_ci fs.unlinkSync('fs29.txt'); 2661cb0ef41Sopenharmony_ci fs.unlinkSync('fs30.txt'); 2671cb0ef41Sopenharmony_ci }); 2681cb0ef41Sopenharmony_ci} 2691cb0ef41Sopenharmony_ci// The key defined in get_fs_name_by_type function in node_file.cc and node_dir.cc 2701cb0ef41Sopenharmony_citests.access = wrapper(access); 2711cb0ef41Sopenharmony_citests.chmod = wrapper(chmod); 2721cb0ef41Sopenharmony_citests.chown = wrapper(chown, { uid, gid }); 2731cb0ef41Sopenharmony_citests.close = wrapper(close); 2741cb0ef41Sopenharmony_citests.copyfile = wrapper(copyfile); 2751cb0ef41Sopenharmony_citests.fchmod = wrapper(fchmod); 2761cb0ef41Sopenharmony_citests.fchown = wrapper(fchown, { uid, gid }); 2771cb0ef41Sopenharmony_citests.fdatasync = wrapper(fdatasync); 2781cb0ef41Sopenharmony_citests.fstat = wrapper(fstat); 2791cb0ef41Sopenharmony_citests.fsync = wrapper(fsync); 2801cb0ef41Sopenharmony_citests.ftruncate = wrapper(ftruncate); 2811cb0ef41Sopenharmony_citests.futime = wrapper(futime); 2821cb0ef41Sopenharmony_citests.lutime = wrapper(lutime); 2831cb0ef41Sopenharmony_citests.lchown = wrapper(lchown, { uid, gid }); 2841cb0ef41Sopenharmony_citests.link = wrapper(link); 2851cb0ef41Sopenharmony_citests.lstat = wrapper(lstat); 2861cb0ef41Sopenharmony_citests.mkdir = wrapper(mkdir); 2871cb0ef41Sopenharmony_citests.mkdtemp = wrapper(mktmp); 2881cb0ef41Sopenharmony_citests.open = wrapper(open); 2891cb0ef41Sopenharmony_citests.read = wrapper(read); 2901cb0ef41Sopenharmony_citests.scandir = wrapper(readdir); 2911cb0ef41Sopenharmony_citests.opendir = wrapper(opendir); 2921cb0ef41Sopenharmony_citests.realpath = wrapper(realpath); 2931cb0ef41Sopenharmony_citests.rename = wrapper(rename); 2941cb0ef41Sopenharmony_citests.rmdir = wrapper(rmdir); 2951cb0ef41Sopenharmony_citests.stat = wrapper(stat); 2961cb0ef41Sopenharmony_citests.unlink = wrapper(unlink); 2971cb0ef41Sopenharmony_citests.utime = wrapper(utime); 2981cb0ef41Sopenharmony_citests.write = wrapper(write); 2991cb0ef41Sopenharmony_ci 3001cb0ef41Sopenharmony_ci// On windows, we need permissions to test symlink and readlink. 3011cb0ef41Sopenharmony_ci// We'll only try to run these tests if we have enough privileges. 3021cb0ef41Sopenharmony_ciif (common.canCreateSymLink()) { 3031cb0ef41Sopenharmony_ci tests.symlink = wrapper(symlink); 3041cb0ef41Sopenharmony_ci tests.readlink = wrapper(readlink); 3051cb0ef41Sopenharmony_ci} 3061cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir'); 3071cb0ef41Sopenharmony_citmpdir.refresh(); 3081cb0ef41Sopenharmony_ciconst traceFile = path.join(tmpdir.path, 'node_trace.1.log'); 3091cb0ef41Sopenharmony_ci 3101cb0ef41Sopenharmony_cifor (const tr in tests) { 3111cb0ef41Sopenharmony_ci const proc = cp.spawnSync(process.execPath, 3121cb0ef41Sopenharmony_ci [ '--trace-events-enabled', 3131cb0ef41Sopenharmony_ci '--trace-event-categories', 'node.fs_dir.async,node.fs.async', 3141cb0ef41Sopenharmony_ci '-e', tests[tr] ], 3151cb0ef41Sopenharmony_ci { cwd: tmpdir.path, encoding: 'utf8' }); 3161cb0ef41Sopenharmony_ci 3171cb0ef41Sopenharmony_ci // Make sure the operation is successful. 3181cb0ef41Sopenharmony_ci // Don't use assert with a custom message here. Otherwise the 3191cb0ef41Sopenharmony_ci // inspection in the message is done eagerly and wastes a lot of CPU 3201cb0ef41Sopenharmony_ci // time. 3211cb0ef41Sopenharmony_ci if (proc.status !== 0) { 3221cb0ef41Sopenharmony_ci throw new Error(`${tr}:\n${util.inspect(proc)}`); 3231cb0ef41Sopenharmony_ci } 3241cb0ef41Sopenharmony_ci 3251cb0ef41Sopenharmony_ci // Confirm that trace log file is created. 3261cb0ef41Sopenharmony_ci assert(fs.existsSync(traceFile)); 3271cb0ef41Sopenharmony_ci const data = fs.readFileSync(traceFile); 3281cb0ef41Sopenharmony_ci const { traceEvents } = JSON.parse(data.toString()); 3291cb0ef41Sopenharmony_ci // Confirm that the data we want is produced 3301cb0ef41Sopenharmony_ci const traces = traceEvents.filter((item) => { 3311cb0ef41Sopenharmony_ci return [ 3321cb0ef41Sopenharmony_ci 'node,node.fs,node.fs.async', 3331cb0ef41Sopenharmony_ci 'node,node.fs_dir,node.fs_dir.async', 3341cb0ef41Sopenharmony_ci ].includes(item.cat); 3351cb0ef41Sopenharmony_ci }); 3361cb0ef41Sopenharmony_ci // Confirm that the data we want is produced 3371cb0ef41Sopenharmony_ci assert(traces.length > 0); 3381cb0ef41Sopenharmony_ci assert(traces.some((trace) => { 3391cb0ef41Sopenharmony_ci return trace.name === tr; 3401cb0ef41Sopenharmony_ci })); 3411cb0ef41Sopenharmony_ci} 342