コード例 #1
0
ファイル: main.cpp プロジェクト: F74039017/OS_FileSystem
int main()
{
puts("Create filesystem 1MB filesystem => myfs_create()");
	myfs_create("my_fs", 1*MB);

puts("Create file test file => myfs_file_create()");
	int create_fd = myfs_file_create("test"); // open the duplicate check after
	if(create_fd==-1)
	{
		puts("create file error");
		exit(EXIT_FAILURE);
	}
	else
		myfs_file_close(create_fd);
	showPPT();
	showEntries(5);
	puts("");
	
puts("Open file => myfs_file_open()");
	int fd = myfs_file_open("test");
	showPPT();
	puts("");

puts("Read dog picture and write to test file => myfs_file_write()");
	FILE *fp = fopen("dog.jpg", "rb");
	char buf[100*KB];
	int filesize = fread(buf, sizeof(char), 100*KB, fp);
	fclose(fp);
	fp = NULL;

	int ret = myfs_file_write(fd, buf, filesize);
	printf("Write = %d bytes\n", ret);

puts("Read test file and write to cp_pic.jpg => myfs_file_read()");
	char buf2[100*KB];
	int ret2 = myfs_file_read(fd, buf2, filesize);
	printf("Read = %d bytes\n", ret2);

	fp = fopen("cp_pic.jpg", "wb+");
	fwrite(buf2, sizeof(char), filesize, fp);
	fclose(fp);
	fp = NULL;
	puts("");

puts("Close test file => myfs_file_close()");
	myfs_file_close(fd);
	showPPT();
	puts("");

puts("Create file1 and file2");
	myfs_file_create("file1"); 
	myfs_file_create("file2");
	puts("");

puts("Try to create file1 again");
	if(myfs_file_create("file1")==-1)
		puts("  |=>The file already exist");
	puts("");

puts("Delete file1 => myfs_file_delete()");
	myfs_file_delete("file1");
	puts("");
	showEntries(5);

puts("myfs_destroy()");
puts("Do you want to delete the filesystem? y/n");
	char ch;
	ch = getchar();
	if(ch=='y')
		myfs_destroy("my_fs");

	return 0;
}
コード例 #2
0
int main()
{
	
	//Create filesystem 
	const char* new_filesystem="NEWVD";
	
	if(myfs_create(new_filesystem,max_size)==1)
	{
		printf("file system %s has been set up!\n",new_filesystem);



	}
	else
		printf("file system failed to be set up.\n");

	
	//Create a file called ABC
	const char* first_file="ABC";
	myfs_file_create(first_file);//create a file called abc.
	int first_fd=myfs_file_open(first_file);
	printf("%d is fd of %s \n",first_fd,first_file);
	printf("-------------------------------------------------\n");
	//Create a file called DEF
	const char* second_file="DEF";
    myfs_file_create(second_file);//create a file called def.
	int second_fd=myfs_file_open(second_file);
	printf("%d is fd of %s \n",second_fd,second_file);
	printf("-------------------------------------------------\n");



//write content to file ABC .
	char* write_buf="abcdefg";
	write(first_file,first_fd,write_buf);
	


//write content to file ABC again.
    write_buf="hij";
    write(first_file,first_fd,write_buf);

    //write content to file DEF
    write_buf="klm";
    write(second_file,second_fd,write_buf);
    

	//read file ABC and DEF 
	read(first_file,first_fd,8);//read 8 bytes from file ABC
	read(second_file,second_fd,3);//read 3 bytes from file DEF
	

//test file close by read the Dir content back from VD again
	//close file ABC
	if(myfs_file_close(first_fd)==1)
	{
		printf("file closed successfully!\n");
	}
	else
		printf("Fail to close the file!\n");

	//read Dir info from VD to make sure storing to VD is successfully done.
   printf("Read Dir info from VD to make sure the update of Dir to VD is successfully done.\n");
   fseek(fpVD,4*fat_size,SEEK_SET );
   //memset(buffer,0,strlen(buffer));
   Dir_struct *buffer[DIR_AMOUNT];//read_buffer
   int nfile=0;
   for(int j=0;j<DIR_AMOUNT;j++)
   {
   		buffer[j]=NULL;

   }

   //read file data in Dir which is stored in VD.
   fread(buffer, 32, 1, fpVD);


   //find out the number of file stored in VD.
   for(int j=0;j<DIR_AMOUNT;j++)
   {
   		if(buffer[j]!=NULL)
   			nfile++;

   }
   
   

   //fread(buffer, 32, DIR_AMOUNT, fpVD);
   for(int i=0;i<nfile;i++)
   {	
      printf("Dir%d's Filename :",i);
      for(int j=0;j<strlen(buffer[i]->FILENAME);j++)
         printf("%c",buffer[i]->FILENAME[j]);
      printf("\n");
      printf("%d is Dir%d's First Block \n",buffer[i]->FIRST_BLOCK,i);
      printf("%d is Dir%d's Last Block \n",buffer[i]->LAST_BLOCK,i);
      printf("%d is Dir%d's ID \n",buffer[i]->ID,i);
   }
   fseek(fpVD,SEEK_SET, 0);//rewind
   printf("\n");

//end test file close :Dir
   int  FAT_content[fat_size];
   fseek(fpVD,SEEK_SET, 0);
   //memset(buffer,0,strlen(buffer));
   fread(FAT_content, 4, fat_size, fpVD);
   printf("Read FAT content from VD: ");
   for(int i=0;i<fat_size;i++)
   		printf(" %d", FAT_content[i]);
   printf("\n");
   rewind(fpVD);
   printf("-------------------------------------------------\n");

//end test file close :FAT


   //test delete file abc
   //error if the file doesn't exsit!
   const char* del_filename="ABC";
	if(myfs_file_delete(del_filename)==0)
	{

			printf("The file called : %s does not exist! You can't delete it!\n",del_filename);
			
	}
	else
	{
		printf("The file called :%s has been deleted!\n",del_filename);
	}
	
//test whether ABC is deleted!
//if core dumped means deletion is successful
	/*
	printf("When read content from the deleted file ABC, \n");
	read(first_file,first_fd,8);//read 8 bytes from file ABC
	*/
//end test whether ABC is deleted!
	printf("-------------------------------------------------\n");

   //end test delete file


	
   //test destroy the filesystem NEWVD.
   if(myfs_file_destroy(new_filesystem))
   	printf("Filesystem %s has been destroyed!\n",new_filesystem);
   else 
   	 printf("Filesystem %s wasn't be destroyed properly!\n",new_filesystem);
   	//end test destroy the filesystem NEWVD.
	
	




}