Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Incompatible behaviour of "file exists"

Closed / Fixed3608360fffffffffffffffffffffffffffffffffCreated 14 years agoUpdated 14 years ago
Descriptionplain2013-03-18 08:28:19
There is an incompatibilty concerning "file exists"
in 8.6:

% puts $tcl_patchLevel
8.6.0
% glob *
x.tcl
% file exists *.tcl
1

Whereas in 8.5:
% puts $tcl_patchLevel
8.5.12
% glob *
x.tcl
% file exists *.tcl
0

platform: Win7_x64,
tcl-binaries: ActiveState.

This breaks backward compatibility.
dkfplain2013-03-18 16:59:07
Definitely a problem in implementation of NativeAccess() in tclWinFile.c (the generic layers over that do the right thing) but I really don't know the relevant API. Is this related to 2893771?  (Committed on 2009-11-24 in https://core.tcl.tk/tcl/info/02224b9ef68c so it fits with being an 8.6.* issue.) We're feeding a filename into FindFirstFile which is the directory entry enumerator, which in turn is the only thing that can do that sort of thing with a glob patter AIUI. My knowledge of the deep ins-and-outs of Win32 is too small for me to go further.

Assigning to someone who knows more about the relevant bits than me. Perhaps just testing for glob metacharacters in the name would be enough? I vaguely remember them as being always forbidden in filenames...
nijtmansplain2013-03-18 17:55:09
I will have a look. Thanks!
nijtmansplain2013-03-18 20:03:27
On UNIX (actually Cygwin):
$ tclsh8.5
% puts $tcl_patchLevel
8.5.13
% glob *.tcl
x.tcl
% file exists *.tcl
0

$ tclsh8.6
% puts $tcl_patchLevel
8.6.0
% glob *.tcl
x.tcl
% file exists *.tcl
0
% exit


Conclusion: the fact that using the '*' character this way
in [file exists] worked at all in win32 was an undocumented
feature and is non-portable. On UNIX, the '*' character can be
used in filenames like any other character, only it must be
escaped if the filename goes through a shell.
nijtmans Closed ticket [3608360fff]. [History diff]2013-03-18 20:03:45
nijtmansplain2013-03-18 20:13:17
Apparently, the fix is already backported to 8.4 and 8.5 as well:
======================================
>tclsh84
% puts $tcl_patchLevel
8.4.19
% glob *.tcl
x.tcl
% file exists *.tcl
0
% exit
>tclsh85
% puts $tcl_patchLevel
8.5.13
% glob *.tcl
x.tcl
% file exists *.tcl
0
% exit
dkfplain2013-03-18 20:44:46
I think you're misreading. This is (supposedly) a regression in 8.6 whereby, if 'x.tcl' exists in the current directory (but '*.tcl' does not), [file exists *.tcl] returns 1.

If there's a problem, it exists in that NativeAccess function. (I cannot confirm or deny; I don't have the right platform. I also don't know if the Cygwin build uses the same code; if not, it doesn't count.) I suspect that if there is a problem, it is inside there and related to the use of an API that does expansion. FindFirstFile is such a function, and it is definitely used in the NativeAccess code; the commit I indicated appears to be the one that introduced it. FindFirstFile is documented here: http://msdn.microsoft.com/en-us/library/windows/desktop/aa364418(v=vs.85).aspx

Moreover, I've confirmed that * is an illegal character in a filename on Windows. Search for "reserved characters" in http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx

At the very least, we could have a test for this! In fact, I'll write one. [file exists] definitely shouldn't ever do wildcard expansion behind the scenes on any platform.
nijtmansplain2013-03-18 21:03:13
> I think you're misreading. This is (supposedly) a regression in 8.6
> whereby, if 'x.tcl' exists in the current directory (but '*.tcl' does not),
> [file exists *.tcl] returns 1.
If it's a regression, then it's a regression in 8.5.13 as well, because
I cannot reproduce it any more. But I think we fully agree.
> At the very least, we could have a test for this! In fact, I'll write one.
> [file exists] definitely shouldn't ever do wildcard expansion behind the
> scenes on any platform.
I fully agree with that: [file exists] shouldn't do wildcard expansion
on any platform! That means: this change was not a regression,
it was a bug-fix. I welcome your testcase!
nijtmansplain2013-03-18 21:39:18
allow_comments - 1

Thanks, Donal, for your test-case! I tried it on win32 too, and it works fine.

Closing now.
nijtmansplain2013-03-18 21:39:29
allow_comments - 0
dkfplain2013-03-18 21:40:44
Test committed. If that fails on Windows with a non-Cygwin build, we know there's a serious problem. (I suspect this is all caused by the fact that you can't do an access()-equivalent check on a file easily without running into problems with locking. Which seems supremely stupid.)
dkf Pending ticket [3608360fff]. [History diff]2013-03-18 21:42:23
dkfplain2013-03-19 18:00:55
Uwe reports on comp.lang.tcl in message <[email protected]> that:


here my result with cmdAH.test from trunk
(ActiveState tcl8.6 binaries under win7_x64 as before):

>tclsh cmdAH.test


==== cmdAH-19.12 Bug 3608360: [file exists] mustn't do globbing FAILED
==== Contents of test case:

     list [file exists foo.bar] [file exists *.bar]

---- Result was:
1 1
---- Result should have been (exact matching):
1 0
==== cmdAH-19.12 FAILED

cmdAH.test:     Total   308     Passed  132     Skipped 175     Failed  1
Number of tests skipped for each constraint:
         4       nonPortable
         1       tempNotWin
         3       testchmod
         146     testsetplatform
         1       testvolumetype
         20      unix

-----
Summary: it's a real bug in that configuration.
dkfplain2013-03-19 18:50:18
See bug-3608360 for a possible fix. (Warning: untested!)
nijtmansplain2013-03-19 21:24:15
allow_comments - 1

Fixed now in all branches.
nijtmansplain2013-03-19 21:25:47
>Fixed now in all branches.
I mean  on trunk, of course. core-8-5-branch and earlier never had the bug.
Log in (anonymous is fine) to add a comment.