Skip to content

Commit e4267e2

Browse files
committed
More Windows related fix
1. Support std::fs::path in fcitx::Library 2. Add StandardPaths::openPath to handle common open(fs::path, O_RDONLY).
1 parent 5470686 commit e4267e2

File tree

12 files changed

+69
-42
lines changed

12 files changed

+69
-42
lines changed

.github/workflows/windows.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ on:
1111

1212
jobs:
1313
check-windows:
14+
name: Build on Windows
1415
runs-on: windows-2025
1516
strategy:
1617
fail-fast: false

src/lib/fcitx-utils/library.cpp

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,18 @@
1313
#include <cerrno>
1414
#include <cstdlib>
1515
#include <cstring>
16+
#include <filesystem>
1617
#include <functional>
1718
#include <memory>
1819
#include <string>
1920
#include <utility>
2021
#include "config.h"
22+
#include "flags.h"
2123
#include "macros.h"
2224
#include "misc.h"
25+
#include "standardpaths.h"
2326
#include "stringutils.h"
27+
#include "unixfd.h"
2428

2529
#ifdef HAVE_SYS_MMAN_H
2630
#include <sys/mman.h>
@@ -40,7 +44,8 @@ constexpr bool hasRTLDNoDelete = false;
4044

4145
class LibraryPrivate {
4246
public:
43-
LibraryPrivate(std::string path) : path_(std::move(path)) {}
47+
LibraryPrivate(std::filesystem::path path)
48+
: path_(std::move(path)), pathStr_(path_.string()) {}
4449
~LibraryPrivate() { unload(); }
4550

4651
bool unload() {
@@ -61,14 +66,17 @@ class LibraryPrivate {
6166
handle_ = nullptr;
6267
return true;
6368
}
64-
65-
std::string path_;
69+
const std::filesystem::path path_;
70+
const std::string pathStr_;
6671
void *handle_ = nullptr;
6772
std::string error_;
6873
Flags<fcitx::LibraryLoadHint> loadFlag_;
6974
};
7075

7176
Library::Library(const std::string &path)
77+
: Library(std::filesystem::path(path)) {}
78+
79+
Library::Library(const std::filesystem::path &path)
7280
: d_ptr(std::make_unique<LibraryPrivate>(path)) {}
7381

7482
FCITX_DEFINE_DEFAULT_DTOR_AND_MOVE(Library)
@@ -99,12 +107,13 @@ bool Library::load(Flags<fcitx::LibraryLoadHint> hint) {
99107
if (hint & LibraryLoadHint::NewNameSpace) {
100108
// allow dlopen self
101109
d->handle_ = dlmopen(
102-
LM_ID_NEWLM, !d->path_.empty() ? d->path_.c_str() : nullptr, flag);
110+
LM_ID_NEWLM,
111+
!d->path_.empty() ? d->path_.string().c_str() : nullptr, flag);
103112
} else
104113
#endif
105114
{
106-
d->handle_ =
107-
dlopen(!d->path_.empty() ? d->path_.c_str() : nullptr, flag);
115+
d->handle_ = dlopen(
116+
!d->path_.empty() ? d->path_.string().c_str() : nullptr, flag);
108117
}
109118
if (!d->handle_) {
110119
d->error_ = dlerror();
@@ -153,8 +162,8 @@ bool Library::findData(const char *slug, const char *magic, size_t lenOfMagic,
153162
return true;
154163
}
155164

156-
int fd = open(d->path_.c_str(), O_RDONLY);
157-
if (fd < 0) {
165+
UnixFD fd = StandardPaths::openPath(d->path_);
166+
if (!fd.isValid()) {
158167
d->error_ = strerror(errno);
159168
return false;
160169
}
@@ -163,14 +172,15 @@ bool Library::findData(const char *slug, const char *magic, size_t lenOfMagic,
163172
bool result = false;
164173
do {
165174
struct stat statbuf;
166-
int statresult = fstat(fd, &statbuf);
175+
int statresult = fstat(fd.fd(), &statbuf);
167176
if (statresult < 0) {
168177
d->error_ = strerror(errno);
169178
break;
170179
}
171180
void *data = nullptr;
172181
#ifdef HAVE_SYS_MMAN_H
173-
data = mmap(nullptr, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
182+
data =
183+
mmap(nullptr, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd.fd(), 0);
174184
void *needunmap = nullptr;
175185
needunmap = data;
176186
#endif
@@ -180,7 +190,7 @@ bool Library::findData(const char *slug, const char *magic, size_t lenOfMagic,
180190
if (!data) {
181191
break;
182192
}
183-
if (read(fd, data, statbuf.st_size) != statbuf.st_size) {
193+
if (read(fd.fd(), data, statbuf.st_size) != statbuf.st_size) {
184194
break;
185195
}
186196
}
@@ -200,8 +210,6 @@ bool Library::findData(const char *slug, const char *magic, size_t lenOfMagic,
200210
#endif
201211
} while (false);
202212

203-
close(fd);
204-
205213
return result;
206214
}
207215

@@ -219,6 +227,11 @@ bool Library::isNewNamespaceSupported() {
219227
}
220228

221229
const std::string &Library::path() const {
230+
FCITX_D();
231+
return d->pathStr_;
232+
}
233+
234+
const std::filesystem::path &Library::fspath() const {
222235
FCITX_D();
223236
return d->path_;
224237
}

src/lib/fcitx-utils/library.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
/// \brief Class to handler dynamic library.
1414

1515
#include <cstddef>
16+
#include <filesystem>
1617
#include <functional>
1718
#include <memory>
1819
#include <string>
@@ -35,7 +36,8 @@ class LibraryPrivate;
3536

3637
class FCITXUTILS_EXPORT Library {
3738
public:
38-
Library(const std::string &path = {});
39+
explicit Library(const std::string &path);
40+
explicit Library(const std::filesystem::path &path = {});
3941
FCITX_DECLARE_VIRTUAL_DTOR_MOVE(Library);
4042

4143
bool loaded() const;
@@ -46,6 +48,7 @@ class FCITXUTILS_EXPORT Library {
4648
const std::function<void(const char *data)> &parser);
4749
std::string error();
4850
const std::string &path() const;
51+
const std::filesystem::path &fspath() const;
4952

5053
template <typename Func>
5154
static auto toFunction(void *ptr) {

src/lib/fcitx-utils/standardpath.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <unistd.h>
1414
#include <algorithm>
1515
#include <atomic>
16+
#include <cassert>
1617
#include <cstdint>
1718
#include <cstdio>
1819
#include <cstdlib>
@@ -21,6 +22,7 @@
2122
#include <functional>
2223
#include <map>
2324
#include <memory>
25+
#include <optional>
2426
#include <stdexcept>
2527
#include <string>
2628
#include <string_view>
@@ -670,7 +672,7 @@ std::map<std::string, std::string> StandardPath::locateWithFilter(
670672
scanFiles(type, path,
671673
[&result, &filter](const std::string &path,
672674
const std::string &dir, bool isUser) {
673-
if (!result.count(path) && filter(path, dir, isUser)) {
675+
if (!result.contains(path) && filter(path, dir, isUser)) {
674676
auto fullPath = constructPath(dir, path);
675677
if (fs::isreg(fullPath)) {
676678
result.emplace(path, std::move(fullPath));
@@ -691,7 +693,7 @@ StandardPathFileMap StandardPath::multiOpenFilter(
691693
scanFiles(type, path,
692694
[&result, flags, &filter](const std::string &path,
693695
const std::string &dir, bool isUser) {
694-
if (!result.count(path) && filter(path, dir, isUser)) {
696+
if (!result.contains(path) && filter(path, dir, isUser)) {
695697
auto fullPath = constructPath(dir, path);
696698
int fd = ::open(fullPath.c_str(), flags);
697699
if (fd >= 0) {

src/lib/fcitx-utils/standardpaths.cpp

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include <algorithm>
1414
#include <atomic>
1515
#include <cassert>
16-
#include <chrono>
1716
#include <cstdint>
1817
#include <cstdio>
1918
#include <cstdlib>
@@ -37,6 +36,7 @@
3736
#include <vector>
3837
#include <ranges>
3938
#include <span>
39+
#include "fcitx-utils/unixfd.h"
4040
#include "config.h" // IWYU pragma: keep
4141
#include "environ.h"
4242
#include "fs.h"
@@ -474,20 +474,28 @@ UnixFD StandardPaths::open(StandardPathsType type,
474474
std::filesystem::path *outPath) const {
475475
FCITX_D();
476476
UnixFD retFD;
477-
d->scanDirectories(
478-
type, path, modes, [&retFD, outPath](std::filesystem::path fullPath) {
479-
retFD.give(::open(fullPath.string().c_str(), O_RDONLY));
480-
if (!retFD.isValid()) {
481-
return true;
482-
}
483-
if (outPath) {
484-
*outPath = std::move(fullPath);
485-
}
486-
return false;
487-
});
477+
d->scanDirectories(type, path, modes,
478+
[&retFD, outPath](std::filesystem::path fullPath) {
479+
retFD = openPath(fullPath);
480+
if (!retFD.isValid()) {
481+
return true;
482+
}
483+
if (outPath) {
484+
*outPath = std::move(fullPath);
485+
}
486+
return false;
487+
});
488488
return retFD;
489489
}
490490

491+
UnixFD StandardPaths::openPath(const std::filesystem::path &path) {
492+
#ifdef _WIN32
493+
return UnixFD::own(::_wopen(path.c_str(), O_RDONLY));
494+
#else
495+
return UnixFD::own(::open(path.c_str(), O_RDONLY));
496+
#endif
497+
}
498+
491499
std::vector<UnixFD>
492500
StandardPaths::openAll(StandardPathsType type,
493501
const std::filesystem::path &path,
@@ -500,8 +508,7 @@ StandardPaths::openAll(StandardPathsType type,
500508
}
501509
d->scanDirectories(type, path, modes,
502510
[&retFDs, outPaths](std::filesystem::path fullPath) {
503-
UnixFD fd;
504-
fd.give(::open(fullPath.string().c_str(), O_RDONLY));
511+
UnixFD fd = StandardPaths::openPath(fullPath);
505512
if (!fd.isValid()) {
506513
return true;
507514
}

src/lib/fcitx-utils/standardpaths.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ class FCITXUTILS_EXPORT StandardPaths {
143143
StandardPathsModes modes = StandardPathsMode::Default,
144144
std::filesystem::path *outPath = nullptr) const;
145145

146+
/** \brief Open the path for read. */
147+
static UnixFD openPath(const std::filesystem::path &path);
148+
146149
/** \brief Open the all matched and file for read.
147150
*/
148151
std::vector<UnixFD>

src/lib/fcitx/addonmanager.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include "fcitx-utils/misc_p.h"
2323
#include "fcitx-utils/semver.h"
2424
#include "fcitx-utils/standardpaths.h"
25-
#include "fcitx-utils/unixfd.h"
2625
#include "addoninfo.h"
2726
#include "addoninstance.h"
2827
#include "addoninstance_p.h"
@@ -278,8 +277,7 @@ void AddonManager::load(const std::unordered_set<std::string> &enabled,
278277
}
279278

280279
RawConfig config;
281-
UnixFD fd = UnixFD::own(open(fullName.c_str(), O_RDONLY));
282-
readFromIni(config, fd.fd());
280+
readAsIni(config, StandardPathsType::PkgData, fullName);
283281

284282
// override configuration
285283
auto addon = std::make_unique<Addon>(name, config);

src/lib/fcitx/icontheme.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ class IconThemeCache {
207207
#ifdef _WIN32
208208
FCITX_UNUSED(filename);
209209
#else
210-
auto fd = UnixFD::own(open(filename.string().c_str(), O_RDONLY));
210+
auto fd = StandardPaths::openPath(filename);
211211
if (!fd.isValid()) {
212212
return;
213213
}
@@ -745,7 +745,7 @@ IconTheme::IconTheme(const std::string &name, IconTheme *parent,
745745
readFromIni(config, file.fd());
746746
}
747747
auto path = d->home_ / ".icons" / name / "index.theme";
748-
auto fd = UnixFD::own(open(path.c_str(), O_RDONLY));
748+
auto fd = StandardPaths::openPath(path);
749749
if (fd.fd() >= 0) {
750750
readFromIni(config, fd.fd());
751751
}
@@ -774,8 +774,8 @@ IconTheme::IconTheme(const std::string &name, IconTheme *parent,
774774
readFromIni(config, file.fd());
775775
}
776776
auto path = d->home_ / ".icons" / name / "index.theme";
777-
auto fd = UnixFD::own(open(path.string().c_str(), O_RDONLY));
778-
if (fd.fd() >= 0) {
777+
auto fd = StandardPaths::openPath(path);
778+
if (fd.isValid()) {
779779
readFromIni(config, fd.fd());
780780
}
781781

@@ -884,7 +884,7 @@ std::string IconTheme::defaultIconThemeName() {
884884
std::filesystem::path(*home) / ".kde/share/config/kdeglobals",
885885
"/etc/kde4/kdeglobals"};
886886
for (auto &file : files) {
887-
auto fd = UnixFD::own(open(file.c_str(), O_RDONLY));
887+
auto fd = StandardPaths::openPath(file);
888888
auto theme = getKdeTheme(fd.fd());
889889
if (!theme.empty()) {
890890
return theme;

src/lib/fcitx/inputmethodmanager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ void InputMethodManagerPrivate::loadStaticEntries(
176176
continue;
177177
}
178178
RawConfig config;
179-
UnixFD fd = UnixFD::own(open(fullName.c_str(), O_RDONLY));
179+
UnixFD fd = StandardPaths::openPath(fullName);
180180
readFromIni(config, fd.fd());
181181

182182
InputMethodInfo imInfo;

src/modules/quickphrase/quickphraseprovider.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ void BuiltInQuickPhraseProvider::reloadConfig() {
6363
if (disableFiles.contains(path)) {
6464
continue;
6565
}
66-
UnixFD fd = UnixFD::own(open(p.second.c_str(), O_RDONLY));
66+
UnixFD fd = StandardPaths::openPath(p.second);
6767
if (fd.isValid()) {
6868
load(fd.fd());
6969
}

0 commit comments

Comments
 (0)