11cb0ef41Sopenharmony_ciconst {dirname} = require('path')
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst mkdirpManual = (path, opts, made) => {
41cb0ef41Sopenharmony_ci  opts.recursive = false
51cb0ef41Sopenharmony_ci  const parent = dirname(path)
61cb0ef41Sopenharmony_ci  if (parent === path) {
71cb0ef41Sopenharmony_ci    return opts.mkdirAsync(path, opts).catch(er => {
81cb0ef41Sopenharmony_ci      // swallowed by recursive implementation on posix systems
91cb0ef41Sopenharmony_ci      // any other error is a failure
101cb0ef41Sopenharmony_ci      if (er.code !== 'EISDIR')
111cb0ef41Sopenharmony_ci        throw er
121cb0ef41Sopenharmony_ci    })
131cb0ef41Sopenharmony_ci  }
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci  return opts.mkdirAsync(path, opts).then(() => made || path, er => {
161cb0ef41Sopenharmony_ci    if (er.code === 'ENOENT')
171cb0ef41Sopenharmony_ci      return mkdirpManual(parent, opts)
181cb0ef41Sopenharmony_ci        .then(made => mkdirpManual(path, opts, made))
191cb0ef41Sopenharmony_ci    if (er.code !== 'EEXIST' && er.code !== 'EROFS')
201cb0ef41Sopenharmony_ci      throw er
211cb0ef41Sopenharmony_ci    return opts.statAsync(path).then(st => {
221cb0ef41Sopenharmony_ci      if (st.isDirectory())
231cb0ef41Sopenharmony_ci        return made
241cb0ef41Sopenharmony_ci      else
251cb0ef41Sopenharmony_ci        throw er
261cb0ef41Sopenharmony_ci    }, () => { throw er })
271cb0ef41Sopenharmony_ci  })
281cb0ef41Sopenharmony_ci}
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ciconst mkdirpManualSync = (path, opts, made) => {
311cb0ef41Sopenharmony_ci  const parent = dirname(path)
321cb0ef41Sopenharmony_ci  opts.recursive = false
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci  if (parent === path) {
351cb0ef41Sopenharmony_ci    try {
361cb0ef41Sopenharmony_ci      return opts.mkdirSync(path, opts)
371cb0ef41Sopenharmony_ci    } catch (er) {
381cb0ef41Sopenharmony_ci      // swallowed by recursive implementation on posix systems
391cb0ef41Sopenharmony_ci      // any other error is a failure
401cb0ef41Sopenharmony_ci      if (er.code !== 'EISDIR')
411cb0ef41Sopenharmony_ci        throw er
421cb0ef41Sopenharmony_ci      else
431cb0ef41Sopenharmony_ci        return
441cb0ef41Sopenharmony_ci    }
451cb0ef41Sopenharmony_ci  }
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci  try {
481cb0ef41Sopenharmony_ci    opts.mkdirSync(path, opts)
491cb0ef41Sopenharmony_ci    return made || path
501cb0ef41Sopenharmony_ci  } catch (er) {
511cb0ef41Sopenharmony_ci    if (er.code === 'ENOENT')
521cb0ef41Sopenharmony_ci      return mkdirpManualSync(path, opts, mkdirpManualSync(parent, opts, made))
531cb0ef41Sopenharmony_ci    if (er.code !== 'EEXIST' && er.code !== 'EROFS')
541cb0ef41Sopenharmony_ci      throw er
551cb0ef41Sopenharmony_ci    try {
561cb0ef41Sopenharmony_ci      if (!opts.statSync(path).isDirectory())
571cb0ef41Sopenharmony_ci        throw er
581cb0ef41Sopenharmony_ci    } catch (_) {
591cb0ef41Sopenharmony_ci      throw er
601cb0ef41Sopenharmony_ci    }
611cb0ef41Sopenharmony_ci  }
621cb0ef41Sopenharmony_ci}
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_cimodule.exports = {mkdirpManual, mkdirpManualSync}
65