From 9263cd32843f139942dc45f28b7d577dc033e05f Mon Sep 17 00:00:00 2001 From: Timofey Solomko Date: Sat, 22 Oct 2022 19:28:54 +0300 Subject: [PATCH] [swcomp] Correctly extract hard links from TAR containers Note that creating containers with hard links is still not supported (I do not know how to detect hard links with FileManager). --- Sources/swcomp/Containers/CommonFunctions.swift | 12 +++++++++++- Sources/swcomp/SwcompError.swift | 7 ++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/Sources/swcomp/Containers/CommonFunctions.swift b/Sources/swcomp/Containers/CommonFunctions.swift index a44a395c..0822fb06 100644 --- a/Sources/swcomp/Containers/CommonFunctions.swift +++ b/Sources/swcomp/Containers/CommonFunctions.swift @@ -106,7 +106,17 @@ func writeFile(_ entry: T, _ outputURL: URL, _ verbose: Bool) print("l: \(entryName) -> \(destinationPath!)") } try fileManager.createSymbolicLink(atPath: entryFullURL.path, withDestinationPath: destinationPath!) - // We cannot apply attributes to symbolic link. + // We cannot apply attributes to symbolic links. + return + } else if entry.info.type == .hardLink { + guard let destinationPath = (entry as? TarEntry)?.info.linkName + else { swcompExit(.containerHardLinkDestPath(entryName)) } + if verbose { + print("hl: \(entryName) -> \(destinationPath)") + } + // Note that the order of parameters is inversed for hard links. + try fileManager.linkItem(atPath: destinationPath, toPath: entryFullURL.path) + // We cannot apply attributes to hard links. return } else if entry.info.type == .regular { if verbose { diff --git a/Sources/swcomp/SwcompError.swift b/Sources/swcomp/SwcompError.swift index 671a7c7a..58127595 100644 --- a/Sources/swcomp/SwcompError.swift +++ b/Sources/swcomp/SwcompError.swift @@ -20,6 +20,7 @@ enum SwcompError { case benchmarkCannotGetSubcommandPathWindows case benchmarkCannotAppendToDirectory case containerSymLinkDestPath(String) + case containerHardLinkDestPath(String) case containerNoEntryData(String) case containerOutPathExistsNotDir case fileHandleCannotOpen @@ -55,6 +56,8 @@ enum SwcompError { return 207 case .containerSymLinkDestPath: return 301 + case .containerHardLinkDestPath: + return 311 case .containerNoEntryData: return 302 case .containerOutPathExistsNotDir: @@ -83,7 +86,7 @@ enum SwcompError { case .benchmarkSmallIterCount: return "Iteration count, if set, must be not less than 1." case .benchmarkUnknownCompResult: - return "Unknown comparison " + return "Unknown comparison." case .benchmarkCannotSetup(let benchmark, let input, let error): return "Unable to set up benchmark \(benchmark): input=\(input), error=\(error)." case .benchmarkCannotMeasure(let benchmark, let error): @@ -98,6 +101,8 @@ enum SwcompError { return "Cannot append results to the save path since it is a directory." case .containerSymLinkDestPath(let entryName): return "Unable to get destination path for symbolic link \(entryName)." + case .containerHardLinkDestPath(let entryName): + return "Unable to get destination path for hard link \(entryName)." case .containerNoEntryData(let entryName): return "Unable to get data for the entry \(entryName)." case .containerOutPathExistsNotDir: