1 /* Self tests for scoped_fd for GDB, the GNU debugger.
3 Copyright (C) 2018 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "common/filestuff.h"
24 #include "common/byte-vector.h"
25 #include "common/pathstuff.h"
28 namespace mkdir_recursive {
30 /* Try to create DIR using mkdir_recursive and make sure it exists. */
33 create_dir_and_check (const char *dir)
35 ::mkdir_recursive (dir);
38 if (stat (dir, &st) != 0)
39 perror_with_name (("stat"));
41 return (st.st_mode & S_IFDIR) != 0;
44 /* Test mkdir_recursive. */
49 std::string tmp = get_standard_temp_dir () + "/gdb-selftests";
50 gdb::char_vector base = make_temp_filename (tmp);
52 if (mkdtemp (base.data ()) == NULL)
53 perror_with_name (("mkdtemp"));
55 /* Try not to leave leftover directories. */
57 cleanup_dirs (const char *base)
62 rmdir (string_printf ("%s/a/b/c/d/e", m_base).c_str ());
63 rmdir (string_printf ("%s/a/b/c/d", m_base).c_str ());
64 rmdir (string_printf ("%s/a/b/c", m_base).c_str ());
65 rmdir (string_printf ("%s/a/b", m_base).c_str ());
66 rmdir (string_printf ("%s/a", m_base).c_str ());
72 } cleanup_dirs (base.data ());
74 std::string dir = string_printf ("%s/a/b", base.data ());
75 SELF_CHECK (create_dir_and_check (dir.c_str ()));
77 dir = string_printf ("%s/a/b/c//d/e/", base.data ());
78 SELF_CHECK (create_dir_and_check (dir.c_str ()));
85 _initialize_mkdir_recursive_selftests ()
87 selftests::register_test ("mkdir_recursive",
88 selftests::mkdir_recursive::test);