File names with slashes [was Re: error in os.chdir]

eryk sun eryksun at gmail.com
Sun Jul 1 12:30:05 EDT 2018


On Sun, Jul 1, 2018 at 8:51 AM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> On Sun, 01 Jul 2018 03:18:23 +0000, eryk sun wrote:
>
>> Bear in mind that forward slash is just a  name character in NT.
>
> So, using Python, how could you open, write to, and then read from, a
> file with a slash in its name? Say, something like:

By that I meant that NT's base object namespace treats forward slash
as a name character, i.e. object names in the base NT namespace can
include slash. This includes Device objects in the "\Device" directory
and "DOS" device symbolic links to them in "\Global??" or a
subdirectory of "\Sessions\0\DosDevices", as well as other named
objects (Section, Job, Event, Semaphore, etc). The Windows API stores
most of the latter in a single per-session directory that's named
"\BaseNamedOjects" for session 0 (system services) and
"\Sessions\<session number>\BaseNamedObjects" for interactive
sessions.

The base registry Key object is named "\Registry". Its parse procedure
(for creating and opening key paths) only reserves backslash, which is
the same as the object namespace itself.

>     spam/eggs

You can create a named pipe with that name in the named-pipe file
system. But regular file systems that I've used in Windows reserve
forward slash, even though they don't use it for anything. They'll
just fail the create/open with STATUS_OBJECT_NAME_INVALID. The way to
observe this is to use forward slash in a \\?\ prefixed path. Note
that the prefix must be \\?\, with backslash only. //?/ is basically
the same as //./.

As far as file paths go, "spam/eggs" could be set as a device name,
which from Windows would only be accessible with a \\?\ prefixed path.
For example:

    DefineDosDevice(DDD_RAW_TARGET_PATH, 'spam/eggs',
        '\\Device\\HarddiskVolume2\\Temp')

    f = open(r'\\?\spam/eggs\test.txt', 'w')
    h = msvcrt.get_osfhandle(f.fileno())

    >>> GetFinalPathNameByHandle(h, VOLUME_NAME_DOS)
    '\\\\?\\C:\\Temp\\test.txt'

    >>> GetFinalPathNameByHandle(h, VOLUME_NAME_NT)
    '\\Device\\HarddiskVolume2\\Temp\\test.txt'

> in your home directory. (Is that still C:\\My Documents by default?)

No, the user profile directory is "C:\Users\<username>", i.e. the
%UserProfile% environment variable. In some cases a user may have a
separate home directory configured, which is set in
%HOMEDRIVE%%HOMEPATH%. Commonly no home directory is set, in which
case %HOMEDRIVE%%HOMEPATH% is just the fixed %UserProfile% path.

> And how would that file be displayed in the Windows GUI file explorer?

I suppose if a file system allowed forward slash in names that
Explorer would just display it. When I get the time, I'll attach a
debugger to explorer.exe and hack a forward slash into one of the
names in a directory listing, i.e. a result from WinAPI FindFirstFile
(or native NtQueryDirectoryFile).



More information about the Python-list mailing list