Automatic date update in version.in
[binutils-gdb.git] / gdb / unittests / mkdir-recursive-selftests.c
blob9ad4c4bbfefed40c68edf4a646bbab08ab5dbe4b
1 /* Self tests for scoped_fd for GDB, the GNU debugger.
3 Copyright (C) 2018-2022 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/>. */
20 #include "defs.h"
22 #include "gdbsupport/filestuff.h"
23 #include "gdbsupport/selftest.h"
24 #include "gdbsupport/byte-vector.h"
25 #include "gdbsupport/pathstuff.h"
27 namespace selftests {
28 namespace mkdir_recursive {
30 /* Try to create DIR using mkdir_recursive and make sure it exists. */
32 static bool
33 create_dir_and_check (const char *dir)
35 ::mkdir_recursive (dir);
37 struct stat st;
38 if (stat (dir, &st) != 0)
39 perror_with_name (("stat"));
41 return (st.st_mode & S_IFDIR) != 0;
44 /* Test mkdir_recursive. */
46 static void
47 test ()
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. */
56 struct cleanup_dirs {
57 cleanup_dirs (const char *base)
58 : m_base (base)
61 ~cleanup_dirs () {
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 ());
67 rmdir (m_base);
70 private:
71 const char *m_base;
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 ()));
84 void _initialize_mkdir_recursive_selftests ();
85 void
86 _initialize_mkdir_recursive_selftests ()
88 selftests::register_test ("mkdir_recursive",
89 selftests::mkdir_recursive::test);