file.c
上传用户:kin20054
上传日期:2022-06-28
资源大小:4k
文件大小:12k
源码类别:

操作系统开发

开发平台:

Unix_Linux

  1. #include "file.h"
  2. #include "bitmap.h"
  3. int max(int a,int b)
  4. {
  5.   return a>b?a:b;
  6. }
  7. int add_item(const char* name,int inode_a)
  8. {
  9.   struct item temp;
  10.   memcpy(temp.name,name,4);
  11.   temp.add = inode_a;
  12.   struct inode temp_inode;
  13.   char mem_area[B];
  14.   char mem_area2[B];
  15.       
  16.   read_block(1,mem_area);
  17.   memcpy((char*)&temp_inode,mem_area,16);
  18.   temp_inode.sa[0] += 8;
  19.   int i,root_block,ifend;
  20.   for(i = 3; i > 0; i--)
  21.   {
  22.     if(temp_inode.sa[i] > -1)
  23.     {
  24.       root_block = temp_inode.sa[i];
  25.       read_block(root_block,mem_area2);
  26.       ifend = i;
  27.       break;
  28.     }
  29.   }
  30.   for(i = 0; i < 16; i++)
  31.   {
  32.     if(ldisk[root_block][8*i] == 0)
  33.     {
  34.       memcpy(&mem_area2[8*i],(char*)&temp,8);
  35.       memcpy(mem_area,(char*)&temp_inode,16);
  36.       write_block(root_block,mem_area2);
  37.       write_block(1,mem_area);
  38.       /*
  39.       memcpy(&ldisk[root_block][8*i],(char*)&temp,8);  
  40.       memcpy(ldisk[1],(char*)&temp_inode,16);*/
  41.       return 0;
  42.     }
  43.   }
  44.   
  45.   if(ifend == 3)
  46.     return -1;
  47.   int add = find_block();
  48.   if(add > 0)
  49.   {
  50.     fill_full(add);
  51.     temp_inode.sa[ifend+1] = add;
  52.     char mem_area2[B];
  53.       read_block(root_block,mem_area2);
  54.       memcpy(mem_area2,(char*)&temp,8);
  55.       memcpy(mem_area,(char*)&temp_inode,16);
  56.       write_block(root_block,mem_area2);
  57.       write_block(1,mem_area);/*
  58.     memcpy(ldisk[1],(char*)&temp_inode,16);
  59.     root_block = add;
  60.     memcpy(ldisk[root_block],(char*)&temp,8);*/
  61.     return 0;
  62.   }
  63.   else 
  64.     return -2;
  65. }
  66. int create(const char* name)
  67. {
  68.   char mem_area[B];
  69.   int add = find_block();
  70.   if(add > 0)
  71.   {
  72.     fill_full(add);
  73.     int i,j;
  74.     for(i = 1; i < 32; i++)
  75.       for(j = 0; j < 8; j++)
  76.       {
  77. read_block(i,mem_area);
  78. if(mem_area[16*j] == 0
  79.   &&mem_area[16*j+1] == 0&&mem_area[16*j+2] == 0&&mem_area[16*j+3] == 0&&
  80.   mem_area[16*j+4] == 0&&mem_area[16*j+5] == 0&&mem_area[16*j+6] == 0&&mem_area[16*j+7] == 0&&
  81.   mem_area[16*j+8] == 0&&mem_area[16*j+9] == 0&&mem_area[16*j+10] == 0&&mem_area[16*j+11] == 0&&
  82.   mem_area[16*j+12] == 0&&mem_area[16*j+13] == 0&&mem_area[16*j+14] == 0&&mem_area[16*j+15] == 0)
  83. {
  84.   struct inode new_inode;
  85.   new_inode.sa[0] = 0;
  86.   new_inode.sa[1] = add;new_inode.sa[2] = -1;new_inode.sa[3] = -1;
  87.   //memcpy(&ldisk[i][16*j],(char*)&new_inode,16);
  88.   memcpy(&mem_area[16*j],(char*)&new_inode,16);
  89.   write_block(i,mem_area);
  90.   add_item(name,8*(i-1)+j);
  91.   return 0;
  92. }
  93.       }
  94.   }
  95.   else 
  96.     return -1;
  97. }
  98. int destroy(const char* name)
  99. {
  100.   struct item temp;
  101.   struct inode root_inode;
  102.   struct inode temp_inode;
  103.   char mem_area[B];
  104.   char mem_area2[B];
  105.   read_block(1,mem_area);
  106.   memcpy((char*)&root_inode,mem_area,16);
  107.   root_inode.sa[0] -= 8;
  108.   int i,j,root_block,k;
  109.   for(i = 1; i < 4; i++)
  110.   {
  111.     root_block = root_inode.sa[i];
  112.     read_block(root_block,mem_area2);
  113.     if(root_block > 0)
  114.     {
  115.       for(j = 0; j < 16; j++)
  116.       {
  117. memcpy((char*)&temp,&mem_area2[8*j],8);
  118. if(strcmp(temp.name,name) == 0)
  119. {
  120.   memset(&mem_area2[8*j],0,8);
  121.   int add;
  122.   add = temp.add;
  123.   char mem[B];
  124.   read_block(add/8+1,mem);
  125.   memcpy((char*)&temp_inode,&mem[(add%8)*16],16);
  126.   for(k = 1; k < 4; k++)
  127.   {
  128.     if(temp_inode.sa[i] > 0)
  129.     {
  130.       fill_empty(temp_inode.sa[i]);
  131.     }
  132.   }
  133.   memset(&mem[(add%8)*16],0,16);
  134.   write_block(1,mem_area);
  135.   write_block(add/8+1,mem);
  136.   write_block(root_block,mem_area2);
  137.   //memcpy(ldisk[1],(char*)&root_inode,16);
  138.   return 0;
  139. }
  140.       }
  141.     }
  142.     else
  143.       break;
  144.   }
  145.   return -1;
  146. }
  147. int open(const char* name)
  148. {
  149.   struct item temp;
  150.   struct inode root_inode;
  151.   struct inode temp_inode;
  152.   char mem_area[B];
  153.   char mem_area2[B];
  154.   char mem_area3[B];
  155.   read_block(1,mem_area);
  156.   memcpy((char*)&root_inode,mem_area,16);
  157.   int i,j,root_block,k;
  158.   for(i = 1; i < 4; i++)
  159.   {
  160.     root_block = root_inode.sa[i];
  161.     read_block(root_block,mem_area2);
  162.     if(root_block > 0)
  163.     {
  164.       for(j = 0; j < 16; j++)
  165.       {
  166. memcpy((char*)&temp,&mem_area2[8*j],8);
  167. if(strcmp(temp.name,name) == 0)
  168. {
  169.   for(k = 0; k < 10; k++)
  170.   {
  171.     if(table[k].add == -1)
  172.     {
  173.       table_num = k;
  174.       break;
  175.     }
  176.     else
  177.       table_num = -1;
  178.   }
  179.   if(table_num == -1)
  180.     return -2;
  181.   int add;
  182.   add = temp.add;
  183.   read_block(add/8+1,mem_area3);
  184.   memcpy((char*)&temp_inode,&mem_area3[(add%8)*16],16);
  185.   
  186.   table[table_num].p = 0;
  187.   table[table_num].add = add;
  188.   read_block(temp_inode.sa[1],table[table_num].mem);
  189.   //memcpy(table[table_num].mem,ldisk[temp_inode.sa[1]],B);
  190.   return table_num;
  191. }
  192.       }
  193.     }
  194.     else
  195.       break;
  196.   }
  197.   return -1;
  198. }
  199. int close(int index)
  200. {
  201.   int fsth = table[index].p/B;
  202.   int add = table[index].add;
  203.   char mem_area[B];
  204.   read_block(add/8+1,mem_area);
  205.   struct inode temp_inode;
  206.   memcpy((char*)&temp_inode,&mem_area[(add%8)*16],16);
  207.   int block = temp_inode.sa[fsth+1];
  208.   write_block(block,table[index].mem);
  209.   //memcpy(ldisk[block],table[index].mem,B);
  210.   table[index].add = -1;
  211.   return 0;
  212. }
  213. int read(int index,char* mem,int count)
  214. {
  215.   int add = table[index].add;
  216.   char mem_i[B];
  217.   char mem_area[B];
  218.   read_block(add/8+1,mem_i);
  219.   struct inode temp_inode;
  220.   memcpy((char*)&temp_inode,&mem_i[(add%8)*16],16);
  221.   int p = table[index].p;
  222.   if(count + p > 3*B)
  223.     return -1;
  224.   
  225.   table[index].p += count;
  226.   if(p > 2*B)
  227.   {
  228.     memcpy(mem,&table[index].mem[p%B],count);
  229.     //memcpy(&mem_i[(add%8)*16],(char*)&temp_inode,16);
  230.     //write_block(add/8+1,mem_i);
  231.     return 0;
  232.   }
  233.   if(p > B)
  234.   {
  235.     return 0;
  236.   }
  237.   if(p > B)
  238.   {
  239.     if(count + p > 2*B)
  240.     {
  241.       memcpy(mem,&table[index].mem[p%B],B - p%B);
  242.       read_block(temp_inode.sa[p/B+2],table[index].mem);
  243.       //memcpy(table[index].mem,ldisk[temp_inode.sa[p/B+2]],B);
  244.       memcpy(&mem[B - p%B],table[index].mem,count -B + p%B);
  245.       //memcpy(&ldisk[add/8+1][(add%8)*16],(char*)&temp_inode,16);
  246.       return 0;
  247.     }
  248.     else
  249.     {
  250.       memcpy(mem,&table[index].mem[p%B],count);
  251.       //memcpy(&ldisk[add/8+1][(add%8)*16],(char*)&temp_inode,16);
  252.       return 0;
  253.     }
  254.   }
  255.   if(count + p > 2*B)
  256.   {
  257.     memcpy(mem,&table[index].mem[p%B],B - p%B);
  258.     read_block(temp_inode.sa[p/B+2],table[index].mem);
  259.     //memcpy(table[index].mem,ldisk[temp_inode.sa[p/B+2]],B);
  260.     memcpy(&mem[B - p%B],table[index].mem,B);
  261.     read_block(temp_inode.sa[p/B+3],table[index].mem);
  262.     //memcpy(table[index].mem,ldisk[temp_inode.sa[p/B+3]],B);
  263.     memcpy(&mem[2*B - p%B],table[index].mem,count + p%B - 2*B);
  264.     //memcpy(&ldisk[add/8+1][(add%8)*16],(char*)&temp_inode,16);
  265.     return 0;
  266.   }
  267.   if(count + p > B)
  268.   {
  269.     memcpy(mem,&table[index].mem[p%B],B - p%B);
  270.     read_block(temp_inode.sa[p/B+2],table[index].mem);
  271.     //memcpy(table[index].mem,ldisk[temp_inode.sa[p/B+2]],B);
  272.     memcpy(&mem[B - p%B],table[index].mem,count + p%B - B);
  273.     //memcpy(&ldisk[add/8+1][(add%8)*16],(char*)&temp_inode,16);
  274.     return 0;
  275.   }
  276.   memcpy(mem,&table[index].mem[p%B],count);
  277.   //memcpy(&ldisk[add/8+1][(add%8)*16],(char*)&temp_inode,16);
  278.   return 0;
  279. }
  280. int write(int index,char* mem,int count)
  281. {
  282.   int add = table[index].add;
  283.   char mem_area[B];
  284.   char mem_area2[B];
  285.   read_block(add/8+1,mem_area);
  286.   struct inode temp_inode;
  287.   memcpy((char*)&temp_inode,&mem_area[(add%8)*16],16);
  288.   int p = table[index].p;
  289.   if(count + p > 3*B)
  290.     return -1;
  291.   
  292.   table[index].p += count;
  293.   if(p > 2*B)
  294.   {
  295.     memcpy(&table[index].mem[p%B],mem,count);
  296.     //temp_inode.sa[0] += count;
  297.     temp_inode.sa[0] = max(temp_inode.sa[0],table[index].p);
  298.     memcpy(&mem_area[(add%8)*16],(char*)&temp_inode,16);
  299.     write_block(add/8+1,mem_area);
  300.     return 0;
  301.   }
  302.   if(p > B)
  303.   {
  304.     if(count + p > 2*B)
  305.     {
  306.       if(temp_inode.sa[3] == -1)
  307.       {
  308. int newblock = find_block();
  309. if(newblock > 0)
  310. temp_inode.sa[3] = newblock;
  311. fill_full(newblock);
  312.       }
  313.       memcpy(&table[index].mem[p%B],mem,B - p%B);
  314.       read_block(temp_inode.sa[p/B+2],table[index].mem);
  315.       //memcpy(ldisk[temp_inode.sa[p/B+2]],table[index].mem,B);
  316.       memcpy(table[index].mem,&mem[B - p%B],count -B + p%B);
  317.       temp_inode.sa[0] = max(temp_inode.sa[0],table[index].p);
  318.       //memcpy(&ldisk[add/8+1][(add%8)*16],(char*)&temp_inode,16);
  319.       memcpy(&mem_area[(add%8)*16],(char*)&temp_inode,16);
  320.       write_block(add/8+1,mem_area);
  321.       return 0;
  322.     }
  323.     else
  324.     {
  325.       memcpy(&table[index].mem[p%B],mem,count);
  326.       temp_inode.sa[0] = max(temp_inode.sa[0],table[index].p);
  327.       //memcpy(&ldisk[add/8+1][(add%8)*16],(char*)&temp_inode,16);
  328.       memcpy(&mem_area[(add%8)*16],(char*)&temp_inode,16);
  329.       write_block(add/8+1,mem_area);
  330.       return 0;
  331.     }
  332.   }
  333.   if(count + p > 2*B)
  334.   { 
  335.     if(temp_inode.sa[2] == -1)
  336.     {
  337.       int newblock = find_block();
  338.       if(newblock > 0)
  339.       temp_inode.sa[2] = newblock;
  340.       fill_full(newblock);
  341.     }
  342.     if(temp_inode.sa[3] == -1)
  343.     {
  344.       int newblock = find_block();
  345.       if(newblock > 0)
  346.       temp_inode.sa[3] = newblock;
  347.       fill_full(newblock);
  348.     }
  349.     memcpy(&table[index].mem[p%B],mem,B - p%B);
  350.     read_block(temp_inode.sa[p/B+1],table[index].mem);
  351.    // memcpy(ldisk[temp_inode.sa[p/B+1]],table[index].mem,B);
  352.     memcpy(table[index].mem,mem,B);
  353.     write_block(temp_inode.sa[p/B+2],table[index].mem);
  354.     //memcpy(ldisk[temp_inode.sa[p/B+2]],table[index].mem,B);
  355.     memcpy(table[index].mem,&mem[2*B - p%B],count + p%B - 2*B);
  356.     temp_inode.sa[0] = max(temp_inode.sa[0],table[index].p);
  357.     //memcpy(&ldisk[add/8+1][(add%8)*16],(char*)&temp_inode,16);
  358.     memcpy(&mem_area[(add%8)*16],(char*)&temp_inode,16);
  359.     write_block(add/8+1,mem_area);
  360.     return 0;
  361.   }
  362.   if(count + p > B)
  363.   {
  364.     if(temp_inode.sa[2] == -1)
  365.     {
  366.       int newblock = find_block();
  367.       if(newblock > 0)
  368.       temp_inode.sa[2] = newblock;
  369.       fill_full(newblock);
  370.     }
  371.     memcpy(&table[index].mem[p%B],mem,B - p%B);
  372.     //memcpy(ldisk[temp_inode.sa[p/B+1]],table[index].mem,B);
  373.     read_block(temp_inode.sa[p/B+1],table[index].mem);
  374.     memcpy(table[index].mem,&mem[B - p%B],count + p%B - B);
  375.     temp_inode.sa[0] = max(temp_inode.sa[0],table[index].p);
  376.     //memcpy(&ldisk[add/8+1][(add%8)*16],(char*)&temp_inode,16);
  377.     memcpy(&mem_area[(add%8)*16],(char*)&temp_inode,16);
  378.     write_block(add/8+1,mem_area);
  379.     return 0;
  380.   }
  381.   memcpy(&table[index].mem[p%B],mem,count);
  382.   temp_inode.sa[0] = max(temp_inode.sa[0],table[index].p);
  383.   //memcpy(&ldisk[add/8+1][(add%8)*16],(char*)&temp_inode,16);
  384.   memcpy(&mem_area[(add%8)*16],(char*)&temp_inode,16);
  385.   write_block(add/8+1,mem_area);
  386.   return 0;
  387. }
  388. int lseek(int index,int pos)
  389. {
  390.   int add = table[index].add;
  391.   struct inode temp_inode;
  392.   memcpy((char*)&temp_inode,&ldisk[add/8+1][(add%8)*16],16);
  393.   if(pos > temp_inode.sa[0])
  394.     return -1;
  395.   
  396.   memcpy(ldisk[temp_inode.sa[table[index].p/B+1]],table[index].mem,B);
  397.   table[index].p = pos;
  398.   int p = table[index].p;
  399.   if(p > 2*B)
  400.   {
  401.     read_block(temp_inode.sa[3],temp_inode.sa[p/B+1]);
  402.     //memcpy(table[index].mem,ldisk[temp_inode.sa[3]],B);
  403.     return 0;
  404.   }
  405.   if(p > B)
  406.   {
  407.     read_block(temp_inode.sa[2],temp_inode.sa[p/B+1]);
  408.     //memcpy(table[index].mem,ldisk[temp_inode.sa[2]],B);
  409.     return 0;
  410.   }
  411.   read_block(temp_inode.sa[1],temp_inode.sa[p/B+1]);
  412.   //memcpy(table[index].mem,ldisk[temp_inode.sa[1]],B);
  413.   return 0;
  414. }
  415. int directory()
  416. {
  417.   struct inode root_inode;
  418.   struct inode temp_inode;
  419.   memcpy((char*)&root_inode,ldisk[1],16);
  420.   struct item sub_item;
  421.   int i,add,next = 1,size;
  422.   add = root_inode.sa[next];
  423.   if(root_inode.sa[3] != -1)
  424.     size = 48;
  425.   else if(root_inode.sa[2] != -1)
  426.     size = 32;
  427.   else if(root_inode.sa[1] != -1)
  428.     size = 16;
  429.   int ii = 1;
  430.   int add2;
  431.   char mem_area[B];
  432.   read_block(add,mem_area);
  433.   for(i = 0; i < size; i++)
  434.   {
  435.     
  436.     if(i >= 16*ii)
  437.     {
  438.       add = root_inode.sa[++next];
  439.       printf("a new root blocke number = %dn",add);
  440.       read_block(add,mem_area);
  441.       ii++;
  442.     }
  443.     if(mem_area[8*(i-16*(ii-1))] != 0)
  444.     {
  445.     memcpy((char*)&sub_item,&mem_area[8*(i-16*(ii-1))],8);    
  446.     add2 = sub_item.add;
  447.     memcpy((char*)&temp_inode,&ldisk[add2/8+1][(add2%8)*16],16);
  448.     printf("filename = %s   filesize = %dn",sub_item.name,temp_inode.sa[0]);
  449.     }
  450.   }
  451.   return 0;
  452. }
  453. void print_bit(char b)
  454. {
  455. printf("%c",(b & 0x80)?'1':'0');
  456. printf("%c",(b & 0x40)?'1':'0');
  457. printf("%c",(b & 0x20)?'1':'0');
  458. printf("%c",(b & 0x10)?'1':'0');
  459. printf("%c",(b & 0x08)?'1':'0');
  460. printf("%c",(b & 0x04)?'1':'0');
  461. printf("%c",(b & 0x02)?'1':'0');
  462. printf("%c",(b & 0x01)?'1':'0');
  463. printf("|%c|",b);
  464. }