3

On Linux, chmod can set the permissions of a given file to none, ie. no one can read/write/exec this file. (Wikipedia - Octal modes)

How to do the same with Qt?
I know there is QFile::setPermissions and enum QFile::Permission, but in the enum there is no value none.

Cœur
  • 37,241
  • 25
  • 195
  • 267
gpalex
  • 826
  • 1
  • 11
  • 27
  • 2
    Have you tried `QFile::setPermissions(0)`, this should mean "none". – jpo38 Sep 21 '15 at 15:49
  • 1
    Note: From the documentation of 'Permissions: "Warning: Because of differences in the platforms supported by Qt, the semantics of ReadUser, WriteUser and ExeUser are platform-dependent: On Unix, the rights of the owner of the file are returned and on Windows the rights of the current user are returned. This behavior might change in a future Qt version." –  Sep 21 '15 at 16:35

2 Answers2

4

QFile::setPermissions(0) clears all permissions as it does chmod on Linux.


Update for Windows

According to Qt QFile documentation:

Qt's understanding of file permissions is limited, which affects especially the QFile::setPermissions() function. On Windows, Qt will set only the legacy read-only flag, and that only when none of the Write* flags are passed. Qt does not manipulate access control lists (ACLs), which makes this function mostly useless for NTFS volumes. It may still be of use for USB sticks that use VFAT file systems. POSIX ACLs are not manipulated, either.

So, on Windows it is possible to set or unset legacy read-only flag using QFile::setPermissions(). Usage of that feature is also tricky. To set the read-only flag only some `Read* permission should be configured. However, to remove that flag a write permission should be added, for example:

// set Windows read-only file flag
file.setPermissions(QFile::ReadOther);
// remove Windows read-only file flag
file.setPermissions(QFile::ReadOther | QFile::WriteOther);

The last trick I found in the answer to Qt C++ remove a read only file in windows using

Community
  • 1
  • 1
Orest Hera
  • 6,706
  • 2
  • 21
  • 35
  • Check this out: http://meta.stackoverflow.com/questions/265675/is-it-ok-to-take-someone-elses-comment-and-post-it-as-your-own-answer ;-) – jpo38 Sep 21 '15 at 16:04
  • @jpo38 sorry, however you did not post an answer. I verified that it works on Linux before your comment. – Orest Hera Sep 21 '15 at 16:11
  • Thanks for the reply. You say it works on Linux, but what about Windows? I've tried it on Windows, but I can't get it to work. – gpalex Sep 21 '15 at 17:02
  • 2
    @gpalex In Windows it is much harder, see for example http://stackoverflow.com/questions/5021645/qt-setpermissions-not-setting-permisions QFile::setPermissions() does not manipulate ACLs. There is a thread regarding issues of cross platform file permissions http://stackoverflow.com/questions/592448/c-how-to-set-file-permissions-cross-platform. Unfortunately there is no simple solution for Windows NTFS. – Orest Hera Sep 21 '15 at 17:31
1

Since Permissions is just a typedef for QFlags<Permission>, you can use the constructor QFlags<Permission>(Zero zero = 0) thus:

file.setPermissions(Permissions());

or - in C++ 11:

file.setPermissions({});
Toby Speight
  • 27,591
  • 48
  • 66
  • 103