aboutsummaryrefslogtreecommitdiff
path: root/src/commands/cp.js
blob: ef864fd7d2a4b665373a03eb3f0dd385ea3e50ea (plain)
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
#!/usr/bin/env bun

import { mkdirSync } from "node:fs";
import { copyFile, link as fsLink, lstat, mkdir, readlink, stat } from "node:fs/promises";
import { isAbsolute, join, basename as pathBasename, dirname as pathDirname, resolve } from "node:path";
import { bufferPathBasename, bufferPathJoin, invalidOptionMessage, isBytePath, localeQuotedDiagnostic, localeQuotedEscapedDiagnostic, normalizeLongOptionByPrefix, parseOptions, pathDisplayName, pathLikeJoin, rawCommandArgs, readdirPathEntries, selinuxRuntimeEnabled } from "../shared/common.js";
import { applyCopySecurityContext, areSameFile, backupDestination, backupFileName, backupModeFromEnvironment, copyPath, cpDuplicateDisplayName, cpRestrictiveDirectoryMode, cpShouldDereferenceSource, cpUpdateDecision, ensureBackupDoesNotDestroySource, resolveDestinationOperands, stripTrailingSlashesPath, validateBackupMode, validateUpdateMode, withPreservedCopyCreationContext } from "../shared/copy.js";
import { InvocationError, UsageError, stderr, stdout } from "../shared/diagnostics.js";
import { confirmRemoval, isWriteProtected, modeString, rawArgLooksLikeOption, rawPathNeedsBytes, setFileMode } from "../shared/filesystem.js";
import { defineCommand, runAsMain } from "../shared/command.js";

export const CP_LONG_OPTIONS = ["archive", "attributes-only", "backup", "copy-contents", "debug", "force", "interactive", "dereference", "keep-directory-symlink", "link", "no-clobber", "no-dereference", "preserve", "no-preserve", "parents", "recursive", "reflink", "remove-destination", "sparse", "strip-trailing-slashes", "suffix", "symbolic-link", "target-directory", "no-target-directory", "update", "verbose", "one-file-system", "context", "help", "version"];

export function cpMetaOption(args) {
  const longFlagOptions = new Set(["archive", "attributes-only", "copy-contents", "debug", "force", "interactive", "dereference", "no-dereference", "keep-directory-symlink", "link", "no-clobber", "parents", "recursive", "remove-destination", "strip-trailing-slashes", "symbolic-link", "no-target-directory", "verbose", "one-file-system"]);
  let sawContextValue = false;
  for (let i = 0; i < args.length; i++) {
    const arg = args[i];
    if (arg === "--") return null;
    let normalized = arg;
    if (arg.startsWith("--")) normalized = normalizeLongOptionByPrefix(arg, CP_LONG_OPTIONS);
    const [name, inlineValue] = normalized.startsWith("--") ? normalized.slice(2).split(/=(.*)/s, 2) : [null, undefined];
    if (name === "help" || name === "version") {
      if (inlineValue !== undefined) throw new UsageError(`option '--${name}' doesn't allow an argument`, true);
      if (sawContextValue) stderr("cp: warning: ignoring --context; it requires an SELinux-enabled kernel\n");
      return normalized;
    }
    if (longFlagOptions.has(name)) {
      if (inlineValue !== undefined) throw new UsageError(`option '--${name}' doesn't allow an argument`, true);
      continue;
    }
    if (name === "suffix" || name === "target-directory" || name === "no-preserve" || name === "sparse") {
      if (inlineValue === undefined) i++;
      if (name === "no-preserve" && inlineValue !== undefined) validateCpPreserveList(inlineValue, "--no-preserve");
      if (name === "sparse" && inlineValue !== undefined) validateSparseMode("cp", inlineValue);
      continue;
    }
    if (name === "preserve") {
      if (inlineValue !== undefined) validateCpPreserveList(inlineValue, "--preserve");
      continue;
    }
    if (name === "context") {
      if (inlineValue !== undefined) sawContextValue = true;
      continue;
    }
    if (name === "backup") continue;
    if (name === "reflink") {
      if (inlineValue !== undefined) validateReflinkMode("cp", inlineValue);
      continue;
    }
    if (name === "update") {
      if (inlineValue !== undefined) validateUpdateMode("cp", inlineValue);
      continue;
    }
    if (arg.startsWith("-") && !arg.startsWith("--")) {
      i = scanCpShortMetaOption(args, i);
      continue;
    }
    if (arg.startsWith("-")) throw new UsageError(invalidOptionMessage(arg), true);
    continue;
  }
  return null;
}

