1 // -*- C++ -*- 2 // Filesystem utils for the C++ library testsuite. 3 // 4 // Copyright (C) 2014-2018 Free Software Foundation, Inc. 5 // 6 // This file is part of the GNU ISO C++ Library. This library is free 7 // software; you can redistribute it and/or modify it under the 8 // terms of the GNU General Public License as published by the 9 // Free Software Foundation; either version 3, or (at your option) 10 // any later version. 11 // 12 // This library 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 along 18 // with this library; see the file COPYING3. If not see 19 // <http://www.gnu.org/licenses/>. 20 // 21 22 #ifndef _TESTSUITE_FS_H 23 #define _TESTSUITE_FS_H 1 24 25 // Assume we want std::filesystem in C++17, unless USE_FILESYSTEM_TS defined: 26 #if __cplusplus >= 201703L && ! defined USE_FILESYSTEM_TS 27 #include <filesystem> 28 namespace test_fs = std::filesystem; 29 #else 30 #include <experimental/filesystem> 31 namespace test_fs = std::experimental::filesystem; 32 #endif 33 #include <fstream> 34 #include <string> 35 #include <cstdio> 36 #include <stdlib.h> 37 #include <unistd.h> 38 39 namespace __gnu_test 40 { 41 #define PATH_CHK(p1, p2, fn) \ 42 if ( p1.fn() != p2.fn() ) \ 43 throw test_fs::filesystem_error("comparing '" #fn "' failed", p1, p2, \ 44 std::make_error_code(std::errc::invalid_argument) ) 45 46 void compare_paths(const test_fs::path & p1,const test_fs::path & p2)47 compare_paths(const test_fs::path& p1, 48 const test_fs::path& p2) 49 { 50 PATH_CHK( p1, p2, native ); 51 PATH_CHK( p1, p2, string ); 52 PATH_CHK( p1, p2, empty ); 53 PATH_CHK( p1, p2, has_root_path ); 54 PATH_CHK( p1, p2, has_root_name ); 55 PATH_CHK( p1, p2, has_root_directory ); 56 PATH_CHK( p1, p2, has_relative_path ); 57 PATH_CHK( p1, p2, has_parent_path ); 58 PATH_CHK( p1, p2, has_filename ); 59 PATH_CHK( p1, p2, has_stem ); 60 PATH_CHK( p1, p2, has_extension ); 61 PATH_CHK( p1, p2, is_absolute ); 62 PATH_CHK( p1, p2, is_relative ); 63 auto d1 = std::distance(p1.begin(), p1.end()); 64 auto d2 = std::distance(p2.begin(), p2.end()); 65 if( d1 != d2 ) 66 throw test_fs::filesystem_error( 67 "distance(begin, end)", p1, p2, 68 std::make_error_code(std::errc::invalid_argument) ); 69 } 70 71 const std::string test_paths[] = { 72 "", "/", "//", "/.", "/./", "/a", "/a/", "/a//", "/a/b/c/d", "/a//b", 73 "a", "a/b", "a/b/", "a/b/c", "a/b/c.d", "a/b/..", "a/b/c.", "a/b/.c" 74 }; 75 76 // This is NOT supposed to be a secure way to get a unique name! 77 // We just need a path that doesn't exist for testing purposes. 78 test_fs::path 79 nonexistent_path(std::string file = __builtin_FILE()) 80 { 81 // Include the caller's filename to help identify tests that fail to 82 // clean up the files they create. 83 // Remove .cc extension: 84 if (file.length() > 3 && file.compare(file.length() - 3, 3, ".cc") == 0) 85 file.resize(file.length() - 3); 86 // And directory: 87 auto pos = file.find_last_of("/\\"); 88 if (pos != file.npos) 89 file.erase(0, pos+1); 90 91 test_fs::path p; 92 #if defined(_GNU_SOURCE) || _XOPEN_SOURCE >= 500 || _POSIX_C_SOURCE >= 200112L 93 char tmp[] = "filesystem-test.XXXXXX"; 94 int fd = ::mkstemp(tmp); 95 if (fd == -1) 96 throw test_fs::filesystem_error("mkstemp failed", 97 std::error_code(errno, std::generic_category())); 98 ::unlink(tmp); 99 ::close(fd); 100 if (!file.empty()) 101 file.insert(0, 1, '-'); 102 file.insert(0, tmp); 103 p = file; 104 #else 105 if (file.length() > 64) 106 file.resize(64); 107 char buf[128]; 108 static int counter; 109 #if _GLIBCXX_USE_C99_STDIO 110 std::snprintf(buf, 128, 111 #else 112 std::sprintf(buf, 113 #endif 114 "filesystem-test.%d.%lu-%s", counter++, (unsigned long) ::getpid(), 115 file.c_str()); 116 p = buf; 117 #endif 118 return p; 119 } 120 121 // RAII helper to remove a file on scope exit. 122 struct scoped_file 123 { 124 using path_type = test_fs::path; 125 126 enum adopt_file_t { adopt_file }; 127 128 explicit pathscoped_file129 scoped_file(const path_type& p = nonexistent_path()) : path(p) 130 { std::ofstream{p.native()}; } 131 scoped_filescoped_file132 scoped_file(path_type p, adopt_file_t) : path(p) { } 133 ~scoped_filescoped_file134 ~scoped_file() { if (!path.empty()) remove(path); } 135 136 path_type path; 137 }; 138 139 } // namespace __gnu_test 140 #endif 141