title: flock-ing a C++ ifstream on Linux (GCC 4.6)
body: <h3>context</h3>
  
  <p>I'm slowly writing a specialized web server application in C++ (using the C <a href="https://github.com/davidmoreno/onion" rel="noreferrer">onion http server library</a> and the <a href="http://jsoncpp.sourceforge.net/" rel="noreferrer">JSONCPP library</a> for JSON serialization, if that matters)., for a Linux system with GCC 4.6 compiler (I don't care about portability to non Linux systems, or to GCC before 4.5 or to Clang before 3.0).</p>
  
  <p>I decided to keep the user "database" (there will be very few users, probably one or two, so performance is not a concern, and O(n) access time is acceptable) in JSON format, probably as a small array of JSON objects like</p>
  
  <pre><code> { "_user" : "basile" ;
     "_crypasswd" : "XYZABC123" ; 
     "_email" : "basile@starynkevitch.net" ;
     "firstname" : "Basile" ;
     "lastname" : "Starynkevitch" ;
     "privileges" : "all" ;
   }
  </code></pre>
  
  <p>with the convention (à la <code>.htpasswd</code>) that the <code>_crypasswd</code> field is the <a href="http://linux.die.net/man/3/crypt" rel="noreferrer">crypt(3)</a> "encryption" of the user password, salted by the <code>_user</code> name;</p>
  
  <p>The reason I want to describe users by Json objects is that my application might add (not replace) some JSON fields (like e.g. <code>privileges</code> above) in such Json objects describing users. I'm using <em>JsonCpp</em> as a Json parsing library for C++. This library wants an <code>ifstream</code> to be parsed.</p>
  
  <p>So I am reading my password file with</p>
  
  <pre><code>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&lt;nbu; ix++) {
    const Json::Value&amp;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
      }
    }
  }
  </code></pre>
  
  <hr>
  
  <h2>question</h2>
  
  <p>Obviously, I want to <a href="http://linux.die.net/man/2/flock" rel="noreferrer"><code>flock</code></a> or <a href="http://linux.die.net/man/3/lockf" rel="noreferrer"><code>lockf</code></a> the password file, to ensure that only one process is reading or writing it. To call these functions, I need to get the file descriptor (in Unix parlance) of the <code>ifstream jsinpass</code>. But Google gives me mostly <a href="http://www.ginac.de/~kreckel/fileno/" rel="noreferrer">Kreckel's fileno</a> (which I find complete, but a bit insane) to get the file descriptor of an <code>std::ifstream</code> and I am not sure that the constructor won't pre-read some of it. Hence my <strong>question</strong>:</p>
  
  <h3>how can I lock a C++ <code>ifstream</code> (Linux, GCC 4.6) ?</h3>
  
  <p>(Or do you find some other way to tackle that issue?)</p>
  
  <p>Thanks</p>