Question

I need to know if a specified directory (local or shared path with login credentials) has write permissions or not.

I am using GetFileAttributes but it always returns FILE_ATTRIBUTE_DIRECTORY and nothing else.

My code is something like below

if(storageLocation != "")
{
    //! check if local storage - user name password would be empty
    if(storageUsername == "" && storagePassword == "")
    {
        //! local storage
        //! lets check whether the local path is a valid path or not
        boost::filesystem::path fpath(storageUsername.c_str());
        if(boost::filesystem::exists(fpath))
        {
            DWORD attrib = ::GetFileAttributes(storageLocation.c_str());
            if((attrib != INVALID_FILE_ATTRIBUTES) && 
              ((attrib & FILE_ATTRIBUTE_READONLY) != FILE_ATTRIBUTE_READONLY))
            {
                string strWritePermission = "TRUE";
            }
        }
    }
    else
    {
        uncLocation_t uncLocation;
        uncLocation.m_location = storageLocation;
        uncLocation.m_username = storageUsername;
        uncLocation.m_password = storagePassword;
        if(0 == connectToUNCLocation(uncLocation)) // My function to connect to UNC location
        {
            //! successful connection
            DWORD attrib = ::GetFileAttributes(storageLocation.c_str());
            if((attrib != INVALID_FILE_ATTRIBUTES) && 
               ((attrib & FILE_ATTRIBUTE_READONLY) != FILE_ATTRIBUTE_READONLY))
            {
                string strWritePermission = "TRUE";
            }
        }
    }
}

I don't understand why but GetFileAttributes always return 0x16.

I have tested it by creating a shared folder and creating 2 folders in it. One with read only permissions and other with default permissions. But in all 3 cases (shared folder, read only folder and default permission folder) I am getting same return value.

There is on way to find write permission, to create a temporary file (usinf CreateFile in GENERIC_WRITE mode) and if successfully created, delete it. But I don't want to use this method as I don't want my application to create a temporary file each time user specifies a location.

Please suggest what should be done.

Was it helpful?

Solution

You would need to replicate the security checking that Windows performs. The AccessCheck function will help that. You are currently well wide of the mark in looking at the file attributes. Windows security is so much more complicated than that.

Although you said you did not want to do it, the right solution is not to try to check. Simply do whatever it is you are attempting to do. If the system decides that the user does not have sufficient rights, then CreateFile will fail, and the last error will be set to ERROR_ACCESS_DENIED. There's no need for temporary files. You just try to do whatever it is you are doing, and let it fail. You have to handle failure anyway since there are many ways for a file operation to fail, not just security.

As the saying goes, it is better to ask forgiveness than permission.

OTHER TIPS

I think you are looking for AccessCheck. FYI, this is not a C++ question, but a Windows API question.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top