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
4145class LibraryPrivate {
4246public:
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
7176Library::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
7482FCITX_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
221229const 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}
0 commit comments