Skip to content

Commit 4ece254

Browse files
committed
feat(utils): add std::error_code overloads for mknodat and symlink_at
Callers can opt into error_code path to avoid exception overhead on non-critical paths (e.g. best-effort device/symlink creation).
1 parent ceb40a4 commit 4ece254

4 files changed

Lines changed: 47 additions & 6 deletions

File tree

src/linyaps_box/utils/mknod.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2022-2025 UnionTech Software Technology Co., Ltd.
1+
// SPDX-FileCopyrightText: 2022 - 2026 UnionTech Software Technology Co., Ltd.
22
//
33
// SPDX-License-Identifier: LGPL-3.0-or-later
44

@@ -16,8 +16,25 @@ void linyaps_box::utils::mknodat(const file_descriptor &root,
1616
{
1717
LINYAPS_BOX_DEBUG() << "Create device " << path.string() << " with mode " << mode << " and dev "
1818
<< dev;
19+
1920
if (::mknodat(root.get(), path.c_str(), mode, dev) == 0) {
2021
return;
2122
}
2223
throw std::system_error(errno, std::system_category(), "mknodat");
2324
}
25+
26+
void linyaps_box::utils::mknodat(const file_descriptor &root,
27+
const std::filesystem::path &path,
28+
mode_t mode,
29+
dev_t dev,
30+
std::error_code &ec) noexcept
31+
{
32+
LINYAPS_BOX_DEBUG() << "Create device " << path.string() << " with mode " << mode << " and dev "
33+
<< dev;
34+
35+
ec.clear();
36+
if (::mknodat(root.get(), path.c_str(), mode, dev) == 0) {
37+
return;
38+
}
39+
ec.assign(errno, std::system_category());
40+
}

src/linyaps_box/utils/mknod.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2022-2025 UnionTech Software Technology Co., Ltd.
1+
// SPDX-FileCopyrightText: 2022 - 2026 UnionTech Software Technology Co., Ltd.
22
//
33
// SPDX-License-Identifier: LGPL-3.0-or-later
44

@@ -15,4 +15,10 @@ void mknodat(const file_descriptor &root,
1515
const std::filesystem::path &path,
1616
mode_t mode,
1717
dev_t dev);
18+
19+
void mknodat(const file_descriptor &root,
20+
const std::filesystem::path &path,
21+
mode_t mode,
22+
dev_t dev,
23+
std::error_code &ec) noexcept;
1824
} // namespace linyaps_box::utils

src/linyaps_box/utils/symlink.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2025 UnionTech Software Technology Co., Ltd.
1+
// SPDX-FileCopyrightText: 2025 - 2026 UnionTech Software Technology Co., Ltd.
22
//
33
// SPDX-License-Identifier: LGPL-3.0-or-later
44

@@ -9,8 +9,6 @@
99

1010
#include <linux/limits.h>
1111

12-
#include <array>
13-
1412
void linyaps_box::utils::symlink(const std::filesystem::path &target,
1513
const std::filesystem::path &link_path)
1614
{
@@ -36,6 +34,21 @@ void linyaps_box::utils::symlink_at(const std::filesystem::path &target,
3634
}
3735
}
3836

37+
void linyaps_box::utils::symlink_at(const std::filesystem::path &target,
38+
const file_descriptor &dirfd,
39+
const std::filesystem::path &link_path,
40+
std::error_code &ec) noexcept
41+
{
42+
LINYAPS_BOX_DEBUG() << "Create symlink " << link_path << " which under " << dirfd.current_path()
43+
<< " point to " << target;
44+
45+
ec.clear();
46+
const auto ret = ::symlinkat(target.c_str(), dirfd.get(), link_path.c_str());
47+
if (ret == -1) {
48+
ec.assign(errno, std::system_category());
49+
}
50+
}
51+
3952
std::filesystem::path linyaps_box::utils::readlink(const std::filesystem::path &path)
4053
{
4154
std::error_code ec;

src/linyaps_box/utils/symlink.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2025 UnionTech Software Technology Co., Ltd.
1+
// SPDX-FileCopyrightText: 2025 - 2026 UnionTech Software Technology Co., Ltd.
22
//
33
// SPDX-License-Identifier: LGPL-3.0-or-later
44

@@ -16,6 +16,11 @@ void symlink_at(const std::filesystem::path &target,
1616
const file_descriptor &dirfd,
1717
const std::filesystem::path &link_path);
1818

19+
void symlink_at(const std::filesystem::path &target,
20+
const file_descriptor &dirfd,
21+
const std::filesystem::path &link_path,
22+
std::error_code &ec) noexcept;
23+
1924
std::filesystem::path readlink(const std::filesystem::path &path);
2025

2126
std::filesystem::path readlinkat(const file_descriptor &dirfd, const std::filesystem::path &path);

0 commit comments

Comments
 (0)