0

Currently, I have an application which I am porting from Linux to Windows. I would prefer to use boost where possible.

I currently have the following snippet which I want to port, which is pretty self-explanatory.

return access(backupFile.c_str(), R_OK) == 0;

The problem is that is seems like boost::filesystem does not have a direct equivalent. That is, I can do the following:

namespace fs = boost::filesystem;

if (!fs::is_regular_file(filename, ec))
    return false;

fs::file_status s = fs::status(filename);

// Which permissions should I be testing for?
if (s.permissions() == ???)

The permissions() enum can be found here. However, I as I read it, it is not a direct trananslation, as I would have to test to see if I am in the applicable group and if the group's permissions are also available.

Is there an easier way? Is my interpretation on the behavior correct?

(Of course, I can always attempt to open the file for reading, but that is not the goal of this question).

Damien
  • 785
  • 3
  • 8
  • 18

1 Answers1

1

Since you are porting to windows, according to Microsoft _access() second parameter is:

00 Existence only
02 Write-only
04 Read-only
06 Read and write

so, I think you should use boost permissions that match the above taking into account the following from the permissions reference link:

Windows: All permissions except write are currently ignored. There is only a single
write permission; setting write permission for owner, group, or others sets write
permission for all, and removing write permission for owner, group, or others removes
write permission for all.

Alternatively, you may want to continue using Windows version of access() (_access() for MS compilers) with the parameters Microsoft specifies.

Another class you may want to look into is ATL CAccessToken

DNT
  • 2,356
  • 14
  • 16
  • I haven't worked with CE for many years, I don't remember this one, but if late versions do not have it, there is also GetFileAttributes() API you may want to check :) – DNT May 22 '14 at 09:07
  • Done. Solves my problem. – Damien May 22 '14 at 09:11
  • In MSVC C Standard Library implementation, even today, `_access` and all its derivatives do not verify the `04 Read-only` access mode at all: look into its source code or just revoke read access from a file and check what `_access` returns. – Aleksey F. Nov 10 '21 at 21:44