Re: [RFA 09/23] Remove close cleanup

Tom Tromey
2017-07-19T22:52:24Z

>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:

Pedro> Patch looks good.
Pedro> The bit below made me stop for a second:
[...]

Pedro> Would the following be too clever?
Pedro> /* Restore errno on exit to the value saved by the
Pedro> last save() call. Ctor saves. */
Pedro> struct scoped_errno_restore
[...]

Pedro> /* Save errno and return MNSH_FS_ERROR. */
Pedro> auto fs_err = [&] ()
Pedro> {
Pedro> restore_errno.save ();
Pedro> return MNSH_FS_ERROR;
Pedro> };
[...]

Pedro> [It gets rid of both the scope, and the gotos.]

How about making fd_closer a template, like:

template<int (*CLOSER) (int) = close>
class fd_closer
...
~fd_closer ()
{
if (m_fd != -1)
CLOSER (m_fd);
}

Then in linux-namespaces.c:

/* A function like "close" that saves and restores errno. */

static int
close_saving_fd (int fd)
{
scoped_restore save_errno = make_scoped_restore (&errno);
return close (fd);
}

...

gdb::fd_closer<close_saving_errno> close_fd (fd);

This also removes the scope and the gotos.

The main plus is that it's simpler. The main minus is that it's ad hoc.

Tom