77#include < print>
88#include < string>
99
10+ #include < cerrno>
1011#include < pwd.h>
1112#include < sys/un.h>
12- #include < cerrno>
1313#include < sysdir.h>
1414#include < wordexp.h>
1515
1616#include " PosixSocket.hpp"
1717
18- namespace
19- {
20- std::expected<std::filesystem::path, std::string> GetApplicationSupportPath () noexcept
21- {
22- char appSupport[PATH_MAX];
23- {
24- auto state = sysdir_start_search_path_enumeration ( SYSDIR_DIRECTORY_APPLICATION_SUPPORT, SYSDIR_DOMAIN_MASK_USER);
25- state = sysdir_get_next_search_path_enumeration (state, appSupport);
26- }
18+ namespace {
19+ std::expected<std::filesystem::path, std::string>
20+ GetApplicationSupportPath () noexcept {
21+ char appSupport[PATH_MAX];
22+ {
23+ auto state = sysdir_start_search_path_enumeration (
24+ SYSDIR_DIRECTORY_APPLICATION_SUPPORT, SYSDIR_DOMAIN_MASK_USER);
25+ state = sysdir_get_next_search_path_enumeration (state, appSupport);
26+ }
2727
28- if (!appSupport[0 ]) {
29- return std::unexpected{" Failed to find Application Support directory" };
30- }
28+ if (!appSupport[0 ]) {
29+ return std::unexpected {" Failed to find Application Support directory" };
30+ }
3131
32- // Expand `~/Library/...` to `/Users/..../Library/...`
33- wordexp_t p {};
34- if (const auto ret = wordexp (appSupport, &p, WRDE_NOCMD); ret != 0 ) {
35- return std::unexpected{std::format (" wordexp failed: {} - {}" , errno, strerror (errno))};
36- }
37- std::string expandedAppSupport;
38- for (auto i = 0 ; i < p.we_wordc ; ++i) {
39- if (i == 0 ) {
40- expandedAppSupport = p.we_wordv [i];
41- } else {
42- expandedAppSupport = std::format (" {} {}" , expandedAppSupport, p.we_wordv [i]);
43- }
44- }
45- wordfree (&p);
46- return std::filesystem::path{expandedAppSupport};
32+ // Expand `~/Library/...` to `/Users/..../Library/...`
33+ wordexp_t p {};
34+ if (const auto ret = wordexp (appSupport, &p, WRDE_NOCMD); ret != 0 ) {
35+ return std::unexpected {
36+ std::format (" wordexp failed: {} - {}" , errno, strerror (errno))};
37+ }
38+ std::string expandedAppSupport;
39+ for (auto i = 0 ; i < p.we_wordc ; ++i) {
40+ if (i == 0 ) {
41+ expandedAppSupport = p.we_wordv [i];
42+ } else {
43+ expandedAppSupport
44+ = std::format (" {} {}" , expandedAppSupport, p.we_wordv [i]);
4745 }
46+ }
47+ wordfree (&p);
48+ return std::filesystem::path {expandedAppSupport};
4849}
50+ }// namespace
4951
50- std::expected<std::unique_ptr<Transport>, std::string> Transport::Open () noexcept
51- {
52- return DarwinTransport::Open ();
52+ std::expected<std::unique_ptr<Transport>, std::string>
53+ Transport::Open () noexcept {
54+ return DarwinTransport::Open ();
5355}
5456
55- std::expected<std::unique_ptr<Transport>, std::string> DarwinTransport::Open () noexcept
56- {
57- const auto appSupport = GetApplicationSupportPath ();
58- if (!appSupport) {
59- return std::unexpected{appSupport.error ()};
60- }
61- const auto path = GetSocketPath (*appSupport);
62- if (!path)
63- {
64- return std::unexpected{path.error ()};
65- }
57+ std::expected<std::unique_ptr<Transport>, std::string>
58+ DarwinTransport::Open () noexcept {
59+ const auto appSupport = GetApplicationSupportPath ();
60+ if (!appSupport) {
61+ return std::unexpected {appSupport.error ()};
62+ }
63+ const auto path = GetSocketPath (*appSupport);
64+ if (!path) {
65+ return std::unexpected {path.error ()};
66+ }
6667
67- auto fd = PosixSocket::ConnectToUnixSocket (*path);
68- if (!fd) {
69- return std::unexpected{fd.error ()};
70- }
68+ auto fd = PosixSocket::ConnectToUnixSocket (*path);
69+ if (!fd) {
70+ return std::unexpected {fd.error ()};
71+ }
72+
73+ std::println (" Connected to unix domain socket: {}" , path->string ());
74+ return std::unique_ptr<DarwinTransport> {new DarwinTransport (*std::move (fd))};
75+ }
7176
72- std::println (" Connected to unix domain socket: {}" , path->string ());
73- return std::unique_ptr<DarwinTransport>{new DarwinTransport (*std::move (fd))};
77+ std::expected<size_t , std::string> DarwinTransport::Read (
78+ void * buffer,
79+ const size_t bufferSize) noexcept {
80+ return PosixSocket::Read (*mFD , buffer, bufferSize);
7481}
7582
76- std::expected<size_t , std::string> DarwinTransport::Read (void * buffer, const size_t bufferSize) noexcept
77- {
78- return PosixSocket::Read (*mFD , buffer, bufferSize);
83+ std::expected<void , std::string> DarwinTransport::Write (
84+ const void * buffer,
85+ const size_t bufferSize) noexcept {
86+ return PosixSocket::Write (*mFD , buffer, bufferSize);
7987}
8088
81- DarwinTransport::DarwinTransport (unique_fd fd) noexcept : mFD(std::move(fd))
82- {
89+ DarwinTransport::DarwinTransport (unique_fd fd) noexcept : mFD(std::move(fd)) {
8390}
8491
8592DarwinTransport::~DarwinTransport () = default ;
0 commit comments