export function scanCpShortMetaOption(args, index) {
  const arg = args[index];
  for (let j = 1; j < arg.length; j++) {
    const ch = arg[j];
    if ("finrRapsdlHLPTuvbxZ".includes(ch)) continue;
    if (ch === "t" || ch === "S") return arg.slice(j + 1) ? index : index + 1;
    throw new UsageError(`invalid option -- '${ch}'`, true);
  }
  return index;
}

export async function cpCmd(args) {
  args = normalizeCpArgs(args);
  const { opts, operands } = parseOptions(args, { short: { f: false, i: false, n: false, r: false, R: false, a: false, p: false, s: false, d: false, l: false, H: false, L: false, P: false, u: false, v: false, t: "value", T: false, b: false, S: "value", x: false, Z: false }, long: { force: false, interactive: false, "no-clobber": false, recursive: false, archive: false, preserve: "optional-value", parent: false, parents: false, "symbolic-link": false, "no-dereference": false, link: false, dereference: false, "no-preserve": "value", update: "optional-value", verbose: false, debug: false, reflink: "optional-value", sparse: "value", "target-directory": "value", "no-target-directory": false, "remove-destination": false, rem: false, "attributes-only": false, "copy-contents": false, context: "optional-value", backup: "optional-value", b: "optional-value", suffix: "value", "strip-trailing-slashes": false, "one-file-system": false, "keep-directory-symlink": false, help: false, version: false } });
  const selinuxEnabled = selinuxRuntimeEnabled();
  if (typeof opts.context === "string" && !selinuxEnabled) stderr("cp: warning: ignoring --context; it requires an SELinux-enabled kernel\n");
  if (opts.b != null && opts.backup == null) opts.backup = opts.b;
  if (opts.S != null && opts.suffix == null) opts.suffix = opts.S;
  const reflinkMode = cpReflinkMode(opts);
  const sparseMode = cpSparseMode(opts);
  validateCpPreserveOptions(opts);
  const securityContextOptions = cpSecurityContextOptions(opts, selinuxEnabled);
  const noDereference = opts.d || opts.P || opts["no-dereference"];
  const backupRequested = opts.b || opts.backup != null;
  if (backupRequested) validateBackupMode("cp", opts.backup);
  const overwriteMode = cpOverwriteMode(args, opts);
  if (backupRequested && (overwriteMode === "no-clobber" || opts.update === "none" || opts.update === "none-fail")) {
    throw new UsageError("--backup is mutually exclusive with -n or --update=none-fail", true);
  }
  if (reflinkMode === "always" && opts.sparse != null) throw new UsageError("options --reflink and --sparse are mutually exclusive");
  const { sources, dest, destStat, useDirectoryTarget } = await resolveDestinationOperands("cp", operands, opts);
  if (cpParents(opts) && !useDirectoryTarget) throw new UsageError("with --parents, the destination must be a directory", true);
  const seenSources = new Set();
  const createdTargets = new Map();
  const linkMap = new Map();
  const rawPlan = rawCpPlan(opts, operands, useDirectoryTarget);
  let status = 0;
  for (let i = 0; i < sources.length; i++) {
    const src = sources[i];
    const normalizedSrc = opts["strip-trailing-slashes"] ? stripTrailingSlashesPath(src) : src;
    const rawSource = rawPlan?.sources[i];
    const operationSource = rawSource ? await resolveRawExistingPath(stripTrailingDotPathBytes(rawSource)) : normalizedSrc;
    try {
      const target = rawSource ? rawCpTargetPath(rawSource, rawPlan.dest, rawPlan.useDirectoryTarget, opts) : cpTargetPath(normalizedSrc, dest, useDirectoryTarget, opts);
      const sourceInfo = await cpTopLevelSourceStat(operationSource, opts, noDereference);
      if (!sourceInfo) throw new UsageError(`cannot stat '${src}': No such file or directory`);
      const sourceKey = rawSource ? rawPathKey(rawSource) : resolve(src);
      if (sourceInfo && seenSources.has(sourceKey) && !backupRequested) {
        const kind = sourceInfo.isDirectory() ? "directory" : "file";
        stderr(`cp: warning: source ${kind} '${cpDuplicateDisplayName(src)}' specified more than once\n`);
        if (sourceInfo.isDirectory()) continue;
      }
      seenSources.add(sourceKey);
      const targetKey = isBytePath(target) ? rawPathKey(target) : resolve(target);
      if (backupRequested && createdTargets.has(targetKey) && !cpBackupIsNumbered(opts)) throw new UsageError(`will not overwrite just-created '${target}' with '${src}'`);
      await ensureCpNotIntoSelf(operationSource, target, noDereference);
      const updateDecision = await cpUpdateDecision(operationSource, target, opts, noDereference, overwriteMode);
      if (updateDecision.skip) {
        if (opts.debug) stdout(`skipped '${src}'\n`);
        if (updateDecision.fail) {
          stderr(`cp: not replacing '${target}'\n`);
          status = 1;
        }
        continue;
      }
      const copyForce = opts.f || opts.force || backupRequested || overwriteMode === "interactive";
      if (overwriteMode === "interactive") {
        const targetInfo = await lstat(target).catch(() => null);
        if (targetInfo && !confirmRemoval(cpOverwritePrompt(target, targetInfo))) {
          status = 1;
          continue;
        }
      }
      if (backupRequested) await ensureBackupDoesNotDestroySource("cp", src, target, opts.suffix, opts.backup);
      const sameFile = await cpSameFile(operationSource, target, opts);
      if (sameFile && (opts.l || opts.link) && await cpHardLinkSameFileIsNoop(src, target, opts, backupRequested)) continue;
      if ((opts.l || opts.link) && (opts.d || opts["no-dereference"]) && src !== target && await areSamePathEntry(operationSource, target)) continue;
      if (sameFile && await cpSameFileIsError(src, target, opts, backupRequested)) throw new UsageError(`'${src}' and '${target}' are the same file`);
      if (sameFile && backupRequested && (opts.f || opts.force) && (!(opts.d || opts["no-dereference"]) || src === target) && !await cpTargetIsSymlink(target)) {
        const backup = backupFileName(target, opts.suffix, opts.backup);
        if (opts.l || opts.link) await fsLink(src, backup);
        else await copyFile(src, backup);
        continue;
      }
      const parentModeRestores = cpParents(opts) ? await cpPrepareParentDirectories(operationSource, dest, noDereference, cpPreserveMode(opts), cpPreserveMetadata(opts), securityContextOptions) : [];
      const actualTargetInfo = await lstat(target).catch(() => null);
      const destinationWasReportedExisting = Boolean(!actualTargetInfo && destStat && !useDirectoryTarget && sources.length === 1);
      const targetExisted = Boolean(actualTargetInfo || destinationWasReportedExisting);
      const createdByDifferentSource = createdTargets.has(targetKey) && createdTargets.get(targetKey) !== sourceKey;
      if (!backupRequested && createdByDifferentSource && await cpTargetIsSymlink(target)) {
        throw new UsageError(`will not copy '${src}' through just-created symlink '${target}'`);
      }
      if (!backupRequested && createdByDifferentSource) {
        throw new UsageError(`will not overwrite just-created '${target}' with '${src}'`);
      }
      if (backupRequested && await cpShouldBackupDestination(target)) await backupDestination(target, opts.suffix, opts.backup);
      await copyPath(operationSource, target, {
        force: copyForce,
        removeDestination: opts["remove-destination"] || opts.rem,
        destinationWasReportedExisting,
        recursive: opts.r || opts.R || opts.recursive || opts.a || opts.archive,
        preserveMode: cpPreserveMode(opts),
        preserveDirectoryMode: !cpNoPreserveAttrs(opts).includes("mode"),
        useSourceModeForNewRegularFiles: cpUseSourceModeForNewRegularFiles(opts),
        preserveMetadata: cpPreserveMetadata(opts),
        preserveLinks: cpPreserveLinks(opts),
        preserveXattrs: cpPreserveXattrs(opts),
        requirePreserveXattrs: cpRequirePreserveXattrs(opts),
        ...securityContextOptions,
        attributesOnly: opts["attributes-only"],
        debug: opts.debug,
        linkMap,
        symbolicLink: opts.s || opts["symbolic-link"],
        hardLink: opts.l || opts.link,
        sparseMode,
        noDereference,
        dereferenceAll: opts.L || opts.dereference,
        dereferenceCommandLine: opts.H,
        copyContents: opts["copy-contents"],
        keepDirectorySymlink: opts["keep-directory-symlink"],
        oneFileSystem: opts.x || opts["one-file-system"],
        rootDev: sourceInfo.dev,
        reflinkMode,
        verbose: opts.v || opts.verbose || opts.debug,
      }, true);
      for (const restore of parentModeRestores.reverse()) {
        if (restore.sourcePath != null) await applyCopySecurityContext(restore.path, restore.sourcePath, securityContextOptions);
        if (restore.mode != null) await setFileMode(restore.path, restore.mode).catch(() => {});
      }
      if (!targetExisted || await cpCopiedMatchingSymlink(src, target, noDereference)) createdTargets.set(targetKey, sourceKey);
    } catch (error) {
      status = 1;
      if (error instanceof UsageError) stderr(`cp: ${error.message}\n`);
      else stderr(`cp: ${error.message || String(error)}\n`);
    }
  }
  return status;
}

