pipeid_t Sys_Create_Pipe(s8* Pipe_Name,size_t Pipe_Buffer_Size)
{
    pipeid_t Pipe_ID;
    void* Buffer_Ptr;
    struct List_Head* Traverse_List_Ptr;
    /* See if the name is an empty pointer, or the buffer size wrong */
    if((Pipe_Name==0)||(Pipe_Buffer_Size==0))
    {
        Sys_Set_Errno(EINVPIPE);
        return -1;
    }
    
    Sys_Lock_Scheduler();
    
    /* See if there are any empty blocks */
    if((ptr_int_t)(&Pipe_Empty_List_Head)==(ptr_int_t)(Pipe_Empty_List_Head.Next))
    {
        Sys_Unlock_Scheduler();
        Sys_Set_Errno(ENOEPIPE);
        return -1;
    }
    
    /* If there are, get the pipe ID. We may only use this later */
    Pipe_ID=((struct Pipe*)Pipe_Empty_List_Head.Next)->Pipe_ID;
    
    /* Traverse the list to see if the name is unique */
    Traverse_List_Ptr=Pipe_List_Head.Next;
    while((ptr_int_t)Traverse_List_Ptr!=(ptr_int_t)(&Pipe_List_Head))
    {
        /* If we can find a match, abort */
        if(Sys_Strcmp(Pipe_Name,((struct Pipe*)Traverse_List_Ptr)->Pipe_Name,MAX_STR_LEN)==0)
        {
            Sys_Unlock_Scheduler();
            Sys_Set_Errno(EPIPEEXIST);
            return -1;
        }
        
        Traverse_List_Ptr=Traverse_List_Ptr->Next;
    }

    /* See if we can allocate any memory from the system. The memory allocation is
     * done in the name of "Init".
     */
    Buffer_Ptr=_Sys_Malloc(0,Pipe_Buffer_Size);
    
    /* Allocation failed */
    if(Buffer_Ptr==0)
    {
        Sys_Unlock_Scheduler();
        Sys_Set_Errno(ENOEPIPE);
        return -1;
    }
    
    /* Now we are sure that we have enough resource to set up the pipe */
    Sys_List_Delete_Node(Pipe_CB[Pipe_ID].Head.Prev,Pipe_CB[Pipe_ID].Head.Next);
    Sys_List_Insert_Node(&(Pipe_CB[Pipe_ID].Head),&Pipe_List_Head,Pipe_List_Head.Next);
    
    /* Register the values */
    Pipe_CB[Pipe_ID].Pipe_Name=Pipe_Name;
    Pipe_CB[Pipe_ID].Pipe_Buffer_Size=Pipe_Buffer_Size;
    Pipe_CB[Pipe_ID].Pipe_Buffer_Addr=(ptr_int_t)Buffer_Ptr;
    
    /* Update the statistical variable */
    Pipe_In_Sys++;
    
    Sys_Unlock_Scheduler();
    return Pipe_ID;
}
示例#2
0
/*************Begin Function:_Sys_RAMFS_Fopen*******
Description : The "Fopen" Function For RAMFS For System Use.(Don't Call It Directly In The User Application.)
Input       : u8* Path -The Path Of The File.Like"A\Readme.txt"
              u8 Mode -The Open Mode.Must Be The Following Or Their Combination.
			  See "defines.h" for more details.
#define       READ       0x01
#define       WRITE      0x02
#define       ADD        0x04
#define       CREATE     0x08		//(Other Bits Are Reserved)

			  u8 IO_Buffer -The IO Buffer ID,Or The File Identifier.
			  u32 Absolute_Start_Address -The Absolute Start Address Of The Filesystem. 
Output     :  u8 -If Succeed,"0";else "1".   
**************************************************/
u8 _Sys_RAMFS_Fopen(u8* Path,u8 Mode,u8 IO_Buffer,u32 Absolute_Start_Address)
{
	u8 MFT_Count=0,Create_MFT_Count=0,Name_Count=0,File_Block_Count=0;
	s32 Name=0;
	struct _RAMFS_MFT (*MFT_Ptr)[MAX_FILE_IN_FS]=(struct _RAMFS_MFT (*)[MAX_FILE_IN_FS])Absolute_Start_Address;		//Assign The Address.
	//struct _RAMFS_MFT (*MFT_Ptr)=(struct _RAMFS_MFT (*))Absolute_Start_Address;		//Assign The Address.
	struct RAMFS_FILE_BLOCK (*Block_Ptr)[MAX_BLOCK_NUMBER]=(struct RAMFS_FILE_BLOCK (*)[MAX_BLOCK_NUMBER])(Absolute_Start_Address+MAX_FILE_IN_FS*sizeof(struct _RAMFS_MFT)); 
	for(MFT_Count=0;MFT_Count<MAX_FILE_IN_FS;MFT_Count++)
	{
		if(Sys_Strcmp(((u8*)(*MFT_Ptr)[MFT_Count].File_Name),Path+2,LONGEST_PATH-2)==0)	                                                     //Compare The File Name And Find It Out.
		{	
			//Now The MFT_Count Contains The File ID(Or The Place Of File In The MFT) In The Filesystem.
			IOBCB[IO_Buffer].Root_Name=*(Path);                                                                       //The Root's Name.
			IOBCB[IO_Buffer].FID_In_FS=MFT_Count;                                                                     //The File ID In The Filesystem.
			IOBCB[IO_Buffer].File_Current_Ptr=(u32*)((*MFT_Ptr)[MFT_Count].First_File_Block+sizeof(struct RAMFS_FILE_BLOCK*)+sizeof(u8)); //The Pointer Will Be Kept On The Data.The Switch Of Pointers Will Be Handled By The System.
			IOBCB[IO_Buffer].File_Size=(*MFT_Ptr)[MFT_Count].File_Size;                                                  //The File Size.
			IOBCB[IO_Buffer].Buffer=(u32*)_Sys_Malloc(FILE_BUFFER_LEN);                                               //The Buffer Pointer.	//if(IOCB[IO_Buffer].Buffer=(u32*))..Now We Suggest That The memory Allocation Is Successful.
			IOBCB[IO_Buffer].Buffer_Start=IOBCB[IO_Buffer].Buffer;	                                                 //The Buffer Start Address.
			IOBCB[IO_Buffer].Buffer_End=IOBCB[IO_Buffer].Buffer+FILE_BUFFER_LEN;                                       //The Buffer End Address. 
			return(0);                                                                                               //The File Is Found.
		}

		if((MFT_Count==MAX_FILE_IN_FS)&&((Mode&CREATE_FILE)!=0))                                                          //The File Does Not Exist,And There Is "CREATE".
		{
			for(MFT_Count=0;MFT_Count<MAX_FILE_IN_FS;MFT_Count++)			 //Search For An Empty Slot To Put The File In.
			{
				if((*MFT_Ptr)[MFT_Count].File_Size==0)							 //There Must Be No File In It.
				{	
					for(Name_Count=0;Name_Count<MAX_FILE_IN_FS;Name_Count++)	  //The New File Name Will Be A Integer.Find One That Is Unused.
					{	
						for(Create_MFT_Count=0;Create_MFT_Count<MAX_FILE_IN_FS;Create_MFT_Count++)
						{
							Sys_Sscanf((*MFT_Ptr)[MFT_Count].File_Name,"%d",&Name,0);
						    if(Name==Name_Count)
								break;											  //That Integer Can't Be Used As a Name.
						}
						if(Create_MFT_Count==MAX_FILE_IN_FS)					  //This Indicates That The Whole Table Has Been Gone Through,And The name Will Be Unique.
							break;
					}

					if(Name_Count==MAX_FILE_IN_FS)								 //There is No Vacancy,The Creation Failed.
						return(1); 
					for(File_Block_Count=0;File_Block_Count<MAX_BLOCK_NUMBER;File_Block_Count++)
					{
						if((*Block_Ptr)[File_Block_Count].File_ID==0)				 //There Is No File In This Slot.
						{
							(*Block_Ptr)[File_Block_Count].File_ID=MFT_Count;       //Fill The File ID.
							(*Block_Ptr)[File_Block_Count].Prev_File_Block=EOF;
							(*Block_Ptr)[File_Block_Count].Next_File_Block=EOF;	 //Now The File Is merely Created,It Has Only One Block.
							break;
						}
							 	
					} 
					if(File_Block_Count==MAX_BLOCK_NUMBER) 
						return(1);                              				//There Is No Empty File Block Available.Abort.
					Sys_Sprintf((s8*)(*MFT_Ptr)[MFT_Count].File_Name,"%d",&Name,0,0);       //The Temporary File Name.An Unused Number. 
					(*MFT_Ptr)[MFT_Count].File_Size=0;
					(*MFT_Ptr)[MFT_Count].Read_Write=READ|WRITE;
					(*MFT_Ptr)[MFT_Count].First_File_Block=&((*Block_Ptr)[File_Block_Count])+1+4;//Point To The Data Segment.

					IOBCB[IO_Buffer].Root_Name=*(Path);                                                                       //The Root's Name.
					IOBCB[IO_Buffer].FID_In_FS=MFT_Count;                                                                     //The File ID In The Filesystem.
					IOBCB[IO_Buffer].File_Current_Ptr=(u32*)((*MFT_Ptr)[MFT_Count].First_File_Block+sizeof(struct RAMFS_FILE_BLOCK*)+sizeof(s8));//The Pointer Will Be Kept On The Data.The Switch Of Pointers Will Be Handled By The System.
					IOBCB[IO_Buffer].File_Size=(*MFT_Ptr)[MFT_Count].File_Size;                                                  //The File Size.
					IOBCB[IO_Buffer].Buffer=(u32*)_Sys_Malloc(FILE_BUFFER_LEN);                                               //The Buffer Pointer.	//if(IOCB[IO_Buffer].Buffer=(u32*))..Now We Suggest That The memory Allocation Is Successful.
					IOBCB[IO_Buffer].Buffer_Start=IOBCB[IO_Buffer].Buffer;	                                                 //The Buffer Start Address.
					IOBCB[IO_Buffer].Buffer_End=IOBCB[IO_Buffer].Buffer+FILE_BUFFER_LEN;										 //The Buffer End Address. 
					return(0);
				}
													 
				if(MFT_Count==MAX_FILE_IN_FS-1)      //There is No Empty Slots.
				{
					return(1);						 //The Operation Failed.
				}                                 
			}
				                                                          
		}
	}
	return(1);                      						 //Now The File Is Not Found,And It's Not Allowed To Create Anyone.Failure.
}