codekingpro/portable-devtools
115k
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.mkdirSync = exports.mkdir = void 0;
7const chownr_1 = require("chownr");
8const node_fs_1 = __importDefault(require("node:fs"));
9const promises_1 = __importDefault(require("node:fs/promises"));
10const node_path_1 = __importDefault(require("node:path"));
11const cwd_error_js_1 = require("./cwd-error.js");
12const normalize_windows_path_js_1 = require("./normalize-windows-path.js");
13const symlink_error_js_1 = require("./symlink-error.js");
14const checkCwd = (dir, cb) => {
15 node_fs_1.default.stat(dir, (er, st) => {
16 if (er || !st.isDirectory()) {
17 er = new cwd_error_js_1.CwdError(dir, er?.code || 'ENOTDIR');
18 }
19 cb(er);
20 });
21};
22/**
23 * Wrapper around fs/promises.mkdir for tar's needs.
24 *
25 * The main purpose is to avoid creating directories if we know that
26 * they already exist (and track which ones exist for this purpose),
27 * and prevent entries from being extracted into symlinked folders,
28 * if `preservePaths` is not set.
29 */
30const mkdir = (dir, opt, cb) => {
31 dir = (0, normalize_windows_path_js_1.normalizeWindowsPath)(dir);
32 // if there's any overlap between mask and mode,
33 // then we'll need an explicit chmod
34 /* c8 ignore next */
35 const umask = opt.umask ?? 0o22;
36 const mode = opt.mode | 0o0700;
37 const needChmod = (mode & umask) !== 0;
38 const uid = opt.uid;
39 const gid = opt.gid;
40 const doChown = typeof uid === 'number' &&
41 typeof gid === 'number' &&
42 (uid !== opt.processUid || gid !== opt.processGid);
43 const preserve = opt.preserve;
44 const unlink = opt.unlink;
45 const cwd = (0, normalize_windows_path_js_1.normalizeWindowsPath)(opt.cwd);
46 const done = (er, created) => {
47 if (er) {
48 cb(er);
49 }
50 else {
51 if (created && doChown) {
52 (0, chownr_1.chownr)(created, uid, gid, er => done(er));
53 }
54 else if (needChmod) {
55 node_fs_1.default.chmod(dir, mode, cb);
56 }
57 else {
58 cb();
59 }
60 }
61 };
62 if (dir === cwd) {
63 return checkCwd(dir, done);
64 }
65 if (preserve) {
66 return promises_1.default.mkdir(dir, { mode, recursive: true }).then(made => done(null, made ?? undefined), // oh, ts
67 done);
68 }
69 const sub = (0, normalize_windows_path_js_1.normalizeWindowsPath)(node_path_1.default.relative(cwd, dir));
70 const parts = sub.split('/');
71 mkdir_(cwd, parts, mode, unlink, cwd, undefined, done);
72};
73exports.mkdir = mkdir;
74const mkdir_ = (base, parts, mode, unlink, cwd, created, cb) => {
75 if (!parts.length) {
76 return cb(null, created);
77 }
78 const p = parts.shift();
79 const part = (0, normalize_windows_path_js_1.normalizeWindowsPath)(node_path_1.default.resolve(base + '/' + p));
80 node_fs_1.default.mkdir(part, mode, onmkdir(part, parts, mode, unlink, cwd, created, cb));
81};
82const onmkdir = (part, parts, mode, unlink, cwd, created, cb) => (er) => {
83 if (er) {
84 node_fs_1.default.lstat(part, (statEr, st) => {
85 if (statEr) {
86 statEr.path =
87 statEr.path && (0, normalize_windows_path_js_1.normalizeWindowsPath)(statEr.path);
88 cb(statEr);
89 }
90 else if (st.isDirectory()) {
91 mkdir_(part, parts, mode, unlink, cwd, created, cb);
92 }
93 else if (unlink) {
94 node_fs_1.default.unlink(part, er => {
95 if (er) {
96 return cb(er);
97 }
98 node_fs_1.default.mkdir(part, mode, onmkdir(part, parts, mode, unlink, cwd, created, cb));
99 });
100 }
101 else if (st.isSymbolicLink()) {
102 return cb(new symlink_error_js_1.SymlinkError(part, part + '/' + parts.join('/')));
103 }
104 else {
105 cb(er);
106 }
107 });
108 }
109 else {
110 created = created || part;
111 mkdir_(part, parts, mode, unlink, cwd, created, cb);
112 }
113};
114const checkCwdSync = (dir) => {
115 let ok = false;
116 let code = undefined;
117 try {
118 ok = node_fs_1.default.statSync(dir).isDirectory();
119 }
120 catch (er) {
121 code = er?.code;
122 }
123 finally {
124 if (!ok) {
125 throw new cwd_error_js_1.CwdError(dir, code ?? 'ENOTDIR');
126 }
127 }
128};
129const mkdirSync = (dir, opt) => {
130 dir = (0, normalize_windows_path_js_1.normalizeWindowsPath)(dir);
131 // if there's any overlap between mask and mode,
132 // then we'll need an explicit chmod
133 /* c8 ignore next */
134 const umask = opt.umask ?? 0o22;
135 const mode = opt.mode | 0o700;
136 const needChmod = (mode & umask) !== 0;
137 const uid = opt.uid;
138 const gid = opt.gid;
139 const doChown = typeof uid === 'number' &&
140 typeof gid === 'number' &&
141 (uid !== opt.processUid || gid !== opt.processGid);
142 const preserve = opt.preserve;
143 const unlink = opt.unlink;
144 const cwd = (0, normalize_windows_path_js_1.normalizeWindowsPath)(opt.cwd);
145 const done = (created) => {
146 if (created && doChown) {
147 (0, chownr_1.chownrSync)(created, uid, gid);
148 }
149 if (needChmod) {
150 node_fs_1.default.chmodSync(dir, mode);
151 }
152 };
153 if (dir === cwd) {
154 checkCwdSync(cwd);
155 return done();
156 }
157 if (preserve) {
158 return done(node_fs_1.default.mkdirSync(dir, { mode, recursive: true }) ?? undefined);
159 }
160 const sub = (0, normalize_windows_path_js_1.normalizeWindowsPath)(node_path_1.default.relative(cwd, dir));
161 const parts = sub.split('/');
162 let created = undefined;
163 for (let p = parts.shift(), part = cwd; p && (part += '/' + p); p = parts.shift()) {
164 part = (0, normalize_windows_path_js_1.normalizeWindowsPath)(node_path_1.default.resolve(part));
165 try {
166 node_fs_1.default.mkdirSync(part, mode);
167 created = created || part;
168 }
169 catch (er) {
170 const st = node_fs_1.default.lstatSync(part);
171 if (st.isDirectory()) {
172 continue;
173 }
174 else if (unlink) {
175 node_fs_1.default.unlinkSync(part);
176 node_fs_1.default.mkdirSync(part, mode);
177 created = created || part;
178 continue;
179 }
180 else if (st.isSymbolicLink()) {
181 return new symlink_error_js_1.SymlinkError(part, part + '/' + parts.join('/'));
182 }
183 }
184 }
185 return done(created);
186};
187exports.mkdirSync = mkdirSync;
188//# sourceMappingURL=mkdir.js.map