export async function cpCopiedMatchingSymlink(src, target, noDereference) {
  if (!noDereference) return false;
  const [srcInfo, targetInfo] = await Promise.all([lstat(src).catch(() => null), lstat(target).catch(() => null)]);
  if (!srcInfo?.isSymbolicLink() || !targetInfo?.isSymbolicLink()) return false;
  return await readlink(src).catch(() => null) === await readlink(target).catch(() => null);
}

export async function cpTopLevelSourceStat(src, opts, noDereference) {
  const dereferenceSource = cpShouldDereferenceSource(src, {
    noDereference,
    dereferenceAll: opts.L || opts.dereference,
    dereferenceCommandLine: opts.H,
    recursive: opts.r || opts.R || opts.recursive || opts.a || opts.archive,
    hardLink: opts.l || opts.link,
    preserveLinks: cpPreserveLinks(opts),
  }, true);
  return (dereferenceSource ? stat(src) : lstat(src)).catch(() => null);
}

export function cpOverwritePrompt(target, targetInfo) {
  if (isWriteProtected(targetInfo)) {
    return `cp: replace '${target}', overriding mode ${(targetInfo.mode & 0o7777).toString(8).padStart(4, "0")} (${modeString(targetInfo).slice(1)})? `;
  }
  return `cp: overwrite '${target}'? `;
}

