summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter D'Hoye <peter.dhoye@gmail.com>2007-10-10 23:26:17 +0000
committerPeter D'Hoye <peter.dhoye@gmail.com>2007-10-10 23:26:17 +0000
commit85058f5d9caa0f9768b155c8963eaed6aa989ed5 (patch)
treeb26d50bf03fce5d8b7cea32a9bcb34d340c60015
parentbcdb3217deba791d05ecd46328622831d8ed3b14 (diff)
downloadrockbox-85058f5d9caa0f9768b155c8963eaed6aa989ed5.tar.gz
rockbox-85058f5d9caa0f9768b155c8963eaed6aa989ed5.zip
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
-rw-r--r--apps/codecs/mp3_enc.c3
-rw-r--r--apps/codecs/wav_enc.c5
-rw-r--r--apps/codecs/wavpack_enc.c3
-rw-r--r--firmware/common/file.c21
-rw-r--r--firmware/drivers/fat.c4
-rw-r--r--firmware/pcm_record.c3
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)
2329 2329
2330static bool on_end_file(struct enc_file_event_data *data) 2330static bool on_end_file(struct enc_file_event_data *data)
2331{ 2331{
2332 if (!is_file_data_ok(data) || ci->close(data->rec_file) != 0) 2332 /* always _try_ to write the file header, even on error */
2333 if (ci->close(data->rec_file) != 0)
2333 return false; 2334 return false;
2334 2335
2335 data->rec_file = -1; 2336 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)
144 struct riff_header hdr; 144 struct riff_header hdr;
145 uint32_t data_size; 145 uint32_t data_size;
146 146
147 if (!is_file_data_ok(data)) 147 /* always _try_ to write the file header, even on error */
148 return false;
149 148
150 if (ci->lseek(data->rec_file, 0, SEEK_SET) != 0 || 149 if (ci->lseek(data->rec_file, 0, SEEK_SET) != 0 ||
151 ci->read(data->rec_file, &hdr, sizeof (hdr)) != sizeof (hdr)) 150 ci->read(data->rec_file, &hdr, sizeof (hdr)) != sizeof (hdr))
@@ -387,7 +386,7 @@ enum codec_status codec_main(void)
387 386
388 /* reset parameters to initial state */ 387 /* reset parameters to initial state */
389 ci->enc_set_parameters(NULL); 388 ci->enc_set_parameters(NULL);
390 389
391 /* main application waits for this flag during encoder removing */ 390 /* main application waits for this flag during encoder removing */
392 ci->enc_codec_loaded = 0; 391 ci->enc_codec_loaded = 0;
393 392
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)
257 257
258 uint32_t data_size; 258 uint32_t data_size;
259 259
260 if (!is_file_data_ok(data)) 260 /* always _try_ to write the file header, even on error */
261 return false;
262 261
263 /* read template headers at start */ 262 /* read template headers at start */
264 if (ci->lseek(data->rec_file, 0, SEEK_SET) != 0 || 263 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)
278 if ( file->dirty ) { 278 if ( file->dirty ) {
279 rc = flush_cache(fd); 279 rc = flush_cache(fd);
280 if (rc < 0) 280 if (rc < 0)
281 {
282 /* when failing, try to close the file anyway */
283 fat_closewrite(&(file->fatfile), file->size, file->attr);
281 return rc * 10 - 3; 284 return rc * 10 - 3;
285 }
282 } 286 }
283 287
284 /* truncate? */ 288 /* truncate? */
285 if (file->trunc) { 289 if (file->trunc) {
286 rc = ftruncate(fd, file->size); 290 rc = ftruncate(fd, file->size);
287 if (rc < 0) 291 if (rc < 0)
292 {
293 /* when failing, try to close the file anyway */
294 fat_closewrite(&(file->fatfile), file->size, file->attr);
288 return rc * 10 - 4; 295 return rc * 10 - 4;
296 }
289 } 297 }
290 298
291 /* tie up all loose ends */ 299 /* tie up all loose ends */
@@ -475,6 +483,10 @@ static int readwrite(int fd, void* buf, long count, bool write)
475 struct filedesc* file = &openfiles[fd]; 483 struct filedesc* file = &openfiles[fd];
476 int rc; 484 int rc;
477 485
486 if (fd < 0 || fd > MAX_OPEN_FILES-1) {
487 errno = EINVAL;
488 return -1;
489 }
478 if ( !file->busy ) { 490 if ( !file->busy ) {
479 errno = EBADF; 491 errno = EBADF;
480 return -1; 492 return -1;
@@ -643,6 +655,10 @@ off_t lseek(int fd, off_t offset, int whence)
643 655
644 LDEBUGF("lseek(%d,%ld,%d)\n",fd,offset,whence); 656 LDEBUGF("lseek(%d,%ld,%d)\n",fd,offset,whence);
645 657
658 if (fd < 0 || fd > MAX_OPEN_FILES-1) {
659 errno = EINVAL;
660 return -1;
661 }
646 if ( !file->busy ) { 662 if ( !file->busy ) {
647 errno = EBADF; 663 errno = EBADF;
648 return -1; 664 return -1;
@@ -716,6 +732,10 @@ off_t filesize(int fd)
716{ 732{
717 struct filedesc* file = &openfiles[fd]; 733 struct filedesc* file = &openfiles[fd];
718 734
735 if (fd < 0 || fd > MAX_OPEN_FILES-1) {
736 errno = EINVAL;
737 return -1;
738 }
719 if ( !file->busy ) { 739 if ( !file->busy ) {
720 errno = EBADF; 740 errno = EBADF;
721 return -1; 741 return -1;
@@ -743,3 +763,4 @@ int release_files(int volume)
743 return closed; /* return how many we did */ 763 return closed; /* return how many we did */
744} 764}
745#endif /* #ifdef HAVE_HOTSWAP */ 765#endif /* #ifdef HAVE_HOTSWAP */
766
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,
2084 numsec++; 2084 numsec++;
2085 if ( numsec > (long)fat_bpb->bpb_secperclus || !cluster ) { 2085 if ( numsec > (long)fat_bpb->bpb_secperclus || !cluster ) {
2086 long oldcluster = cluster; 2086 long oldcluster = cluster;
2087 long oldsector = sector;
2088 long oldnumsec = numsec;
2087 if (write) 2089 if (write)
2088 cluster = next_write_cluster(file, cluster, &sector); 2090 cluster = next_write_cluster(file, cluster, &sector);
2089 else { 2091 else {
@@ -2099,7 +2101,9 @@ long fat_readwrite( struct fat_file *file, long sectorcount,
2099 if ( write ) { 2101 if ( write ) {
2100 /* remember last cluster, in case 2102 /* remember last cluster, in case
2101 we want to append to the file */ 2103 we want to append to the file */
2104 sector = oldsector;
2102 cluster = oldcluster; 2105 cluster = oldcluster;
2106 numsec = oldnumsec;
2103 clusternum--; 2107 clusternum--;
2104 i = -1; /* Error code */ 2108 i = -1; /* Error code */
2105 break; 2109 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)
901 INC_ENC_INDEX(enc_rd_index); 901 INC_ENC_INDEX(enc_rd_index);
902 902
903 if (errors != 0) 903 if (errors != 0)
904 {
905 pcmrec_end_file();
904 break; 906 break;
907 }
905 908
906 if (flush_num == PCMREC_FLUSH_MINI && 909 if (flush_num == PCMREC_FLUSH_MINI &&
907 ++chunks_flushed >= MINI_CHUNKS) 910 ++chunks_flushed >= MINI_CHUNKS)