aboutsummaryrefslogtreecommitdiffstats
path: root/src/disk.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/disk.c')
-rw-r--r--src/disk.c73
1 files changed, 73 insertions, 0 deletions
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;
+}