Lumiera  0.pre.03
»edit your freedom«
file.hpp
Go to the documentation of this file.
1 /*
2  FILE.hpp - Filesystem access and helpers
3 
4  Copyright (C) Lumiera.org
5  2022, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (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, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 */
22 
23 
37 #ifndef LIB_STAT_FILE_H
38 #define LIB_STAT_FILE_H
39 
40 
41 #include "lib/error.hpp"
42 #include "lib/symbol.hpp"
43 #include "lib/meta/util.hpp"
44 
45 #include <filesystem>
46 #include <cstdlib>
47 
48 
49 namespace fs = std::filesystem;
50 namespace std::filesystem {
51 
52  extern const string UNIX_HOMEDIR_SYMBOL;
53  extern lib::Literal UNIX_HOMEDIR_ENV;
54 
55 
56  inline fs::path
57  getHomePath()
58  {
59  auto home = std::getenv(UNIX_HOMEDIR_ENV);
60  if (not home)
61  throw lumiera::error::Config{"Program environment doesn't define $HOME (Unix home directory)."};
62  return fs::path{home};
63  }
64 
65 
69  inline fs::path
70  consolidated (fs::path rawPath)
71  {
72  if (rawPath.empty())
73  return rawPath;
74  if (UNIX_HOMEDIR_SYMBOL == *rawPath.begin())
75  rawPath = getHomePath() / rawPath.lexically_proximate(UNIX_HOMEDIR_SYMBOL);
76 
77  return fs::exists(rawPath)? fs::absolute(
78  fs::canonical(rawPath))
79  : rawPath;
80  }
81 
82 
84  inline bool
85  has_perm (fs::path const& p, fs::perms permissionMask)
86  {
87  return (fs::status(p).permissions() & permissionMask) == permissionMask;
88  }
89 
91  inline bool
92  can_read (fs::path const& p)
93  {
94  return has_perm (p, fs::perms::owner_read);
95  }
96 
97  inline bool
98  can_write (fs::path const& p)
99  {
100  return has_perm (p, fs::perms::owner_write);
101  }
102 
103  inline bool
104  can_exec (fs::path const& p)
105  {
106  return has_perm (p, fs::perms::owner_exec);
107  }
108 }//(End)namespace fs
109 
110 
111 
112 namespace util {
113 
115  template<>
116  struct StringConv<fs::path, void>
117  {
118  static std::string
119  invoke (fs::path path) noexcept
120  try {
121  return "≺"+std::string{path}+"≻";
122  }
123  catch(...)
124  { return lib::meta::FAILURE_INDICATOR; }
125  };
126 
127 }//(End)namespace util
128 #endif /*LIB_STAT_FILE_H*/
fs::path consolidated(fs::path rawPath)
resolves symlinks, ~ (Unix home dir) and relative specs
Definition: file.hpp:70
bool has_perm(fs::path const &p, fs::perms permissionMask)
check if the denoted path p has at least the given permissions
Definition: file.hpp:85
Simple and lightweight helpers for metaprogramming and type detection.
inline string literal This is a marker type to indicate that
Definition: symbol.hpp:85
bool can_read(fs::path const &p)
check if the owner has read permissions on the denoted file or directory
Definition: file.hpp:92
Derived specific exceptions within Lumiera&#39;s exception hierarchy.
Definition: error.hpp:199
Marker types to indicate a literal string and a Symbol.
Lumiera error handling (C++ interface).
failsafe invocation of custom string conversion.
Definition: meta/util.hpp:399