From 42446ce852c1ad0851a01d631d1b49f92b4c7e1b Mon Sep 17 00:00:00 2001
From: Bah Thierno-Souleymane <thiernosouleymane.bah.etu@univ-lille.fr>
Date: Fri, 26 Mar 2021 01:47:49 +0100
Subject: [PATCH] feat(init_volume function added)

---
 tpfs/vol.c | 26 +++++++++++++++++++++++---
 tpfs/vol.h | 19 ++++++++++++++++++-
 2 files changed, 41 insertions(+), 4 deletions(-)

diff --git a/tpfs/vol.c b/tpfs/vol.c
index db6c47c..fcc58c3 100644
--- a/tpfs/vol.c
+++ b/tpfs/vol.c
@@ -14,9 +14,9 @@ void load_mbr()
     /* read_sector(buffer, 0, 0); */
     read_sector_n(buffer, 0, 0, sizeof(mbr_t));
     memcpy(&mbr, buffer, sizeof(mbr_t));
-    if (mbr.magic != MBR_MAGIC)
+    if (mbr.magic != MAGIC)
     {
-        mbr.magic = MBR_MAGIC;
+        mbr.magic = MAGIC;
         mbr.nb_vols = 0;
     }
 }
@@ -158,4 +158,24 @@ void remove_vol(int vol)
     for (i = vol; i < mbr.nb_vols - 1; i++)
         mbr.vols[i] = mbr.vols[i + 1];
     mbr.nb_vols--;
-}
\ No newline at end of file
+}
+
+void init_volume(unsigned int vol)
+{
+    int i;
+    vsuper_t super;
+
+    super.magic = MAGIC;
+    super.serie = vol;
+    sprintf(super.name, "volume %i", vol);
+    super.id = vol;
+    super.first_free = 1;
+    write_bloc(vol, SUPER_BLOC, &super);
+
+    for (int i = 1; i < HDA_SECTORSIZE; i++)
+    {
+        vblockchain_t blockchain;
+        blockchain.next = (i + 1) % HDA_SECTORSIZE;
+        write_bloc(vol, i, &blockchain);
+    }
+}
diff --git a/tpfs/vol.h b/tpfs/vol.h
index b91337a..ab076af 100644
--- a/tpfs/vol.h
+++ b/tpfs/vol.h
@@ -2,9 +2,10 @@
 #define VOL_H
 
 #define VOLS_MAX 8
-#define MBR_MAGIC 0xCAFE /* Savoir si le mbr a déjà été initialisé ou non */
+#define MAGIC 0xCAFE /* Savoir si le mbr a déjà été initialisé ou non */
 #define FMT_DATA 0xCAFEBABE
 #define FMT_SIZE 0x1
+#define SUPER_BLOC 0
 
 typedef enum
 {
@@ -30,6 +31,20 @@ typedef struct mbr_s
 
 mbr_t mbr;
 
+typedef struct vsuper_s
+{
+    unsigned int magic;
+    unsigned int serie;
+    unsigned char *name;
+    unsigned int id;
+    unsigned int first_free;
+} vsuper_t;
+
+typedef struct vblockchain_s
+{
+    unsigned int next;
+} vblockchain_t;
+
 void load_mbr(void);
 void save_mbr(void);
 void get_sector_cylinder(unsigned int vol, unsigned int nbloc, unsigned int *sector_cylinder);
@@ -42,4 +57,6 @@ void create_vol(unsigned int nb_blocs, unsigned int cylinder, unsigned int secto
 void display_vols();
 void remove_vol();
 
+void init_volume(unsigned int vol);
+
 #endif
-- 
GitLab