0

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?

4 Answers4

1

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.

Bart Louwers
  • 873
  • 9
  • 28
1

These steps could work for you:

  1. read and store the file(s) to memory (or perhaps a temporary storage if memory is a problem)
  2. implement a file-change watcher (concrete solutions here How do I make my program watch for file modification in C++? )
  3. if a change occurs, you could:
  • overwrite the changed file (from data you have in memory or in temporary storage); or create the file anew if it was deleted
  • notify the user that the files have been changed and the program might not work correctly

Not sure about this one, never tried it, but could theoretically block access:

  • Open the file(stream) for reading and writing but don't close it (until program finishes)
  • try to even read or write from/to file
Jax297
  • 617
  • 1
  • 6
  • 8
  • My answer would have been to keep the files open, too. It's not 100% reliable for network file systems. For example with NFS a file that isn't used for some time can be deleted and the next access would get an error due to a stale file handle. So read the files into memory or copy them if it's critical to always work. – Goswin von Brederlow Mar 12 '22 at 13:17
1

File management is always tricky because it is operating system dependant, although most OS behave similarly. Here you have some ideas that could work:

  • If the .xml files are never modified: make them read-only. The C++17 standard introduced a method to configure file permissions (https://en.cppreference.com/w/cpp/filesystem/permissions), so you can always ensure that they are read-only from your application. However, this will not prevent some user deleting them, but for example in linux you will see a warning when trying to remove the files with "rm".
  • If the files are not that big, I would just parse the XML files at the beginning of the program and keep the data structures in RAM, so then you can just forget about the actual files in disk.
  • An alternative would be to just copy the .xml files to a temporary location, rarely someone will be deleting temporary files. The "tmp" directories are platform dependant, but the lib has a method to create temporary files, so you could create one for each xml and copy their contents: http://www.cplusplus.com/reference/cstdio/tmpfile/
Dharman
  • 30,962
  • 25
  • 85
  • 135
AlexCL
  • 412
  • 1
  • 5
1

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.