Chaste Commit::fa89f2b838c1edb21a1eaec92ee3a2eacc9255dd
FilesystemPermissions.hpp
1/*
2
3Copyright (c) 2005-2026, University of Oxford.
4All rights reserved.
5
6University of Oxford means the Chancellor, Masters and Scholars of the
7University of Oxford, having an administrative office at Wellington
8Square, Oxford OX1 2JD, UK.
9
10This file is part of Chaste.
11
12Redistribution and use in source and binary forms, with or without
13modification, are permitted provided that the following conditions are met:
14 * Redistributions of source code must retain the above copyright notice,
15 this list of conditions and the following disclaimer.
16 * Redistributions in binary form must reproduce the above copyright notice,
17 this list of conditions and the following disclaimer in the documentation
18 and/or other materials provided with the distribution.
19 * Neither the name of the University of Oxford nor the names of its
20 contributors may be used to endorse or promote products derived from this
21 software without specific prior written permission.
22
23THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34*/
35
36#ifndef FILESYSTEMPERMISSIONS_HPP_
37#define FILESYSTEMPERMISSIONS_HPP_
38
39#include <system_error>
40
41#include "FileFinder.hpp"
42
48{
49public:
53 static fs::perms GetFilePermissions()
54 {
55 return fs::perms::owner_read | fs::perms::owner_write | fs::perms::group_read;
56 }
57
61 static fs::perms GetDirectoryPermissions()
62 {
63 return fs::perms::owner_all | fs::perms::group_read | fs::perms::group_exec
64 | fs::perms::others_read | fs::perms::others_exec;
65 }
66
72 static void SetFilePermissions(const fs::path& rPath)
73 {
74 // Best-effort: some filesystems (e.g. macOS Docker bind mounts) do not support
75 // changing permissions, so use the non-throwing overload and ignore any failure.
76 // Correct permissions are desirable but are not required for correctness, so this
77 // must never cause an abort.
78 std::error_code ec;
79 fs::permissions(rPath, GetFilePermissions(), fs::perm_options::replace, ec);
80 }
81
87 static void SetDirectoryPermissions(const fs::path& rPath)
88 {
89 // Best-effort; see SetFilePermissions for the rationale.
90 std::error_code ec;
91 fs::permissions(rPath, GetDirectoryPermissions(), fs::perm_options::replace, ec);
92 }
93
101 static bool CreateDirectoryWithPermissions(const fs::path& rPath)
102 {
103 bool created_dir = fs::create_directory(rPath);
104 if (created_dir)
105 {
107 }
108 return created_dir;
109 }
110
118 static bool CreateDirectoriesWithPermissions(const fs::path& rPath)
119 {
120 if (fs::exists(rPath))
121 {
122 return false;
123 }
124
125 fs::path parent_path = rPath.parent_path();
126 bool created_parent = false;
127 if (!parent_path.empty() && parent_path != rPath && !fs::exists(parent_path))
128 {
129 created_parent = CreateDirectoriesWithPermissions(parent_path);
130 }
131
132 return CreateDirectoryWithPermissions(rPath) || created_parent;
133 }
134
141 static void CopyFileWithPermissions(const fs::path& rFromPath, const fs::path& rToPath)
142 {
143 fs::copy_file(rFromPath, rToPath);
144 SetFilePermissions(rToPath);
145 }
146};
147
148#endif /*FILESYSTEMPERMISSIONS_HPP_*/
static bool CreateDirectoriesWithPermissions(const fs::path &rPath)
static void SetDirectoryPermissions(const fs::path &rPath)
static fs::perms GetFilePermissions()
static void CopyFileWithPermissions(const fs::path &rFromPath, const fs::path &rToPath)
static fs::perms GetDirectoryPermissions()
static bool CreateDirectoryWithPermissions(const fs::path &rPath)
static void SetFilePermissions(const fs::path &rPath)