fix: various bug fixes in desktop components
This commit is contained in:
@@ -243,7 +243,7 @@ void filemanager_read_dir(FileManagerState* fm) {
|
||||
|
||||
// Sort: directories first, then alphabetical (case-insensitive)
|
||||
for (int i = 1; i < fm->entry_count; i++) {
|
||||
char tmp_name[64];
|
||||
char tmp_name[128];
|
||||
int tmp_type = fm->entry_types[i];
|
||||
int tmp_size = fm->entry_sizes[i];
|
||||
bool tmp_isdir = fm->is_dir[i];
|
||||
@@ -385,9 +385,11 @@ bool filemanager_delete_recursive(const char* path) {
|
||||
}
|
||||
|
||||
bool filemanager_copy_file(const char* src, const char* dst) {
|
||||
if (!src || !dst || montauk::streq(src, dst)) return false;
|
||||
|
||||
int sfd = montauk::open(src);
|
||||
if (sfd < 0) return false;
|
||||
int size = (int)montauk::getsize(sfd);
|
||||
uint64_t size = montauk::getsize(sfd);
|
||||
|
||||
// Create destination
|
||||
int dfd = montauk::fcreate(dst);
|
||||
@@ -396,33 +398,46 @@ bool filemanager_copy_file(const char* src, const char* dst) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ok = true;
|
||||
if (size > 0) {
|
||||
// Cap at 4 MB to avoid exhausting memory
|
||||
if (size > 4 * 1024 * 1024) {
|
||||
montauk::close(sfd);
|
||||
montauk::close(dfd);
|
||||
montauk::fdelete(dst);
|
||||
return false;
|
||||
}
|
||||
uint8_t* buf = (uint8_t*)montauk::malloc(size);
|
||||
static constexpr uint64_t COPY_CHUNK = 64 * 1024;
|
||||
uint64_t buf_size = size < COPY_CHUNK ? size : COPY_CHUNK;
|
||||
uint8_t* buf = (uint8_t*)montauk::malloc(buf_size);
|
||||
if (!buf) {
|
||||
montauk::close(sfd);
|
||||
montauk::close(dfd);
|
||||
montauk::fdelete(dst);
|
||||
return false;
|
||||
}
|
||||
montauk::read(sfd, buf, 0, size);
|
||||
montauk::fwrite(dfd, buf, 0, size);
|
||||
|
||||
uint64_t off = 0;
|
||||
while (off < size) {
|
||||
uint64_t todo64 = size - off;
|
||||
if (todo64 > buf_size) todo64 = buf_size;
|
||||
int todo = (int)todo64;
|
||||
int got = montauk::read(sfd, buf, off, todo64);
|
||||
if (got != todo) { ok = false; break; }
|
||||
int wrote = montauk::fwrite(dfd, buf, off, todo64);
|
||||
if (wrote != todo) { ok = false; break; }
|
||||
off += todo64;
|
||||
}
|
||||
|
||||
montauk::mfree(buf);
|
||||
}
|
||||
|
||||
montauk::close(sfd);
|
||||
montauk::close(dfd);
|
||||
if (!ok) {
|
||||
montauk::fdelete(dst);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Recursive directory copy
|
||||
bool filemanager_copy_dir_recursive(const char* src, const char* dst) {
|
||||
if (!src || !dst || montauk::streq(src, dst)) return false;
|
||||
|
||||
montauk::fmkdir(dst);
|
||||
|
||||
const char* names[64];
|
||||
|
||||
Reference in New Issue
Block a user