Skip to content

Commit 33323a9

Browse files
committed
fix dos
1 parent 6d9034e commit 33323a9

File tree

2 files changed

+79
-64
lines changed

2 files changed

+79
-64
lines changed

include/fast_io_hosted/filesystem/dos_at.h

Lines changed: 12 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -111,65 +111,16 @@ inline constexpr dos_at_flags &operator^=(dos_at_flags &x, dos_at_flags y) noexc
111111
namespace details
112112
{
113113

114-
struct my_dos_concat_path_common_result
115-
{
116-
bool failed{};
117-
::fast_io::tlc::string path = ::fast_io::tlc::string();
118-
};
119-
120-
inline my_dos_concat_path_common_result my_dos_concat_path_common(int dirfd, char const *pathname) noexcept
121-
{
122-
if (dirfd == -100)
123-
{
124-
return {false, ::fast_io::tlc::string(::fast_io::mnp::os_c_str(pathname))};
125-
}
126-
else
127-
{
128-
auto pathname_cstr{::fast_io::noexcept_call(::__get_fd_name, dirfd)};
129-
if (pathname_cstr == nullptr) [[unlikely]]
130-
{
131-
return {true};
132-
}
133-
134-
// check vaildity
135-
auto const sz{::fast_io::cstr_len(pathname)};
136-
137-
if (sz > 255) [[unlikely]]
138-
{
139-
return {true};
140-
}
141-
142-
if (::fast_io::details::is_invalid_dos_filename_with_size(pathname, sz)) [[unlikely]]
143-
{
144-
return {true};
145-
}
146-
147-
// concat
148-
return {false, ::fast_io::tlc::concat_fast_io_tlc(::fast_io::mnp::os_c_str(pathname_cstr), ::fast_io::mnp::chvw(u8'\\'), para_pathname)};
149-
}
150-
}
151-
152-
template <bool always_terminate = true>
153-
inline ::fast_io::tlc::string my_dos_concat_path(int dirfd, char const *pathname) noexcept(always_terminate)
154-
{
155-
auto [failed, path] = ::fast_io::details::my_dos_concat_path_common(dirfd, pathname);
156-
if (failed) [[unlikely]]
157-
{
158-
::fast_io::system_call_throw_error<always_terminate>(-1);
159-
}
160-
return ::std::move(path);
161-
}
162-
163114
inline void dos_renameat_impl(int olddirfd, char const *oldpath, int newdirfd, char const *newpath)
164115
{
165-
::fast_io::system_call_throw_error(::fast_io::posix::my_dos_rename(::fast_io::details::my_dos_concat_path(olddirfd, oldpath).c_str(),
166-
::fast_io::details::my_dos_concat_path(newdirfd, newpath).c_str()));
116+
::fast_io::system_call_throw_error(::fast_io::posix::my_dos_rename(::fast_io::details::my_dos_concat_tlc_path(olddirfd, oldpath).c_str(),
117+
::fast_io::details::my_dos_concat_tlc_path(newdirfd, newpath).c_str()));
167118
}
168119

169120
inline void dos_linkat_impl(int olddirfd, char const *oldpath, int newdirfd, char const *newpath)
170121
{
171-
::fast_io::system_call_throw_error(::fast_io::posix::my_dos_link(::fast_io::details::my_dos_concat_path(olddirfd, oldpath).c_str(),
172-
::fast_io::details::my_dos_concat_path(newdirfd, newpath).c_str()));
122+
::fast_io::system_call_throw_error(::fast_io::posix::my_dos_link(::fast_io::details::my_dos_concat_tlc_path(olddirfd, oldpath).c_str(),
123+
::fast_io::details::my_dos_concat_tlc_path(newdirfd, newpath).c_str()));
173124
}
174125

175126
template <posix_api_22 dsp, typename... Args>
@@ -187,7 +138,7 @@ inline auto dos22_api_dispatcher(int olddirfd, char const *oldpath, int newdirfd
187138

188139
inline void dos_symlinkat_impl(char const *oldpath, int newdirfd, char const *newpath)
189140
{
190-
::fast_io::system_call_throw_error(::fast_io::posix::my_dos_symlink(oldpath, ::fast_io::details::my_dos_concat_path(newdirfd, newpath).c_str()));
141+
::fast_io::system_call_throw_error(::fast_io::posix::my_dos_symlink(oldpath, ::fast_io::details::my_dos_concat_tlc_path(newdirfd, newpath).c_str()));
191142
}
192143

193144
template <posix_api_12 dsp, typename... Args>
@@ -201,37 +152,37 @@ inline auto dos12_api_dispatcher(char const *oldpath, int newdirfd, char const *
201152

202153
inline void dos_faccessat_impl(int dirfd, char const *pathname, int flags)
203154
{
204-
::fast_io::system_call_throw_error(::fast_io::posix::my_dos_access(::fast_io::details::my_dos_concat_path(dirfd, pathname).c_str(), flags));
155+
::fast_io::system_call_throw_error(::fast_io::posix::my_dos_access(::fast_io::details::my_dos_concat_tlc_path(dirfd, pathname).c_str(), flags));
205156
}
206157

207158
inline void dos_fchownat_impl(int dirfd, char const *pathname, uintmax_t owner, uintmax_t group)
208159
{
209160
// chown does nothing under MS-DOS, so just check is_valid filename
210-
::fast_io::system_call_throw_error(::fast_io::posix::my_dos_chown(::fast_io::details::my_dos_concat_path(dirfd, pathname).c_str(),
161+
::fast_io::system_call_throw_error(::fast_io::posix::my_dos_chown(::fast_io::details::my_dos_concat_tlc_path(dirfd, pathname).c_str(),
211162
static_cast<int>(owner), static_cast<int>(group)));
212163
}
213164

214165
inline void dos_fchmodat_impl(int dirfd, char const *pathname, mode_t mode)
215166
{
216-
::fast_io::system_call_throw_error(::fast_io::posix::my_dos_chmod(::fast_io::details::my_dos_concat_path(dirfd, pathname).c_str(), mode));
167+
::fast_io::system_call_throw_error(::fast_io::posix::my_dos_chmod(::fast_io::details::my_dos_concat_tlc_path(dirfd, pathname).c_str(), mode));
217168
}
218169

219170
inline posix_file_status dos_fstatat_impl(int dirfd, char const *pathname)
220171
{
221172
struct stat buf;
222173

223-
::fast_io::system_call_throw_error(::fast_io::posix::my_dos_stat(::fast_io::details::my_dos_concat_path(dirfd, pathname).c_str(), __builtin_addressof(buf)));
174+
::fast_io::system_call_throw_error(::fast_io::posix::my_dos_stat(::fast_io::details::my_dos_concat_tlc_path(dirfd, pathname).c_str(), __builtin_addressof(buf)));
224175
return ::fast_io::details::struct_stat_to_posix_file_status(buf);
225176
}
226177

227178
inline void dos_mkdirat_impl(int dirfd, char const *pathname, mode_t mode)
228179
{
229-
::fast_io::system_call_throw_error(::fast_io::posix::my_dos_mkdir(::fast_io::details::my_dos_concat_path(dirfd, pathname).c_str(), mode));
180+
::fast_io::system_call_throw_error(::fast_io::posix::my_dos_mkdir(::fast_io::details::my_dos_concat_tlc_path(dirfd, pathname).c_str(), mode));
230181
}
231182

232183
inline void dos_unlinkat_impl(int dirfd, char const *pathname)
233184
{
234-
::fast_io::system_call_throw_error(::fast_io::posix::my_dos_unlink(::fast_io::details::my_dos_concat_path(dirfd, pathname).c_str()));
185+
::fast_io::system_call_throw_error(::fast_io::posix::my_dos_unlink(::fast_io::details::my_dos_concat_tlc_path(dirfd, pathname).c_str()));
235186
}
236187

237188
inline constexpr ::std::time_t unix_timestamp_to_time_t(unix_timestamp stmp) noexcept
@@ -271,7 +222,7 @@ inline void dos_utimensat_impl(int dirfd, char const *pathname, unix_timestamp_o
271222
::fast_io::details::unix_timestamp_to_time_t(last_modification_time),
272223
};
273224

274-
::fast_io::system_call_throw_error(::fast_io::posix::my_dos_utime(::fast_io::details::my_dos_concat_path(dirfd, pathname).c_str(), __builtin_addressof(ts)));
225+
::fast_io::system_call_throw_error(::fast_io::posix::my_dos_utime(::fast_io::details::my_dos_concat_tlc_path(dirfd, pathname).c_str(), __builtin_addressof(ts)));
275226
}
276227

277228
template <posix_api_1x dsp, typename... Args>

include/fast_io_hosted/platforms/posix.h

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161

6262
namespace fast_io
6363
{
64+
6465
#if ((!defined(_WIN32) || defined(__WINE__)) || defined(__CYGWIN__))
6566
namespace posix
6667
{
@@ -846,13 +847,76 @@ extern int my_posix_open_noexcept(char const *pathname, int flags, mode_t mode)
846847

847848
#if defined(__MSDOS__)
848849

849-
template <bool always_terminate>
850-
inline ::fast_io::tlc::string my_dos_concat_path(int, char const *) noexcept(always_terminate);
850+
using dos_path_tlc_string = ::fast_io::containers::basic_string<char, ::fast_io::native_thread_local_allocator>;
851+
852+
template <typename... Args>
853+
constexpr inline dos_path_tlc_string concat_dos_path_tlc_string(Args &&...args)
854+
{
855+
constexpr bool type_error{::fast_io::operations::defines::print_freestanding_okay<::fast_io::details::dummy_buffer_output_stream<char>, Args...>};
856+
if constexpr (type_error)
857+
{
858+
return ::fast_io::basic_general_concat<false, char, dos_path_tlc_string>(::fast_io::io_print_forward<char>(::fast_io::io_print_alias(args))...);
859+
}
860+
else
861+
{
862+
static_assert(type_error, "some types are not printable, so we cannot concat dos_path_tlc_string");
863+
return {};
864+
}
865+
}
866+
867+
struct my_dos_concat_tlc_path_common_result
868+
{
869+
bool failed{};
870+
dos_path_tlc_string path{};
871+
};
872+
873+
inline constexpr my_dos_concat_tlc_path_common_result my_dos_concat_tlc_path_common(int dirfd, char const *pathname) noexcept
874+
{
875+
if (dirfd == -100)
876+
{
877+
return {false, dos_path_tlc_string{::fast_io::mnp::os_c_str(pathname)}};
878+
}
879+
else
880+
{
881+
auto fd_pathname_cstr{::fast_io::noexcept_call(::__get_fd_name, dirfd)};
882+
if (fd_pathname_cstr == nullptr) [[unlikely]]
883+
{
884+
return {true};
885+
}
886+
887+
// check vaildity
888+
auto const sz{::fast_io::cstr_len(pathname)};
889+
890+
if (sz > 255) [[unlikely]]
891+
{
892+
return {true};
893+
}
894+
895+
if (::fast_io::details::is_invalid_dos_filename_with_size(pathname, sz)) [[unlikely]]
896+
{
897+
return {true};
898+
}
899+
900+
// concat
901+
return {false, concat_dos_path_tlc_string(::fast_io::mnp::os_c_str(fd_pathname_cstr), ::fast_io::mnp::chvw('\\'), ::fast_io::mnp::os_c_str(pathname))};
902+
}
903+
}
904+
905+
template <bool always_terminate = true>
906+
inline constexpr dos_path_tlc_string my_dos_concat_tlc_path(int dirfd, char const *pathname) noexcept(always_terminate)
907+
{
908+
auto [failed, path]{my_dos_concat_tlc_path_common(dirfd, pathname)};
909+
if (failed) [[unlikely]]
910+
{
911+
::fast_io::system_call_throw_error<always_terminate>(-1);
912+
}
913+
return path;
914+
}
851915

852916
template <bool always_terminate = false>
853917
inline int my_posix_openat(int dirfd, char const *pathname, int flags, mode_t mode)
854918
{
855-
int fd{::fast_io::details::my_posix_open_noexcept(::fast_io::details::my_dos_concat_path<always_terminate>(dirfd, pathname).c_str(), flags, mode)};
919+
int fd{::fast_io::details::my_posix_open_noexcept(my_dos_concat_tlc_path<always_terminate>(dirfd, pathname).c_str(), flags, mode)};
856920
system_call_throw_error<always_terminate>(fd);
857921
return fd;
858922
}

0 commit comments

Comments
 (0)