c++ - Why does realpath() return error EEXIST? -
my program running in linux environment, compiled gcc version 4.4.7.
i using realpath()
"canonicalize" file paths. path of every directory , file feed realpath()
exists, of course essential realpath()
work properly.
however, realpath()
fail error code 17, name eexist
, string description "file exists".
that baffles me. of course exists, scream @ realpath()
. realpath()
unmoved ranting.
documentation realpath()
@ http://pubs.opengroup.org/onlinepubs/009695399/functions/realpath.html lists errors cause fail, eexist
not 1 of them.
why realpath()
fail in way?
examples of directory , file paths cause eexist
error:
- an absolute path directory:
/alpha/bravo/charlie/delta
- an absolute path file:
/alpha/bravo/charlie/foo.txt
- a relative path file:
../../charlie/foo.txt
- a path file has dot in it:
/alpha/bravo/charlie/./foo.txt
but examples not definitive, because other files exact same patterns, , in same directories, succeeed.
there not seem rhyme or reason directory or file cause eexist
error. error typically happens first file path try canonicalize, , not subsequent ones. however, cannot kludge around merely trying canonicalize first file again; error keep happening it.
program snippet:
#include <string> #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <limits.h> // path_max using std; string pathcanonicalize( string const & path ) { string result; char szresult[ path_max ]; ::realpath( path.c_str(), szresult ); if ( errno == eexist ) { // why? cerr << "realpath: error code " << errno << ", " << ::strerror( errno ) << ": '" << path << "'. of course file exists!" << endl; result = path; } else if ( errno ) { cerr << "realpath: error code " << errno << ", " << ::strerror( errno ) << ": '" << path << "'" << endl; result = path; } else { result = szresult; } return result; }
you should never, ever check errno
without specific reason.
perhaps whatever internal operation realpath
happened last failed eexist
. or maybe errno
happened eexist
previous operation failed , realpath
didn't change it.
if didn't cause realpath
fail, why care?
from own link:
upon successful completion, realpath() shall return pointer resolved name. otherwise, realpath() shall return null pointer , set errno indicate error, , contents of buffer pointed resolved_name undefined.
notice doesn't errno
set in particular if realpath
succeeds. why checking errno
before checking if realpath
succeeded?
Comments
Post a Comment