0

Possible Duplicate:
Getting a FILE* from a std::fstream

I need access to the file descriptor so that I can use the flock() (or fcntl(), I don't care) system call to create and release advisory file locks. But I couldn't see any way to get at it. Nor any alternative approaches that didn't smell of a horrible hack.

Any suggestion? Am I missing an obvious way to implement file locking?

Community
  • 1
  • 1
Don Doerner
  • 71
  • 1
  • 4
  • In a word, no. GCC used to provide a way, but the feature was removed some 10 years ago and AFAICT there are no plans to put it back. – n. m. could be an AI Jan 28 '13 at 17:17
  • There's certainly no portable way to do this. The `basic_filebuf` that is a member of the `fstream` probably contains a `FILE *`, but there is no (portable) way to get at it. – Marshall Clow Jan 28 '13 at 17:20
  • Unfortunate. I am using boost::serialization; input/output archives need [io]fstreams to their constructors. But in order to provide the opportunity of correct operation, I had hoped to use flock/fcntl on the underlying file descriptor. – Don Doerner Jan 28 '13 at 17:31
  • You can use `boost::stream` instead of `std::stream` for serialization. You can also write your own `streambuf` exposing the file descriptor (not very hard to do). – n. m. could be an AI Jan 28 '13 at 19:37

3 Answers3

0

It appears that I cannot get access to the underlying file descriptor in a [io]fstream. Nor do there seem to be c'tors that will let me build an [io]fstream from an existing file descriptor. This approach to my locking problem is apparently infeasible.

I have found another workable solution to my locking problem, however: I will use the boost::interprocess synchronization mechanisms (in particular, a boost::interprocess::sharable_lock) to achieve the same end. See http://www.boost.org/doc/libs/1_52_0/doc/html/interprocess/synchronization_mechanisms.html#interprocess.synchronization_mechanisms.sharable_upgradable_mutexes.sharable_upgradable_locks

After I have implemented this, I will report back the ease/difficulty, and additional pointers.

Don Doerner
  • 71
  • 1
  • 4
  • You can build an iostream from an existing file descriptor with [boost::iostreams](http://www.boost.org/doc/libs/1_52_0/libs/iostreams/doc/classes/file_descriptor.html#file_descriptor) – Cubbi Jan 28 '13 at 18:44
0

Instead of reverting to the C world, use <mutex> and any other threading functionality available in C++. If you don't have access to a C++11 compiler, time to upgrade.

rubenvb
  • 74,642
  • 33
  • 187
  • 332
  • Looked at that, but seems to have several problems. First, there does not seem to be any option for shareable/exclusive semantics (as there is with flock/fcntl). I could easily enough invent this, but I am lazy... The second problem is that it doesn't seem to offer cross-process semantics, which was the whole point of using it. – Don Doerner Jan 28 '13 at 18:58
-1

How do I lock files using fopen()?

I believe this question shows you how to use flock() (look at the first answer). Note that the only reason this is an answer, not a comment, is because I can't find the comment button :)

Community
  • 1
  • 1
Polar
  • 186
  • 7
  • 18