Skip to content

Commit

Permalink
Merge pull request #23 from mlocati/set-content-atomic-v2
Browse files Browse the repository at this point in the history
Make Set-Content atomic and use exclusive access
  • Loading branch information
mlocati authored Jan 18, 2019
2 parents 26151e6 + 0582104 commit 35428b1
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion PhpManager/private/Set-PhpIniLine.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,27 @@
if (Test-Path -Path $Path -PathType Container) {
$Path = [System.IO.Path]::Combine($Path, 'php.ini')
}
Set-Content -Path $Path -Value $Lines
for ($i = 1; ; $i++) {
try {
$stream = [System.IO.File]::Open($Path, 'Create', 'Write', 'Read')
break
} catch {
if ($i -ge 3) {
throw
}
}
}
$contents = $Lines | Out-String
try {
$writer = New-Object System.IO.StreamWriter($stream)
try {
$writer.Write($contents)
} finally {
$writer.Dispose()
}
} finally {
$stream.Dispose()
}
}
end {
}
Expand Down

0 comments on commit 35428b1

Please sign in to comment.