export function normalizeCpArgs(args) {
  const out = [];
  let end = false;
  for (const arg of args) {
    if (end || arg === "--") {
      out.push(arg);
      if (arg === "--") end = true;
      continue;
    }
    out.push(arg.startsWith("--") ? normalizeLongOptionByPrefix(arg, CP_LONG_OPTIONS) : arg);
  }
  return out;
}

export function cpOverwriteMode(args, opts) {
  let mode = "replace";
  for (let i = 0; i < args.length; i++) {
    const arg = args[i];
    if (arg === "--") break;
    if (arg === "-i" || arg === "--interactive") mode = "interactive";
    else if (arg === "-n" || arg === "--no-clobber") mode = "no-clobber";
    else if (/^-[^-]/.test(arg)) {
      for (const ch of arg.slice(1)) {
        if (ch === "i") mode = "interactive";
        else if (ch === "n") mode = "no-clobber";
      }
    }
  }
  if (mode === "replace" && (opts.i || opts.interactive)) return "interactive";
  if (mode === "replace" && (opts.n || opts["no-clobber"])) return "no-clobber";
  return mode;
}

export function cpReflinkMode(opts) {
  if (opts.reflink == null) return opts.debug && opts.sparse == null ? "auto" : "never";
  const mode = opts.reflink === true ? "always" : String(opts.reflink);
  if (mode === "always" || mode === "auto" || mode === "never") return mode;
  validateReflinkMode("cp", mode);
}

