Thanks guys for your input.
I made several tests last night. Given the weird behaviour I thought the problem was when the folder had too many files and the memory of the camera couldn't cope with it. So I tested with 200 photos and erasing 100 random JPG files. And it worked like a charm! It took like one minute but no hanging.
But then I did the same 200 photos but erasing only one JPG and the camera frizzed.
I will try to change the searching method (may be several partial searches instead of one big loop), I still think there is a problem with the camera's memory but unfortunately I don't understand how things work at low level so may be another developer has a clue. May be there is a way to know state of the memory, if it is full etc.
I'll let you know when I have a new build.
Any clues developers?
This is the Purge RAW function code, three main parts:
1.- is executed when erasing everything in DCIM folder
2.- is executed when a Canon folder is selected
3.- is executed when you call the function in the file list
//-------------------------------------------------------------------
static void fselect_purge_cb(unsigned int btn) {
DIR *d, *d2, *d3, *d4;
struct dirent *de, *de2, *de3, *de4;
struct fitem *ptr, *ptr2;
char sub_dir[20], sub_dir_search[20];
char selected_item[256];
int i, found=0;
if (btn==MBOX_BTN_YES) {
//If selected folder is DCIM (this is to purge all RAW files in any Canon folder)
if (selected->name[0] == 'D' && selected->name[1] == 'C' && selected->name[2] == 'I' && selected->name[3] == 'M') {
sprintf(current_dir+strlen(current_dir), "/%s", selected->name);
d=opendir(current_dir);
while ((de=readdir(d)) != NULL) {//Loop to find all Canon folders
if (de->name[0] != '.' && de->name[1] != '.') {//If item is not UpDir
sprintf(sub_dir, "%s/%s", current_dir, de->name);
d2=opendir(sub_dir);
while ((de2=readdir(d2)) != NULL) {//Loop to find all the RAW files inside a Canon folder
if (de2->name[0] == 'C' || de2->name[9] == 'C') {//If file is RAW (Either CRW/CR2 prefix or file extension)
d3=opendir(current_dir);
while ((de3=readdir(d3)) != NULL) {//Loop to find all Canon folders
if (de3->name[0] != '.' && de3->name[1] != '.') {//If item is not UpDir
sprintf(sub_dir_search, "%s/%s", current_dir, de3->name);
d4=opendir(sub_dir_search);
while ((de4=readdir(d4)) != NULL) {//Loop to find a corresponding JPG file inside a Canon folder
if (de2->name[4] == de4->name[4] && de2->name[5] == de4->name[5] &&//If the four digits of the Canon number are the same
de2->name[6] == de4->name[6] && de2->name[7] == de4->name[7] &&
de4->name[9] == 'J' && !(de4->name[0] == 'C' || de4->name[9] == 'C' || de4->name[0] == 0xE5)) {//If file is JPG, is not CRW/CR2 and is not a deleted item
started();
found=1;//A JPG file with the same Canon number was found
}
}
closedir(d4);
}
}
closedir(d3);
//If no JPG found, delete RAW file
if (found == 0) {
sprintf(selected_item, "%s/%s", sub_dir, de2->name);
remove(selected_item);
finished();
}
else {
found=0;
finished();
}
}
}
closedir(d2);
}
}
closedir(d);
i=strlen(current_dir);
while (current_dir[--i] != '/');
current_dir[i]=0;
}
//If item is a Canon folder (this is to purge all RAW files inside a single Canon folder)
else if (selected->name[3] == 'C') {
sprintf(current_dir+strlen(current_dir), "/%s", selected->name);
d=opendir(current_dir);
while ((de=readdir(d)) != NULL) {//Loop to find all the RAW files inside the Canon folder
if (de->name[0] == 'C' || de->name[9] == 'C') {//If file is RAW (Either CRW/CR2 prefix or file extension)
d2=opendir(current_dir);
while ((de2=readdir(d2)) != NULL) {//Loop to find a corresponding JPG file inside the Canon folder
if (de->name[4] == de2->name[4] && de->name[5] == de2->name[5] &&//If the four digits of the Canon number are the same
de->name[6] == de2->name[6] && de->name[7] == de2->name[7] &&
de2->name[9] == 'J' && !(de2->name[0] == 'C' || de2->name[9] == 'C' || de2->name[0] == 0xE5)) {//If file is JPG and is not CRW/CR2 and is not a deleted item
started();
found=1;//A JPG file with the same Canon number was found
}
}
closedir(d2);
//If no JPG found, delete RAW file
if (found == 0) {
sprintf(selected_item, "%s/%s", current_dir, de->name);
remove(selected_item);
finished();
}
else {
found=0;
finished();
}
}
}
closedir(d);
i=strlen(current_dir);
while (current_dir[--i] != '/');
current_dir[i]=0;
}
else {
//If inside a Canon folder (files list)
for (ptr=head; ptr; ptr=ptr->next) {//Loop to find all the RAW files in the list
if ((ptr->name[0] == 'C' || ptr->name[9] == 'C') && !(ptr->marked)) {//If file is RAW (Either CRW/CR2 prefix or file extension) and is not marked
for (ptr2=head; ptr2; ptr2=ptr2->next) {//Loop to find a corresponding JPG file in the list
if (ptr->name[4] == ptr2->name[4] && ptr->name[5] == ptr2->name[5] &&//If the four digits of the Canon number are the same
ptr->name[6] == ptr2->name[6] && ptr->name[7] == ptr2->name[7] &&
ptr2->name[9] == 'J' && !(ptr2->name[0] == 'C' || ptr2->name[9] == 'C')) {//If file is JPG and is not CRW/CR2
started();
found=1;
}
}
//If no JPG found, delete RAW file
if (found == 0) {
sprintf(selected_file, "%s/%s", current_dir, ptr->name);
remove(selected_file);
finished();
}
else {
found=0;
finished();
}
}
}
}
gui_fselect_read_dir(current_dir);
}
gui_fselect_redraw = 2;
}