| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- #include <stdio.h>
- #include <stdint.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <errno.h>
- #include "utils.h"
- ssize_t read_from_buffer(char *fname, int fd, char *buffer, uint64_t size, uint64_t base) {
- ssize_t rc;
- char *buf = buffer;
- off_t offset = base;
- size_t count = 0;
- int loop = 0;
- while (count < size) {
- uint64_t bytes = size - count;
- if (bytes > RW_MAX_SIZE) {
- bytes = RW_MAX_SIZE;
- }
- if (offset) {
- rc = lseek(fd, offset, SEEK_SET);
- if (rc != offset) {
- fprintf(stderr, "%s, seek off 0x%lx != 0x%lx\n", fname, rc, offset);
- perror("seek file");
- return -EIO;
- }
- }
- /* Read data from file into buffer */
- rc = read(fd, buf, bytes);
- if (rc < 0) {
- fprintf(stderr, "%s, read failed\n", fname);
- perror("read file");
- return -EIO;
- }
- count += rc;
- if (rc != bytes) {
- fprintf(stderr, "%s, read failed, rc=%ld, bytes=%ld\n", fname, rc, bytes);
- break;
- }
- buf += bytes;
- offset += bytes;
- loop++;
- }
- if (count != size && loop) {
- fprintf(stderr, "%s, read failed, count=%ld, size=%ld\n", fname, count, size);
- return -EIO;
- }
- return count;
- }
- ssize_t write_to_buffer(char *fname, int fd, char *buffer, uint64_t size, uint64_t base) {
- ssize_t rc;
- char *buf = buffer;
- off_t offset = base;
- size_t count = 0;
- int loop = 0;
- while (count < size) {
- uint64_t bytes = size - count;
- if (bytes > RW_MAX_SIZE) {
- bytes = RW_MAX_SIZE;
- }
- if (offset) {
- rc = lseek(fd, offset, SEEK_SET);
- if (rc != offset) {
- fprintf(stderr, "%s, seek off 0x%lx != 0x%lx\n", fname, rc, offset);
- perror("seek file");
- return -EIO;
- }
- }
- /* Write data from buffer into file */
- rc = write(fd, buf, bytes);
- if (rc < 0) {
- fprintf(stderr, "%s,write 0x%lx @ 0x%lx @ 0x%lx failed %ld.\n", fname, bytes, offset, rc);
- perror("write file");
- return -EIO;
- }
- count += rc;
- printf("count = %ld, bytes = %ld\n", count, size);
- if (rc != bytes) {
- fprintf(stderr, "%s, write underflow 0x%lx/0x%lx @ 0x%lx.\n", fname, rc, bytes, offset);
- break;
- }
- buf += bytes;
- offset += bytes;
- loop++;
- }
- if (count != size && loop) {
- fprintf(stderr, "%s, write underflow, 0x%lx/0x%lx.\n", fname, count, size);
- return -EIO;
- }
- return count;
- }
|