export function cpSparseMode(opts) {
  if (opts.sparse == null) return "auto";
  const mode = String(opts.sparse);
  if (mode === "always" || mode === "auto" || mode === "never") return mode;
  validateSparseMode("cp", mode);
}

export function cpBackupIsNumbered(opts) {
  const mode = opts.backup === true || opts.backup == null ? backupModeFromEnvironment() : String(opts.backup);
  return mode === "numbered" || mode === "t";
}

export async function cpShouldBackupDestination(target) {
  const info = await lstat(target).catch(() => null);
  return Boolean(info && !info.isDirectory());
}

export async function cpPrepareParentDirectories(src, destRoot, noDereference, preserveMode, preserveMetadata = false, securityOptions = {}) {
  if (noDereference) await lstat(src);
  else await stat(src);
  const parentRel = pathDirname(cpParentPath(src));
  if (parentRel === "." || parentRel === "") return [];
  const parentParts = parentRel.split("/").filter(Boolean);
  let destPrefix = destRoot;
  let srcPrefix = isAbsolute(src) ? "/" : "";
  const restores = [];
  for (const part of parentParts) {
    destPrefix = join(destPrefix, part);
    srcPrefix = srcPrefix === "" || srcPrefix === "/" ? `${srcPrefix}${part}` : join(srcPrefix, part);
    const mode = preserveMode ? (await stat(srcPrefix).catch(() => null))?.mode & 0o7777 : undefined;
    const createMode = mode != null ? cpRestrictiveDirectoryMode(mode) : preserveMetadata ? 0o700 : 0o755;
    const exists = Boolean(await lstat(destPrefix).catch(() => null));
    if (!exists) {
      if (securityOptions.preserveContextAtCreation) {
        await withPreservedCopyCreationContext(srcPrefix, securityOptions, () => mkdirSync(destPrefix, { mode: createMode }));
      } else {
        await mkdir(destPrefix, { recursive: false, mode: createMode });
      }
    }
    if (mode !== undefined || securityOptions.preserveContext) {
      restores.push({ path: destPrefix, mode, sourcePath: securityOptions.preserveContext ? srcPrefix : null });
    }
  }
  return restores;
}

export function cpPreserveMode(opts) {
  if (opts.preserve != null) {
    const attrs = opts.preserve === true ? ["mode", "ownership", "timestamps"] : String(opts.preserve).split(",");
    if (attrs.includes("all") || attrs.includes("mode")) return true;
  }
  if (opts.p || opts.a || opts.archive) return !cpNoPreserveAttrs(opts).includes("mode");
  return false;
}

export const CP_PRESERVE_VALID_ARGUMENTS = [
  ["mode"],
  ["timestamps"],
  ["ownership"],
  ["links"],
  ["context"],
  ["xattr"],
  ["all"],
];

export function validateCpPreserveOptions(opts) {
  if (opts.preserve != null && opts.preserve !== true) {
    validateCpPreserveList(String(opts.preserve), "--preserve");
  }
  if (opts["no-preserve"] != null) validateCpPreserveList(String(opts["no-preserve"]), "--no-preserve");
}

