File tree Expand file tree Collapse file tree 1 file changed +11
-6
lines changed Expand file tree Collapse file tree 1 file changed +11
-6
lines changed Original file line number Diff line number Diff line change @@ -428,6 +428,12 @@ uint32_t *virtio_blk_init(virtio_blk_state_t *vblk,
428
428
goto fail ;
429
429
}
430
430
431
+ struct stat st ;
432
+ if (fstat (disk_fd , & st ) == -1 ) {
433
+ rv_log_error ("fstat failed" );
434
+ goto disk_size_fail ;
435
+ }
436
+
431
437
const char * disk_file_dirname = dirname (disk_file );
432
438
if (!disk_file_dirname ) {
433
439
rv_log_error ("Fail dirname disk_file: %s" , disk_file );
@@ -436,6 +442,10 @@ uint32_t *virtio_blk_init(virtio_blk_state_t *vblk,
436
442
/* Get the disk size */
437
443
uint64_t disk_size ;
438
444
if (!strcmp (disk_file_dirname , "/dev" )) { /* from /dev/, leverage ioctl */
445
+ if ((st .st_mode & S_IFMT ) != S_IFBLK ) {
446
+ rv_log_error ("%s is not block device" , disk_file );
447
+ goto fail ;
448
+ }
439
449
#if !defined(__EMSCRIPTEN__ )
440
450
#if defined(__APPLE__ )
441
451
uint32_t block_size ;
@@ -456,12 +466,7 @@ uint32_t *virtio_blk_init(virtio_blk_state_t *vblk,
456
466
}
457
467
#endif
458
468
#endif /* !defined(__EMSCRIPTEN__) */
459
- } else { /* other path, stat it as normal file */
460
- struct stat st ;
461
- if (fstat (disk_fd , & st ) == -1 ) {
462
- rv_log_error ("fstat failed" );
463
- goto disk_size_fail ;
464
- }
469
+ } else { /* other path, get the size of block device via stat buffer */
465
470
disk_size = st .st_size ;
466
471
}
467
472
VBLK_PRIV (vblk )-> disk_size = disk_size ;
You can’t perform that action at this time.
0 commit comments