Boost logo

Boost Users :

From: Allen Cronce (allenslists_at_[hidden])
Date: 2008-08-10 12:31:34


Hi all,

I need to programmatically test for permissions errors during path
iteration. I think what I need to do is catch the filesystem
exception, then drill out the error code and compare it to the
permission_denied error value.

Here's a code fragment written under boost 1.35 that demonstrates what
I'm talking about:

        try
        {
                // Force a permissions error on *nix machines when not running as
root.
                // Note that this code is is for demonstration purposes only. Assume
that
                // in the real implementation I'm doing something else here with the
                // filesystem that *might* throw a permissions error.
                boost::filesystem::path testDirPath( "/usr/local/cant_touch_this" );
                create_directory(testDirPath);
        }
        catch(const boost::filesystem::filesystem_error& e)
        {
                // Check the error code to see if it's a permissions error
                if (boost::system::posix_error::permission_denied == e.code().value())
                {
                        std::cout << "The error code indicates a permissions error, as
expected. " << std::endl;
                }
                else
                {
                        std::cout << "Hmm... I didn't get a permissions error. That's
wrong." << std::endl;
                }
        }

Is this basically the right approach?

Note that one concern I have is maintainability. A coworker wrote
similar code to the above, but under boost 1.33.1. That code broke
under 1.35 due to significant changes to the filesystem exceptions
mechanism.

I understand that there are no guarantees moving forward to new boost
versions. I knew the job was dangerous when I took it ;-) But if
there's anything I can do today to future-proof a little bit, that
would be great.

Thanks in advance for any feedback or suggestions.

Best,

--
Allen Cronce

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net