1'use strict'; 2const common = require('../common'); 3const assert = require('assert'); 4const cp = require('child_process'); 5const fs = require('fs'); 6const path = require('path'); 7const util = require('util'); 8 9const tests = Object.create(null); 10 11let gid = 1; 12let uid = 1; 13 14if (!common.isWindows) { 15 gid = process.getgid(); 16 uid = process.getuid(); 17} 18 19tests['fs.sync.access'] = 'fs.writeFileSync("fs0.txt", "123", "utf8");' + 20 'fs.accessSync("fs0.txt");' + 21 'fs.unlinkSync("fs0.txt")'; 22tests['fs.sync.chmod'] = 'fs.writeFileSync("fs1.txt", "123", "utf8");' + 23 'fs.chmodSync("fs1.txt",100);' + 24 'fs.unlinkSync("fs1.txt")'; 25tests['fs.sync.chown'] = 'fs.writeFileSync("fs2.txt", "123", "utf8");' + 26 `fs.chownSync("fs2.txt", ${uid}, ${gid});` + 27 'fs.unlinkSync("fs2.txt")'; 28tests['fs.sync.close'] = 'fs.writeFileSync("fs3.txt", "123", "utf8");' + 29 'fs.unlinkSync("fs3.txt")'; 30tests['fs.sync.copyfile'] = 'fs.writeFileSync("fs4.txt", "123", "utf8");' + 31 'fs.copyFileSync("fs4.txt","a.txt");' + 32 'fs.unlinkSync("fs4.txt")'; 33tests['fs.sync.fchmod'] = 'fs.writeFileSync("fs5.txt", "123", "utf8");' + 34 'const fd = fs.openSync("fs5.txt", "r+");' + 35 'fs.fchmodSync(fd,100);' + 36 'fs.unlinkSync("fs5.txt")'; 37tests['fs.sync.fchown'] = 'fs.writeFileSync("fs6.txt", "123", "utf8");' + 38 'const fd = fs.openSync("fs6.txt", "r+");' + 39 `fs.fchownSync(fd, ${uid}, ${gid});` + 40 'fs.unlinkSync("fs6.txt")'; 41tests['fs.sync.fdatasync'] = 'fs.writeFileSync("fs7.txt", "123", "utf8");' + 42 'const fd = fs.openSync("fs7.txt", "r+");' + 43 'fs.fdatasyncSync(fd);' + 44 'fs.unlinkSync("fs7.txt")'; 45tests['fs.sync.fstat'] = 'fs.writeFileSync("fs8.txt", "123", "utf8");' + 46 'fs.readFileSync("fs8.txt");' + 47 'fs.unlinkSync("fs8.txt")'; 48tests['fs.sync.fsync'] = 'fs.writeFileSync("fs9.txt", "123", "utf8");' + 49 'const fd = fs.openSync("fs9.txt", "r+");' + 50 'fs.fsyncSync(fd);' + 51 'fs.unlinkSync("fs9.txt")'; 52tests['fs.sync.ftruncate'] = 'fs.writeFileSync("fs10.txt", "123", "utf8");' + 53 'const fd = fs.openSync("fs10.txt", "r+");' + 54 'fs.ftruncateSync(fd, 1);' + 55 'fs.unlinkSync("fs10.txt")'; 56tests['fs.sync.futimes'] = 'fs.writeFileSync("fs11.txt", "123", "utf8");' + 57 'const fd = fs.openSync("fs11.txt", "r+");' + 58 'fs.futimesSync(fd,1,1);' + 59 'fs.unlinkSync("fs11.txt")'; 60tests['fs.sync.lchown'] = 'fs.writeFileSync("fs12.txt", "123", "utf8");' + 61 `fs.lchownSync("fs12.txt", ${uid}, ${gid});` + 62 'fs.unlinkSync("fs12.txt")'; 63tests['fs.sync.link'] = 'fs.writeFileSync("fs13.txt", "123", "utf8");' + 64 'fs.linkSync("fs13.txt", "fs14.txt");' + 65 'fs.unlinkSync("fs13.txt");' + 66 'fs.unlinkSync("fs14.txt")'; 67tests['fs.sync.lstat'] = 'fs.writeFileSync("fs15.txt", "123", "utf8");' + 68 'fs.lstatSync("fs15.txt");' + 69 'fs.unlinkSync("fs15.txt")'; 70tests['fs.sync.mkdir'] = 'fs.mkdirSync("fstemp0");' + 71 'fs.rmdirSync("fstemp0")'; 72tests['fs.sync.mkdtemp'] = 'const fp = fs.mkdtempSync("fstemp1");' + 73 'fs.rmdirSync(fp)'; 74tests['fs.sync.open'] = 'fs.writeFileSync("fs16.txt", "123", "utf8");' + 75 'fs.unlinkSync("fs16.txt")'; 76tests['fs.sync.read'] = 'fs.writeFileSync("fs17.txt", "123", "utf8");' + 77 'fs.readFileSync("fs17.txt");' + 78 'fs.unlinkSync("fs17.txt")'; 79tests['fs.sync.readdir'] = 'fs.readdirSync("./")'; 80tests['fs.sync.realpath'] = 'fs.writeFileSync("fs18.txt", "123", "utf8");' + 81 'fs.linkSync("fs18.txt", "fs19.txt");' + 82 'fs.realpathSync.native("fs19.txt");' + 83 'fs.unlinkSync("fs18.txt");' + 84 'fs.unlinkSync("fs19.txt")'; 85tests['fs.sync.rename'] = 'fs.writeFileSync("fs20.txt", "123", "utf8");' + 86 'fs.renameSync("fs20.txt","fs21.txt"); ' + 87 'fs.unlinkSync("fs21.txt")'; 88tests['fs.sync.rmdir'] = 'fs.mkdirSync("fstemp2");' + 89 'fs.rmdirSync("fstemp2")'; 90tests['fs.sync.stat'] = 'fs.writeFileSync("fs22.txt", "123", "utf8");' + 91 'fs.statSync("fs22.txt");' + 92 'fs.unlinkSync("fs22.txt")'; 93tests['fs.sync.unlink'] = 'fs.writeFileSync("fs23.txt", "123", "utf8");' + 94 'fs.linkSync("fs23.txt", "fs24.txt");' + 95 'fs.unlinkSync("fs23.txt");' + 96 'fs.unlinkSync("fs24.txt")'; 97tests['fs.sync.utimes'] = 'fs.writeFileSync("fs25.txt", "123", "utf8");' + 98 'fs.utimesSync("fs25.txt",1,1);' + 99 'fs.unlinkSync("fs25.txt")'; 100tests['fs.sync.write'] = 'fs.writeFileSync("fs26.txt", "123", "utf8");' + 101 'fs.unlinkSync("fs26.txt")'; 102 103// On windows, we need permissions to test symlink and readlink. 104// We'll only try to run these tests if we have enough privileges. 105if (common.canCreateSymLink()) { 106 tests['fs.sync.symlink'] = 'fs.writeFileSync("fs27.txt", "123", "utf8");' + 107 'fs.symlinkSync("fs27.txt", "fs28.txt");' + 108 'fs.unlinkSync("fs27.txt");' + 109 'fs.unlinkSync("fs28.txt")'; 110 tests['fs.sync.readlink'] = 'fs.writeFileSync("fs29.txt", "123", "utf8");' + 111 'fs.symlinkSync("fs29.txt", "fs30.txt");' + 112 'fs.readlinkSync("fs30.txt");' + 113 'fs.unlinkSync("fs29.txt");' + 114 'fs.unlinkSync("fs30.txt")'; 115} 116 117const tmpdir = require('../common/tmpdir'); 118tmpdir.refresh(); 119const traceFile = path.join(tmpdir.path, 'node_trace.1.log'); 120 121for (const tr in tests) { 122 const proc = cp.spawnSync(process.execPath, 123 [ '--trace-events-enabled', 124 '--trace-event-categories', 'node.fs.sync', 125 '-e', tests[tr] ], 126 { cwd: tmpdir.path, encoding: 'utf8' }); 127 128 // Make sure the operation is successful. 129 // Don't use assert with a custom message here. Otherwise the 130 // inspection in the message is done eagerly and wastes a lot of CPU 131 // time. 132 if (proc.status !== 0) { 133 throw new Error(`${tr}:\n${util.inspect(proc)}`); 134 } 135 136 // Confirm that trace log file is created. 137 assert(fs.existsSync(traceFile)); 138 const data = fs.readFileSync(traceFile); 139 const traces = JSON.parse(data.toString()).traceEvents; 140 assert(traces.length > 0); 141 142 // C++ fs sync trace events should be generated. 143 assert(traces.some((trace) => { 144 if (trace.pid !== proc.pid) 145 return false; 146 if (trace.cat !== 'node,node.fs,node.fs.sync') 147 return false; 148 if (trace.name !== tr) 149 return false; 150 return true; 151 })); 152} 153