Index: fat.c =================================================================== --- fat.c (Revision 159) +++ fat.c (Arbeitskopie) @@ -1446,6 +1446,17 @@ uint16_t cluster_offset = dd->entry_offset; struct fat_read_dir_callback_arg arg; + if(cluster_offset >= cluster_size) + { + /* The latest call hit the border of the last cluster in + * the chain, but it still returned a directory entry. + * So we now reset the handle and signal the caller the + * end of the listing. + */ + fat_reset_dir(dd); + return 0; + } + /* reset callback arguments */ memset(&arg, 0, sizeof(arg)); memset(dir_entry, 0, sizeof(*dir_entry)); @@ -1489,15 +1500,32 @@ if(cluster_offset >= cluster_size) { /* we reached the cluster border and switch to the next cluster */ - cluster_offset = 0; /* get number of next cluster */ - if(!(cluster_num = fat_get_next_cluster(fs, cluster_num))) + if((cluster_num = fat_get_next_cluster(fs, cluster_num)) != 0) { + cluster_offset = 0; + continue; + } + + /* we are at the end of the cluster chain */ + if(!arg.finished) + { /* directory entry not found, reset directory handle */ - cluster_num = dd->dir_entry.cluster; - break; + fat_reset_dir(dd); + return 0; } + else + { + /* The current execution of the function has been successful, + * so we can not signal an end of the directory listing to + * the caller, but must wait for the next call. So we keep an + * invalid cluster offset to mark this directory handle's + * traversal as finished. + */ + } + + break; } }