fix: various bug fixes in desktop components
This commit is contained in:
@@ -8,6 +8,19 @@
|
||||
|
||||
namespace filemanager {
|
||||
|
||||
static bool path_is_same_or_descendant(const char* path, const char* root) {
|
||||
if (!path || !root) return false;
|
||||
|
||||
int root_len = montauk::slen(root);
|
||||
while (root_len > 0 && root[root_len - 1] == '/') root_len--;
|
||||
if (root_len <= 0) return false;
|
||||
|
||||
for (int i = 0; i < root_len; i++) {
|
||||
if (path[i] == '\0' || path[i] != root[i]) return false;
|
||||
}
|
||||
return path[root_len] == '\0' || path[root_len] == '/';
|
||||
}
|
||||
|
||||
void filemanager_do_copy(FileManagerState* fm) {
|
||||
if (fm->selected < 0 || fm->selected >= fm->entry_count) return;
|
||||
if (filemanager_is_computer_view(fm) || filemanager_is_virtual_view(fm) ||
|
||||
@@ -36,11 +49,13 @@ void filemanager_do_paste(FileManagerState* fm) {
|
||||
const char* basename = path_basename(fm->clipboard_path);
|
||||
char dst[512];
|
||||
filemanager_build_fullpath(dst, 512, fm->current_path, basename);
|
||||
if (montauk::streq(dst, fm->clipboard_path)) return;
|
||||
|
||||
// Check if source is a directory by trying to readdir it
|
||||
const char* probe[1];
|
||||
int probe_count = montauk::readdir(fm->clipboard_path, probe, 1);
|
||||
bool src_is_dir = (probe_count >= 0);
|
||||
if (src_is_dir && path_is_same_or_descendant(dst, fm->clipboard_path)) return;
|
||||
|
||||
bool ok;
|
||||
if (src_is_dir)
|
||||
|
||||
@@ -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