Skip to content
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

Path::file_stem() does not work with directories that contain dots #133399

Open
hacknus opened this issue Nov 23, 2024 · 5 comments
Open

Path::file_stem() does not work with directories that contain dots #133399

hacknus opened this issue Nov 23, 2024 · 5 comments
Labels
A-filesystem Area: `std::fs` A-io Area: `std::io`, `std::fs`, `std::net` and `std::path` C-discussion Category: Discussion or questions that doesn't represent real issues. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@hacknus
Copy link

hacknus commented Nov 23, 2024

I'm dealing with directories that contain dots in the name:

let path = Path::new("2024.11.23_directory");
dbg!(path.file_stem());

I expected it to just return the pure filename, since it does not have an extension/suffix.
"2024.11.23_directory"

Instead, it just returns whatever is before the last dot:
"2024.11"

Meta

rustc --version --verbose:

rustc 1.82.0 (f6e511eec 2024-10-15)
binary: rustc
commit-hash: f6e511eec7342f59a25f7c0534f1dbea00d01b14
commit-date: 2024-10-15
host: aarch64-apple-darwin
release: 1.82.0
LLVM version: 19.1.1

This happens in the source code in path.rs where:

   pub fn file_stem(&self) -> Option<&OsStr> {
        self.file_name().map(rsplit_file_at_dot).and_then(|(before, after)| before.or(after))
    }

This clearly shows that it just splits the filename at the last dot, regardless if it is a directory or a file.

@hacknus hacknus added the C-bug Category: This is a bug. label Nov 23, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Nov 23, 2024
@ryanavella
Copy link

ryanavella commented Nov 23, 2024

This is likely (?) intentional behavior. Some devs operate on abstract paths that don't necessarily exist on a mounted filesystem. It is also worth noting that checking if a path is a directory would introduce a new syscall to Path::file_stem.

@hacknus
Copy link
Author

hacknus commented Nov 23, 2024

@ryanavella I disagree. A directory does not have an extension, so we cannot return a file stem (or extension for that matter). I see that an additional syscall is required for my suggestion, but I don't see the use of the file_stem() function if it does not handle that.

For abstract paths, which do not exist on the filesystem, the is_dir() method returns false, so the behavior wouldn't change.

@ryanavella
Copy link

ryanavella commented Nov 23, 2024

For abstract paths, which do not exist on the filesystem, the is_dir() method returns false, so the behavior wouldn't change.

The problem is if a process calls Path::file_stem on an abstract path, and another process has coincidentally created a directory with the same name so that it now exists. For these abstract paths, Path::file_stem on the same Path could return different results depending on filesystem state.

@rvolgers
Copy link

Some systems do use extensions with directories. For example, on OSX, apps are distributed as app bundles, which are just directories with the extension ".app".

@hacknus
Copy link
Author

hacknus commented Nov 24, 2024

Some systems do use extensions with directories. For example, on OSX, apps are distributed as app bundles, which are just directories with the extension ".app".

Fair point. Yes.

For abstract paths, which do not exist on the filesystem, the is_dir() method returns false, so the behavior wouldn't change.

The problem is if a process calls Path::file_stem on an abstract path, and another process has coincidentally created a directory with the same name so that it now exists. For these abstract paths, Path::file_stem on the same Path could return different results depending on filesystem state.

I see..

Python's pathlib also handles it the same way.

However, I still think that cases where there are dots occurring in the name of a directory, the behavior of the std::path library is not useable. But a solution is not as straight forward as I thought..

@jieyouxu jieyouxu added T-libs Relevant to the library team, which will review and decide on the PR/issue. C-discussion Category: Discussion or questions that doesn't represent real issues. A-filesystem Area: `std::fs` A-io Area: `std::io`, `std::fs`, `std::net` and `std::path` and removed C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Nov 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-filesystem Area: `std::fs` A-io Area: `std::io`, `std::fs`, `std::net` and `std::path` C-discussion Category: Discussion or questions that doesn't represent real issues. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants