! Write to an open file * * @param[in,out] r newlib reentrancy struct * @param[in,out] fd Pointer to archive_file_t * @param[in] ptr Pointer to data to write * @param[in] len Length of data to write * * @returns number of bytes written * @returns -1 for error */
| 622 | * @returns -1 for error |
| 623 | */ |
| 624 | static ssize_t |
| 625 | archive_write(struct _reent *r, |
| 626 | void *fd, |
| 627 | const char *ptr, |
| 628 | size_t len) |
| 629 | { |
| 630 | Result rc; |
| 631 | u32 bytes; |
| 632 | u32 sync = 0; |
| 633 | |
| 634 | /* get pointer to our data */ |
| 635 | archive_file_t *file = (archive_file_t*)fd; |
| 636 | |
| 637 | /* check that the file was opened with write access */ |
| 638 | if((file->flags & O_ACCMODE) == O_RDONLY) |
| 639 | { |
| 640 | r->_errno = EBADF; |
| 641 | return -1; |
| 642 | } |
| 643 | |
| 644 | /* check if this is synchronous or not */ |
| 645 | if(file->flags & O_SYNC) |
| 646 | sync = FS_WRITE_FLUSH | FS_WRITE_UPDATE_TIME; |
| 647 | |
| 648 | if(file->flags & O_APPEND) |
| 649 | { |
| 650 | /* append means write from the end of the file */ |
| 651 | rc = FSFILE_GetSize(file->fd, &file->offset); |
| 652 | if(R_FAILED(rc)) |
| 653 | { |
| 654 | r->_errno = archive_translate_error(rc); |
| 655 | return -1; |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | rc = FSFILE_Write(file->fd, &bytes, file->offset, |
| 660 | (u32*)ptr, len, sync); |
| 661 | if(R_FAILED(rc)) |
| 662 | { |
| 663 | r->_errno = archive_translate_error(rc); |
| 664 | return -1; |
| 665 | } |
| 666 | |
| 667 | file->offset += bytes; |
| 668 | |
| 669 | return bytes; |
| 670 | } |
| 671 | |
| 672 | /*! Read from an open file |
| 673 | * |
nothing calls this directly
no test coverage detected