荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: GyeaonWoo (柏林恋人), 信区: Linux
标  题: Linus's Linux--fs/file_dev.c
发信站: 荔园晨风BBS站 (Mon Aug 25 19:54:34 2003), 站内信件

#include <errno.h>
#include <fcntl.h>

#include <linux/sched.h>
#include <linux/kernel.h>
#include <asm/segment.h>

#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))

int file_read(struct m_inode * inode, struct file * filp, char * buf,
int count)
{
        int left,chars,nr;
        struct buffer_head * bh;

        if ((left=count)<=0)
                return 0;
        while (left) {
                if (nr = bmap(inode,(filp->f_pos)/BLOCK_SIZE)) {
                        if (!(bh=bread(inode->i_dev,nr)))
                                break;
                } else
                        bh = NULL;
                nr = filp->f_pos % BLOCK_SIZE;
                chars = MIN( BLOCK_SIZE-nr , left );
                filp->f_pos += chars;
                left -= chars;
                if (bh) {
                        char * p = nr + bh->b_data;
                        while (chars-->0)
                                put_fs_byte(*(p++),buf++);
                        brelse(bh);
                } else {
                        while (chars-->0)
                                put_fs_byte(0,buf++);
                }
        }
        inode->i_atime = CURRENT_TIME;
        return (count-left)?(count-left):-ERROR;
}

int file_write(struct m_inode * inode, struct file * filp, char * buf,
int count)
{
        off_t pos;
        int block,c;
        struct buffer_head * bh;
        char * p;
        int i=0;

/*
 * ok, append may not work when many processes are writing at the same
time
 * but so what. That way leads to madness anyway.
 */
        if (filp->f_flags & O_APPEND)
                pos = inode->i_size;
        else
                pos = filp->f_pos;
        while (i<count) {
                if (!(block = create_block(inode,pos/BLOCK_SIZE)))
                        break;
                if (!(bh=bread(inode->i_dev,block)))
                        break;
                c = pos % BLOCK_SIZE;
                p = c + bh->b_data;
                bh->b_dirt = 1;
                c = BLOCK_SIZE-c;
                if (c > count-i) c = count-i;
                pos += c;
                if (pos > inode->i_size) {
                        inode->i_size = pos;
                        inode->i_dirt = 1;
                }
                i += c;
                while (c-->0)
                        *(p++) = get_fs_byte(buf++);
                brelse(bh);
        }
        inode->i_mtime = CURRENT_TIME;
        if (!(filp->f_flags & O_APPEND)) {
                filp->f_pos = pos;
                inode->i_ctime = CURRENT_TIME;
        }
        return (i?i:-1);
}

--
终于能按照自己的内心写作了
却不能按照一个人的内心生活
花太香,只好梦蝶一场唉唉

※ 来源:·荔园晨风BBS站 bbs.szu.edu.cn·[FROM: 192.168.35.5]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店