src/init.c

Go to the documentation of this file.
00001 /* 
00002 * /giis/init.c This file  Initialize file system informations.
00003 *
00004 * Copyright (C)  2005. G.Lakshmipathi.<lakshmipathi_g@rediffmail.com>
00005 *
00006 */
00007 
00008 #include"giis.h"
00009 
00010 /*
00011 * init() - Just invokes other init functions
00012 */
00013 
00014 int init ()
00015 {
00016   if (search4fs () == -1)
00017   {
00018     printf ("\nFile system Not Detected....Aborting\n");
00019     return -1;
00020   }
00021   else
00022     printf ("found.\n\tDetected file system %s ", device_name);
00023 
00024   printf ("\n\nSearching for Superblock....");
00025 
00026 
00027   if (search4super_block () == -1)
00028   {
00029     printf ("\nCan't  find Super block...Aborting\n");
00030     return -1;
00031   }
00032   else
00033     printf ("found.\n\tSuper block is at %u.\n\nGathering informations.......",
00034             fs.super_block_offset);
00035 
00036 
00037   set_file_system ();
00038 
00039   if (search4group_desc () == -1)
00040   {
00041     printf ("\nUnable to initialize Group Descriptor...Aborting\n");
00042     return -1;
00043   }
00044   else
00045     printf ("\n\tGroup Descriptor found.");
00046 
00047   if (init_files () == -1)
00048   {
00049     printf ("\n\n Files not initialized Aborting....");
00050     return -1;
00051   }
00052   else
00053     printf ("\n\nAll files are Initialized...");
00054 
00055   return 1;
00056 }
00057 
00058 /*
00059 * search4fs() will search for device name in /etc/mtab file. 
00060 */
00061 
00062 int search4fs ()
00063 {
00064   int fp, i = 0, c = 1;
00065   char a;
00066   fp = open ("/etc/mtab", 0);
00067   if (fp == -1)
00068     ERROR
00069     do
00070     {
00071       i = read (fp, &a, 1);
00072     CHECK}
00073     while (a != '/');
00074   c = 0;
00075   while (a != ' ')
00076   {
00077     device_name[c] = a;
00078     c++;
00079     i = read (fp, &a, 1);
00080   }
00081   device_name[c] = '\0';
00082   i = open (device_name, 0);
00083 }
00084 
00085 /*
00086 * search4super_block() - Search for super block in the file system. 
00087 */
00088 
00089 int search4super_block ()
00090 {
00091   int i, offset = DEFAULT_OFFSET_OF_SUPER_BLOCK;
00092 
00093   fd = open (device_name, ACCESS);
00094   if (fd == -1)
00095     ERROR
00096       /*
00097          Now we opened the devide say /dev/hda7 Then search for superblock on its
00098          offset which is normally at 1024
00099        */
00100       while (offset < 4096)
00101     {
00102       lseek (fd, offset, 0);
00103       i = read (fd, isb.buffer, SUPER_BLOCK_SIZE);
00104       if (i == -1)
00105         ERROR if (isb.sb.s_magic == 61267)
00106         {
00107           fs.super_block_offset = offset;
00108           return 1;
00109         }
00110       offset += offset;
00111     }
00112   return -1;
00113 }
00114 
00115 /*
00116 * set_file_system()- Initialize  file system details.
00117 */
00118 
00119 
00120 void set_file_system ()
00121 {
00122   int i, pow;
00123   fs.inodes_per_group = isb.sb.s_inodes_per_group;
00124   fs.blocks_per_group = isb.sb.s_blocks_per_group;
00125   fs.inodes_count = isb.sb.s_inodes_count;
00126   fs.blocks_count = isb.sb.s_blocks_count;
00127   for (i = 1, pow = 1; i <= isb.sb.s_log_block_size; i++)
00128     pow = pow * 2;
00129   fs.block_size = 1024 * pow;
00130   fs.inodes_per_block = fs.block_size / INODE_SIZE;
00131   fs.inode_table_size = fs.inodes_per_group / fs.inodes_per_block;
00132   fs.first_data_block = isb.sb.s_first_data_block;
00133   fs.first_group_desc = fs.block_size;
00134   fs.groups_count =
00135     (fs.blocks_count - fs.first_data_block + fs.blocks_per_group) / fs.blocks_per_group;
00136   printf (" recorded...proceed...");
00137 }
00138 
00139 /* 
00140 * search4group_desc() - Initialize Group descriptor details. 
00141 */
00142 
00143 int search4group_desc ()
00144 {
00145   int i;
00146   lseek (fd, fs.first_group_desc, 0);
00147   i = read (fd, igd.buffer, GROUP_DESC_SIZE);
00148   CHECK return 0;
00149 }
00150 
00151 /*
00152 * init_files() - Create or open files or directories 
00153 */
00154 
00155 int init_files ()
00156 {
00157   int fp;
00158 
00159 
00160   /* Create Directory. one can use O_CREAT | O_EXCL but i prefer... */
00161 
00162   fp = open (INSTALL_DIR1, 2);
00163   if (errno == ENOENT)
00164   {
00165     fp = mkdir (INSTALL_DIR1, 040777);
00166     if (fp == -1)
00167     {                           /* This error is can not be  ENOENT :) */
00168       perror ("creat:");
00169       return -1;
00170     }
00171     else
00172     {
00173       printf ("\n\n Creating files....");
00174       printf ("\n %s created...", INSTALL_DIR1);
00175     }
00176 
00177   }
00178   close (fp);
00179 
00180   /* Create Directory */
00181 
00182   fp = open (INSTALL_DIR2, 2);
00183   if (errno == ENOENT)
00184   {
00185     fp = mkdir (INSTALL_DIR2, 040777);
00186     if (fp == -1)
00187     {
00188       perror ("creat:");
00189       return -1;
00190     }
00191     else
00192       printf ("\n %s created...", INSTALL_DIR2);
00193 
00194   }
00195   close (fp);
00196 
00197   /* Create Directory */
00198 
00199   fp = open (INSTALL_DIR3, 2);
00200   if (errno == ENOENT)
00201   {
00202     fp = mkdir (INSTALL_DIR3, 040777);
00203     if (fp == -1)
00204     {
00205       perror ("creat:");
00206       return -1;
00207     }
00208     else
00209       printf ("\n %s created...", INSTALL_DIR3);
00210 
00211   }
00212   close (fp);
00213 
00214 
00215   /* Create or open  DIR_INFO_FILE */
00216 
00217   fp = open (DIR_INFO_FILE, 2);
00218   if (errno == ENOENT)
00219   {
00220     fp = creat (DIR_INFO_FILE, 0700);
00221     if (fp == -1)
00222     {
00223       perror ("creat:");
00224       return -1;
00225     }
00226     else
00227       printf ("\n %s created...", DIR_INFO_FILE);
00228 
00229   }
00230   close (fp);
00231 
00232   /* Create or open  FILE_INFO_FILE */
00233 
00234   fp = open (FILE_INFO_FILE, 2);
00235   if (errno == ENOENT)
00236   {
00237     fp = creat (FILE_INFO_FILE, 0700);
00238     if (fp == -1)
00239     {
00240       perror ("creat:");
00241       return -1;
00242     }
00243     else
00244       printf ("\n %s created...", FILE_INFO_FILE);
00245 
00246   }
00247   close (fp);
00248 
00249   /* Same ...  */
00250 
00251   fp = open (SIND_INFO_FILE, 2);
00252   if (errno == ENOENT)
00253   {
00254     fp = creat (SIND_INFO_FILE, 0700);
00255     if (fp == -1)
00256     {
00257       perror ("creat:");
00258       return -1;
00259     }
00260     else
00261       printf ("\n %s created...", SIND_INFO_FILE);
00262 
00263   }
00264   close (fp);
00265 
00266   /* Same again.. */
00267 
00268   fp = open (DIND_INFO_FILE, 2);
00269   if (errno == ENOENT)
00270   {
00271     fp = creat (DIND_INFO_FILE, 0700);
00272     if (fp == -1)
00273     {
00274       perror ("creat:");
00275       return -1;
00276     }
00277     else
00278       printf ("\n %s created...", DIND_INFO_FILE);
00279   }
00280   close (fp);
00281 
00282   /* Cut and Pasted  again except file name ;-) */
00283 
00284   fp = open (SAMPLE_DATA_FILE, 2);
00285   if (errno == ENOENT)
00286   {
00287     fp = creat (SAMPLE_DATA_FILE, 0700);
00288     if (fp == -1)
00289     {
00290       perror ("creat:");
00291       return -1;
00292     }
00293     else
00294       printf ("\n %s created...", SAMPLE_DATA_FILE);
00295   }
00296   close (fp);
00297   //For Unrecoverable Files
00298   fp = open (INSTALL_DIR4, 2);
00299   if (errno == ENOENT)
00300   {
00301     fp = mkdir (INSTALL_DIR4, 040777);
00302     if (fp == -1)
00303     {                           /* This error is can not be  ENOENT :) */
00304       perror ("creat:");
00305       return -1;
00306     }
00307     else
00308     {
00309       printf ("\n %s created...", INSTALL_DIR4);
00310     }
00311 
00312   }
00313   close (fp);
00314 
00315 
00316 
00317 
00318   return 1;
00319 }
00320 
00321 /*
00322 * fs_display()  : Display file system details 
00323 */
00324 int fs_display ()
00325 {
00326   printf ("\n\n");
00327   printf ("\n\t\t File System details of Your System ");
00328   printf ("\n\t\t -----------------------------------\n");
00329   printf ("\n\t Root Device       : %s ", device_name);
00330   printf ("\n\t Magic Number      : %x ", isb.sb.s_magic);
00331   printf ("\n\t Root Inode offset : %u ", fs.root_inode_offset);
00332   printf ("\n\t Total Inodes      : %u ", fs.inodes_count);
00333   printf ("\n\t Total Blocks      : %u ", fs.blocks_count);
00334   printf ("\n\t Inodes per Group  : %u ", fs.inodes_per_group);
00335   printf ("\n\t Blocks per Group  : %u ", fs.blocks_per_group);
00336   printf ("\n\t Block size        : %u ", fs.block_size);
00337   printf ("\n\t Inodes per Block  : %u ", fs.inodes_per_block);
00338   printf ("\n\t Inode Table size  : %u ", fs.inode_table_size);
00339   printf ("\n\t Groups count      : %u ", fs.groups_count);
00340   return 1;
00341 }

Generated on Wed Jul 25 20:43:33 2007 for giis by  doxygen 1.5.1