Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -199,33 +199,40 @@ private struct BrowserStackCLIDownloader {
return BrowserStackCLIArtifact(version: info.version, executableURL: expectedExecutableURL)
}

if fileManager.fileExists(atPath: versionDirectory.path) {
try fileManager.removeItem(at: versionDirectory)
}
try fileManager.createDirectory(at: versionDirectory, withIntermediateDirectories: true)

Diagnostics.remark("BrowserStackAccessibilityLint: Downloading CLI \(info.version)...")

// Download into a temporary directory to avoid TOCTOU races
let tempDirectory = cacheRoot.appendingPathComponent(".download-\(UUID().uuidString)", isDirectory: true)
try fileManager.createDirectory(at: tempDirectory, withIntermediateDirectories: true)
defer { try? fileManager.removeItem(at: tempDirectory) }

#if os(Windows)
let archiveURL = versionDirectory.appendingPathComponent("browserstack-cli.zip")
let archiveURL = tempDirectory.appendingPathComponent("browserstack-cli.zip")
try await download(from: info.resolvedURL, to: archiveURL)
Diagnostics.remark("BrowserStackAccessibilityLint: Extracting CLI \(info.version)...")
try unzip(archive: archiveURL, into: versionDirectory)
try unzip(archive: archiveURL, into: tempDirectory)
try? fileManager.removeItem(at: archiveURL)
#else
try extractWithBsdtar(from: info.resolvedURL, into: versionDirectory)
try extractWithBsdtar(from: info.resolvedURL, into: tempDirectory)
#endif

let locatedBinary = try locateExecutable(in: versionDirectory, preferredName: executableName)
let finalBinaryURL: URL
if locatedBinary.lastPathComponent == executableName {
finalBinaryURL = locatedBinary
} else {
finalBinaryURL = expectedExecutableURL
if fileManager.fileExists(atPath: finalBinaryURL.path) {
try fileManager.removeItem(at: finalBinaryURL)
let locatedBinary = try locateExecutable(in: tempDirectory, preferredName: executableName)

// Atomically swap: remove old version dir, move temp into place
if fileManager.fileExists(atPath: versionDirectory.path) {
try fileManager.removeItem(at: versionDirectory)
}
try fileManager.moveItem(at: tempDirectory, to: versionDirectory)

let finalBinaryURL = versionDirectory.appendingPathComponent(locatedBinary.lastPathComponent, isDirectory: false)
if locatedBinary.lastPathComponent != executableName {
let expectedURL = versionDirectory.appendingPathComponent(executableName, isDirectory: false)
if fileManager.fileExists(atPath: expectedURL.path) {
try fileManager.removeItem(at: expectedURL)
}
try fileManager.moveItem(at: locatedBinary, to: finalBinaryURL)
try fileManager.moveItem(at: finalBinaryURL, to: expectedURL)
try ensureExecutablePermissions(at: expectedURL)
return BrowserStackCLIArtifact(version: info.version, executableURL: expectedURL)
}

try ensureExecutablePermissions(at: finalBinaryURL)
Expand Down
Loading