From 91f2963c1f6ef8abd54ae6b78a30f61906199778 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Wed, 10 Jun 2015 19:33:44 +0200 Subject: [PATCH] renamed ff_open to ff_find, added "savepos" thing to a few funcs --- fat16.c | 30 ++++++++++++++++++++++++++---- fat16.h | 6 +++--- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/fat16.c b/fat16.c index 0866529..def454f 100644 --- a/fat16.c +++ b/fat16.c @@ -761,13 +761,20 @@ void ff_root(const FAT16* fat, FFILE* file) /** * Find a file with given "display name" in this directory. * If file is found, "dir" will contain it's handle. - * Either way, "dir" gets modified and you may need to rewind it afterwards. */ -bool ff_open(FFILE* dir, const char* name) +bool ff_find(FFILE* file, const char* name) { + // save orig pos + FSAVEPOS orig = ff_savepos(file); + char fname[11]; ff_rawname(name, fname); - return dir_find_file_raw(dir, fname); + bool ret = dir_find_file_raw(file, fname); + + if (!ret) + ff_reopen(file, &orig); + + return ret; } @@ -819,6 +826,8 @@ bool find_empty_file_slot(FFILE* file) bool ff_newfile(FFILE* file, const char* name) { + const FSAVEPOS orig = ff_savepos(file); + // Convert filename to zero padded raw string char fname[11]; ff_rawname(name, fname); @@ -827,11 +836,17 @@ bool ff_newfile(FFILE* file, const char* name) bool exists = dir_find_file_raw(file, fname); ff_first(file); // rewind dir if (exists) + { + ff_reopen(file, &orig); return false; // file already exists in the dir. + } if (!find_empty_file_slot(file)) + { + ff_reopen(file, &orig); return false; // error finding a slot + } // Write into the new slot const uint16_t newclu = alloc_cluster(file->fat); @@ -847,6 +862,8 @@ bool ff_newfile(FFILE* file, const char* name) */ bool ff_mkdir(FFILE* file, const char* name) { + const FSAVEPOS orig = ff_savepos(file); + // Convert filename to zero padded raw string char fname[11]; ff_rawname(name, fname); @@ -855,11 +872,16 @@ bool ff_mkdir(FFILE* file, const char* name) bool exists = dir_find_file_raw(file, fname); ff_first(file); // rewind dir if (exists) + { + ff_reopen(file, &orig); return false; // file already exusts in the dir. + } if (!find_empty_file_slot(file)) + { + ff_reopen(file, &orig); return false; // error finding a slot - + } // Write into the new slot const uint16_t newclu = alloc_cluster(file->fat); diff --git a/fat16.h b/fat16.h index 149daec..12592e5 100644 --- a/fat16.h +++ b/fat16.h @@ -249,10 +249,10 @@ void ff_first(FFILE* file); /** * Find a file with given "display name" in this directory, and open it. - * If file is found, "dir" will contain it's handle. - * If file is NOT found, the handle points to the last entry of the directory. + * If file is found, "file" will contain it's handle. + * Otherwise, the handle is unchanged. */ -bool ff_open(FFILE* dir, const char* name); +bool ff_find(FFILE* file, const char* name); // -------- FILE INSPECTION -----------