export function cpSecurityContextOptions(opts, selinuxEnabled) {
  const noPreserve = cpNoPreserveAttrs(opts);
  const preserveAttrs = opts.preserve == null || opts.preserve === true ? [] : String(opts.preserve).split(",");
  const explicitlyRequired = preserveAttrs.includes("context");
  if (explicitlyRequired && !selinuxEnabled) throw new InvocationError("cannot preserve security context without an SELinux-enabled kernel", 1, false);
  const requestedDefault = Boolean(opts.Z || opts.context === true);
  const explicitContext = typeof opts.context === "string" && selinuxEnabled ? opts.context : null;
  if (explicitlyRequired && (requestedDefault || explicitContext != null)) {
    throw new InvocationError("cannot set target context and preserve it", 1, false);
  }
  const preserveRequested = !noPreserve.some((attr) => attr === "all" || attr === "context")
    && (opts.a || opts.archive || preserveAttrs.includes("all") || explicitlyRequired);
  const preserveContext = selinuxEnabled && preserveRequested && !requestedDefault && explicitContext == null;
  const restoreContext = selinuxEnabled && requestedDefault;
  return {
    selinuxEnabled,
    preserveContext,
    // GNU creates with the source label before applying a pathname default.
    // If no default is defined, -Z therefore retains the source label.
    preserveContextAtCreation: preserveContext || restoreContext,
    requirePreserveContext: explicitlyRequired,
    restoreContext,
    explicitContext,
  };
}

export function validateCpPreserveList(value, option) {
  const valid = new Set(["mode", "timestamps", "ownership", "link", "links", "context", "xattr", "all"]);
  const attrs = [];
  for (const attr of value.split(",")) {
    if (attr === "") throw new InvocationError(cpPreserveInvalidMessage(attr, option, "ambiguous"), 1, true);
    if (!valid.has(attr)) throw new InvocationError(cpPreserveInvalidMessage(attr, option, "invalid"), 1, true);
    attrs.push(attr);
  }
  return attrs;
}

export function cpPreserveInvalidMessage(value, option, kind) {
  return `${kind} argument ${localeQuotedEscapedDiagnostic(value)} for ${localeQuotedDiagnostic(option)}\nValid arguments are:\n${CP_PRESERVE_VALID_ARGUMENTS.map((group) => `  - ${group.map(localeQuotedDiagnostic).join(", ")}`).join("\n")}`;
}

export function cpPreserveMetadata(opts) {
  const preserve = opts.p || opts.a || opts.archive || opts.preserve != null;
  if (!preserve) return false;
  if (cpNoPreserveAttrs(opts).includes("all")) return false;
  if (opts.preserve != null && !(opts.p || opts.a || opts.archive)) {
    const attrs = opts.preserve === true ? ["mode", "ownership", "timestamps"] : String(opts.preserve).split(",");
    return attrs.includes("all") || attrs.includes("ownership") || attrs.includes("timestamps");
  }
  return true;
}

export function cpNoPreserveAttrs(opts) {
  return String(opts["no-preserve"] ?? "").split(",").filter(Boolean);
}

export function cpPreserveLinks(opts) {
  if (cpNoPreserveAttrs(opts).includes("links")) return false;
  if (opts.d || opts.a || opts.archive) return true;
  if (opts.preserve == null) return false;
  const attrs = opts.preserve === true ? ["mode", "ownership", "timestamps"] : String(opts.preserve).split(",");
  return attrs.includes("all") || attrs.includes("links") || attrs.includes("link");
}

export function cpPreserveXattrs(opts) {
  if (cpNoPreserveAttrs(opts).some((attr) => attr === "all" || attr === "xattr")) return false;
  if (opts.a || opts.archive) return true;
  if (opts.preserve == null) return false;
  const attrs = opts.preserve === true ? ["mode", "ownership", "timestamps"] : String(opts.preserve).split(",");
  return attrs.includes("all") || attrs.includes("xattr");
}

export function cpRequirePreserveXattrs(opts) {
  if (opts.preserve == null || opts.preserve === true) return false;
  return String(opts.preserve).split(",").includes("xattr");
}

export function cpUseSourceModeForNewRegularFiles(opts) {
  return !cpNoPreserveAttrs(opts).some((attr) => attr === "all" || attr === "mode");
}

export function cpParents(opts) {
  return Boolean(opts.parents || opts.parent);
}

export function cpTargetPath(src, dest, useDirectoryTarget, opts) {
  if (cpParents(opts)) return join(dest, cpParentPath(src));
  return useDirectoryTarget ? join(dest, pathBasename(src.replace(/\/+$/, ""))) : dest;
}

