Context
I am slowly writing a specialized web server application in C ++ (using the C moon http server library and the JSONCPP library to serialize JSON, if that matters). for a Linux system with GCC 4.6 compiler (I'm not interested in portability for non-Linux systems or for GCC up to 4.5 or Clang up to 3.0).
I decided to keep the user "database" (there will be very few users, perhaps one or two, so performance is not a concern, and O (n) access time is acceptable) in JSON format, possibly like a small array of JSON objects such as
{ "_user" : "basile" ; "_crypasswd" : "XYZABC123" ; "_email" : " basile@starynkevitch.net " ; "firstname" : "Basile" ; "lastname" : "Starynkevitch" ; "privileges" : "all" ; }
with the agreement (Γ la .htpasswd ) that the _crypasswd field is crypt (3) "encrypt" the user password salted under the name _user ;
The reason I want to describe users with Json objects is because my application can add (not replace) some JSON fields (e.g. privileges above) in such Json objects describing users. I am using JsonCpp as a Json parsing library for C ++. This library wants to analyze ifstream .
So, I am reading the password file
extern char* iaca_passwd_path; // the path of the password file std::ifstream jsinpass(iaca_passwd_path); Json::Value jpassarr; Json::Reader reader; reader.parse(jsinpass,jpassarr,true); jsinpass.close(); assert (jpassarr.isArray()); for (int ix=0; ix<nbu; ix++) { const Json::Value&jcuruser= jpassarr[ix]; assert(jcuruser.isObject()); if (jcuruser["_user"].compare(user) == 0) { std::string crypasswd = jcuruser["_crypasswd"].asString(); if (crypasswd.compare(crypted_password(user,password)) == 0) { // good user } } }
Question
Obviously, I want a flock or lockf password file to make sure that only one process reads or writes it. To call these functions, I need to get a file descriptor (on Unix parlance) ifstream jsinpass . But Google gives me basically Kreckel fileno (which I find complete, but a little crazy) to get the std::ifstream file descriptor, and I'm not sure if the constructor will not read some of them beforehand. Hence my question :
how can i block c ++ ifstream (linux, gcc 4.6)?
(Or will you find another way to solve this problem?)
thanks