Nominate our 2022 Qt Champions!

how to make a folder non writable

  • Hi
    I have a need to make a folder non writable
    I have made a QFile object and set the permissions to 0 but I am still able to perform a mkpath operation

    		QFile file(dir.path());
    		QString s = file.fileName();
    		QFile::Permissions permissions = file.permissions();
    		QFile::Permissions oldPermissions = permissions;
    		permissions &= QFile::ExeGroup;
    		permissions &= QFile::ReadOwner;
    		permissions &= QFile::WriteOwner;
    		permissions &= QFile::ReadUser;
    		permissions &= QFile::WriteUser;
    		permissions &= QFile::ExeUser;
    		permissions &= QFile::ReadGroup;
    		permissions &= QFile::WriteGroup;
    		permissions &= QFile::ReadOther;
    		permissions &= QFile::WriteOther;
    		permissions &= QFile::ExeOther;

    What is the correct way of doing this


  • Lifetime Qt Champion

    Are you sure QFile will do it with a folder ?
    Maybe try
    QFile::Permissions perm = QFileInfo( "c:/ACCESS_DENIED/" ).permissions();
    Since it mention folder also.
    Never tried it! so just suggestion.

  • Hi
    Thanks for your reply
    I dont think this will allow me to set the permissions on the folder though

  • Lifetime Qt Champion

    Yeah, after I posted ,
    I realized that QFileInfo seems to have no setPermissions so
    its back to QFile again. sorry.

    Its under linux I assume ?

    So the user the program runs under/as do have the permission to set permissions ?

  • Actually this is running under Windows
    Dont think I can do it using Qt tho

  • Lifetime Qt Champion

    and you did notice
    extern Q_CORE_EXPORT int qt_ntfs_permission_lookup;

    Well, if you trying to set the Read Only flag for the folder,
    i think you are right that it wont work with Qt and you would have to resort to

    If that is the effect you want to get.

  • OK
    Thanks for your help

Log in to reply