export function rawCpPlan(opts, operands, useDirectoryTarget) {
  const raw = rawCommandArgs("cp");
  if (!raw) return null;
  const rawOperands = parseRawCpOperands(raw);
  if (rawOperands.length !== operands.length) return null;
  if (!rawOperands.some((operand, index) => rawPathNeedsBytes(operand) || !operand.equals(Buffer.from(operands[index])))) return null;
  if (useDirectoryTarget) return { sources: rawOperands.slice(0, -1), dest: rawOperands.at(-1), useDirectoryTarget: true };
  return { sources: rawOperands.slice(0, -1), dest: rawOperands.at(-1), useDirectoryTarget: false };
}

export function parseRawCpOperands(rawArgs) {
  const operands = [];
  for (let i = 0; i < rawArgs.length; i++) {
    const arg = rawArgs[i];
    if (arg.equals(Buffer.from("--"))) {
      operands.push(...rawArgs.slice(i + 1));
      break;
    }
    if (!rawArgLooksLikeOption(arg)) {
      operands.push(arg);
      continue;
    }
    const text = arg.toString();
    if (["-t", "--target-directory", "--target", "--suffix", "-S"].includes(text)) {
      i++;
      continue;
    }
    if (/^(--target-directory|--target|--suffix|--backup|--preserve|--no-preserve|--update|--reflink|--sparse)=/.test(text)) continue;
    if (arg.length > 2 && arg[0] === 0x2d) {
      const letters = arg.toString().slice(1);
      const valueIndex = [...letters].findIndex((ch) => ch === "t" || ch === "S");
      if (valueIndex !== -1 && valueIndex === letters.length - 1) i++;
    }
  }
  return operands;
}

export function rawCpTargetPath(src, dest, useDirectoryTarget, opts) {
  if (cpParents(opts)) return bufferPathJoin(dest, Buffer.from(cpParentPath(pathDisplayName(src))));
  if (rawPathEndsWithSlashDot(src)) return dest;
  return useDirectoryTarget ? bufferPathJoin(dest, bufferPathBasename(stripTrailingSlashBytes(src))) : dest;
}

export function rawPathEndsWithSlashDot(path) {
  return path.length >= 2 && path[path.length - 2] === 0x2f && path[path.length - 1] === 0x2e;
}

export function stripTrailingSlashBytes(path) {
  let end = path.length;
  while (end > 1 && path[end - 1] === 0x2f) end--;
  return path.subarray(0, end);
}

export function stripTrailingDotPathBytes(path) {
  if (path.length >= 2 && path[path.length - 2] === 0x2f && path[path.length - 1] === 0x2e) return path.subarray(0, path.length - 2);
  return path;
}

export function rawPathKey(path) {
  return Buffer.from(path).toString("hex");
}

export async function resolveRawExistingPath(path) {
  if (await lstat(path).then(() => true, () => false)) return path;
  const absolute = path[0] === 0x2f;
  const parts = Buffer.from(path).toString("binary").split("/").map((part) => Buffer.from(part, "binary"));
  let current = absolute ? Buffer.from("/") : Buffer.from(".");
  for (const part of parts) {
    if (!part.length || part.equals(Buffer.from("."))) continue;
    if (part.equals(Buffer.from(".."))) {
      current = pathLikeJoin(current, part);
      continue;
    }
    const entries = await readdirPathEntries(current);
    const display = part.toString();
    let match = entries.find((entry) => Buffer.from(entry).equals(part) || pathDisplayName(entry) === display);
    if (!match && rawPathNeedsBytes(part)) {
      const candidates = entries.filter((entry) => rawPathNeedsBytes(entry));
      if (candidates.length === 1) match = candidates[0];
    }
    if (!match) return path;
    current = pathLikeJoin(current, match);
  }
  return current;
}

export function cpParentPath(src) {
  const stripped = String(src).replace(/^\/+/, "").replace(/\/+$/, "");
  return stripped || ".";
}

export async function ensureCpNotIntoSelf(src, target, noDereference) {
  const sourceInfo = noDereference ? await lstat(src).catch(() => null) : await stat(src).catch(() => null);
  if (!sourceInfo?.isDirectory()) return;
  const sourcePath = resolve(pathDisplayName(src));
  const targetPath = resolve(pathDisplayName(target));
  if (targetPath === sourcePath || targetPath.startsWith(`${sourcePath}/`)) {
    throw new UsageError(`cannot copy a directory, '${pathDisplayName(src)}', into itself, '${pathDisplayName(target)}'`);
  }
}

