Skip to content

Commit 8a006d2

Browse files
committed
Rewrite fileCopy
See #68
1 parent a60382e commit 8a006d2

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

src/file_utils.cc

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <string.h>
99
#include <zlib.h>
1010

11-
#include <filesystem>
11+
#include <vector>
1212

1313
// 0x452740
1414
int fileCopyDecompressed(const char* existingFilePath, const char* newFilePath)
@@ -155,9 +155,40 @@ void fileCopy(const char* existingFilePath, const char* newFilePath, bool overwr
155155
strcpy(nativeNewFilePath, newFilePath);
156156
compat_windows_path_to_native(nativeNewFilePath);
157157

158-
std::error_code ec;
159-
std::filesystem::copy_options options = overwrite
160-
? std::filesystem::copy_options::overwrite_existing
161-
: std::filesystem::copy_options::none;
162-
std::filesystem::copy_file(std::filesystem::path(nativeExistingFilePath), std::filesystem::path(nativeNewFilePath), options, ec);
158+
char outMode[4];
159+
outMode[0] = 'w';
160+
outMode[1] = 'b';
161+
162+
if (!overwrite) {
163+
outMode[2] = 'x';
164+
}
165+
166+
FILE* in = fopen(nativeExistingFilePath, "rb");
167+
FILE* out = fopen(nativeNewFilePath, outMode);
168+
if (in != NULL && out != NULL) {
169+
std::vector<unsigned char> buffer(0xFFFF);
170+
171+
size_t bytesRead;
172+
while ((bytesRead = fread(buffer.data(), sizeof(*buffer.data()), buffer.size(), in)) > 0) {
173+
size_t bytesWritten;
174+
size_t offset = 0;
175+
while ((bytesWritten = fwrite(buffer.data() + offset, sizeof(*buffer.data()), bytesRead, out)) > 0) {
176+
bytesRead -= bytesWritten;
177+
offset += bytesWritten;
178+
}
179+
180+
if (bytesWritten < 0) {
181+
bytesRead = -1;
182+
break;
183+
}
184+
}
185+
}
186+
187+
if (in != NULL) {
188+
fclose(in);
189+
}
190+
191+
if (out != NULL) {
192+
fclose(out);
193+
}
163194
}

0 commit comments

Comments
 (0)