"Fossies" - the Fresh Open Source Software Archive

Member "giis_4.6.2/src/init.c" (6 Nov 2012, 8992 Bytes) of package /linux/misc/old/giis_4.6.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /* 
    2 * /giis/init.c This file  Initialize file system informations.
    3 *
    4 * Copyright (C) 2005,2006,2007,2008,2009,2010,2011,2012 Lakshmipathi.G <lakshmipathi.g@giis.co.in>.
    5 * This file may be redistributed  under the terms of the GNU Public License.
    6 *
    7 */
    8 
    9 #include"giis.h"
   10 
   11 /*
   12 * init() - Just invokes other init functions
   13 */
   14 
   15 int init ()
   16 {
   17 int temp_fd,i;
   18 char buf[100]="";
   19   if (search4fs () == -1)
   20   {
   21     printf ("\nFile system Not Detected....Aborting\n");
   22 
   23     return -1;
   24   }
   25   else
   26     printf ("found.\n\tDetected file system %s ", device_name);
   27 
   28   
   29   printf ("\n\nSearching for Superblock....");
   30 
   31 
   32   if (search4super_block () == -1)
   33   {
   34     printf ("\nCan't  find Super block...Aborting\n");
   35     exit(0);
   36   }
   37   else
   38     printf ("found.\n\tSuper block is at %lu.\n\nGathering informations.......",
   39             fs.super_block_offset);
   40 
   41 
   42   set_file_system ();
   43 
   44   if (search4group_desc () == -1)
   45   {
   46     printf ("\nUnable to initialize Group Descriptor...Aborting\n");
   47     return -1;
   48   }
   49   else
   50     printf ("\n\tGroup Descriptor found.");
   51 
   52   if (init_files () == -1)
   53   {
   54     printf ("\n\n Files not initialized Aborting....");
   55     return -1;
   56   }
   57   else
   58     printf ("\n\nAll files are Initialized...");
   59 
   60   return 1;
   61 }
   62 
   63 /*
   64 * search4fs() will search for device name in /etc/mtab file. 
   65 */
   66 
   67 int search4fs ()
   68 {
   69   int fp, i = 0, c = 1;
   70   char a;
   71   fp = open ("/etc/mtab", 0);
   72   if (fp == -1)
   73   {
   74     perror ("");
   75     printf ("Error Number:%d", errno);
   76     return -1;
   77   }
   78   do
   79   {
   80     i = read (fp, &a, 1);
   81     if (i == -1)
   82     {
   83       perror ("");
   84       printf ("Error Number:%d", errno);
   85       return -1;
   86     }
   87   }
   88   while (a != '/');
   89   c = 0;
   90   while (a != ' ')
   91   {
   92     device_name[c] = a;
   93     c++;
   94     i = read (fp, &a, 1);
   95   }
   96   device_name[c] = '\0';
   97  // i = open (device_name, 0);
   98 //Verify whether it's a valid device 
   99 //Check giis.conf file for device name
  100   if(access(device_name,F_OK)!=0){
  101     printf("\n\tAuto-Detect:%s:Device not found ",device_name);
  102     printf("\n\tReading Configuration file /etc/giis.conf\n");
  103     read_config_file("$DEVICE = ");
  104     
  105     
  106         if(access(device_name,F_OK)!=0){
  107         printf("\n\tSearch Device : Both Auto-Detect and Config file Failed..Aborting ");
  108         exit(0);
  109         }
  110         
  111     }
  112     //get auto update time to auto_time variable.
  113     read_config_file("$TIME = ");
  114     //convert into int
  115     auto_time2=atoi(auto_time);
  116     //convert into seconds
  117     auto_time2=auto_time2*60;
  118 
  119     //get dir max depth into s_level_value variable.
  120     read_config_file("$LEVEL_VALUE = ");
  121     //convert into int
  122     level_value=atoi(s_level_value);
  123     
  124     return JSJ_NO_RETURN;
  125 }
  126 
  127 /*
  128 * search4super_block() - Search for super block in the file system. 
  129 */
  130 
  131 int search4super_block ()
  132 {
  133   int i, offset = DEFAULT_OFFSET_OF_SUPER_BLOCK;
  134 
  135   fd = open (device_name, ACCESS);
  136   if (fd == -1)
  137   {
  138     perror ("");
  139     printf ("Error Number:%d ");
  140     printf("\n Please make sure you edited /etc/giis.conf with correct device name");
  141     return -1;
  142   }
  143   /*
  144      Now we opened the devide say /dev/hda7 Then search for superblock on its
  145      offset which is normally at 1024
  146    */
  147   while (offset < 4096)
  148   {
  149     lseek (fd, offset, 0);
  150     i = read (fd, isb.buffer, SUPER_BLOCK_SIZE);
  151     if (i == -1)
  152     {
  153       perror ("");
  154       printf ("Error Number:%d", errno);
  155       return -1;
  156     }
  157     if (isb.sb.s_magic == 61267)
  158     {
  159       fs.super_block_offset = offset;
  160       return 1;
  161     }
  162     offset += offset;
  163   }
  164   return -1;
  165 }
  166 
  167 /*
  168 * set_file_system()- Initialize  file system details.
  169 */
  170 
  171 
  172 void set_file_system ()
  173 {
  174   int i, pow;
  175   fs.inodes_per_group = isb.sb.s_inodes_per_group;
  176   fs.blocks_per_group = isb.sb.s_blocks_per_group;
  177   fs.inodes_count = isb.sb.s_inodes_count;
  178   fs.blocks_count = isb.sb.s_blocks_count;
  179   for (i = 1, pow = 1; i <= isb.sb.s_log_block_size; i++)
  180     pow = pow * 2;
  181   fs.block_size = 1024 * pow;
  182   fs.inode_size = isb.sb.s_inode_size;
  183   fs.inodes_per_block = fs.block_size / fs.inode_size;
  184   fs.inode_table_size = fs.inodes_per_group / fs.inodes_per_block;
  185   fs.first_data_block = isb.sb.s_first_data_block;
  186   fs.first_group_desc = fs.block_size;
  187   fs.groups_count =
  188     (fs.blocks_count - fs.first_data_block + fs.blocks_per_group) / fs.blocks_per_group;
  189   printf (" recorded...proceed...");
  190 }
  191 
  192 /* 
  193 * search4group_desc() - Initialize Group descriptor details. 
  194 */
  195 
  196 int search4group_desc ()
  197 {
  198   int i;
  199   lseek (fd, fs.first_group_desc, 0);
  200   i = read (fd, igd.buffer, GROUP_DESC_SIZE);
  201   if (i == -1)
  202   {
  203     perror ("");
  204     printf ("Error Number:%d", errno);
  205     return -1;
  206   }
  207 
  208   return 0;
  209 }
  210 
  211 /*
  212 * init_files() - Create or open files or directories 
  213 */
  214 
  215 int init_files ()
  216 {
  217   int fp;
  218 
  219 
  220   /* Create Directory. one can use O_CREAT | O_EXCL but i prefer... */
  221 
  222   fp = open (INSTALL_DIR1, 2);
  223   if (errno == ENOENT)
  224   {
  225     fp = mkdir (INSTALL_DIR1, 040777);
  226     if (fp == -1)
  227     {                           /* This error is can not be  ENOENT :) */
  228       perror ("creat:");
  229       return -1;
  230     }
  231     else
  232     {
  233       printf ("\n\n Creating files....");
  234       printf ("\n %s created...", INSTALL_DIR1);
  235     }
  236 
  237   }
  238  // close (fp);
  239 
  240   /* Create Directory */
  241 
  242   fp = open (INSTALL_DIR2, 2);
  243   if (errno == ENOENT)
  244   {
  245     fp = mkdir (INSTALL_DIR2, 040777);
  246     if (fp == -1)
  247     {
  248       perror ("creat:");
  249       return -1;
  250     }
  251     else
  252       printf ("\n %s created...", INSTALL_DIR2);
  253 
  254   }
  255 //  close (fp);
  256 
  257   /* Create Directory */
  258 
  259   fp = open (INSTALL_DIR3, 2);
  260   if (errno == ENOENT)
  261   {
  262     fp = mkdir (INSTALL_DIR3, 040777);
  263     if (fp == -1)
  264     {
  265       perror ("creat:");
  266       return -1;
  267     }
  268     else
  269       printf ("\n %s created...", INSTALL_DIR3);
  270 
  271   }
  272 //  close (fp);
  273 
  274 
  275   /* Create or open  DIR_INFO_FILE */
  276 
  277   fp = open (DIR_INFO_FILE, 2);
  278   if (errno == ENOENT)
  279   {
  280     fp = creat (DIR_INFO_FILE, 0700);
  281     if (fp == -1)
  282     {
  283       perror ("creat:");
  284       return -1;
  285     }
  286     else
  287       printf ("\n %s created...", DIR_INFO_FILE);
  288 
  289   }
  290   //close (fp);
  291 
  292   /* Create or open  FILE_INFO_FILE */
  293 
  294   fp = open (FILE_INFO_FILE, 2);
  295   if (errno == ENOENT)
  296   {
  297     fp = creat (FILE_INFO_FILE, 0700);
  298     if (fp == -1)
  299     {
  300       perror ("creat:");
  301       return -1;
  302     }
  303     else
  304       printf ("\n %s created...", FILE_INFO_FILE);
  305 
  306   }
  307   //close (fp);
  308 
  309   /* Same ...  */
  310 
  311   fp = open (SIND_INFO_FILE, 2);
  312   if (errno == ENOENT)
  313   {
  314     fp = creat (SIND_INFO_FILE, 0700);
  315     if (fp == -1)
  316     {
  317       perror ("creat:");
  318       return -1;
  319     }
  320     else
  321       printf ("\n %s created...", SIND_INFO_FILE);
  322 
  323   }
  324   //close (fp);
  325 
  326   /* Same again.. */
  327 
  328   fp = open (DIND_INFO_FILE, 2);
  329   if (errno == ENOENT)
  330   {
  331     fp = creat (DIND_INFO_FILE, 0700);
  332     if (fp == -1)
  333     {
  334       perror ("creat:");
  335       return -1;
  336     }
  337     else
  338       printf ("\n %s created...", DIND_INFO_FILE);
  339   }
  340   //close (fp);
  341 
  342   /* Cut and Pasted  again except file name ;-) */
  343 
  344   fp = open (SAMPLE_DATA_FILE, 2);
  345   if (errno == ENOENT)
  346   {
  347     fp = creat (SAMPLE_DATA_FILE, 0700);
  348     if (fp == -1)
  349     {
  350       perror ("creat:");
  351       return -1;
  352     }
  353     else
  354       printf ("\n %s created...", SAMPLE_DATA_FILE);
  355   }
  356   //close (fp);
  357   //For Unrecoverable Files
  358   fp = open (INSTALL_DIR4, 2);
  359   if (errno == ENOENT)
  360   {
  361     fp = mkdir (INSTALL_DIR4, 040777);
  362     if (fp == -1)
  363     {                           /* This error is can not be  ENOENT :) */
  364       perror ("creat:");
  365       return -1;
  366     }
  367     else
  368     {
  369       printf ("\n %s created...", INSTALL_DIR4);
  370     }
  371 
  372   }
  373   //close (fp);
  374 
  375 /* Create or open  GIIS_RERE_FILE */
  376 
  377   fp = open (GIIS_RERE_FILE, O_CREAT | O_RDWR | O_TRUNC);
  378   if (errno == ENOENT)
  379   {
  380     fp = creat (GIIS_RERE_FILE, 0700);
  381     if (fp == -1)
  382     {
  383       perror ("creat:");
  384       return -1;
  385     }
  386     else
  387       printf ("\n %s created...", GIIS_RERE_FILE);
  388 
  389   }
  390 
  391   return 1;
  392 }
  393 
  394 /*
  395 * fs_display()  : Display file system details 
  396 */
  397 int fs_display ()
  398 {
  399   printf ("\n\n");
  400   printf ("\n\t\t File System details of Your System ");
  401   printf ("\n\t\t -----------------------------------\n");
  402   printf ("\n\t Root Device       : %s ", device_name);
  403   printf ("\n\t Magic Number      : %x ", isb.sb.s_magic);
  404   printf ("\n\t Root Inode offset : %llu ", fs.root_inode_offset);
  405   printf ("\n\t Total Inodes      : %llu ", fs.inodes_count);
  406   printf ("\n\t Total Blocks      : %llu ", fs.blocks_count);
  407   printf ("\n\t Inodes per Group  : %lu ", fs.inodes_per_group);
  408   printf ("\n\t Blocks per Group  : %lu ", fs.blocks_per_group);
  409   printf ("\n\t Block size        : %u ", fs.block_size);
  410   printf ("\n\t Inodes per Block  : %u ", fs.inodes_per_block);
  411   printf ("\n\t Inode Table size  : %u ", fs.inode_table_size);
  412   printf ("\n\t Groups count      : %u ", fs.groups_count);
  413   return 1;
  414 }