@@ -1899,9 +1899,9 @@ extension Workspace {
1899
1899
// Download the artifacts
1900
1900
let downloadedArtifacts = try self . download ( artifactsToDownload, diagnostics: diagnostics)
1901
1901
artifactsToAdd. append ( contentsOf: downloadedArtifacts)
1902
-
1902
+
1903
1903
// Extract the local archived artifacts
1904
- let extractedLocalArtifacts = self . extract ( artifactsToExtract, diagnostics: diagnostics)
1904
+ let extractedLocalArtifacts = try self . extract ( artifactsToExtract, diagnostics: diagnostics)
1905
1905
artifactsToAdd. append ( contentsOf: extractedLocalArtifacts)
1906
1906
1907
1907
// Add the new artifacts
@@ -2028,16 +2028,7 @@ extension Workspace {
2028
2028
defer { group. leave ( ) }
2029
2029
2030
2030
let parentDirectory = self . location. artifactsDirectory. appending ( component: artifact. packageRef. name)
2031
- let tempExtractionDirectory = self . location. artifactsDirectory. appending ( components: " extract " , artifact. targetName)
2032
-
2033
- do {
2034
- try fileSystem. createDirectory ( parentDirectory, recursive: true )
2035
- if fileSystem. exists ( tempExtractionDirectory) {
2036
- try fileSystem. removeFileTree ( tempExtractionDirectory)
2037
- }
2038
- try fileSystem. createDirectory ( tempExtractionDirectory, recursive: true )
2039
- } catch {
2040
- tempDiagnostics. emit ( error)
2031
+ guard tempDiagnostics. wrap ( { try fileSystem. createDirectory ( parentDirectory, recursive: true ) } ) else {
2041
2032
continue
2042
2033
}
2043
2034
@@ -2064,12 +2055,19 @@ extension Workspace {
2064
2055
case . success:
2065
2056
let archiveChecksum = self . checksum ( forBinaryArtifactAt: archivePath, diagnostics: tempDiagnostics )
2066
2057
guard archiveChecksum == artifact. checksum else {
2067
- tempDiagnostics. emit (
2068
- . artifactInvalidChecksum( targetName: artifact. targetName, expectedChecksum: artifact. checksum, actualChecksum: archiveChecksum) )
2058
+ tempDiagnostics. emit ( . artifactInvalidChecksum( targetName: artifact. targetName, expectedChecksum: artifact. checksum, actualChecksum: archiveChecksum) )
2069
2059
tempDiagnostics. wrap { try self . fileSystem. removeFileTree ( archivePath) }
2070
2060
return
2071
2061
}
2072
2062
2063
+ guard let tempExtractionDirectory = tempDiagnostics. wrap ( { ( ) -> AbsolutePath in
2064
+ let path = self . location. artifactsDirectory. appending ( components: " extract " , artifact. packageRef. name, artifact. targetName, UUID ( ) . uuidString)
2065
+ try self . fileSystem. forceCreateDirectory ( at: path)
2066
+ return path
2067
+ } ) else {
2068
+ return
2069
+ }
2070
+
2073
2071
// TODO: Use the same extraction logic for both remote and local archived artifacts.
2074
2072
group. enter ( )
2075
2073
self . archiver. extract ( from: archivePath, to: tempExtractionDirectory, completion: { extractResult in
@@ -2079,17 +2077,19 @@ extension Workspace {
2079
2077
case . success:
2080
2078
var artifactPath : AbsolutePath ? = nil
2081
2079
tempDiagnostics. wrap {
2082
- // copy from temp location to actual location
2083
- let content = try self . fileSystem. getDirectoryContents ( tempExtractionDirectory)
2084
- for file in content {
2085
- let source = tempExtractionDirectory. appending ( component: file)
2086
- let destination = parentDirectory. appending ( component: file)
2087
- if self . fileSystem. exists ( destination) {
2088
- try self . fileSystem. removeFileTree ( destination)
2089
- }
2090
- try self . fileSystem. copy ( from: source, to: destination)
2091
- if destination. basenameWithoutExt == artifact. targetName {
2092
- artifactPath = destination
2080
+ try self . fileSystem. withLock ( on: parentDirectory, type: . exclusive) {
2081
+ // copy from temp location to actual location
2082
+ let content = try self . fileSystem. getDirectoryContents ( tempExtractionDirectory)
2083
+ for file in content {
2084
+ let source = tempExtractionDirectory. appending ( component: file)
2085
+ let destination = parentDirectory. appending ( component: file)
2086
+ if self . fileSystem. exists ( destination) {
2087
+ try self . fileSystem. removeFileTree ( destination)
2088
+ }
2089
+ try self . fileSystem. copy ( from: source, to: destination)
2090
+ if destination. basenameWithoutExt == artifact. targetName {
2091
+ artifactPath = destination
2092
+ }
2093
2093
}
2094
2094
}
2095
2095
// remove temp location
@@ -2134,23 +2134,16 @@ extension Workspace {
2134
2134
return result. map { $0 }
2135
2135
}
2136
2136
2137
- private func extract( _ artifacts: [ ManagedArtifact ] , diagnostics: DiagnosticsEngine ) -> [ ManagedArtifact ] {
2137
+ private func extract( _ artifacts: [ ManagedArtifact ] , diagnostics: DiagnosticsEngine ) throws -> [ ManagedArtifact ] {
2138
2138
let result = ThreadSafeArrayStore < ManagedArtifact > ( )
2139
2139
let group = DispatchGroup ( )
2140
2140
2141
2141
for artifact in artifacts {
2142
- let tempExtractionDirectory = self . location. artifactsDirectory. appending ( components: " extract " , artifact. targetName)
2143
2142
let destinationDirectory = self . location. artifactsDirectory. appending ( component: artifact. packageRef. name)
2143
+ try fileSystem. createDirectory ( destinationDirectory, recursive: true )
2144
2144
2145
- do {
2146
- try fileSystem. createDirectory ( destinationDirectory, recursive: true )
2147
- if fileSystem. exists ( tempExtractionDirectory) {
2148
- try fileSystem. removeFileTree ( tempExtractionDirectory)
2149
- }
2150
- try fileSystem. createDirectory ( tempExtractionDirectory, recursive: true )
2151
- } catch {
2152
- diagnostics. emit ( error)
2153
- }
2145
+ let tempExtractionDirectory = self . location. artifactsDirectory. appending ( components: " extract " , artifact. packageRef. name, artifact. targetName, UUID ( ) . uuidString)
2146
+ try self . fileSystem. forceCreateDirectory ( at: tempExtractionDirectory)
2154
2147
2155
2148
group. enter ( )
2156
2149
self . archiver. extract ( from: artifact. path, to: tempExtractionDirectory, completion: { extractResult in
0 commit comments