-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstatus.hh
62 lines (52 loc) · 1.77 KB
/
status.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright (c) 2022 Mikael Simonsson <https://mikaelsimonsson.com>.
// SPDX-License-Identifier: BSL-1.0
// # Get information about a file path or a file descriptor
// If the file path is a symbolic link this returns information about the file the link references,
// use `file::symlink::status()` to get information about the link.
#pragma once
#include "snn-core/null_term.hh"
#include "snn-core/result.hh"
#include "snn-core/file/info.hh"
#include "snn-core/system/error.hh"
#include <sys/stat.h> // fstat, stat
namespace snn::file
{
// ## File descriptor
[[nodiscard]] inline result<file::info> status(const int fd) noexcept
{
file::info info{container::do_not_initialize};
if (::fstat(fd, &info.internal()) == 0)
{
return info;
}
return error_code{errno, system::error_category};
}
[[nodiscard]] inline result<void> status(const int fd, file::info& info) noexcept
{
if (::fstat(fd, &info.internal()) == 0)
{
return {};
}
return error_code{errno, system::error_category};
}
// ## Path
[[nodiscard]] inline result<file::info> status(
const transient<null_term<const char*>> path) noexcept
{
file::info info{container::do_not_initialize};
if (::stat(path.get().get(), &info.internal()) == 0)
{
return info;
}
return error_code{errno, system::error_category};
}
[[nodiscard]] inline result<void> status(const transient<null_term<const char*>> path,
file::info& info) noexcept
{
if (::stat(path.get().get(), &info.internal()) == 0)
{
return {};
}
return error_code{errno, system::error_category};
}
}