Does Linux / GCC have file sharing allow-deny type support?

Viewed 94

Back in DOS, Borland C/C++ you had O_DENYxxx flags available on open(). On MS Visual Studio / Windows you have sopen(), some flavors of open() have O_SHARE_xxxx flags available (ibm docs for example). But I can't seem to find what Linux has available that works similarly?

1 Answers

No, Linux doesn't have any of these flags. In general on Unix systems, any process with adequate permissions may perform any permitted operation on a file at any time.

There are, however, locking facilities, both with fcntl and with flock, that allow cooperating processes to take read and write locks on a file or parts of it to prevent unintentional conflicts. There is mandatory locking that can be done with fcntl, but it is unreliable and deprecated as of kernel 4.5; you can see the fcntl(2) man page for more details. If your program will ever run over NFS, fcntl is preferred for locking, because flock doesn't work there.

Alternatively, if you want a simpler interface, you can use something like liblockfile, which provides a straightforward, NFS-safe interface for locking.

In general, the sharing locking available on DOS and Windows is considered a bad idea on Unix because it prevents processes from doing things like renaming over files or other operations which are widely expected to be available and succeed.

Related