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

Conversation

abertelrud
Copy link
Contributor

FileLock constructs the name of the lock file based on the path of the file being locked. When this path is very long, the name of the constructed lock file exceeds NAME_MAX (or more specifically the directory entry name length limit of the file system on which the directory that contains it resides, but NAME_MAX is a decent approximation on modern file systems commonly in use).

This change truncates it to NAME_MAX UTF-8 bytes (starting from the end), rounding down to avoid splitting Unicode scalars (but making no effort to avoid splitting clusters, since they don't affect the validity of the filename). Another possible refinement would be to also include a hash of the elided part of the filename — in the interest of avoiding complexity this implementation doesn't do that, and it's not clear that it's needed given that 255 (the common value of NAME_MAX) usually results in a unique enough name in any practical circumstances.

rdar://87471360

@abertelrud
Copy link
Contributor Author

@swift-ci please test

@abertelrud abertelrud self-assigned this Jan 12, 2022
@abertelrud abertelrud added ready Author believes the PR is ready to be merged & any feedback has been addressed and removed ready Author believes the PR is ready to be merged & any feedback has been addressed labels Jan 12, 2022
Copy link
Contributor

@tomerd tomerd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

…is long

FileLock constructs the name of the lock file based on the path of the file being locked.  When this path is very long, the name of the constructed lock file exceeds `NAME_MAX` (or more specifically the directory entry name length limit of the file system on which the directory that contains it resides, but `NAME_MAX` is a decent approximation on modern file systems commonly in use).

This change truncates it to `NAME_MAX` UTF-8 bytes (starting from the end), rounding down to avoid splitting Unicode scalars (but making no effort to avoid splitting clusters, since they don't affect the validity of the filename).  Another possible refinement would be to also include a hash of the elided part of the filename — in the interest of avoiding complexity this implementation doesn't do that, and it's not clear that it's needed given that 255 (the common value of `NAME_MAX`) usually results in a unique enough name in any practical circumstances.

rdar://87471360
@abertelrud abertelrud force-pushed the eng/87471360-long-filelock-names branch from b06347f to 2a2ba2a Compare January 12, 2022 23:45
@abertelrud
Copy link
Contributor Author

@swift-ci please test

@abertelrud
Copy link
Contributor Author

abertelrud commented Jan 13, 2022

Linux: No space left on device (being discussed in other fora, just mentioning it here as to why there's no successful build yet)

@abertelrud
Copy link
Contributor Author

@swift-ci please test linux

@abertelrud abertelrud merged commit bccbd93 into main Jan 13, 2022
@abertelrud abertelrud deleted the eng/87471360-long-filelock-names branch January 13, 2022 16:07
@compnerd
Copy link
Member

compnerd added a commit that referenced this pull request Jan 13, 2022
compnerd added a commit to compnerd/swift-tools-support-core that referenced this pull request Jan 13, 2022
This fixes multiple issues with the file locking implementation on
Windows.  `AbsolutePath.root` is meaningless on Windows - there is
no concept of a singular root on Windows, instead you have have 26
individual roots - A-Z.  Stripping the count of characters for the
root path leaves us with an invalid path string as the root drives
have been stripped.  The next character is an invalid character on
most file systems, and so we must replace `:`.  Instead of using a
`MAX_NAME` to compute a length of the ARC, enforce a limit for the
complete path (though an ARC would be limited to 255 codepoints on
NTFS).  This incidentally also corrects the emitted path, previous
path computation left us with the file in PWD.
compnerd added a commit to compnerd/swift-tools-support-core that referenced this pull request Jan 13, 2022
This fixes multiple issues with the file locking implementation on
Windows.  `AbsolutePath.root` is meaningless on Windows - there is
no concept of a singular root on Windows, instead you have have 26
individual roots - A-Z.  Stripping the count of characters for the
root path leaves us with an invalid path string as the root drives
have been stripped.  The next character is an invalid character on
most file systems, and so we must replace `:`.  Instead of using a
`MAX_NAME` to compute a length of the ARC, enforce a limit for the
complete path (though an ARC would be limited to 255 codepoints on
NTFS).  This incidentally also corrects the emitted path, previous
path computation left us with the file in PWD.
compnerd added a commit to compnerd/swift-tools-support-core that referenced this pull request Jan 13, 2022
This fixes multiple issues with the file locking implementation on
Windows.  `AbsolutePath.root` is meaningless on Windows - there is
no concept of a singular root on Windows, instead you have have 26
individual roots - A-Z.  Stripping the count of characters for the
root path leaves us with an invalid path string as the root drives
have been stripped.  The next character is an invalid character on
most file systems, and so we must replace `:`.  Instead of using a
`MAX_NAME` to compute a length of the ARC, enforce a limit for the
complete path (though an ARC would be limited to 255 codepoints on
NTFS).  This incidentally also corrects the emitted path, previous
path computation left us with the file in PWD.
compnerd added a commit to compnerd/swift-tools-support-core that referenced this pull request Jan 13, 2022
This fixes multiple issues with the file locking implementation on
Windows.  `AbsolutePath.root` is meaningless on Windows - there is
no concept of a singular root on Windows, instead you have have 26
individual roots - A-Z.  Stripping the count of characters for the
root path leaves us with an invalid path string as the root drives
have been stripped.  The next character is an invalid character on
most file systems, and so we must replace `:`.  Instead of using a
`MAX_NAME` to compute a length of the ARC, enforce a limit for the
complete path (though an ARC would be limited to 255 codepoints on
NTFS).  This incidentally also corrects the emitted path, previous
path computation left us with the file in PWD.
abertelrud pushed a commit that referenced this pull request Jan 13, 2022
This fixes multiple issues with the file locking implementation on
Windows.  `AbsolutePath.root` is meaningless on Windows - there is
no concept of a singular root on Windows, instead you have have 26
individual roots - A-Z.  Stripping the count of characters for the
root path leaves us with an invalid path string as the root drives
have been stripped.  The next character is an invalid character on
most file systems, and so we must replace `:`.  Instead of using a
`MAX_NAME` to compute a length of the ARC, enforce a limit for the
complete path (though an ARC would be limited to 255 codepoints on
NTFS).  This incidentally also corrects the emitted path, previous
path computation left us with the file in PWD.
compnerd added a commit that referenced this pull request Jan 14, 2022
TSCBasic: repair the Windows build after #277
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants