Skip to content

Commit

Permalink
(cpm) Close/reopen the file so that SEEK_END returns a decent value
Browse files Browse the repository at this point in the history
  • Loading branch information
suborb committed Nov 10, 2023
1 parent 162135b commit d385d14
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
7 changes: 6 additions & 1 deletion libsrc/target/cpm/fcntl/cache.c
Expand Up @@ -27,14 +27,19 @@ int cpm_cache_get(struct fcb *fcb, unsigned long record_nr, int for_read)
return 0; return 0;
} }


/**
* \retval 0 = Nothing to do
* \retval 1 = Flushed cache
* \retval -1 = Error flushing
*/
int cpm_cache_flush(struct fcb *fcb) int cpm_cache_flush(struct fcb *fcb)
{ {
if ( fcb->dirty ) { if ( fcb->dirty ) {
_putoffset(fcb->ranrec,fcb->cached_record); _putoffset(fcb->ranrec,fcb->cached_record);
bdos(CPM_SDMA,fcb->buffer); bdos(CPM_SDMA,fcb->buffer);
if ( bdos(CPM_WRAN,fcb) == 0 ) { if ( bdos(CPM_WRAN,fcb) == 0 ) {
fcb->dirty = 0; fcb->dirty = 0;
return 0; return 1;
} }
return -1; return -1;
} }
Expand Down
12 changes: 11 additions & 1 deletion libsrc/target/cpm/fcntl/fsync.c
Expand Up @@ -23,7 +23,17 @@ int fsync(int fd)
switch ( fc->use ) { switch ( fc->use ) {
case U_WRITE: case U_WRITE:
case U_RDWR: case U_RDWR:
cpm_cache_flush(fc); switch ( cpm_cache_flush(fc) ) {
case -1: // Error
// We couldn't write the sector to disc
return -1;
default: // We did it, close and reopen the file
bdos(CPM_CLS,fc);
if ( bdos(CPM_OPN,fc) == -1 ) {
// TODO: Set error
return -1;
}
}
return 0; return 0;
} }
// TODO: Set EINVAL // TODO: Set EINVAL
Expand Down
1 change: 1 addition & 0 deletions libsrc/target/cpm/fcntl/lseek.c
Expand Up @@ -32,6 +32,7 @@ long lseek(int fd,long posn, int whence)
pos = fc->rwptr + posn; pos = fc->rwptr + posn;
break; break;
case 2: case 2:
if ( (fc->use == U_WRITE || fc->use == U_RDWR) && fsync(fd) != 0 ) return -1L;
bdos(CPM_CFS,fc); bdos(CPM_CFS,fc);
pos = (unsigned long) (fc->ranrec[0] + 256 * fc->ranrec[1]) * 128L; pos = (unsigned long) (fc->ranrec[0] + 256 * fc->ranrec[1]) * 128L;
if (fc->ranrec[2]&1) if (fc->ranrec[2]&1)
Expand Down

0 comments on commit d385d14

Please sign in to comment.