]>
Commit | Line | Data |
---|---|---|
e418a61a TT |
1 | /* Self tests for scoped_fd for GDB, the GNU debugger. |
2 | ||
b811d2c2 | 3 | Copyright (C) 2018-2020 Free Software Foundation, Inc. |
e418a61a TT |
4 | |
5 | This file is part of GDB. | |
6 | ||
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. | |
11 | ||
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. | |
16 | ||
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/>. */ | |
19 | ||
20 | #include "defs.h" | |
21 | ||
268a13a5 TT |
22 | #include "gdbsupport/filestuff.h" |
23 | #include "gdbsupport/selftest.h" | |
24 | #include "gdbsupport/byte-vector.h" | |
25 | #include "gdbsupport/pathstuff.h" | |
e418a61a TT |
26 | |
27 | namespace selftests { | |
28 | namespace mkdir_recursive { | |
29 | ||
30 | /* Try to create DIR using mkdir_recursive and make sure it exists. */ | |
31 | ||
32 | static bool | |
33 | create_dir_and_check (const char *dir) | |
34 | { | |
35 | ::mkdir_recursive (dir); | |
36 | ||
37 | struct stat st; | |
38 | if (stat (dir, &st) != 0) | |
39 | perror_with_name (("stat")); | |
40 | ||
41 | return (st.st_mode & S_IFDIR) != 0; | |
42 | } | |
43 | ||
44 | /* Test mkdir_recursive. */ | |
45 | ||
46 | static void | |
47 | test () | |
48 | { | |
e8d8cce6 SM |
49 | std::string tmp = get_standard_temp_dir () + "/gdb-selftests"; |
50 | gdb::char_vector base = make_temp_filename (tmp); | |
e418a61a | 51 | |
e8d8cce6 | 52 | if (mkdtemp (base.data ()) == NULL) |
e418a61a TT |
53 | perror_with_name (("mkdtemp")); |
54 | ||
55 | /* Try not to leave leftover directories. */ | |
56 | struct cleanup_dirs { | |
57 | cleanup_dirs (const char *base) | |
58 | : m_base (base) | |
59 | {} | |
60 | ||
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); | |
68 | } | |
69 | ||
70 | private: | |
71 | const char *m_base; | |
e8d8cce6 | 72 | } cleanup_dirs (base.data ()); |
e418a61a | 73 | |
e8d8cce6 | 74 | std::string dir = string_printf ("%s/a/b", base.data ()); |
e418a61a TT |
75 | SELF_CHECK (create_dir_and_check (dir.c_str ())); |
76 | ||
e8d8cce6 | 77 | dir = string_printf ("%s/a/b/c//d/e/", base.data ()); |
e418a61a TT |
78 | SELF_CHECK (create_dir_and_check (dir.c_str ())); |
79 | } | |
80 | ||
81 | } | |
82 | } | |
83 | ||
6c265988 | 84 | void _initialize_mkdir_recursive_selftests (); |
e418a61a TT |
85 | void |
86 | _initialize_mkdir_recursive_selftests () | |
87 | { | |
e418a61a TT |
88 | selftests::register_test ("mkdir_recursive", |
89 | selftests::mkdir_recursive::test); | |
e418a61a TT |
90 | } |
91 |