I have some files (.xml
extensions) that my app requires them to be present as long as my application is open.
So, is there a cross-platform solution to mark these files as "File in use" so that the user cannot delete or modify them?
I have some files (.xml
extensions) that my app requires them to be present as long as my application is open.
So, is there a cross-platform solution to mark these files as "File in use" so that the user cannot delete or modify them?
Since you specify you need it to work cross-platform, you might want to use Qt with QFile::setPermissions and set it to QFileDevice::ReadOwner
. Do note the platform-specifc notes the documentation makes. There is nothing similar in the C++ Standard Library as far as I am aware.
Edit: turns out I was wrong! Since C++17 can use std::filesystem::permissions
and set the permissions to read-only.
These steps could work for you:
Not sure about this one, never tried it, but could theoretically block access:
File management is always tricky because it is operating system dependant, although most OS behave similarly. Here you have some ideas that could work:
Since it is platform-dependent and you want a cross-platform solution, you'll have to make it by preprocessor flags. So you have to consider all the platforms that your application will have support for and write special code for each of them. As far as I know with Windows and Linux you can use std::filesystem::permissions
. Just set read-only and OS will automatically warn the user once he wants to remove any of marked files. Also, tmpfile is mentioned in the answers could be a good fit if you don't say I exactly need to set file permissions.