Skip to content

Commit 1c76d16

Browse files
committed
refactor: create_hlink
1 parent a3d6f79 commit 1c76d16

1 file changed

Lines changed: 20 additions & 5 deletions

File tree

os/win.ps1

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,28 @@ function create_hlink {
2121
[Parameter(Mandatory)][string]$target_path,
2222
[Parameter(Mandatory)][string]$source_path
2323
)
24+
25+
if (-not (Test-Path $source_path)) {
26+
log_error "Source path '$source_path' does not exist."
27+
return
28+
}
29+
30+
$target_dir = Split-Path $target_path -Parent
31+
if ($target_dir -and -not (Test-Path $target_dir)) {
32+
New-Item -ItemType Directory -Path $target_dir -Force | Out-Null
33+
}
34+
2435
if (Test-Path $target_path) {
25-
Remove-Item $target_path -Force
36+
Remove-Item $target_path -Force -Recurse
2637
}
27-
New-Item -ItemType HardLink -Path $target_path -Value $source_path -Force | Out-Null
28-
$item = Get-Item $target_path
29-
if ($item.LinkType -ne "HardLink") {
30-
log_error "File at $target_path is not a hardlink"
38+
39+
try {
40+
$item = New-Item -ItemType HardLink -Path $target_path -Value $source_path -Force -ErrorAction Stop
41+
if ($item.LinkType -ne "HardLink") {
42+
log_error "File at $target_path is not a hardlink"
43+
}
44+
} catch {
45+
log_error "Failed to create hardlink at '$target_path': $_"
3146
}
3247
}
3348

0 commit comments

Comments
 (0)