-1

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.

Blacktempel
  • 3,935
  • 3
  • 29
  • 53
Garfield
  • 175
  • 7
  • 17

2 Answers2

2

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • I missed out a point in my question. When user specifies a location, I want him to know if this location is correct. Wrong location means, non-existence of path or non-write access to the directory. I want to intimate user that the path you have specified does not have write permission and you can change it. This is the reason I don't want to use CreateFile for checking It will be highly useful, if you can provide a sample code for checking permissions(using AccessCheck). I want to use this location for dumping data (text files, jpeg images etc). I need to know if path has write permission – Garfield Nov 15 '13 at 07:29
  • You can find lots of samples of using AccessCheck. You can just do websearch. No need for anyone here to repeat that. Anyway, I would not want to encourage what I feel is the wrong solution. – David Heffernan Nov 15 '13 at 07:36
  • There are valid use cases to check the permission before. For example validating the directories in a configuration when a service started, instead of failing later when the service wants create a file within them. – Calmarius Aug 05 '20 at 10:52
1

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

Domi
  • 22,151
  • 15
  • 92
  • 122