# Boost :

From: Robert Mathews (rmathews_at_[hidden])
Date: 2005-03-14 14:58:43

Hi:

I was trying to do something simple with the boost::filesystem library on
Win32 && I noticed that it has trouble with UNC paths. I think I noticed 2
bugs with boost::filesystem::exists (possible patch attached). Finally, I
also had a question about what consistutes a "part" of a path.

Here's the context: I was trying to get all of the parts of a path, starting
at the front and moving to the end in order to find out at which point a
path was invalid. So, I wrote a path-checker that went something like this:

using namespace std;
namespace fs = boost::filesystem;

void check_path(const string iPath)
{
fs::path::default_name_check(fs::native);
fs::path aCurrentPath;
for(fs::path::iterator aItr1=iPath.begin(); aItr1!=iPath.end(); aItr1++)
{
aCurrentPath /= *aItr1;
bool aExists(fs::exists(aCurrentPath));
if(!aExists)
{
cout << "Hey, this was invalid path and it failed at
aCurrentPath" << endl;
return false;
}
}
return true;
}

The function works fine if you give it paths like "D:/foo/bar", but a UNC
path like \\computername\share\foo\bar doesn't work. The reason is that the
path::iterator returns the following results for the beginning of the UNC
path:
"//computername"
"/"
"share"
"/"
"foo"
... etc
and "//computername" is not a valid path.

1) Why is the separator "/" a separate part of the path? Shouldn't there be
a way to only get the directory parts of the path w/o the separators? Just
curious .. I couldn't find a reference to that in the discussion list.
2) boost::filesystem::exists("//computername") will return false, always.
That seems like the wrong answer, given that the path::iterator has returned
it as a part of the path. I notice that the filesystem code does otherwise
understand UNC paths otherwise.
3) boost::filesystem::exists("//computername/nofreelunch") will return true,
if even "nofreelunch" doesn't exist.

Looking at the implementation of filesystem::exists in boost
libs/filesystems/src/operations_posix_windows.cpp line 324, I'd like to
propose the following changes to fix cases 2 & 3.

To fix case 3 above, the error code return for an invalid share is not
of error codes to check. That should be uncontroversial.
To fix case 2 above, one must notice that you have a UNC path with 1
element - ie, a computer name. At the point you can either follow the hint
in the code and simply return true, or you can call a different WIN32
function to see if the server exists.

Attached is a proposed patch for both issues.

Thanks.

begin 666 operations_posix_windows.cpp.patch
M26YD97_at_Z(&]P97)A=&EO;G-?<&]S:7A?=VEN9&]W<RYC<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1R:6YG*"DN8U]S='(H*2 I(#T] M(#!X1D9&1D9&1D8I#0H@(" @(" @>PT*(" @(" @(" @(%5)3E0_at_97)R(#T@ M.CI'971,87-T17)R;W(H*3L-"BT@(" @(" @("!I9B_at_H97)R(#T]($524D]2
M7T9)3$5?3D]47T9/54Y$*2!\?" H97)R(#T]($524D]27TE.5D%,241?4$%2
M04U%5$52*2!\?" H97)R(#T]($524D]27U!!5$A?3D]47T9/54Y$*2!\?" H
M97)R(#T]($524D]27TE.5D%,241?3D%-12DI#0HK(" @(" @(" @:68H*&5R M<B ]/2!%4E)/4E]&24Q%7TY/5%]&3U5.1"D@?'P@*&5R<B ]/2!%4E)/4E]) M3E9!3$E$7U!!4D%-151%4BD@?'P@*&5R<B ]/2!%4E)/4E]0051(7TY/5%]& M3U5.1"D@?'P@*&5R<B ]/2!%4E)/4E])3E9!3$E$7TY!344I#0HK"0D)*&5R M<B ]/2!%4E)/4E]"041?3D544$%42"D-"BL)"2 @("D-"B @(" @(" @(" @
M("!R971U<FX_at_9F%L<V4[("\O($=E=$9I;&5!='1R:6)U=&5S(&9A:6QE9"!B
M96-A=7-E('1H92!P871H(&1O97,@;F]T(&5X:7-T#0H@(" @(" @(" @+R\@
M9F]R(&%N>2!O=&AE<B!E<G)O<B!W92!A<W-U;64@=&AE(&9I;&4_at_9&]E<R!E
M>&ES="!A;F0_at_9F%L;"!T:')O=6=H+ T*(" @(" @(" @("\O('1H:7,@;6%Y
M(&YO="!B92!T:&4_at_8F5S="!P;VQI8WD@=&AO=6=H+BXN(" H2DT@,C P-# S
%,S I#0H

end