11556Srgrimes/* Self tests for scoped_fd for GDB, the GNU debugger.
21556Srgrimes
31556Srgrimes   Copyright (C) 2018-2020 Free Software Foundation, Inc.
41556Srgrimes
51556Srgrimes   This file is part of GDB.
61556Srgrimes
71556Srgrimes   This program is free software; you can redistribute it and/or modify
81556Srgrimes   it under the terms of the GNU General Public License as published by
91556Srgrimes   the Free Software Foundation; either version 3 of the License, or
101556Srgrimes   (at your option) any later version.
111556Srgrimes
121556Srgrimes   This program is distributed in the hope that it will be useful,
131556Srgrimes   but WITHOUT ANY WARRANTY; without even the implied warranty of
141556Srgrimes   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
151556Srgrimes   GNU General Public License for more details.
161556Srgrimes
171556Srgrimes   You should have received a copy of the GNU General Public License
181556Srgrimes   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
191556Srgrimes
201556Srgrimes#include "defs.h"
211556Srgrimes
221556Srgrimes#include "gdbsupport/filestuff.h"
231556Srgrimes#include "gdbsupport/selftest.h"
241556Srgrimes#include "gdbsupport/byte-vector.h"
251556Srgrimes#include "gdbsupport/pathstuff.h"
261556Srgrimes
271556Srgrimesnamespace selftests {
281556Srgrimesnamespace mkdir_recursive {
291556Srgrimes
301556Srgrimes/* Try to create DIR using mkdir_recursive and make sure it exists.  */
311556Srgrimes
321556Srgrimesstatic bool
331556Srgrimescreate_dir_and_check (const char *dir)
341556Srgrimes{
351556Srgrimes  ::mkdir_recursive (dir);
361556Srgrimes
371556Srgrimes  struct stat st;
3820420Ssteve  if (stat (dir, &st) != 0)
391556Srgrimes    perror_with_name (("stat"));
401556Srgrimes
411556Srgrimes  return (st.st_mode & S_IFDIR) != 0;
421556Srgrimes}
431556Srgrimes
4436049Scharnier/* Test mkdir_recursive.  */
4536049Scharnier
4636049Scharnierstatic void
4736049Scharniertest ()
4850471Speter{
491556Srgrimes  std::string tmp = get_standard_temp_dir () + "/gdb-selftests";
501556Srgrimes  gdb::char_vector base = make_temp_filename (tmp);
511556Srgrimes
521556Srgrimes  if (mkdtemp (base.data ()) == NULL)
531556Srgrimes    perror_with_name (("mkdtemp"));
541556Srgrimes
5531664Seivind  /* Try not to leave leftover directories.  */
561556Srgrimes  struct cleanup_dirs {
571556Srgrimes    cleanup_dirs (const char *base)
581556Srgrimes      : m_base (base)
591556Srgrimes    {}
6090644Simp
6177409Simp    ~cleanup_dirs () {
6290644Simp      rmdir (string_printf ("%s/a/b/c/d/e", m_base).c_str ());
631556Srgrimes      rmdir (string_printf ("%s/a/b/c/d", m_base).c_str ());
641556Srgrimes      rmdir (string_printf ("%s/a/b/c", m_base).c_str ());
651556Srgrimes      rmdir (string_printf ("%s/a/b", m_base).c_str ());
6650544Smharo      rmdir (string_printf ("%s/a", m_base).c_str ());
671556Srgrimes      rmdir (m_base);
681556Srgrimes    }
691556Srgrimes
701556Srgrimes  private:
7150544Smharo    const char *m_base;
721556Srgrimes  } cleanup_dirs (base.data ());
7390110Simp
7490110Simp  std::string dir = string_printf ("%s/a/b", base.data ());
7590110Simp  SELF_CHECK (create_dir_and_check (dir.c_str ()));
7690110Simp
771556Srgrimes  dir = string_printf ("%s/a/b/c//d/e/", base.data ());
781556Srgrimes  SELF_CHECK (create_dir_and_check (dir.c_str ()));
7990110Simp}
801556Srgrimes
8191085Smarkm}
8291085Smarkm}
8390114Simp
841556Srgrimesvoid _initialize_mkdir_recursive_selftests ();
851556Srgrimesvoid
8677409Simp_initialize_mkdir_recursive_selftests ()
871556Srgrimes{
8850544Smharo  selftests::register_test ("mkdir_recursive",
891556Srgrimes			    selftests::mkdir_recursive::test);
901556Srgrimes}
9114154Swosch
9214166Swosch