Qt setPermissions not setting permisions

走远了吗. 提交于 2020-01-11 10:13:12

问题


I'm not sure why but my code bellow does not set permissions for a file. Not sure what could be wrong with the code.

SYSTEM: Windows XP Pro SP2. Running in Admin account as admin. Newest Qt framework and files.

//Get file permissions of ref file.
QFile::Permissions qpPerm1;
qpPerm1 = QFile::permissions("E:/dir1/dir2/File1.txt");

//Set file permissions of a file.
bool isOK=0;
isOK = QFile::setPermissions("E:/dir4/dir5/file2.txt",qpPerm1);
qout << "Perms set? " << isOK << endl;

Return value is TRUE... claims it set the permissions but it did not. To be clear, file2.txt is a copy of file1.txt. I set permisions of file1 to be something but file2.txt has different permissions after I run my code.


回答1:


As it was written QT doesn't support changing access rights for groups on windows. For full control over file permissions try using Access Control List which is located in security descriptor.

MSDN suggests this:

To retrieve the security descriptor of a file or directory object, call the GetNamedSecurityInfo or GetSecurityInfo function. To change the security descriptor of a file or directory object, call the SetNamedSecurityInfo or SetSecurityInfo function.

I think in your case you could try GetNamedSecurityInfo from the first file and pass it to SetNamedSecurityInfo for the second one.


Also you can assign NULL ACL and it will reset all group permissions and grant full access for everyone:

SetNamedSecurityInfoA("C:\file.txt", SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, NULL, NULL, NULL, NULL);



回答2:


Since you're mentionning WinXP Pro SP2, I'll assume your files are on NTFS.

According to Qt's documentation :

Note that Qt does not by default check for permissions on NTFS file systems, as this may decrease the performance of file handling considerably. It is possible to force permission checking on NTFS by including the following code in your source:

extern Q_CORE_EXPORT int qt_ntfs_permission_lookup;

Permission checking is then turned on and off by incrementing and decrementing qt_ntfs_permission_lookup by 1.

qt_ntfs_permission_lookup++; // turn checking on
qt_ntfs_permission_lookup--; // turn it off again


来源:https://stackoverflow.com/questions/5021645/qt-setpermissions-not-setting-permisions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!