@@ -339,43 +339,37 @@ static bool supports_pread_pwrite(uint32_t mode)
339
339
}
340
340
}
341
341
342
- static ssize_t zvfs_rw (int fd , void * buf , size_t sz , bool is_write , const size_t * from_offset )
342
+ static ssize_t zvfs_rw_unlocked (int fd , void * buf , size_t sz , bool is_write ,
343
+ const size_t * from_offset )
343
344
{
344
345
bool prw ;
345
346
ssize_t res ;
346
347
const size_t * off ;
347
348
348
- if (_check_fd (fd ) < 0 ) {
349
- return -1 ;
350
- }
351
-
352
- (void )k_mutex_lock (& fdtable [fd ].lock , K_FOREVER );
353
-
354
349
prw = supports_pread_pwrite (fdtable [fd ].mode );
355
350
if (from_offset != NULL && !prw ) {
356
351
/*
357
352
* Seekable file types should support pread() / pwrite() and per-fd offset passing.
358
353
* Otherwise, it's a bug.
359
354
*/
360
355
errno = ENOTSUP ;
361
- res = -1 ;
362
- goto unlock ;
356
+ return -1 ;
363
357
}
364
358
365
359
/* If there is no specified from_offset, then use the current offset of the fd */
366
360
off = (from_offset == NULL ) ? & fdtable [fd ].offset : from_offset ;
367
361
368
362
if (is_write ) {
369
363
if (fdtable [fd ].vtable -> write_offs == NULL ) {
370
- res = -1 ;
371
364
errno = EIO ;
365
+ return -1 ;
372
366
} else {
373
367
res = fdtable [fd ].vtable -> write_offs (fdtable [fd ].obj , buf , sz , * off );
374
368
}
375
369
} else {
376
370
if (fdtable [fd ].vtable -> read_offs == NULL ) {
377
- res = -1 ;
378
371
errno = EIO ;
372
+ return -1 ;
379
373
} else {
380
374
res = fdtable [fd ].vtable -> read_offs (fdtable [fd ].obj , buf , sz , * off );
381
375
}
@@ -388,7 +382,21 @@ static ssize_t zvfs_rw(int fd, void *buf, size_t sz, bool is_write, const size_t
388
382
fdtable [fd ].offset += res ;
389
383
}
390
384
391
- unlock :
385
+ return res ;
386
+ }
387
+
388
+ static ssize_t zvfs_rw (int fd , void * buf , size_t sz , bool is_write , const size_t * from_offset )
389
+ {
390
+ ssize_t res ;
391
+
392
+ if (_check_fd (fd ) < 0 ) {
393
+ return -1 ;
394
+ }
395
+
396
+ (void )k_mutex_lock (& fdtable [fd ].lock , K_FOREVER );
397
+
398
+ res = zvfs_rw_unlocked (fd , buf , sz , is_write , from_offset );
399
+
392
400
k_mutex_unlock (& fdtable [fd ].lock );
393
401
394
402
return res ;
@@ -562,6 +570,81 @@ int zvfs_ioctl(int fd, unsigned long request, va_list args)
562
570
return fdtable [fd ].vtable -> ioctl (fdtable [fd ].obj , request , args );
563
571
}
564
572
573
+ int zvfs_lock_file (FILE * file , k_timeout_t timeout )
574
+ {
575
+ int fd ;
576
+ int prev_errno ;
577
+ struct fd_entry * entry ;
578
+
579
+ fd = z_libc_file_get_fd (file );
580
+ prev_errno = errno ;
581
+ if (_check_fd (fd ) < 0 ) {
582
+ if (errno != prev_errno ) {
583
+ errno = prev_errno ;
584
+ }
585
+ return -1 ;
586
+ }
587
+
588
+ entry = & fdtable [fd ];
589
+ return k_mutex_lock (& entry -> lock , timeout );
590
+ }
591
+
592
+ int zvfs_unlock_file (FILE * file )
593
+ {
594
+ int fd ;
595
+ int prev_errno ;
596
+ struct fd_entry * entry ;
597
+
598
+ fd = z_libc_file_get_fd (file );
599
+ prev_errno = errno ;
600
+ if (_check_fd (fd ) < 0 ) {
601
+ if (errno != prev_errno ) {
602
+ errno = prev_errno ;
603
+ }
604
+ return -1 ;
605
+ }
606
+
607
+ entry = & fdtable [fd ];
608
+ return k_mutex_unlock (& entry -> lock );
609
+ }
610
+
611
+ int zvfs_getc_unlocked (FILE * stream )
612
+ {
613
+ int fd ;
614
+ int res ;
615
+ char buf ;
616
+
617
+ fd = z_libc_file_get_fd (stream );
618
+ if (_check_fd (fd ) < 0 ) {
619
+ return EOF ;
620
+ }
621
+
622
+ res = zvfs_rw_unlocked (fd , & buf , 1 , false, NULL );
623
+ if (res <= 0 ) {
624
+ return EOF ;
625
+ }
626
+
627
+ return (int )buf ;
628
+ }
629
+
630
+ int zvfs_putc_unlocked (int c , FILE * stream )
631
+ {
632
+ int fd ;
633
+ int res ;
634
+ char buf = (char )c ;
635
+
636
+ fd = z_libc_file_get_fd (stream );
637
+ if (_check_fd (fd ) < 0 ) {
638
+ return EOF ;
639
+ }
640
+
641
+ res = zvfs_rw_unlocked (fd , & buf , 1 , true, NULL );
642
+ if (res <= 0 ) {
643
+ return EOF ;
644
+ }
645
+
646
+ return c ;
647
+ }
565
648
566
649
#if defined(CONFIG_POSIX_DEVICE_IO )
567
650
/*
0 commit comments