From a501062f338339d1dbafebc55841cbdc570a27b6 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 9 Sep 2007 22:29:50 +0000 Subject: *** empty log message *** --- src/disk.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/disk.c (limited to 'src/disk.c') diff --git a/src/disk.c b/src/disk.c new file mode 100644 index 0000000..8ccaf8e --- /dev/null +++ b/src/disk.c @@ -0,0 +1,73 @@ + +#include "project.h" + + +DISK * +disk_open (char *fn) +{ + DISK *ret = calloc (1, sizeof (DISK)); + off_t o; + + ret->fd = open (fn, O_RDWR); + + if (ret->fd < 0) + { + free (ret); + return NULL; + } + + + ret->name = strdup (fn); + + ret->lbas = lseek (ret->fd, (off_t) 0, SEEK_END); + + + ret->lbas = ret->lbas / (uint64_t) SECTOR_SIZE; + + fprintf (stderr, "Opened %s with size of %d lbas\n", ret->name, + (int) ret->lbas); + + return ret; +} + +void +disk_read (DISK * d, void *buf, uint64_t lba, int lbas) +{ + lba *= SECTOR_SIZE; + + if (lseek (d->fd, lba, SEEK_SET) != lba) + { + perror ("lseek"); + abort (); + } + + lbas *= SECTOR_SIZE; + if (read (d->fd, buf, lbas) != lbas) + { + perror ("read"); + abort (); + } +} +void +disk_write (DISK * d, void *buf, uint64_t lba, int lbas) +{ + lba *= SECTOR_SIZE; + + if (lseek (d->fd, lba, SEEK_SET) != lba) + { + perror ("lseek"); + abort (); + } + + lbas *= SECTOR_SIZE; + if (write (d->fd, buf, lbas) != lbas) + { + perror ("write"); + abort (); + } +} + +uint64_t disk_lbas(DISK *d) +{ +return d->lbas; +} -- cgit v1.2.3