Add comments.
[pintos-anon] / src / filesys / filehdr.c
1 #include "filehdr.h"
2 #include "bitmap.h"
3 #include "debug.h"
4 #include "malloc.h"
5 #include "filesys.h"
6 #include "lib.h"
7
8 /* Allocates sectors from bitmap B for the content of a file
9    whose size is LENGTH bytes, and returns a new `struct filehdr'
10    properly initialized for the file.
11    It is the caller's responsible to allocate a sector for the
12    file header itself, and to write the file header and bitmap
13    to disk.
14    If memory or disk allocation fails, returns a null pointer,
15    leaving bitmap B is unchanged. */
16 struct filehdr *
17 filehdr_allocate (struct bitmap *b, off_t length) 
18 {
19   struct filehdr *h;
20   size_t sector_cnt;
21
22   ASSERT (b != NULL);
23   ASSERT (length >= 0);
24
25   h = calloc (1, sizeof *h);
26   if (h == NULL)
27     return NULL;
28
29   h->length = length;
30   sector_cnt = (length / DISK_SECTOR_SIZE) + (length % DISK_SECTOR_SIZE > 0);
31   while (h->sector_cnt < sector_cnt)
32     {
33       size_t sector = bitmap_find_and_set (b);
34       if (sector == BITMAP_ERROR)
35         {
36           filehdr_deallocate (h, b);
37           free (h);
38           return NULL;
39         }
40       h->sectors[h->sector_cnt++] = sector;
41     }
42
43   return h;
44 }
45
46 /* Marks the sectors for H's content as free in bitmap B.
47    Neither H's own disk sector nor its memory are freed. */
48 void
49 filehdr_deallocate (struct filehdr *h, struct bitmap *b) 
50 {
51   size_t i;
52   
53   ASSERT (h != NULL);
54   ASSERT (b != NULL);
55
56   for (i = 0; i < h->sector_cnt; i++)
57     bitmap_reset (b, h->sectors[i]);
58 }
59
60 /* Reads a file header from FILEHDR_SECTOR
61    and returns a new `struct filehdr' that contains it.
62    Returns a null pointer fi memory allocation fails. */
63 struct filehdr *
64 filehdr_read (disk_sector_t filehdr_sector) 
65 {
66   struct filehdr *h = calloc (1, sizeof *h);
67   if (h == NULL)
68     return NULL;
69
70   ASSERT (sizeof *h == DISK_SECTOR_SIZE);
71   disk_read (filesys_disk, filehdr_sector, h);
72
73   return h;
74 }
75
76 /* Writes H to disk in sector FILEHDR_SECTOR. */
77 void
78 filehdr_write (const struct filehdr *h, disk_sector_t filehdr_sector) 
79 {
80   ASSERT (h != NULL);
81   ASSERT (sizeof *h == DISK_SECTOR_SIZE);
82   disk_write (filesys_disk, filehdr_sector, h);
83 }
84
85 /* Frees the memory (but not the on-disk sector) associated with
86    H. */
87 void
88 filehdr_destroy (struct filehdr *h) 
89 {
90   free (h);
91 }
92
93 /* Returns the disk sector that contains byte offset POS within
94    the file with header H.
95    Returns -1 if H does not contain data for a byte at offset
96    POS. */
97 disk_sector_t
98 filehdr_byte_to_sector (const struct filehdr *h, off_t pos) 
99 {
100   size_t idx;
101
102   ASSERT (h != NULL);
103
104   idx = pos / DISK_SECTOR_SIZE;
105   return idx < h->sector_cnt ? h->sectors[idx] : (disk_sector_t) -1;
106 }
107
108 /* Returns the length, in bytes, of the file with header H, */
109 off_t
110 filehdr_length (const struct filehdr *h)
111 {
112   ASSERT (h != NULL);
113   return h->length;
114 }
115
116 /* Prints a representation of H to the system console. */
117 void
118 filehdr_print (const struct filehdr *h) 
119 {
120   size_t i;
121   
122   printk ("File header: %jd bytes, %zd sectors (",
123           (intmax_t) h->length, h->sector_cnt);
124
125   /* This loop could be unsafe for large h->sector_cnt, can you
126      see why? */
127   for (i = 0; i < h->sector_cnt; i++) 
128     {
129       if (i != 0)
130         printk (", ");
131       printk ("%jd", (intmax_t) h->sectors[i]); 
132     }
133   printk (")\n");
134 }