GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-82367: Use `FindFirstFile` Win32 API in `ntpath.realpath()` by moonsikpark · Pull Request #110298 · python/cpython

gh-82367: Use FindFirstFile Win32 API in ntpath.realpath() #110298

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.

Sign up for GitHub

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
zware merged 1 commit into python:main from moonsikpark:gh-82367-use-findfirstfile-in-nt-realpath
Oct 5, 2023

Uh oh!

There was an error while loading. Please reload this page.

Merged

gh-82367: Use FindFirstFile Win32 API in ntpath.realpath() #110298

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
1 commit
Select commit
2b5a29e
gh-82367: Use `FindFirstFile` Win32 API in `ntpath.realpath()` (#110298)
moonsikpark Oct 3, 2023
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

3 changes: 2 additions & 1 deletion 3 Doc/library/os.path.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,8 @@ the :mod:`glob` module.)

Return the canonical path of the specified filename, eliminating any symbolic
links encountered in the path (if they are supported by the operating
system).
system). On Windows, this function will also resolve MS-DOS (also called 8.3)
style names such as ``C:\\PROGRA~1`` to ``C:\\Program Files``.

If a path doesn't exist or a symlink loop is encountered, and *strict* is
``True``, :exc:`OSError` is raised. If *strict* is ``False``, the path is
Expand Down
3 changes: 3 additions & 0 deletions 3 Doc/whatsnew/3.13.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ Other Language Changes
if supported.
(Contributed by Victor Stinner in :gh:`109649`.)

* :func:`os.path.realpath` now resolves MS-DOS style file names even if
the file is not accessible.
(Contributed by Moonsik Park in :gh:`82367`.)

New Modules
===========
Expand Down
16 changes: 10 additions & 6 deletions 16 Lib/ntpath.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import genericpath
from genericpath import *


__all__ = ["normcase","isabs","join","splitdrive","splitroot","split","splitext",
"basename","dirname","commonprefix","getsize","getmtime",
"getatime","getctime", "islink","exists","lexists","isdir","isfile",
Expand Down Expand Up @@ -601,7 +600,7 @@ def abspath(path):
return _abspath_fallback(path)

try:
from nt import _getfinalpathname, readlink as _nt_readlink
from nt import _findfirstfile, _getfinalpathname, readlink as _nt_readlink
except ImportError:
# realpath is a no-op on systems without _getfinalpathname support.
realpath = abspath
Expand Down Expand Up @@ -688,10 +687,15 @@ def _getfinalpathname_nonstrict(path):
except OSError:
# If we fail to readlink(), let's keep traversing
pass
path, name = split(path)
# TODO (bpo-38186): Request the real file name from the directory
# entry using FindFirstFileW. For now, we will return the path
# as best we have it
# If we get these errors, try to get the real name of the file without accessing it.
if ex.winerror in (1, 5, 32, 50, 87, 1920, 1921):
try:
name = _findfirstfile(path)
path, _ = split(path)
except OSError:
path, name = split(path)
else:
path, name = split(path)
if path and not name:
return path + tail
tail = join(name, tail) if tail else name
Expand Down
43 changes: 43 additions & 0 deletions 43 Lib/test/test_ntpath.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import ntpath
import os
import string
import subprocess
import sys
import unittest
import warnings
Expand Down Expand Up @@ -637,6 +638,48 @@ def test_realpath_cwd(self):
with os_helper.change_cwd(test_dir_short):
self.assertPathEqual(test_file_long, ntpath.realpath("file.txt"))

@unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
def test_realpath_permission(self):
# Test whether python can resolve the real filename of a
# shortened file name even if it does not have permission to access it.
ABSTFN = ntpath.realpath(os_helper.TESTFN)

os_helper.unlink(ABSTFN)
os_helper.rmtree(ABSTFN)
os.mkdir(ABSTFN)
self.addCleanup(os_helper.rmtree, ABSTFN)

test_file = ntpath.join(ABSTFN, "LongFileName123.txt")
test_file_short = ntpath.join(ABSTFN, "LONGFI~1.TXT")

with open(test_file, "wb") as f:
f.write(b"content")
Comment thread
moonsikpark marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

# Automatic generation of short names may be disabled on
# NTFS volumes for the sake of performance.
# They're not supported at all on ReFS and exFAT.
subprocess.run(
# Try to set the short name manually.
['fsutil.exe', 'file', 'setShortName', test_file, 'LONGFI~1.TXT'],
creationflags=subprocess.DETACHED_PROCESS
)

try:
self.assertPathEqual(test_file, ntpath.realpath(test_file_short))
except AssertionError:
raise unittest.SkipTest('the filesystem seems to lack support for short filenames')

# Deny the right to [S]YNCHRONIZE on the file to
# force nt._getfinalpathname to fail with ERROR_ACCESS_DENIED.
p = subprocess.run(
['icacls.exe', test_file, '/deny', '*S-1-5-32-545:(S)'],
creationflags=subprocess.DETACHED_PROCESS
)

if p.returncode:
raise unittest.SkipTest('failed to deny access to the test file')

self.assertPathEqual(test_file, ntpath.realpath(test_file_short))

def test_expandvars(self):
with os_helper.EnvironmentVarGuard() as env:
env.clear()
Expand Down
1 change: 1 addition & 0 deletions 1 Misc/ACKS
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1373,6 +1373,7 @@ Peter Parente
Alexandre Parenteau
Dan Parisien
HyeSoo Park
Moonsik Park
William Park
Claude Paroz
Heikki Partanen
Expand Down
2 changes: 2 additions & 0 deletions 2 Misc/NEWS.d/next/Windows/2023-10-03-12-30-59.gh-issue-82367.nxwfMx.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:func:`os.path.realpath` now resolves MS-DOS style file names even if
the file is not accessible. Patch by Moonsik Park.
40 changes: 39 additions & 1 deletion 40 Modules/clinic/posixmodule.c.h
Show comments
View file
Open in desktop

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

32 changes: 32 additions & 0 deletions 32 Modules/posixmodule.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -4809,6 +4809,37 @@ os__getfinalpathname_impl(PyObject *module, path_t *path)
return result;
}

/*[clinic input]
os._findfirstfile
path: path_t
/
A function to get the real file name without accessing the file in Windows.
[clinic start generated code]*/

static PyObject *
os__findfirstfile_impl(PyObject *module, path_t *path)
/*[clinic end generated code: output=106dd3f0779c83dd input=0734dff70f60e1a8]*/
{
PyObject *result;
HANDLE hFindFile;
WIN32_FIND_DATAW wFileData;
WCHAR *wRealFileName;

Py_BEGIN_ALLOW_THREADS
hFindFile = FindFirstFileW(path->wide, &wFileData);
Py_END_ALLOW_THREADS

if (hFindFile == INVALID_HANDLE_VALUE) {
path_error(path);
return NULL;
}

wRealFileName = wFileData.cFileName;
result = PyUnicode_FromWideChar(wRealFileName, wcslen(wRealFileName));
FindClose(hFindFile);
return result;
}


/*[clinic input]
os._getvolumepathname
Expand Down Expand Up @@ -15961,6 +15992,7 @@ static PyMethodDef posix_methods[] = {
OS__GETFULLPATHNAME_METHODDEF
OS__GETDISKUSAGE_METHODDEF
OS__GETFINALPATHNAME_METHODDEF
OS__FINDFIRSTFILE_METHODDEF
OS__GETVOLUMEPATHNAME_METHODDEF
OS__PATH_SPLITROOT_METHODDEF
OS__PATH_NORMPATH_METHODDEF
Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.