Skip to content

FileLock encounters a runtime error when the path of the locked file is long #277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 13, 2022
Merged
Show file tree
Hide file tree
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
17 changes: 13 additions & 4 deletions Sources/TSCBasic/Lock.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
This source file is part of the Swift.org open source project

Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See http://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -178,12 +178,21 @@ public final class FileLock {
throw FileSystemError(.notDirectory, lockFilesDirectory)
}
// use the parent path to generate unique filename in temp
var lockFileName = (resolveSymlinks(fileToLock.parentDirectory).appending(component: fileToLock.basename)).components.joined(separator: "_")
var lockFileName = (resolveSymlinks(fileToLock.parentDirectory).appending(component: fileToLock.basename)).components.joined(separator: "_") + ".lock"
if lockFileName.hasPrefix(AbsolutePath.root.pathString) {
lockFileName = String(lockFileName.dropFirst(AbsolutePath.root.pathString.count))
}
let lockFilePath = lockFilesDirectory.appending(component: lockFileName + ".lock")

// back off until it occupies at most `NAME_MAX` UTF-8 bytes but without splitting scalars
// (we might split clusters but it's not worth the effort to keep them together as long as we get a valid file name)
var lockFileUTF8 = lockFileName.utf8.suffix(Int(NAME_MAX))
while String(lockFileUTF8) == nil {
// in practice this will only be a few iterations
lockFileUTF8 = lockFileUTF8.dropFirst()
}
// we will never end up with nil since we have ASCII characters at the end
lockFileName = String(lockFileUTF8) ?? lockFileName
let lockFilePath = lockFilesDirectory.appending(component: lockFileName)

let lock = FileLock(at: lockFilePath)
return try lock.withLock(type: type, body)
}
Expand Down
8 changes: 7 additions & 1 deletion Tests/TSCBasicTests/FileSystemTests.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
This source file is part of the Swift.org open source project

Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See http://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -767,6 +767,12 @@ class FileSystemTests: XCTestCase {
let lockFile = tempDir.appending(component: "lockfile")

try _testFileSystemFileLock(fileSystem: localFileSystem, fileA: fileA, fileB: fileB, lockFile: lockFile)

// Test some long and edge case paths. We arrange to split between the C and the Cedilla if NAME_MAX is 255.
let longEdgeCase1 = tempDir.appending(component: String(repeating: "Façade! ", count: Int(NAME_MAX)).decomposedStringWithCanonicalMapping)
try localFileSystem.withLock(on: longEdgeCase1, type: .exclusive, {})
let longEdgeCase2 = tempDir.appending(component: String(repeating: "🏁", count: Int(NAME_MAX)).decomposedStringWithCanonicalMapping)
try localFileSystem.withLock(on: longEdgeCase2, type: .exclusive, {})
}
}

Expand Down