|
1 | 1 | /*
|
2 | 2 | This source file is part of the Swift.org open source project
|
3 | 3 |
|
4 |
| - Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors |
| 4 | + Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors |
5 | 5 | Licensed under Apache License v2.0 with Runtime Library Exception
|
6 | 6 |
|
7 | 7 | See http://swift.org/LICENSE.txt for license information
|
@@ -178,12 +178,21 @@ public final class FileLock {
|
178 | 178 | throw FileSystemError(.notDirectory, lockFilesDirectory)
|
179 | 179 | }
|
180 | 180 | // use the parent path to generate unique filename in temp
|
181 |
| - var lockFileName = (resolveSymlinks(fileToLock.parentDirectory).appending(component: fileToLock.basename)).components.joined(separator: "_") |
| 181 | + var lockFileName = (resolveSymlinks(fileToLock.parentDirectory).appending(component: fileToLock.basename)).components.joined(separator: "_") + ".lock" |
182 | 182 | if lockFileName.hasPrefix(AbsolutePath.root.pathString) {
|
183 | 183 | lockFileName = String(lockFileName.dropFirst(AbsolutePath.root.pathString.count))
|
184 | 184 | }
|
185 |
| - let lockFilePath = lockFilesDirectory.appending(component: lockFileName + ".lock") |
186 |
| - |
| 185 | + // back off until it occupies at most `NAME_MAX` UTF-8 bytes but without splitting scalars |
| 186 | + // (we might split clusters but it's not worth the effort to keep them together as long as we get a valid file name) |
| 187 | + var lockFileUTF8 = lockFileName.utf8.suffix(Int(NAME_MAX)) |
| 188 | + while String(lockFileUTF8) == nil { |
| 189 | + // in practice this will only be a few iterations |
| 190 | + lockFileUTF8 = lockFileUTF8.dropFirst() |
| 191 | + } |
| 192 | + // we will never end up with nil since we have ASCII characters at the end |
| 193 | + lockFileName = String(lockFileUTF8) ?? lockFileName |
| 194 | + let lockFilePath = lockFilesDirectory.appending(component: lockFileName) |
| 195 | + |
187 | 196 | let lock = FileLock(at: lockFilePath)
|
188 | 197 | return try lock.withLock(type: type, body)
|
189 | 198 | }
|
|
0 commit comments