mirror of
https://github.com/libvirt/libvirt.git
synced 2025-02-25 18:55:26 -06:00
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:
@@ -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) < 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) < 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>
|
||||
|
||||
Reference in New Issue
Block a user