[Python-Dev] Special file "nul" in Windows and os.stat

Roel Schroeven rschroev_nospam_ml at fastmail.fm
Tue Nov 6 21:52:00 CET 2007


Martin v. Löwis schreef:
>>> Sure - but what does stat then do when passed NUL?
>> AFAIK then it doesn't fill in the size and time fields of the structure 
>> (or sets them to a useless/invalid value).
>>
>> (See http://msdn2.microsoft.com/en-us/library/14h5k7ff(vs.71).aspx)
> 
> What specifically on that page tells me how the fields get filled
> for NUL? If you are referring to the "if path refers to a device..."
> sentence:

Yes, I was

> how does it determine that NUL is a device?

I'm not sure. I suppose it just calls GetFileSize() etc. The docs for 
GetFileSize() say "You cannot use the GetFileSize function with a handle 
of a nonseeking device such as a pipe or a communications device. To 
determine the file type for hFile, use the GetFileType function."

GetFileType() on its turn returns one of:

FILE_TYPE_UNKNOWN: The type of the specified file is unknown.
FILE_TYPE_DISK: The specified file is a disk file.
FILE_TYPE_CHAR: The specified file is a character file, typically an LPT 
device or a console.
FILE_TYPE_PIPE: The specified file is either a named or anonymous pipe.

But I don't know where it is specified which names refer to devices. I 
tried to query all device names with QueryDosDevice() (with a very 
simple C program; I can give you the code though I doubt it's useful), 
but that returns 208 names (on my system); much more than the LPT1, CON, 
NUL etc. It also includes stuff like, on my system, "Dritek_KB_Filter", 
"Sony Ericsson T610 Series Bluetooth (TM) Modem #2" etc. I've tried 
calling _stat() on those names and it returns -1 meaning "File not 
found". That behavior is clearly different from CON, NUL etc.

I thought I've seen the complete list on MSDN some time before, but I 
can't find it now.

>> See above: if stat() (_stat() actually) is called on NUL (or another 
>> device), I don't think it does anything useful with these fields.
> 
> You mean, it does nothing documented... AFAICT from the code, it always
> fills in something.

Yes, it returns 0xffffffff in the time fields and 0 in the size field 
(at least on my system, Windows XP SP1). I made another small C++ 
program to see what _stat() does (again, I can give you the code if you 
want).

With a normal file:

$ ./stat stat.cpp
gid     0
atime   1194169674 = 2007-11-04T10:47:54
ctime   1194167463 = 2007-11-04T10:11:03
dev     2
ino     0
mode    0x81b6
mtime   1194381734 = 2007-11-06T21:42:14
nlink   1
rdev    2
size    1342
uid     0

With a device:

$ ./stat NUL
gid     0
atime   4294967295 = invalid time
ctime   4294967295 = invalid time
dev     2
ino     0
mode    0x81b6
mtime   4294967295 = invalid time
nlink   1
rdev    2
size    0
uid     0

(The $ and ./ is because I ran the program from an msys shell)
(it says "invalid time" because localtime() returns NULL)

In summary, I'm afraid all of this doesn't really help very much...

-- 
The saddest aspect of life right now is that science gathers knowledge
faster than society gathers wisdom.
   -- Isaac Asimov

Roel Schroeven



More information about the Python-Dev mailing list