All files / lib/internal/fs rimraf.js

67.74% Statements 210/310
61.02% Branches 36/59
81.82% Functions 9/11
67.74% Lines 210/310

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 1071x 1071x 1071x 1071x 1071x                     1071x 1071x 1071x 1071x 138x 138x 1071x 1071x 1071x 1071x 1071x 1x 1x     1x 1x 1071x 128x 128x 942x 942x 942x                 942x 942x 942x 1071x 1071x 138x 138x                                 138x 138x 128x 128x 128x 68x 68x     68x 60x 60x 128x 128x 138x 138x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 1060x 1060x 1060x 1060x 1060x 1060x 1060x       1060x 1060x 1060x 1060x 1060x 68x 68x 68x 138x 138x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 138x 138x 2128x 2128x 2128x 2128x 2128x 2128x 318x 318x     318x 318x 318x 1810x 1810x 1810x 2128x 2128x 1145x 1145x 2128x 3x 3x 3x 3x 3x 3x       2128x 138x 138x 1145x 1145x 1145x 1145x 1145x 1145x 1145x                           1145x 1145x 138x 138x 665x 665x 665x 665x 477x 477x 477x     477x 477x 476x 476x 476x 476x 476x 476x 476x 1464x 1464x 1464x 476x 476x 476x 476x 476x 474x 474x 474x                           474x   1x 477x 477x 665x 138x 138x                                                   138x 138x 138x  
// This file is a modified version of the rimraf module on npm. It has been
// modified in the following ways:
// - Use of the assert module has been replaced with core's error system.
// - All code related to the glob dependency has been removed.
// - Bring your own custom fs module is not currently supported.
// - Some basic code cleanup.
'use strict';
 
const {
  ArrayPrototypeForEach,
  Promise,
  SafeSet,
} = primordials;
 
const { Buffer } = require('buffer');
const fs = require('fs');
const {
  chmod,
  chmodSync,
  lstat,
  lstatSync,
  readdir,
  readdirSync,
  rmdir,
  rmdirSync,
  stat,
  statSync,
  unlink,
  unlinkSync
} = fs;
const { sep } = require('path');
const { setTimeout } = require('timers');
const { sleep } = require('internal/util');
const notEmptyErrorCodes = new SafeSet(['ENOTEMPTY', 'EEXIST', 'EPERM']);
const retryErrorCodes = new SafeSet(
  ['EBUSY', 'EMFILE', 'ENFILE', 'ENOTEMPTY', 'EPERM']);
