Debian Codegraph @

libstdc++-v3/testsuite/27_io/filesystem/operations/create_symlink.cc

raw
1 // Copyright (C) 2016-2025 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17
18 // { dg-do run { target c++17 } }
19 // { dg-require-filesystem-ts "" }
20
21 #include <filesystem>
22 #include <testsuite_hooks.h>
23 #include <testsuite_fs.h>
24
25 namespace fs = std::filesystem;
26
27 void
28 test01()
29 {
30 std::error_code ec, ec2;
31 __gnu_test::scoped_file f;
32 auto tgt = f.path;
33
34 // Test empty path.
35 fs::path p;
36 create_symlink(tgt, p, ec );
37 VERIFY( ec );
38 try
39 {
40 create_symlink(tgt, p);
41 }
42 catch (const std::filesystem::filesystem_error& ex)
43 {
44 ec2 = ex.code();
45 VERIFY( ex.path1() == tgt );
46 VERIFY( ex.path2() == p );
47 }
48 VERIFY( ec2 == ec );
49 }
50
51 void
52 test02()
53 {
54 const std::error_code bad_ec = make_error_code(std::errc::invalid_argument);
55 std::error_code ec, ec2;
56 __gnu_test::scoped_file f;
57 auto tgt = f.path;
58
59 // Test non-existent path
60 auto p = __gnu_test::nonexistent_path();
61 VERIFY( !exists(p) );
62
63 ec = bad_ec;
64 create_symlink(tgt, p, ec); // create the symlink once
65 VERIFY( !ec );
66 VERIFY( exists(p) );
67 VERIFY( is_symlink(p) );
68 remove(p);
69 create_symlink(tgt, p); // create the symlink again
70 VERIFY( exists(p) );
71 VERIFY( is_symlink(p) );
72
73 ec.clear();
74 create_symlink(tgt, p, ec); // Try to create existing symlink
75 VERIFY( ec );
76 try
77 {
78 create_symlink(tgt, p);
79 }
80 catch (const std::filesystem::filesystem_error& ex)
81 {
82 ec2 = ex.code();
83 VERIFY( ex.path1() == tgt );
84 VERIFY( ex.path2() == p );
85 }
86 VERIFY( ec2 == ec );
87
88 remove(p);
89 }
90
91 int
92 main()
93 {
94 test01();
95 }