yaffs打开文件yaffs_open

int yaffs_open(const YCHAR *path, int oflag, int mode)
{
	yaffs_Object *obj = NULL;
	yaffs_Object *dir = NULL;
	YCHAR *name;
	int handle = -1;
	yaffsfs_Handle *h = NULL;
	int alreadyOpen = 0;
	int alreadyExclusive = 0;
	int openDenied = 0;
	int symDepth = 0;
	int errorReported = 0;
	
	int i;
	
	
	// todo sanity check oflag (eg. can't have O_TRUNC without WRONLY or RDWR
	
	
	yaffsfs_Lock();
	
         //得到一个未使用的文件Handle,一个文件Handle表示文件打开,它的个数不能超过YAFFSFS_N_HANDLES
	handle = yaffsfs_GetHandle();
	
	if(handle >= 0)//如果得到文件Handle
	{
                  //得到yaffsfs_Handle指针
		h = yaffsfs_GetHandlePointer(handle);
	
	         //试图根据文件路径找到文件对象
		// try to find the exisiting object
		obj = yaffsfs_FindObject(NULL,path,0);
		
                  //如果是链接文件,再找一次
		if(obj && obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
		{
		
			obj = yaffsfs_FollowLink(obj,symDepth++);
		}

		if(obj && obj->variantType != YAFFS_OBJECT_TYPE_FILE)
		{
			obj = NULL;
		}

		if(obj)//表示文件对象找到
		{
			// Check if the object is already in use
			alreadyOpen = alreadyExclusive = 0;
			
                           //如果这个文件正在使用,并且它打开时已经加了O_EXCL(表示独占使用),则将alreadyExclusive置1,后面将判断。
			for(i = 0; i <= YAFFSFS_N_HANDLES; i++)
			{
				
				if(i != handle &&
				   yaffsfs_handle[i].inUse &&
				    obj == yaffsfs_handle[i].obj)
				 {
				 	alreadyOpen = 1;
					if(yaffsfs_handle[i].exclusive)
					{
						alreadyExclusive = 1;
					}
				 }
			}

			if(((oflag & O_EXCL) && alreadyOpen) || alreadyExclusive)
			{
				openDenied = 1;//表示操作失败
			}
			
			// Open should fail if O_CREAT and O_EXCL are specified
			if((oflag & O_EXCL) && (oflag & O_CREAT))
			{//如果文件存在,不允许使用O_EXCL和O_CREAT打开文件
				openDenied = 1;
				yaffsfs_SetError(-EEXIST);
				errorReported = 1;
			}
			
			// Check file permissions
                           //检查权限
			if( (oflag & (O_RDWR | O_WRONLY)) == 0 &&     // ie O_RDONLY
			   !(obj->yst_mode & S_IREAD))
			{
				openDenied = 1;
			}
                            
			if( (oflag & O_RDWR) && 
			   !(obj->yst_mode & S_IREAD))
			{
				openDenied = 1;
			}

			if( (oflag & (O_RDWR | O_WRONLY)) && 
			   !(obj->yst_mode & S_IWRITE))
			{
				openDenied = 1;
			}
			
		}
		
		else if((oflag & O_CREAT))
                  //文件不存在,并加了O_CREAT标志
		{
			// Let's see if we can create this file
                           //判断路径是否存在
			dir = yaffsfs_FindDirectory(NULL,path,&name,0);
			if(dir)
			{//如果路径存在,则新建文件
				obj = yaffs_MknodFile(dir,name,mode,0,0);	
			}
			else
			{
				yaffsfs_SetError(-ENOTDIR);
				errorReported = 1;
			}
		}
		
		if(obj && !openDenied)
		{
                   //文件存在或新建文件成功,并且权限正确,则初始化
			h->obj = obj;
			h->inUse = 1;
	    	h->readOnly = (oflag & (O_WRONLY | O_RDWR)) ? 0 : 1;
			h->append =  (oflag & O_APPEND) ? 1 : 0;
			h->exclusive = (oflag & O_EXCL) ? 1 : 0;
			h->position = 0;
			
			obj->inUse++;
			if((oflag & O_TRUNC) && !h->readOnly)
			{
                             //文件不是只读,且加了O_TRUNC标志(表示打开文件是清空文件)
				//todo truncate
				yaffs_ResizeFile(obj,0);
			}
			
		}
		else  //出错处理
		{
			yaffsfs_PutHandle(handle);
			if(!errorReported)
			{
				yaffsfs_SetError(-EACCES);
				errorReported = 1;
			}
			handle = -1;
		}

	}

	yaffsfs_Unlock();

	return handle;
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值