const isWindows = process.platform === 'win32';
const epermHandler = isWindows ? fixWinEPERM : _rmdir;
const epermHandlerSync = isWindows ? fixWinEPERMSync : _rmdirSync;
const readdirEncoding = 'buffer';
const separator = Buffer.from(sep);
 
 
function rimraf(path, options, callback) {
  let retries = 0;
 
  _rimraf(path, options, function CB(err) {
    if (err) {
      if (retryErrorCodes.has(err.code) && retries < options.maxRetries) {
        retries++;
        const delay = retries * options.retryDelay;
        return setTimeout(_rimraf, delay, path, options, CB);
      }

      // The file is already gone.
      if (err.code === 'ENOENT')
        err = null;
    }
 
    callback(err);
  });
}
 
 
function _rimraf(path, options, callback) {
  // SunOS lets the root user unlink directories. Use lstat here to make sure
  // it's not a directory.
  lstat(path, (err, stats) => {
    if (err) {
      if (err.code === 'ENOENT')
        return callback(null);

      // Windows can EPERM on stat.
      if (isWindows && err.code === 'EPERM')
        return fixWinEPERM(path, options, err, callback);
    } else if (stats.isDirectory()) {
      return _rmdir(path, options, err, callback);
    }
 
    unlink(path, (err) => {
      if (err) {
        if (err.code === 'ENOENT')
          return callback(null);
        if (err.code === 'EISDIR')
          return _rmdir(path, options, err, callback);
        if (err.code === 'EPERM') {
          return epermHandler(path, options, err, callback);
        }
      }
 
      return callback(err);
    });
  });
}
 
 
function fixWinEPERM(path, options, originalErr, callback) {
  chmod(path, 0o666, (err) => {
    if (err)
      return callback(err.code === 'ENOENT' ? null : originalErr);

    stat(path, (err, stats) => {
      if (err)
        return callback(err.code === 'ENOENT' ? null : originalErr);

      if (stats.isDirectory())
        _rmdir(path, options, originalErr, callback);
      else
        unlink(path, callback);
    });
  });
}
 
 
function _rmdir(path, options, originalErr, callback) {
  rmdir(path, (err) => {
    if (err) {
      if (notEmptyErrorCodes.has(err.code))
        return _rmchildren(path, options, callback);
      if (err.code === 'ENOTDIR')
        return callback(originalErr);
    }
 
    callback(err);
  });
}
 
 
function _rmchildren(path, options, callback) {
  const pathBuf = Buffer.from(path);
 
  readdir(pathBuf, readdirEncoding, (err, files) => {
    if (err)
      return callback(err);
 
    let numFiles = files.length;
 
    if (numFiles === 0)
      return rmdir(path, callback);
 
    let done = false;
 
    ArrayPrototypeForEach(files, (child) => {
      const childPath = Buffer.concat([pathBuf, separator, child]);
 
      rimraf(childPath, options, (err) => {
        if (done)
          return;
 
        if (err) {
          done = true;
          return callback(err);
        }
 
        numFiles--;
        if (numFiles === 0)
          rmdir(path, callback);
      });
    });
  });
}
 
 
function rimrafPromises(path, options) {
  return new Promise((resolve, reject) => {
    rimraf(path, options, (err) => {
      if (err)
        return reject(err);
 
      resolve();
    });
  });
}
 
 
function rimrafSync(path, options) {
  let stats;
 
  try {
    stats = lstatSync(path);
  } catch (err) {
    if (err.code === 'ENOENT')
      return;

    // Windows can EPERM on stat.
    if (isWindows && err.code === 'EPERM')
      fixWinEPERMSync(path, options, err);
  }
 
  try {
    // SunOS lets the root user unlink directories.
    if (stats?.isDirectory())
      _rmdirSync(path, options, null);
    else
      _unlinkSync(path, options);
  } catch (err) {
    if (err.code === 'ENOENT')
      return;
    if (err.code === 'EPERM')
      return epermHandlerSync(path, options, err);
    if (err.code !== 'EISDIR')
      throw err;

    _rmdirSync(path, options, err);
  }
}
 
 
function _unlinkSync(path, options) {
  const tries = options.maxRetries + 1;
 
  for (let i = 1; i <= tries; i++) {
    try {
      return unlinkSync(path);
    } catch (err) {
      // Only sleep if this is not the last try, and the delay is greater
      // than zero, and an error was encountered that warrants a retry.
      if (retryErrorCodes.has(err.code) &&
          i < tries &&
          options.retryDelay > 0) {
        sleep(i * options.retryDelay);
      } else if (err.code === 'ENOENT') {
        // The file is already gone.
        return;
      } else if (i === tries) {
        throw err;
      }
    }
  }
}
 
 
function _rmdirSync(path, options, originalErr) {
  try {
    rmdirSync(path);
  } catch (err) {
    if (err.code === 'ENOENT')
      return;
    if (err.code === 'ENOTDIR') {
      throw originalErr || err;
    }
 
    if (notEmptyErrorCodes.has(err.code)) {
      // Removing failed. Try removing all children and then retrying the
      // original removal. Windows has a habit of not closing handles promptly
      // when files are deleted, resulting in spurious ENOTEMPTY failures. Work
      // around that issue by retrying on Windows.
      const pathBuf = Buffer.from(path);
 
      ArrayPrototypeForEach(readdirSync(pathBuf, readdirEncoding), (child) => {
        const childPath = Buffer.concat([pathBuf, separator, child]);
 
        rimrafSync(childPath, options);
      });
 
      const tries = options.maxRetries + 1;
 
      for (let i = 1; i <= tries; i++) {
        try {
          return fs.rmdirSync(path);
        } catch (err) {
          // Only sleep if this is not the last try, and the delay is greater
          // than zero, and an error was encountered that warrants a retry.
          if (retryErrorCodes.has(err.code) &&
              i < tries &&
              options.retryDelay > 0) {
            sleep(i * options.retryDelay);
          } else if (err.code === 'ENOENT') {
            // The file is already gone.
            return;
          } else if (i === tries) {
            throw err;
          }
        }
      }
    }
 
    throw originalErr || err;
  }
}
 
 
function fixWinEPERMSync(path, options, originalErr) {
  try {
    chmodSync(path, 0o666);
  } catch (err) {
    if (err.code === 'ENOENT')
      return;

    throw originalErr;
  }

  let stats;

  try {
    stats = statSync(path, { throwIfNoEntry: false });
  } catch {
    throw originalErr;
  }

  if (stats === undefined) return;

  if (stats.isDirectory())
    _rmdirSync(path, options, originalErr);
  else
    _unlinkSync(path, options);
}
 
 
module.exports = { rimraf, rimrafPromises, rimrafSync };