| @@ -425,6 +425,23 @@ func isPermissionError(err error) bool { |
| 425 | strings.Contains(msg, "operation not permitted") |
425 | strings.Contains(msg, "operation not permitted") |
| 426 | } |
426 | } |
| 427 | |
427 | |
| |
428 | // isCrossDeviceError reports rename/link failures when src and dest are on |
| |
429 | // different filesystems (e.g. /tmp tmpfs → ~/.local/bin on disk). |
| |
430 | func isCrossDeviceError(err error) bool { |
| |
431 | if err == nil { |
| |
432 | return false |
| |
433 | } |
| |
434 | var linkErr *os.LinkError |
| |
435 | if errors.As(err, &linkErr) { |
| |
436 | err = linkErr.Err |
| |
437 | } |
| |
438 | msg := strings.ToLower(err.Error()) |
| |
439 | return strings.Contains(msg, "cross-device") || |
| |
440 | strings.Contains(msg, "cross device") || |
| |
441 | strings.Contains(msg, "exdev") || |
| |
442 | strings.Contains(msg, "invalid cross-device link") |
| |
443 | } |
| |
444 | |
| 428 | func promptSudoPassword(prompt string) (string, error) { |
445 | func promptSudoPassword(prompt string) (string, error) { |
| 429 | if prompt == "" { |
446 | if prompt == "" { |
| 430 | prompt = "Sudo password: " |
447 | prompt = "Sudo password: " |
| @@ -469,31 +486,121 @@ func installExecutableWithSudo(src, dest string) error { |
| 469 | return nil |
486 | return nil |
| 470 | } |
487 | } |
| 471 | |
488 | |
| 472 | func replaceExecutable(tmpPath, executablePath string) error { |
489 | // createUpdateTempFile prefers a sibling of the executable (same FS as install |
| 473 | if runtime.GOOS == "windows" { |
490 | // path). Falls back to os.TempDir when the install directory is not writable. |
| 474 | oldPath := executablePath + ".old" |
491 | func createUpdateTempFile(executablePath, binaryName string) (string, *os.File, error) { |
| 475 | if err := os.Rename(executablePath, oldPath); err != nil { |
492 | dir := filepath.Dir(executablePath) |
| 476 | return fmt.Errorf("failed to rename old executable: %w", err) |
493 | // Try same directory first for atomic rename. |
| |
494 | if f, err := os.CreateTemp(dir, ".curd-download-*"); err == nil { |
| |
495 | return f.Name(), f, nil |
| |
496 | } |
| |
497 | // Fall back to system temp (may be cross-device; replaceExecutable handles that). |
| |
498 | name := "curd-update-" + binaryName |
| |
499 | if binaryName == "" { |
| |
500 | name = "curd-update-bin" |
| |
501 | } |
| |
502 | path := filepath.Join(os.TempDir(), name) |
| |
503 | f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0755) |
| |
504 | if err != nil { |
| |
505 | return "", nil, err |
| |
506 | } |
| |
507 | return path, f, nil |
| |
508 | } |
| |
509 | |
| |
510 | func copyFileReplace(src, dst string) error { |
| |
511 | in, err := os.Open(src) |
| |
512 | if err != nil { |
| |
513 | return err |
| |
514 | } |
| |
515 | defer in.Close() |
| |
516 | |
| |
517 | // Write to a sibling temp on the destination filesystem, then rename into place. |
| |
518 | dir := filepath.Dir(dst) |
| |
519 | tmp, err := os.CreateTemp(dir, ".curd-update-*") |
| |
520 | if err != nil { |
| |
521 | return err |
| |
522 | } |
| |
523 | tmpName := tmp.Name() |
| |
524 | cleanup := true |
| |
525 | defer func() { |
| |
526 | if cleanup { |
| |
527 | _ = os.Remove(tmpName) |
| 477 | } |
528 | } |
| 478 | if err := os.Rename(tmpPath, executablePath); err != nil { |
529 | }() |
| 479 | _ = os.Rename(oldPath, executablePath) |
530 | |
| 480 | return fmt.Errorf("failed to rename new executable: %w", err) |
531 | if err := tmp.Chmod(0755); err != nil { |
| |
532 | _ = tmp.Close() |
| |
533 | return err |
| |
534 | } |
| |
535 | if _, err := io.Copy(tmp, in); err != nil { |
| |
536 | _ = tmp.Close() |
| |
537 | return err |
| |
538 | } |
| |
539 | if err := tmp.Sync(); err != nil { |
| |
540 | _ = tmp.Close() |
| |
541 | return err |
| |
542 | } |
| |
543 | if err := tmp.Close(); err != nil { |
| |
544 | return err |
| |
545 | } |
| |
546 | |
| |
547 | // Atomic replace when possible (same directory = same filesystem). |
| |
548 | if err := os.Rename(tmpName, dst); err != nil { |
| |
549 | // Windows / busy binary: move dest aside first. |
| |
550 | oldPath := dst + ".old" |
| |
551 | _ = os.Remove(oldPath) |
| |
552 | if renOld := os.Rename(dst, oldPath); renOld != nil && !os.IsNotExist(renOld) { |
| |
553 | return renOld |
| |
554 | } |
| |
555 | if renNew := os.Rename(tmpName, dst); renNew != nil { |
| |
556 | _ = os.Rename(oldPath, dst) |
| |
557 | return renNew |
| 481 | } |
558 | } |
| 482 | _ = os.Remove(oldPath) |
559 | _ = os.Remove(oldPath) |
| 483 | return nil |
| |
| 484 | } |
560 | } |
| |
561 | cleanup = false |
| |
562 | return nil |
| |
563 | } |
| 485 | |
564 | |
| |
565 | // replaceExecutable installs the downloaded binary over the running executable. |
| |
566 | // Handles: same-FS rename, cross-device copy, busy binary swap, and sudo when needed. |
| |
567 | func replaceExecutable(tmpPath, executablePath string) error { |
| |
568 | // 1) Fast path: rename when both paths share a filesystem. |
| 486 | if err := os.Rename(tmpPath, executablePath); err == nil { |
569 | if err := os.Rename(tmpPath, executablePath); err == nil { |
| 487 | return nil |
570 | return nil |
| 488 | } else if !isPermissionError(err) { |
571 | } else if isPermissionError(err) { |
| 489 | return fmt.Errorf("failed to replace executable: %w", err) |
572 | updateUserMessage(GetGlobalConfig(), "Update needs elevated permissions to replace the installed binary.") |
| |
573 | if sudoErr := installExecutableWithSudo(tmpPath, executablePath); sudoErr != nil { |
| |
574 | return sudoErr |
| |
575 | } |
| |
576 | _ = os.Remove(tmpPath) |
| |
577 | return nil |
| |
578 | } else if !isCrossDeviceError(err) { |
| |
579 | // Unexpected rename failure — still try copy-into-place before giving up. |
| |
580 | Log(fmt.Sprintf("rename to %s failed (%v); trying copy replace", executablePath, err)) |
| 490 | } |
581 | } |
| 491 | |
582 | |
| 492 | // Permission denied — e.g. installed to /usr/local/bin. Ask for sudo. |
583 | // 2) Cross-device (or other rename failure): copy onto dest filesystem then swap. |
| 493 | updateUserMessage(GetGlobalConfig(), "Update needs elevated permissions to replace the installed binary.") |
584 | if err := copyFileReplace(tmpPath, executablePath); err == nil { |
| 494 | if err := installExecutableWithSudo(tmpPath, executablePath); err != nil { |
585 | _ = os.Remove(tmpPath) |
| 495 | return err |
586 | return nil |
| |
587 | } else if isPermissionError(err) { |
| |
588 | updateUserMessage(GetGlobalConfig(), "Update needs elevated permissions to replace the installed binary.") |
| |
589 | if sudoErr := installExecutableWithSudo(tmpPath, executablePath); sudoErr != nil { |
| |
590 | return fmt.Errorf("failed to replace executable: %w", sudoErr) |
| |
591 | } |
| |
592 | _ = os.Remove(tmpPath) |
| |
593 | return nil |
| |
594 | } else { |
| |
595 | // Last resort: sudo install from original temp (covers weird FS layouts). |
| |
596 | if isCrossDeviceError(err) || runtime.GOOS != "windows" { |
| |
597 | Log(fmt.Sprintf("copy replace failed (%v); trying sudo", err)) |
| |
598 | updateUserMessage(GetGlobalConfig(), "Update needs elevated permissions to replace the installed binary.") |
| |
599 | if sudoErr := installExecutableWithSudo(tmpPath, executablePath); sudoErr == nil { |
| |
600 | _ = os.Remove(tmpPath) |
| |
601 | return nil |
| |
602 | } |
| |
603 | } |
| |
604 | return fmt.Errorf("failed to replace executable: %w", err) |
| 496 | } |
605 | } |
| 497 | _ = os.Remove(tmpPath) |
| |
| 498 | return nil |
| |
| 499 | } |
606 | } |