- Visual C++源码
- Visual Basic源码
- C++ Builder源码
- Java源码
- Delphi源码
- C/C++源码
- PHP源码
- Perl源码
- Python源码
- Asm源码
- Pascal源码
- Borland C++源码
- Others源码
- SQL源码
- VBScript源码
- JavaScript源码
- ASP/ASPX源码
- C#源码
- Flash/ActionScript源码
- matlab源码
- PowerBuilder源码
- LabView源码
- Flex源码
- MathCAD源码
- VBA源码
- IDL源码
- Lisp/Scheme源码
- VHDL源码
- Objective-C源码
- Fortran源码
- tcl/tk源码
- QT源码
file.c
资源名称:os2.tar.gz [点击查看]
上传用户:kin20054
上传日期:2022-06-28
资源大小:4k
文件大小:12k
源码类别:
操作系统开发
开发平台:
Unix_Linux
- #include "file.h"
- #include "bitmap.h"
- int max(int a,int b)
- {
- return a>b?a:b;
- }
- int add_item(const char* name,int inode_a)
- {
- struct item temp;
- memcpy(temp.name,name,4);
- temp.add = inode_a;
- struct inode temp_inode;
- char mem_area[B];
- char mem_area2[B];
- read_block(1,mem_area);
- memcpy((char*)&temp_inode,mem_area,16);
- temp_inode.sa[0] += 8;
- int i,root_block,ifend;
- for(i = 3; i > 0; i--)
- {
- if(temp_inode.sa[i] > -1)
- {
- root_block = temp_inode.sa[i];
- read_block(root_block,mem_area2);
- ifend = i;
- break;
- }
- }
- for(i = 0; i < 16; i++)
- {
- if(ldisk[root_block][8*i] == 0)
- {
- memcpy(&mem_area2[8*i],(char*)&temp,8);
- memcpy(mem_area,(char*)&temp_inode,16);
- write_block(root_block,mem_area2);
- write_block(1,mem_area);
- /*
- memcpy(&ldisk[root_block][8*i],(char*)&temp,8);
- memcpy(ldisk[1],(char*)&temp_inode,16);*/
- return 0;
- }
- }
- if(ifend == 3)
- return -1;
- int add = find_block();
- if(add > 0)
- {
- fill_full(add);
- temp_inode.sa[ifend+1] = add;
- char mem_area2[B];
- read_block(root_block,mem_area2);
- memcpy(mem_area2,(char*)&temp,8);
- memcpy(mem_area,(char*)&temp_inode,16);
- write_block(root_block,mem_area2);
- write_block(1,mem_area);/*
- memcpy(ldisk[1],(char*)&temp_inode,16);
- root_block = add;
- memcpy(ldisk[root_block],(char*)&temp,8);*/
- return 0;
- }
- else
- return -2;
- }
- int create(const char* name)
- {
- char mem_area[B];
- int add = find_block();
- if(add > 0)
- {
- fill_full(add);
- int i,j;
- for(i = 1; i < 32; i++)
- for(j = 0; j < 8; j++)
- {
- read_block(i,mem_area);
- if(mem_area[16*j] == 0
- &&mem_area[16*j+1] == 0&&mem_area[16*j+2] == 0&&mem_area[16*j+3] == 0&&
- mem_area[16*j+4] == 0&&mem_area[16*j+5] == 0&&mem_area[16*j+6] == 0&&mem_area[16*j+7] == 0&&
- mem_area[16*j+8] == 0&&mem_area[16*j+9] == 0&&mem_area[16*j+10] == 0&&mem_area[16*j+11] == 0&&
- mem_area[16*j+12] == 0&&mem_area[16*j+13] == 0&&mem_area[16*j+14] == 0&&mem_area[16*j+15] == 0)
- {
- struct inode new_inode;
- new_inode.sa[0] = 0;
- new_inode.sa[1] = add;new_inode.sa[2] = -1;new_inode.sa[3] = -1;
- //memcpy(&ldisk[i][16*j],(char*)&new_inode,16);
- memcpy(&mem_area[16*j],(char*)&new_inode,16);
- write_block(i,mem_area);
- add_item(name,8*(i-1)+j);
- return 0;
- }
- }
- }
- else
- return -1;
- }
- int destroy(const char* name)
- {
- struct item temp;
- struct inode root_inode;
- struct inode temp_inode;
- char mem_area[B];
- char mem_area2[B];
- read_block(1,mem_area);
- memcpy((char*)&root_inode,mem_area,16);
- root_inode.sa[0] -= 8;
- int i,j,root_block,k;
- for(i = 1; i < 4; i++)
- {
- root_block = root_inode.sa[i];
- read_block(root_block,mem_area2);
- if(root_block > 0)
- {
- for(j = 0; j < 16; j++)
- {
- memcpy((char*)&temp,&mem_area2[8*j],8);
- if(strcmp(temp.name,name) == 0)
- {
- memset(&mem_area2[8*j],0,8);
- int add;
- add = temp.add;
- char mem[B];
- read_block(add/8+1,mem);
- memcpy((char*)&temp_inode,&mem[(add%8)*16],16);
- for(k = 1; k < 4; k++)
- {
- if(temp_inode.sa[i] > 0)
- {
- fill_empty(temp_inode.sa[i]);
- }
- }
- memset(&mem[(add%8)*16],0,16);
- write_block(1,mem_area);
- write_block(add/8+1,mem);
- write_block(root_block,mem_area2);
- //memcpy(ldisk[1],(char*)&root_inode,16);
- return 0;
- }
- }
- }
- else
- break;
- }
- return -1;
- }
- int open(const char* name)
- {
- struct item temp;
- struct inode root_inode;
- struct inode temp_inode;
- char mem_area[B];
- char mem_area2[B];
- char mem_area3[B];
- read_block(1,mem_area);
- memcpy((char*)&root_inode,mem_area,16);
- int i,j,root_block,k;
- for(i = 1; i < 4; i++)
- {
- root_block = root_inode.sa[i];
- read_block(root_block,mem_area2);
- if(root_block > 0)
- {
- for(j = 0; j < 16; j++)
- {
- memcpy((char*)&temp,&mem_area2[8*j],8);
- if(strcmp(temp.name,name) == 0)
- {
- for(k = 0; k < 10; k++)
- {
- if(table[k].add == -1)
- {
- table_num = k;
- break;
- }
- else
- table_num = -1;
- }
- if(table_num == -1)
- return -2;
- int add;
- add = temp.add;
- read_block(add/8+1,mem_area3);
- memcpy((char*)&temp_inode,&mem_area3[(add%8)*16],16);
- table[table_num].p = 0;
- table[table_num].add = add;
- read_block(temp_inode.sa[1],table[table_num].mem);
- //memcpy(table[table_num].mem,ldisk[temp_inode.sa[1]],B);
- return table_num;
- }
- }
- }
- else
- break;
- }
- return -1;
- }
- int close(int index)
- {
- int fsth = table[index].p/B;
- int add = table[index].add;
- char mem_area[B];
- read_block(add/8+1,mem_area);
- struct inode temp_inode;
- memcpy((char*)&temp_inode,&mem_area[(add%8)*16],16);
- int block = temp_inode.sa[fsth+1];
- write_block(block,table[index].mem);
- //memcpy(ldisk[block],table[index].mem,B);
- table[index].add = -1;
- return 0;
- }
- int read(int index,char* mem,int count)
- {
- int add = table[index].add;
- char mem_i[B];
- char mem_area[B];
- read_block(add/8+1,mem_i);
- struct inode temp_inode;
- memcpy((char*)&temp_inode,&mem_i[(add%8)*16],16);
- int p = table[index].p;
- if(count + p > 3*B)
- return -1;
- table[index].p += count;
- if(p > 2*B)
- {
- memcpy(mem,&table[index].mem[p%B],count);
- //memcpy(&mem_i[(add%8)*16],(char*)&temp_inode,16);
- //write_block(add/8+1,mem_i);
- return 0;
- }
- if(p > B)
- {
- return 0;
- }
- if(p > B)
- {
- if(count + p > 2*B)
- {
- memcpy(mem,&table[index].mem[p%B],B - p%B);
- read_block(temp_inode.sa[p/B+2],table[index].mem);
- //memcpy(table[index].mem,ldisk[temp_inode.sa[p/B+2]],B);
- memcpy(&mem[B - p%B],table[index].mem,count -B + p%B);
- //memcpy(&ldisk[add/8+1][(add%8)*16],(char*)&temp_inode,16);
- return 0;
- }
- else
- {
- memcpy(mem,&table[index].mem[p%B],count);
- //memcpy(&ldisk[add/8+1][(add%8)*16],(char*)&temp_inode,16);
- return 0;
- }
- }
- if(count + p > 2*B)
- {
- memcpy(mem,&table[index].mem[p%B],B - p%B);
- read_block(temp_inode.sa[p/B+2],table[index].mem);
- //memcpy(table[index].mem,ldisk[temp_inode.sa[p/B+2]],B);
- memcpy(&mem[B - p%B],table[index].mem,B);
- read_block(temp_inode.sa[p/B+3],table[index].mem);
- //memcpy(table[index].mem,ldisk[temp_inode.sa[p/B+3]],B);
- memcpy(&mem[2*B - p%B],table[index].mem,count + p%B - 2*B);
- //memcpy(&ldisk[add/8+1][(add%8)*16],(char*)&temp_inode,16);
- return 0;
- }
- if(count + p > B)
- {
- memcpy(mem,&table[index].mem[p%B],B - p%B);
- read_block(temp_inode.sa[p/B+2],table[index].mem);
- //memcpy(table[index].mem,ldisk[temp_inode.sa[p/B+2]],B);
- memcpy(&mem[B - p%B],table[index].mem,count + p%B - B);
- //memcpy(&ldisk[add/8+1][(add%8)*16],(char*)&temp_inode,16);
- return 0;
- }
- memcpy(mem,&table[index].mem[p%B],count);
- //memcpy(&ldisk[add/8+1][(add%8)*16],(char*)&temp_inode,16);
- return 0;
- }
- int write(int index,char* mem,int count)
- {
- int add = table[index].add;
- char mem_area[B];
- char mem_area2[B];
- read_block(add/8+1,mem_area);
- struct inode temp_inode;
- memcpy((char*)&temp_inode,&mem_area[(add%8)*16],16);
- int p = table[index].p;
- if(count + p > 3*B)
- return -1;
- table[index].p += count;
- if(p > 2*B)
- {
- memcpy(&table[index].mem[p%B],mem,count);
- //temp_inode.sa[0] += count;
- temp_inode.sa[0] = max(temp_inode.sa[0],table[index].p);
- memcpy(&mem_area[(add%8)*16],(char*)&temp_inode,16);
- write_block(add/8+1,mem_area);
- return 0;
- }
- if(p > B)
- {
- if(count + p > 2*B)
- {
- if(temp_inode.sa[3] == -1)
- {
- int newblock = find_block();
- if(newblock > 0)
- temp_inode.sa[3] = newblock;
- fill_full(newblock);
- }
- memcpy(&table[index].mem[p%B],mem,B - p%B);
- read_block(temp_inode.sa[p/B+2],table[index].mem);
- //memcpy(ldisk[temp_inode.sa[p/B+2]],table[index].mem,B);
- memcpy(table[index].mem,&mem[B - p%B],count -B + p%B);
- temp_inode.sa[0] = max(temp_inode.sa[0],table[index].p);
- //memcpy(&ldisk[add/8+1][(add%8)*16],(char*)&temp_inode,16);
- memcpy(&mem_area[(add%8)*16],(char*)&temp_inode,16);
- write_block(add/8+1,mem_area);
- return 0;
- }
- else
- {
- memcpy(&table[index].mem[p%B],mem,count);
- temp_inode.sa[0] = max(temp_inode.sa[0],table[index].p);
- //memcpy(&ldisk[add/8+1][(add%8)*16],(char*)&temp_inode,16);
- memcpy(&mem_area[(add%8)*16],(char*)&temp_inode,16);
- write_block(add/8+1,mem_area);
- return 0;
- }
- }
- if(count + p > 2*B)
- {
- if(temp_inode.sa[2] == -1)
- {
- int newblock = find_block();
- if(newblock > 0)
- temp_inode.sa[2] = newblock;
- fill_full(newblock);
- }
- if(temp_inode.sa[3] == -1)
- {
- int newblock = find_block();
- if(newblock > 0)
- temp_inode.sa[3] = newblock;
- fill_full(newblock);
- }
- memcpy(&table[index].mem[p%B],mem,B - p%B);
- read_block(temp_inode.sa[p/B+1],table[index].mem);
- // memcpy(ldisk[temp_inode.sa[p/B+1]],table[index].mem,B);
- memcpy(table[index].mem,mem,B);
- write_block(temp_inode.sa[p/B+2],table[index].mem);
- //memcpy(ldisk[temp_inode.sa[p/B+2]],table[index].mem,B);
- memcpy(table[index].mem,&mem[2*B - p%B],count + p%B - 2*B);
- temp_inode.sa[0] = max(temp_inode.sa[0],table[index].p);
- //memcpy(&ldisk[add/8+1][(add%8)*16],(char*)&temp_inode,16);
- memcpy(&mem_area[(add%8)*16],(char*)&temp_inode,16);
- write_block(add/8+1,mem_area);
- return 0;
- }
- if(count + p > B)
- {
- if(temp_inode.sa[2] == -1)
- {
- int newblock = find_block();
- if(newblock > 0)
- temp_inode.sa[2] = newblock;
- fill_full(newblock);
- }
- memcpy(&table[index].mem[p%B],mem,B - p%B);
- //memcpy(ldisk[temp_inode.sa[p/B+1]],table[index].mem,B);
- read_block(temp_inode.sa[p/B+1],table[index].mem);
- memcpy(table[index].mem,&mem[B - p%B],count + p%B - B);
- temp_inode.sa[0] = max(temp_inode.sa[0],table[index].p);
- //memcpy(&ldisk[add/8+1][(add%8)*16],(char*)&temp_inode,16);
- memcpy(&mem_area[(add%8)*16],(char*)&temp_inode,16);
- write_block(add/8+1,mem_area);
- return 0;
- }
- memcpy(&table[index].mem[p%B],mem,count);
- temp_inode.sa[0] = max(temp_inode.sa[0],table[index].p);
- //memcpy(&ldisk[add/8+1][(add%8)*16],(char*)&temp_inode,16);
- memcpy(&mem_area[(add%8)*16],(char*)&temp_inode,16);
- write_block(add/8+1,mem_area);
- return 0;
- }
- int lseek(int index,int pos)
- {
- int add = table[index].add;
- struct inode temp_inode;
- memcpy((char*)&temp_inode,&ldisk[add/8+1][(add%8)*16],16);
- if(pos > temp_inode.sa[0])
- return -1;
- memcpy(ldisk[temp_inode.sa[table[index].p/B+1]],table[index].mem,B);
- table[index].p = pos;
- int p = table[index].p;
- if(p > 2*B)
- {
- read_block(temp_inode.sa[3],temp_inode.sa[p/B+1]);
- //memcpy(table[index].mem,ldisk[temp_inode.sa[3]],B);
- return 0;
- }
- if(p > B)
- {
- read_block(temp_inode.sa[2],temp_inode.sa[p/B+1]);
- //memcpy(table[index].mem,ldisk[temp_inode.sa[2]],B);
- return 0;
- }
- read_block(temp_inode.sa[1],temp_inode.sa[p/B+1]);
- //memcpy(table[index].mem,ldisk[temp_inode.sa[1]],B);
- return 0;
- }
- int directory()
- {
- struct inode root_inode;
- struct inode temp_inode;
- memcpy((char*)&root_inode,ldisk[1],16);
- struct item sub_item;
- int i,add,next = 1,size;
- add = root_inode.sa[next];
- if(root_inode.sa[3] != -1)
- size = 48;
- else if(root_inode.sa[2] != -1)
- size = 32;
- else if(root_inode.sa[1] != -1)
- size = 16;
- int ii = 1;
- int add2;
- char mem_area[B];
- read_block(add,mem_area);
- for(i = 0; i < size; i++)
- {
- if(i >= 16*ii)
- {
- add = root_inode.sa[++next];
- printf("a new root blocke number = %dn",add);
- read_block(add,mem_area);
- ii++;
- }
- if(mem_area[8*(i-16*(ii-1))] != 0)
- {
- memcpy((char*)&sub_item,&mem_area[8*(i-16*(ii-1))],8);
- add2 = sub_item.add;
- memcpy((char*)&temp_inode,&ldisk[add2/8+1][(add2%8)*16],16);
- printf("filename = %s filesize = %dn",sub_item.name,temp_inode.sa[0]);
- }
- }
- return 0;
- }
- void print_bit(char b)
- {
- printf("%c",(b & 0x80)?'1':'0');
- printf("%c",(b & 0x40)?'1':'0');
- printf("%c",(b & 0x20)?'1':'0');
- printf("%c",(b & 0x10)?'1':'0');
- printf("%c",(b & 0x08)?'1':'0');
- printf("%c",(b & 0x04)?'1':'0');
- printf("%c",(b & 0x02)?'1':'0');
- printf("%c",(b & 0x01)?'1':'0');
- printf("|%c|",b);
- }