Lumiera 0.pre.04
»edit your freedom«
Loading...
Searching...
No Matches
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_FILE_H
29#define LIB_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
40namespace fs = std::filesystem;
41namespace std::filesystem {
42
43 extern const string UNIX_HOMEDIR_SYMBOL;
45
46
47 inline fs::path
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::weakly_canonical(rawPath);
69 }
70
71
73 inline bool
74 has_perm (fs::path const& p, fs::perms permissionMask)
75 {
76 return (fs::status(p).permissions() & permissionMask) == permissionMask;
77 }
78
80 inline bool
81 can_read (fs::path const& p)
82 {
83 return has_perm (p, fs::perms::owner_read);
84 }
85
86 inline bool
87 can_write (fs::path const& p)
88 {
89 return has_perm (p, fs::perms::owner_write);
90 }
91
92 inline bool
93 can_exec (fs::path const& p)
94 {
95 return has_perm (p, fs::perms::owner_exec);
96 }
97}//(End)namespace fs
98
99
100
101namespace util {
102
104 template<>
105 struct StringConv<fs::path, void>
106 {
107 static std::string
108 invoke (fs::path path) noexcept
109 try {
110 return "≺"+std::string{path}+"≻";
111 }
112 catch(...)
114 };
115
116}//(End)namespace util
117#endif /*LIB_FILE_H*/
Inline string literal.
Definition symbol.hpp:78
Lumiera error handling (C++ interface).
Simple and lightweight helpers for metaprogramming and type detection.
const string FAILURE_INDICATOR
LumieraError< LERR_(CONFIG), Invalid > Config
Definition error.hpp:212
const string UNIX_HOMEDIR_SYMBOL
Definition file.cpp:25
bool can_exec(fs::path const &p)
Definition file.hpp:93
lib::Literal UNIX_HOMEDIR_ENV
Definition file.cpp:26
bool can_write(fs::path const &p)
Definition file.hpp:87
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:74
fs::path consolidated(fs::path rawPath)
resolves symlinks, ~ (Unix home dir) and relative specs
Definition file.hpp:61
fs::path getHomePath()
Definition file.hpp:48
bool can_read(fs::path const &p)
check if the owner has read permissions on the denoted file or directory
Definition file.hpp:81
static std::string invoke(fs::path path) noexcept
Definition file.hpp:108
failsafe invocation of custom string conversion.
Marker types to indicate a literal string and a Symbol.