aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.posix/stat.ccm
blob: ac19f97701020bfc7e46e8eaeefce538dc487243 (plain) (blame)
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
// This source code is released into the public domain.
module;

// Basic wrappers around stat() and fstat().

#include <sys/stat.h>

export module nihil.posix:stat;

import nihil.std;
import nihil.core;

import :fd;

namespace nihil {

export [[nodiscard]] auto
stat(std::filesystem::path const &path) -> std::expected<struct ::stat, error>
{
	auto guard = save_errno();

	struct ::stat sb{};
	auto ret = ::stat(path.string().c_str(), &sb);
	if (ret == -1)
		return std::unexpected(error(sys_error()));
	return sb;
}

export [[nodiscard]] auto stat(fd const &fd) -> std::expected<struct ::stat, error>
{
	auto guard = save_errno();

	struct ::stat sb{};
	auto ret = ::fstat(fd.get(), &sb);
	if (ret == -1)
		return std::unexpected(error(sys_error()));
	return sb;
}

} // namespace nihil