Skip to content

Commit 4d3f6fc

Browse files
committed
workshop/{Library,Plan}*: use directory FileDescriptor instead of std::filesystem
1 parent 935c67a commit 4d3f6fc

File tree

4 files changed

+28
-26
lines changed

4 files changed

+28
-26
lines changed

src/workshop/Library.hxx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
#pragma once
66

77
#include "io/Logger.hxx"
8+
#include "io/UniqueFileDescriptor.hxx"
89

910
#include <chrono>
10-
#include <filesystem>
1111
#include <memory>
1212
#include <string>
1313
#include <map>
@@ -53,7 +53,8 @@ class Library {
5353

5454
const LLogger logger;
5555

56-
const std::filesystem::path path;
56+
const std::string path;
57+
const UniqueFileDescriptor directory_fd;
5758

5859
std::map<std::string, PlanEntry, std::less<>> plans;
5960

@@ -63,14 +64,12 @@ class Library {
6364
struct statx_timestamp mtime{};
6465

6566
public:
66-
explicit Library(std::filesystem::path &&_path) noexcept
67-
:logger("library"),
68-
path(std::move(_path)) {}
67+
explicit Library(const char *path);
6968

7069
Library(const Library &other) = delete;
7170

72-
const std::filesystem::path &GetPath() const noexcept {
73-
return path;
71+
const FileDescriptor GetDirectory() const noexcept {
72+
return directory_fd;
7473
}
7574

7675
/**

src/workshop/MultiLibrary.hxx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ class MultiLibrary {
1616
std::forward_list<Library> libraries;
1717

1818
public:
19-
void InsertPath(std::filesystem::path &&_path) {
20-
libraries.emplace_front(std::move(_path));
19+
void InsertPath(const char *_path) {
20+
libraries.emplace_front(_path);
2121
}
2222

2323
bool Update(std::chrono::steady_clock::time_point now, bool force) {

src/workshop/PlanLibrary.cxx

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
#include "Plan.hxx"
77
#include "StatxTimestamp.hxx"
88
#include "lib/fmt/ExceptionFormatter.hxx"
9+
#include "io/DirectoryReader.hxx"
10+
#include "io/FileAt.hxx"
11+
#include "io/FileName.hxx"
12+
#include "io/Open.hxx"
913
#include "util/CharUtil.hxx"
1014

11-
#include <fmt/std.h>
12-
1315
#include <assert.h>
1416
#include <fcntl.h> // for AT_*
1517
#include <unistd.h>
@@ -20,6 +22,11 @@
2022
#include <sys/stat.h>
2123
#include <time.h>
2224

25+
Library::Library(const char *_path)
26+
:logger("library"),
27+
path(_path),
28+
directory_fd(OpenPath(_path, O_DIRECTORY)) {}
29+
2330
static constexpr bool
2431
is_valid_plan_name_char(char ch) noexcept
2532
{
@@ -52,29 +59,29 @@ Library::UpdatePlans(std::chrono::steady_clock::time_point now)
5259

5360
bool modified = false;
5461

55-
for (const auto &entry : std::filesystem::directory_iterator(path)) {
56-
const auto name = entry.path().filename();
57-
if (!is_valid_plan_name(name.c_str()))
62+
DirectoryReader dr{OpenDirectory({directory_fd, "."})};
63+
while (const char *filename = dr.Read()) {
64+
if (IsSpecialFilename(filename) || !is_valid_plan_name(filename))
5865
continue;
5966

60-
auto old_i = old_plans.find(name.native());
67+
auto old_i = old_plans.find(filename);
6168
decltype(old_i) i;
6269

6370
if (old_i != old_plans.end()) {
6471
i = plans.emplace(std::piecewise_construct,
65-
std::forward_as_tuple(name.native()),
72+
std::forward_as_tuple(filename),
6673
std::forward_as_tuple(std::move(old_i->second)))
6774
.first;
6875
old_plans.erase(old_i);
6976
} else {
7077
i = plans.emplace(std::piecewise_construct,
71-
std::forward_as_tuple(name.native()),
78+
std::forward_as_tuple(filename),
7279
std::forward_as_tuple())
7380
.first;
7481
modified = true;
7582
}
7683

77-
if (UpdatePlan(name.c_str(), i->second, now))
84+
if (UpdatePlan(filename, i->second, now))
7885
modified = true;
7986
}
8087

src/workshop/PlanUpdate.cxx

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
#include "lib/fmt/ExceptionFormatter.hxx"
1010
#include "util/Exception.hxx"
1111

12-
#include <fmt/std.h>
13-
1412
#include <assert.h>
1513
#include <fcntl.h> // for AT_*
1614
#include <stdlib.h>
@@ -33,16 +31,14 @@ Library::CheckPlanModified(const char *name, PlanEntry &entry,
3331
{
3432
int ret;
3533

36-
const auto plan_path = GetPath() / name;
37-
3834
struct statx stx;
39-
ret = statx(-1, plan_path.c_str(), AT_STATX_FORCE_SYNC,
35+
ret = statx(directory_fd.Get(), name, AT_STATX_FORCE_SYNC,
4036
STATX_TYPE|STATX_MTIME,
4137
&stx);
4238
if (ret < 0) {
4339
if (ret != ENOENT)
44-
logger.Fmt(2, "failed to stat {:?}: {}",
45-
plan_path, strerror(errno));
40+
logger.Fmt(2, "failed to stat {:?}/{:?}: {}",
41+
path, name, strerror(errno));
4642

4743
entry.Clear();
4844

@@ -114,7 +110,7 @@ Library::LoadPlan(const char *name, PlanEntry &entry,
114110

115111
logger.Fmt(6, "loading plan {:?}", name);
116112

117-
const auto plan_path = GetPath() / name;
113+
const auto plan_path = fmt::format("{}/{}", path, name);
118114

119115
try {
120116
entry.plan.reset(new Plan(LoadPlanFile(plan_path)));

0 commit comments

Comments
 (0)