⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 open.cpp

📁 JFFS的源代码
💻 CPP
字号:
/*各种flags在fcntl.h中定义*/
#include <io.h>
#include "../jffs/jffstypes.h"
#include "../jffs/stat.h"
#include "../jffs/fs.h"
#include "../jffs/errno.h"

#define D(x) x

struct file files_fd[NR_OPEN]; /*静态分配空间*/
struct inode files_inode[NR_OPEN];
fd_set files_open_fds;
int current_fd;

/*来自file.h*/
struct file * fget(unsigned long fd)
{
	struct file * file = (struct file *)NULL;
	if (fd < NR_OPEN) {
		file = &files_fd[fd];
		if (file)
			file->f_count++;
	}
	return file;
}
/*void __fput(struct file *filp, struct inode *inode)
{
	if (filp->f_op && filp->f_op->release)
		filp->f_op->release(inode,filp);
	filp->f_inode = NULL;
	if (filp->f_mode & FMODE_WRITE)
		put_write_access(inode);
	iput(inode);
}*/

void fput(struct file *file, struct inode *inode)
{
	int count = file->f_count-1;
	/*if (!count)
		__fput(file, inode);*/
	file->f_count = count;
}

/*
 * Find a empty file descriptor entry, and mark it busy
 */
int get_unused_fd(void)
{
	int fd;

	FD_SET(0, &files_open_fds);	/*!!!屏蔽0值fd, 与C接口兼容*/

	fd = find_next_zero_bit(&files_open_fds, NR_OPEN, 0);
	if( fd>= NR_OPEN )
	{
		return -EMFILE;
	}
	
	FD_SET(fd, &files_open_fds);
	files_fd[fd].f_inode = &files_inode[fd]; /*!!!*/
	return fd;
}
void put_unused_fd(int fd)
{
	FD_CLR(fd, &files_open_fds);
}

static int do_open(const char * filename,int flags,int mode, int fd)
{
	struct inode * inode;
	struct file * f;
	int flag,error;

	f = fget(fd);
	
	f->f_flags = flag = flags;
	error = open_namei(filename,flag,mode,&inode,(struct inode *)NULL);
	
	if (error)
		goto cleanup_file;

	f->f_inode = inode;
	f->f_pos = 0;
	f->f_reada = 0;
	f->f_op = (struct file_operations * )NULL;
	if (inode->i_op)
		f->f_op = inode->i_op->default_file_ops;
	
	if (f->f_op && f->f_op->open) {
		error = f->f_op->open(inode,f);
		if (error)
			goto cleanup_all;
	}

	return 0;

cleanup_all:
	iput(inode);
cleanup_file:
	f->f_count--;	/*???*/
	return error;
}

/*保留权限mode,但不使用, 只是为了减少移植代码修改量*/
int mf_open(char * filename,int flags,int mode)
{
	int fd, error;
	char *p;				/*livefall@163.com 2005.01.09*/
	
	fd = get_unused_fd();	/*取文件描述符, 所有任务共用FD数组*/
	
	if (fd < 0){
		printf("mf_open: 文件描述符被用尽!\n");
		return fd;
	}
	/*根据路径填充相应的文件操作指针
	如果在flash目录下
	??? 如果文件在flash中不存在, 需要创建, 并写入数据, 如何进行.
	在open_namei中根据标志创建, 在jffs中使用jffs_create创建不带数据的文件头
	如果在RAM目录下
	如果是para.dat
	如果是ycpara.dat
	其它路径非法 */

	/*文件名大写->小写转化*/
/*	char *p = filename;		livefall@163.com 2005.01.09*/
	p = filename;
	while(*p){
		if(*p >= 'A' && *p <= 'Z') *p += 0x20;
		p++;
	}
	
	current_fd = fd;
	error = do_open(filename,flags,mode, fd);
	
	if (!error)	
		return fd;
	else  {      
		put_unused_fd(fd);
		return error;
	}
}

int mf_close(unsigned int fd)
{
	if(fd>=NR_OPEN) return -2;
	put_unused_fd(fd);
	return 0;	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -