Skip to content

Commit 6778ecf

Browse files
CopilotJohnAmadis
andcommitted
Fix implementation to match dmdrvi interface specification
Co-authored-by: JohnAmadis <17320783+JohnAmadis@users.noreply.github.com>
1 parent 3790d6d commit 6778ecf

1 file changed

Lines changed: 48 additions & 109 deletions

File tree

src/dmdevfs.c

Lines changed: 48 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ typedef struct
5151
typedef struct
5252
{
5353
driver_node_t* driver; // Driver associated with this file
54-
dmdrvi_file_t driver_file; // Driver file handle
54+
void* driver_handle; // Driver device handle
5555
const char* path; // File path
5656
int mode; // File open mode
5757
int attr; // File attributes
@@ -233,11 +233,12 @@ dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _fopen, (dmfsi_context_t ctx,
233233
return DMFSI_ERR_GENERAL;
234234
}
235235

236-
// Open the file through the driver
237-
int result = dmdrvi_open(driver_node->driver_context, &handle->driver_file, path, mode, attr);
238-
if(result != 0)
236+
// Open the device through the driver
237+
// Note: dmdrvi_open only takes context and flags, returns device handle
238+
handle->driver_handle = dmdrvi_open(driver_node->driver_context, mode);
239+
if(handle->driver_handle == NULL)
239240
{
240-
DMOD_LOG_ERROR("Driver failed to open file: %s\n", path);
241+
DMOD_LOG_ERROR("Driver failed to open device: %s\n", path);
241242
Dmod_Free(handle);
242243
return DMFSI_ERR_GENERAL;
243244
}
@@ -274,7 +275,7 @@ dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _fclose, (dmfsi_context_t ctx
274275
dmod_dmdrvi_close_t dmdrvi_close = Dmod_GetDifFunction(handle->driver->driver, dmod_dmdrvi_close_sig);
275276
if(dmdrvi_close != NULL)
276277
{
277-
dmdrvi_close(handle->driver->driver_context, handle->driver_file);
278+
dmdrvi_close(handle->driver->driver_context, handle->driver_handle);
278279
}
279280

280281
Dmod_Free(handle);
@@ -310,12 +311,9 @@ dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _fread, (dmfsi_context_t ctx,
310311
return DMFSI_ERR_NOT_FOUND;
311312
}
312313

313-
int result = dmdrvi_read(handle->driver->driver_context, handle->driver_file, buffer, size, read);
314-
if(result != 0)
315-
{
316-
if(read) *read = 0;
317-
return DMFSI_ERR_GENERAL;
318-
}
314+
// dmdrvi_read returns size_t (bytes read), not error code
315+
size_t bytes_read = dmdrvi_read(handle->driver->driver_context, handle->driver_handle, buffer, size);
316+
if(read) *read = bytes_read;
319317

320318
return DMFSI_OK;
321319
}
@@ -349,18 +347,16 @@ dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _fwrite, (dmfsi_context_t ctx
349347
return DMFSI_ERR_NOT_FOUND;
350348
}
351349

352-
int result = dmdrvi_write(handle->driver->driver_context, handle->driver_file, buffer, size, written);
353-
if(result != 0)
354-
{
355-
if(written) *written = 0;
356-
return DMFSI_ERR_GENERAL;
357-
}
350+
// dmdrvi_write returns size_t (bytes written), not error code
351+
size_t bytes_written = dmdrvi_write(handle->driver->driver_context, handle->driver_handle, buffer, size);
352+
if(written) *written = bytes_written;
358353

359354
return DMFSI_OK;
360355
}
361356

362357
/**
363358
* @brief Seek to a position in a file
359+
* @note Not supported for device drivers - devices are typically non-seekable
364360
*/
365361
dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _lseek, (dmfsi_context_t ctx, void* fp, long offset, int whence) )
366362
{
@@ -375,22 +371,15 @@ dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _lseek, (dmfsi_context_t ctx,
375371
return DMFSI_ERR_INVALID;
376372
}
377373

378-
file_handle_t* handle = (file_handle_t*)fp;
379-
380-
// Get the dmdrvi_lseek function
381-
dmod_dmdrvi_lseek_t dmdrvi_lseek = Dmod_GetDifFunction(handle->driver->driver, dmod_dmdrvi_lseek_sig);
382-
if(dmdrvi_lseek == NULL)
383-
{
384-
DMOD_LOG_ERROR("Driver does not implement dmdrvi_lseek\n");
385-
return DMFSI_ERR_NOT_FOUND;
386-
}
387-
388-
int result = dmdrvi_lseek(handle->driver->driver_context, handle->driver_file, offset, whence);
389-
return result;
374+
// Device drivers typically don't support seek operations
375+
// Return error to indicate operation not supported
376+
DMOD_LOG_ERROR("lseek not supported for device drivers\n");
377+
return DMFSI_ERR_GENERAL;
390378
}
391379

392380
/**
393381
* @brief Get current position in a file
382+
* @note Not supported for device drivers - devices are typically non-seekable
394383
*/
395384
dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, long, _tell, (dmfsi_context_t ctx, void* fp) )
396385
{
@@ -405,21 +394,14 @@ dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, long, _tell, (dmfsi_context_t ctx,
405394
return -1;
406395
}
407396

408-
file_handle_t* handle = (file_handle_t*)fp;
409-
410-
// Get the dmdrvi_tell function
411-
dmod_dmdrvi_tell_t dmdrvi_tell = Dmod_GetDifFunction(handle->driver->driver, dmod_dmdrvi_tell_sig);
412-
if(dmdrvi_tell == NULL)
413-
{
414-
DMOD_LOG_ERROR("Driver does not implement dmdrvi_tell\n");
415-
return -1;
416-
}
417-
418-
return dmdrvi_tell(handle->driver->driver_context, handle->driver_file);
397+
// Device drivers typically don't support tell operations
398+
DMOD_LOG_ERROR("tell not supported for device drivers\n");
399+
return -1;
419400
}
420401

421402
/**
422403
* @brief Check if at end of file
404+
* @note Device drivers typically operate in streaming mode - always return 0 (not at EOF)
423405
*/
424406
dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _eof, (dmfsi_context_t ctx, void* fp) )
425407
{
@@ -434,21 +416,14 @@ dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _eof, (dmfsi_context_t ctx, v
434416
return 1;
435417
}
436418

437-
file_handle_t* handle = (file_handle_t*)fp;
438-
439-
// Get the dmdrvi_eof function
440-
dmod_dmdrvi_eof_t dmdrvi_eof = Dmod_GetDifFunction(handle->driver->driver, dmod_dmdrvi_eof_sig);
441-
if(dmdrvi_eof == NULL)
442-
{
443-
DMOD_LOG_ERROR("Driver does not implement dmdrvi_eof\n");
444-
return 1;
445-
}
446-
447-
return dmdrvi_eof(handle->driver->driver_context, handle->driver_file);
419+
// Device drivers typically don't have EOF concept
420+
// Return 0 (not at EOF) as devices can always potentially provide more data
421+
return 0;
448422
}
449423

450424
/**
451425
* @brief Get file size
426+
* @note Device drivers represent devices, not files with fixed sizes. Use stat for size info.
452427
*/
453428
dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, long, _size, (dmfsi_context_t ctx, void* fp) )
454429
{
@@ -465,15 +440,16 @@ dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, long, _size, (dmfsi_context_t ctx,
465440

466441
file_handle_t* handle = (file_handle_t*)fp;
467442

468-
// Get the dmdrvi_size function
469-
dmod_dmdrvi_size_t dmdrvi_size = Dmod_GetDifFunction(handle->driver->driver, dmod_dmdrvi_size_sig);
470-
if(dmdrvi_size == NULL)
443+
// Try to get size from stat if available
444+
dmdrvi_stat_t stat;
445+
int result = driver_stat(handle->driver, handle->path, &stat);
446+
if(result == 0)
471447
{
472-
DMOD_LOG_ERROR("Driver does not implement dmdrvi_size\n");
473-
return -1;
448+
return (long)stat.size;
474449
}
475450

476-
return dmdrvi_size(handle->driver->driver_context, handle->driver_file);
451+
// Size not available for this device
452+
return -1;
477453
}
478454

479455
/**
@@ -558,7 +534,7 @@ dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _fflush, (dmfsi_context_t ctx
558534
return DMFSI_OK;
559535
}
560536

561-
int result = dmdrvi_flush(handle->driver->driver_context, handle->driver_file);
537+
int result = dmdrvi_flush(handle->driver->driver_context, handle->driver_handle);
562538
if(result != 0)
563539
{
564540
return DMFSI_ERR_GENERAL;
@@ -569,6 +545,7 @@ dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _fflush, (dmfsi_context_t ctx
569545

570546
/**
571547
* @brief Sync file to storage
548+
* @note For device drivers, sync/flush are equivalent operations
572549
*/
573550
dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _sync, (dmfsi_context_t ctx, void* fp) )
574551
{
@@ -585,15 +562,15 @@ dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _sync, (dmfsi_context_t ctx,
585562

586563
file_handle_t* handle = (file_handle_t*)fp;
587564

588-
// Get the dmdrvi_sync function
589-
dmod_dmdrvi_sync_t dmdrvi_sync = Dmod_GetDifFunction(handle->driver->driver, dmod_dmdrvi_sync_sig);
590-
if(dmdrvi_sync == NULL)
565+
// Get the dmdrvi_flush function (sync and flush are equivalent for devices)
566+
dmod_dmdrvi_flush_t dmdrvi_flush = Dmod_GetDifFunction(handle->driver->driver, dmod_dmdrvi_flush_sig);
567+
if(dmdrvi_flush == NULL)
591568
{
592569
// Sync not supported by driver, return OK
593570
return DMFSI_OK;
594571
}
595572

596-
int result = dmdrvi_sync(handle->driver->driver_context, handle->driver_file);
573+
int result = dmdrvi_flush(handle->driver->driver_context, handle->driver_handle);
597574
if(result != 0)
598575
{
599576
return DMFSI_ERR_GENERAL;
@@ -759,6 +736,7 @@ dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _stat, (dmfsi_context_t ctx,
759736

760737
/**
761738
* @brief Delete a file
739+
* @note Not supported for device drivers - devices cannot be deleted through filesystem operations
762740
*/
763741
dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _unlink, (dmfsi_context_t ctx, const char* path) )
764742
{
@@ -774,33 +752,14 @@ dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _unlink, (dmfsi_context_t ctx
774752
return DMFSI_ERR_INVALID;
775753
}
776754

777-
// Find the driver node for this file
778-
driver_node_t* driver_node = find_driver_node(ctx, path);
779-
if(driver_node == NULL)
780-
{
781-
DMOD_LOG_ERROR("File not found: %s\n", path);
782-
return DMFSI_ERR_NOT_FOUND;
783-
}
784-
785-
// Get the dmdrvi_unlink function
786-
dmod_dmdrvi_unlink_t dmdrvi_unlink = Dmod_GetDifFunction(driver_node->driver, dmod_dmdrvi_unlink_sig);
787-
if(dmdrvi_unlink == NULL)
788-
{
789-
DMOD_LOG_ERROR("Driver does not implement dmdrvi_unlink\n");
790-
return DMFSI_ERR_NOT_FOUND;
791-
}
792-
793-
int result = dmdrvi_unlink(driver_node->driver_context, path);
794-
if(result != 0)
795-
{
796-
return DMFSI_ERR_GENERAL;
797-
}
798-
799-
return DMFSI_OK;
755+
// Device files cannot be deleted through filesystem operations
756+
DMOD_LOG_ERROR("unlink not supported for device drivers\n");
757+
return DMFSI_ERR_GENERAL;
800758
}
801759

802760
/**
803761
* @brief Rename a file
762+
* @note Not supported for device drivers - devices cannot be renamed through filesystem operations
804763
*/
805764
dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _rename, (dmfsi_context_t ctx, const char* oldpath, const char* newpath) )
806765
{
@@ -816,29 +775,9 @@ dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _rename, (dmfsi_context_t ctx
816775
return DMFSI_ERR_INVALID;
817776
}
818777

819-
// Find the driver node for the old file
820-
driver_node_t* driver_node = find_driver_node(ctx, oldpath);
821-
if(driver_node == NULL)
822-
{
823-
DMOD_LOG_ERROR("File not found: %s\n", oldpath);
824-
return DMFSI_ERR_NOT_FOUND;
825-
}
826-
827-
// Get the dmdrvi_rename function
828-
dmod_dmdrvi_rename_t dmdrvi_rename = Dmod_GetDifFunction(driver_node->driver, dmod_dmdrvi_rename_sig);
829-
if(dmdrvi_rename == NULL)
830-
{
831-
DMOD_LOG_ERROR("Driver does not implement dmdrvi_rename\n");
832-
return DMFSI_ERR_NOT_FOUND;
833-
}
834-
835-
int result = dmdrvi_rename(driver_node->driver_context, oldpath, newpath);
836-
if(result != 0)
837-
{
838-
return DMFSI_ERR_GENERAL;
839-
}
840-
841-
return DMFSI_OK;
778+
// Device files cannot be renamed through filesystem operations
779+
DMOD_LOG_ERROR("rename not supported for device drivers\n");
780+
return DMFSI_ERR_GENERAL;
842781
}
843782

844783

0 commit comments

Comments
 (0)