aboutsummaryrefslogtreecommitdiffstats
path: root/src/disk.c
blob: a36a7dfcbecf548c2779ec0f49db906d846d5758 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#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;
}