deprecate fclose() and introduce VIR_{FORCE_}FCLOSE()

Similarly to deprecating close(), I am now deprecating fclose() and
introduce VIR_FORCE_FCLOSE() and VIR_FCLOSE(). Also, fdopen() is replaced with
VIR_FDOPEN().

Most of the files are opened in read-only mode, so usage of
VIR_FORCE_CLOSE() seemed appropriate. Others that are opened in write
mode already had the fclose()<  0 check and I converted those to
VIR_FCLOSE()<  0.

I did not find occurrences of possible double-closed files on the way.
This commit is contained in:
Stefan Berger
2010-11-16 21:13:29 -05:00
parent 7942fd1c26
commit 7b7cb1ecc9
26 changed files with 173 additions and 93 deletions

View File

@@ -414,25 +414,45 @@
<h2><a name="file_handling">File handling</a></h2>
<p>
Use of the close() API is deprecated in libvirt code base to help
avoiding double-closing of a file descriptor. Instead of this API,
use the macro from files.h
Usage of the <code>fdopen()</code>, <code>close()</code>, <code>fclose()</code>
APIs is deprecated in libvirt code base to help avoiding double-closing of files
or file descriptors, which is particulary dangerous in a multi-threaded
applications. Instead of these APIs, use the macros from files.h
</p>
<ul>
<ul>
<li><p>eg opening a file from a file descriptor</p>
<pre>
if ((file = VIR_FDOPEN(fd, "r")) == NULL) {
virReportSystemError(errno, "%s",
_("failed to open file from file descriptor"));
return -1;
}
/* fd is now invalid; only access the file using file variable */
</pre></li>
<li><p>e.g. close a file descriptor</p>
<pre>
if (VIR_CLOSE(fd) &lt; 0) {
virReportSystemError(errno, _("failed to close file"));
virReportSystemError(errno, "%s", _("failed to close file"));
}
</pre>
</li>
</pre></li>
<li><p>eg close a file descriptor in an error path, without losing
<li><p>eg close a file</p>
<pre>
if (VIR_FCLOSE(file) &lt; 0) {
virReportSystemError(errno, "%s", _("failed to close file"));
}
</pre></li>
<li><p>eg close a file or file descriptor in an error path, without losing
the previous <code>errno</code> value</p>
<pre>
VIR_FORCE_CLOSE(fd);
VIR_FORCE_FCLOSE(file);
</pre>
</li>
</ul>