This encourages the callers to use automatic file descriptor management.
Change-Id: I137a81df6f3607b457e28c35aafde8ed6f3a3344
new_dir.c_str (), safe_strerror (errno));
gdb::char_vector temp_name = make_temp_filename (new_name);
- scoped_fd to_fd (gdb_mkostemp_cloexec (&temp_name[0]));
+ scoped_fd to_fd = gdb_mkostemp_cloexec (&temp_name[0]);
gdb::unlinker unlink_file_on_error (temp_name.data ());
if (to_fd.get () < 0)
filename_temp = make_temp_filename (filename);
- scoped_fd out_file_fd (gdb_mkostemp_cloexec (filename_temp.data (),
- O_BINARY));
+ scoped_fd out_file_fd = gdb_mkostemp_cloexec (filename_temp.data (),
+ O_BINARY);
if (out_file_fd.get () == -1)
perror_with_name (("mkstemp"));
test_destroy ()
{
char filename[] = "scoped_fd-selftest-XXXXXX";
- int fd = gdb_mkostemp_cloexec (filename);
+ int fd = gdb_mkostemp_cloexec (filename).release ();
SELF_CHECK (fd >= 0);
unlink (filename);
test_release ()
{
char filename[] = "scoped_fd-selftest-XXXXXX";
- int fd = gdb_mkostemp_cloexec (filename);
+ int fd = gdb_mkostemp_cloexec (filename).release ();
SELF_CHECK (fd >= 0);
unlink (filename);
{
char filename[] = "scoped_fd-selftest-XXXXXX";
- ::scoped_fd sfd (gdb_mkostemp_cloexec (filename));
+ ::scoped_fd sfd = gdb_mkostemp_cloexec (filename);
SELF_CHECK (sfd.get () >= 0);
unlink (filename);
test_normal ()
{
char filename[] = "scoped_mmapped_file-selftest-XXXXXX";
- int fd = gdb_mkostemp_cloexec (filename);
- SELF_CHECK (fd >= 0);
+ {
+ scoped_fd fd = gdb_mkostemp_cloexec (filename);
+ SELF_CHECK (fd.get () >= 0);
- SELF_CHECK (write (fd, "Hello!", 7) == 7);
- close (fd);
+ SELF_CHECK (write (fd.get (), "Hello!", 7) == 7);
+ }
gdb::unlinker unlink_test_file (filename);
/* Like mkstemp, but ensures that the file descriptor is
close-on-exec. */
-static inline int
+static inline scoped_fd
gdb_mkostemp_cloexec (char *name_template, int flags = 0)
{
/* gnulib provides a mkostemp replacement if needed. */
- return mkostemp (name_template, flags | O_CLOEXEC);
+ return scoped_fd (mkostemp (name_template, flags | O_CLOEXEC));
}
/* Convenience wrapper for the above, which takes the filename as an