From 85058f5d9caa0f9768b155c8963eaed6aa989ed5 Mon Sep 17 00:00:00 2001 From: Peter D'Hoye Date: Wed, 10 Oct 2007 23:26:17 +0000 Subject: Fix FS #5852 by trying to properly close and update the recorded file, and give the FAT the correct file info. Add filehandle checks to some file functions. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@15072 a1c6a512-1295-4272-9138-f99709370657 --- apps/codecs/mp3_enc.c | 3 ++- apps/codecs/wav_enc.c | 5 ++--- apps/codecs/wavpack_enc.c | 3 +-- firmware/common/file.c | 21 +++++++++++++++++++++ firmware/drivers/fat.c | 4 ++++ firmware/pcm_record.c | 3 +++ 6 files changed, 33 insertions(+), 6 deletions(-) diff --git a/apps/codecs/mp3_enc.c b/apps/codecs/mp3_enc.c index 08cbb18f03..1ec41ad45e 100644 --- a/apps/codecs/mp3_enc.c +++ b/apps/codecs/mp3_enc.c @@ -2329,7 +2329,8 @@ static bool on_start_file(struct enc_file_event_data *data) static bool on_end_file(struct enc_file_event_data *data) { - if (!is_file_data_ok(data) || ci->close(data->rec_file) != 0) + /* always _try_ to write the file header, even on error */ + if (ci->close(data->rec_file) != 0) return false; data->rec_file = -1; diff --git a/apps/codecs/wav_enc.c b/apps/codecs/wav_enc.c index a11aaa07d6..eb43f8b240 100644 --- a/apps/codecs/wav_enc.c +++ b/apps/codecs/wav_enc.c @@ -144,8 +144,7 @@ static bool on_end_file(struct enc_file_event_data *data) struct riff_header hdr; uint32_t data_size; - if (!is_file_data_ok(data)) - return false; + /* always _try_ to write the file header, even on error */ if (ci->lseek(data->rec_file, 0, SEEK_SET) != 0 || ci->read(data->rec_file, &hdr, sizeof (hdr)) != sizeof (hdr)) @@ -387,7 +386,7 @@ enum codec_status codec_main(void) /* reset parameters to initial state */ ci->enc_set_parameters(NULL); - + /* main application waits for this flag during encoder removing */ ci->enc_codec_loaded = 0; diff --git a/apps/codecs/wavpack_enc.c b/apps/codecs/wavpack_enc.c index 744b98721c..cf58a5973a 100644 --- a/apps/codecs/wavpack_enc.c +++ b/apps/codecs/wavpack_enc.c @@ -257,8 +257,7 @@ static bool on_end_file(struct enc_file_event_data *data) uint32_t data_size; - if (!is_file_data_ok(data)) - return false; + /* always _try_ to write the file header, even on error */ /* read template headers at start */ if (ci->lseek(data->rec_file, 0, SEEK_SET) != 0 || diff --git a/firmware/common/file.c b/firmware/common/file.c index ea2471ae92..9cab001e8d 100644 --- a/firmware/common/file.c +++ b/firmware/common/file.c @@ -278,14 +278,22 @@ int fsync(int fd) if ( file->dirty ) { rc = flush_cache(fd); if (rc < 0) + { + /* when failing, try to close the file anyway */ + fat_closewrite(&(file->fatfile), file->size, file->attr); return rc * 10 - 3; + } } /* truncate? */ if (file->trunc) { rc = ftruncate(fd, file->size); if (rc < 0) + { + /* when failing, try to close the file anyway */ + fat_closewrite(&(file->fatfile), file->size, file->attr); return rc * 10 - 4; + } } /* tie up all loose ends */ @@ -475,6 +483,10 @@ static int readwrite(int fd, void* buf, long count, bool write) struct filedesc* file = &openfiles[fd]; int rc; + if (fd < 0 || fd > MAX_OPEN_FILES-1) { + errno = EINVAL; + return -1; + } if ( !file->busy ) { errno = EBADF; return -1; @@ -643,6 +655,10 @@ off_t lseek(int fd, off_t offset, int whence) LDEBUGF("lseek(%d,%ld,%d)\n",fd,offset,whence); + if (fd < 0 || fd > MAX_OPEN_FILES-1) { + errno = EINVAL; + return -1; + } if ( !file->busy ) { errno = EBADF; return -1; @@ -716,6 +732,10 @@ off_t filesize(int fd) { struct filedesc* file = &openfiles[fd]; + if (fd < 0 || fd > MAX_OPEN_FILES-1) { + errno = EINVAL; + return -1; + } if ( !file->busy ) { errno = EBADF; return -1; @@ -743,3 +763,4 @@ int release_files(int volume) return closed; /* return how many we did */ } #endif /* #ifdef HAVE_HOTSWAP */ + diff --git a/firmware/drivers/fat.c b/firmware/drivers/fat.c index f117119258..a4fa7aa933 100644 --- a/firmware/drivers/fat.c +++ b/firmware/drivers/fat.c @@ -2084,6 +2084,8 @@ long fat_readwrite( struct fat_file *file, long sectorcount, numsec++; if ( numsec > (long)fat_bpb->bpb_secperclus || !cluster ) { long oldcluster = cluster; + long oldsector = sector; + long oldnumsec = numsec; if (write) cluster = next_write_cluster(file, cluster, §or); else { @@ -2099,7 +2101,9 @@ long fat_readwrite( struct fat_file *file, long sectorcount, if ( write ) { /* remember last cluster, in case we want to append to the file */ + sector = oldsector; cluster = oldcluster; + numsec = oldnumsec; clusternum--; i = -1; /* Error code */ break; diff --git a/firmware/pcm_record.c b/firmware/pcm_record.c index ac12fe2ba0..361689de3a 100644 --- a/firmware/pcm_record.c +++ b/firmware/pcm_record.c @@ -901,7 +901,10 @@ static void pcmrec_flush(unsigned flush_num) INC_ENC_INDEX(enc_rd_index); if (errors != 0) + { + pcmrec_end_file(); break; + } if (flush_num == PCMREC_FLUSH_MINI && ++chunks_flushed >= MINI_CHUNKS) -- cgit v1.2.3