export async function cpSameFileIsError(src, target, opts, backupRequested) {
  if (opts.s || opts["symbolic-link"]) return !await cpTargetIsReplaceableSymlink(src, target);
  if (opts.l || opts.link) return false;
  if (backupRequested && (opts.d || opts["no-dereference"])) {
    const srcInfo = await lstat(src).catch(() => null);
    if (srcInfo?.isSymbolicLink() && src !== target) return false;
  }
  if (backupRequested && (opts.f || opts.force)) {
    const srcInfo = await lstat(src).catch(() => null);
    if (srcInfo && !srcInfo.isSymbolicLink()) return false;
    if (await cpTargetIsSymlink(target)) return false;
  }
  if (backupRequested && resolve(src) !== resolve(target)) {
    const srcInfo = await lstat(src).catch(() => null);
    if (srcInfo && !srcInfo.isSymbolicLink()) return false;
    if (await cpTargetIsSymlink(target)) return false;
  }
  if ((opts["remove-destination"] || opts.rem) && resolve(src) !== resolve(target)) {
    const srcInfo = await lstat(src).catch(() => null);
    if (srcInfo && !srcInfo.isSymbolicLink()) return false;
    if (await cpTargetIsSymlink(target)) return false;
  }
  return true;
}

export async function cpSameFile(src, target, opts) {
  if (opts.d || opts.P || opts["no-dereference"]) {
    const [srcInfo, targetInfo] = await Promise.all([lstat(src).catch(() => null), lstat(target).catch(() => null)]);
    if (srcInfo?.isSymbolicLink() && targetInfo?.isSymbolicLink()) return resolve(src) === resolve(target) && src === target;
  }
  return areSameFile(src, target);
}

export function validateReflinkMode(program, mode) {
  if (["auto", "always", "never"].includes(mode)) return;
  const kind = mode === "" ? "ambiguous" : "invalid";
  throw new UsageError(`${kind} argument ${localeQuotedEscapedDiagnostic(mode)} for ${localeQuotedDiagnostic("--reflink")}\nValid arguments are:\n  - ${localeQuotedDiagnostic("auto")}\n  - ${localeQuotedDiagnostic("always")}\n  - ${localeQuotedDiagnostic("never")}`, true);
}

export function validateSparseMode(program, mode) {
  if (["never", "auto", "always"].includes(mode)) return;
  const kind = mode === "" ? "ambiguous" : "invalid";
  throw new UsageError(`${kind} argument ${localeQuotedEscapedDiagnostic(mode)} for ${localeQuotedDiagnostic("--sparse")}\nValid arguments are:\n  - ${localeQuotedDiagnostic("never")}\n  - ${localeQuotedDiagnostic("auto")}\n  - ${localeQuotedDiagnostic("always")}`, true);
}

export async function cpHardLinkSameFileIsNoop(src, target, opts, backupRequested) {
  if (backupRequested && (opts.f || opts.force) && resolve(src) === resolve(target)) return false;
  const targetInfo = await lstat(target).catch(() => null);
  return Boolean(targetInfo && !targetInfo.isSymbolicLink());
}

export async function cpTargetIsSymlink(target) {
  return Boolean((await lstat(target).catch(() => null))?.isSymbolicLink());
}

export async function cpTargetIsReplaceableSymlink(src, target) {
  if (resolve(src) === resolve(target)) return false;
  return cpTargetIsSymlink(target);
}

export async function areSamePathEntry(a, b) {
  const [as, bs] = await Promise.all([lstat(a).catch(() => null), lstat(b).catch(() => null)]);
  return Boolean(as && bs && areSamePathStats(as, bs));
}

export function areSamePathStats(a, b) {
  return a.dev === b.dev && a.ino === b.ino;
}

const singleCall = defineCommand("cp", cpCmd, cpMetaOption);
export default singleCall;

if (import.meta.main) await runAsMain(singleCall);