Skip to content

Commit 2ba701f

Browse files
authored
Merge pull request #39 from choco-technologies/copilot/add-eof-support-to-dmdevfs
Add dmdrvi v0.5 `eof` API support with file-size fallback
2 parents cafacd0 + 1cf9194 commit 2ba701f

1 file changed

Lines changed: 24 additions & 3 deletions

File tree

src/dmdevfs.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ typedef struct
5656
const char* path; // File path
5757
int mode; // File open mode
5858
int attr; // File attributes
59+
size_t bytes_read; // Total bytes read (used for EOF calculation when driver has no eof API)
5960
} file_handle_t;
6061

6162
/**
@@ -253,6 +254,7 @@ dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _fopen, (dmfsi_context_t ctx,
253254
handle->path = Dmod_StrDup(path);
254255
handle->mode = mode;
255256
handle->attr = attr;
257+
handle->bytes_read = 0;
256258

257259
*fp = handle;
258260
return DMFSI_OK;
@@ -326,6 +328,7 @@ dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _fread, (dmfsi_context_t ctx,
326328
// dmdrvi_read returns size_t (bytes read), not error code
327329
size_t bytes_read = dmdrvi_read(handle->driver->driver_context, handle->driver_handle, buffer, size);
328330
if(read) *read = bytes_read;
331+
handle->bytes_read += bytes_read;
329332

330333
return DMFSI_OK;
331334
}
@@ -413,7 +416,9 @@ dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, long, _tell, (dmfsi_context_t ctx,
413416

414417
/**
415418
* @brief Check if at end of file
416-
* @note Device drivers typically operate in streaming mode - always return 0 (not at EOF)
419+
* @note If the driver implements the eof API, it is used directly.
420+
* Otherwise, EOF is determined by comparing bytes read against the file size.
421+
* If file size is unavailable, 0 (not at EOF) is returned as EOF cannot be determined.
417422
*/
418423
dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _eof, (dmfsi_context_t ctx, void* fp) )
419424
{
@@ -428,8 +433,24 @@ dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _eof, (dmfsi_context_t ctx, v
428433
return 1;
429434
}
430435

431-
// Device drivers typically don't have EOF concept
432-
// Return 0 (not at EOF) as devices can always potentially provide more data
436+
file_handle_t* handle = (file_handle_t*)fp;
437+
438+
// If the driver implements the eof API, delegate to it
439+
dmod_dmdrvi_eof_t dmdrvi_eof = Dmod_GetDifFunction(handle->driver->driver, dmod_dmdrvi_eof_sig);
440+
if(dmdrvi_eof != NULL)
441+
{
442+
return dmdrvi_eof(handle->driver->driver_context, handle->driver_handle);
443+
}
444+
445+
// Fallback: compare bytes read against file size reported by driver
446+
dmdrvi_stat_t stat = {0};
447+
int result = driver_stat(handle->driver, handle->path, &stat);
448+
if(result == 0)
449+
{
450+
return (handle->bytes_read >= (size_t)stat.size) ? 1 : 0;
451+
}
452+
453+
// Size not available - cannot determine EOF
433454
return 0;
434455
}
435456

0 commit comments

Comments
 (0)