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)
5  2022, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 */
13 
14 
28 #ifndef LIB_STAT_FILE_H
29 #define LIB_STAT_FILE_H
30 
31 
32 #include "lib/error.hpp"
33 #include "lib/symbol.hpp"
34 #include "lib/meta/util.hpp"
35 
36 #include <filesystem>
37 #include <cstdlib>
38 
39 
40 namespace fs = std::filesystem;
41 namespace std::filesystem {
42 
43  extern const string UNIX_HOMEDIR_SYMBOL;
44  extern lib::Literal UNIX_HOMEDIR_ENV;
45 
46 
47  inline fs::path
48  getHomePath()
49  {
50  auto home = std::getenv(UNIX_HOMEDIR_ENV);
51  if (not home)
52  throw lumiera::error::Config{"Program environment doesn't define $HOME (Unix home directory)."};
53  return fs::path{home};
54  }
55 
56 
60  inline fs::path
61  consolidated (fs::path rawPath)
62  {
63  if (rawPath.empty())
64  return rawPath;
65  if (UNIX_HOMEDIR_SYMBOL == *rawPath.begin())
66  rawPath = getHomePath() / rawPath.lexically_proximate(UNIX_HOMEDIR_SYMBOL);
67 
68  return fs::exists(rawPath)? fs::absolute(
69  fs::canonical(rawPath))
70  : rawPath;
71  }
72 
73 
75  inline bool
76  has_perm (fs::path const& p, fs::perms permissionMask)
77  {
78  return (fs::status(p).permissions() & permissionMask) == permissionMask;
79  }
80 
82  inline bool
83  can_read (fs::path const& p)
84  {
85  return has_perm (p, fs::perms::owner_read);
86  }
87 
88  inline bool
89  can_write (fs::path const& p)
90  {
91  return has_perm (p, fs::perms::owner_write);
92  }
93 
94  inline bool
95  can_exec (fs::path const& p)
96  {
97  return has_perm (p, fs::perms::owner_exec);
98  }
99 }//(End)namespace fs
100 
101 
102 
103 namespace util {
104 
106  template<>
107  struct StringConv<fs::path, void>
108  {
109  static std::string
110  invoke (fs::path path) noexcept
111  try {
112  return "≺"+std::string{path}+"≻";
113  }
114  catch(...)
115  { return lib::meta::FAILURE_INDICATOR; }
116  };
117 
118 }//(End)namespace util
119 #endif /*LIB_STAT_FILE_H*/
fs::path consolidated(fs::path rawPath)
resolves symlinks, ~ (Unix home dir) and relative specs
Definition: file.hpp:61
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:76
Simple and lightweight helpers for metaprogramming and type detection.
inline string literal This is a marker type to indicate that
Definition: symbol.hpp:76
bool can_read(fs::path const &p)
check if the owner has read permissions on the denoted file or directory
Definition: file.hpp:83
Derived specific exceptions within Lumiera&#39;s exception hierarchy.
Definition: error.hpp:190
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:390