title: Do I need to close a file before calling syncfs()
body: <p>On my embedded system, I want to make sure that the data is safely written when I close a file - if the system reports that the data was saved, the user should be able to remove power immediately.</p>
  
  <p>I know that the proper way to do this is <code>fsync()</code>, <code>fclose()</code>, and <code>fsync()</code> on the directory (cfr. <a href="http://blog.httrack.com/blog/2013/11/15/everything-you-always-wanted-to-know-about-fsync" rel="nofollow noreferrer">this blog entry</a>). However, it's a bit tricky to get a file descriptor for the directory in my case (I'd have to go through <code>/proc/self/fd</code> to find back the filename and derive the directory from there). It would be much simpler for me to just do <code>syncfs()</code> on the entire filesystem - I know that this is the only file that is open on the filesystem anyway.</p>
  
  <p>Now my question is:</p>
  
  <ul>
  <li>Is it sufficient to do <code>syncfs()</code>?</li>
  <li>Do I need to <code>fclose()</code> the <code>FILE *</code> first (for the directory entry to be up-to-date)? Or is <code>fflush()</code> sufficient?</li>
  <li>If it needs to be closed, is it useful to <code>dup()</code> the file descriptor before closing so I can use it directly for <code>syncfs()</code>?</li>
  </ul>