📄 read_write.c
字号:
/////////////////////////////////////////////////////////////////////
#include "general.h"
#include "min_max.h"
#include "s_isdirreg.h"
#include "hd_info_struct.h"
#include "dir_entry.h"
#include "msdos_dir_entry.h"
#include "d_inode.h"
#include "m_inode.h"
#include "buffer_head.h"
#include "fat_cache.h"
#include "file.h"
#include "hd_request_struct.h"
#include "super_block.h"
#include "common_head.h"
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
int file_rddir(struct file * filep, char * buf, int length)
{
struct m_inode * m_inodep;
struct buffer_head * bh;
struct m_inode * temp_minodep;
char * p;
int block, offset, left, left0, count, temp_val0, temp_val1, temp_val2;
int i;
m_inodep = filep->f_inode;
temp_val0 = length / (NAME_LEN + 9);
temp_val1 = (m_inodep->i_size - filep->f_pos) / (NAME_LEN + 4);
left = MIN(temp_val0, temp_val1);
if (left <= 0)
return 0;
count = 0;
while (left) {
if ((!(block = bmap(m_inodep , filep->f_pos / BLOCK_SIZE))) ||
(!(bh = bread(m_inodep->i_dev , block , 0))))
return count;
offset = filep->f_pos % BLOCK_SIZE;
temp_val2 = (BLOCK_SIZE - offset) / (NAME_LEN + 4);
temp_val2 = MIN(temp_val1, temp_val2);
left0 = MIN(temp_val0, temp_val2);
p = bh->b_data + offset;
while (left0) {
if (!(*((int *)p))) {
p = p + NAME_LEN + 4;
goto file_rddir_0;
}
if (!(temp_minodep = iget(m_inodep->i_dev , (*((int *)p))))){
brelse(bh);
return count;
}
p = p + 4;
for (i = 0; i < NAME_LEN; i++)
put_gs_byte(*p++,buf++);
put_gs_byte(temp_minodep->i_mode,buf++);
put_gs_long(temp_minodep->i_mtime,(long *)buf);
buf = buf + 4;
put_gs_long(temp_minodep->i_size,(long *)buf);
buf = buf + 4;
iput(temp_minodep);
count++;
temp_val0--;
file_rddir_0:
temp_val1--;
temp_val2--;
left0 = MIN(temp_val0, temp_val2);
filep->f_pos = filep->f_pos + NAME_LEN + 4;
}
brelse(bh);
left = MIN(temp_val0, temp_val1);
}
return count;
}
int file_read(struct file * filep, char * buf, int count)
{
struct m_inode * m_inodep;
struct buffer_head * bh;
char * p;
int block,offset,left,chars;
m_inodep = filep->f_inode;
if ((left = count) <= 0)
return 0;
while (left) {
if (!(block = bmap(m_inodep , filep->f_pos / BLOCK_SIZE)))
bh = NULL;
else if (!(bh = bread(m_inodep->i_dev , block , 0)))
break;
offset = filep->f_pos % BLOCK_SIZE;
chars = MIN(BLOCK_SIZE - offset , left);
filep->f_pos += chars;
left -= chars;
if (bh) {
p = bh->b_data + offset;
while (chars--)
put_gs_byte(*p++,buf++);
brelse(bh);
}
else {
while (chars--)
put_gs_byte(0,buf++);
}
}
return (count - left);
}
int file_write(struct file * filep, char * buf, int count)
{
struct m_inode * m_inodep;
struct buffer_head * bh;
char * p;
int block,offset,left,chars;
unsigned long pos;
m_inodep = filep->f_inode;
if ((left = count) <= 0)
return 0;
if (filep->f_flag & O_APPEND)
pos = m_inodep->i_size;
else
pos = filep->f_pos;
while (left) {
if (!(block = create_block(m_inodep , pos / BLOCK_SIZE)))
break;
if (!(bh = bread(m_inodep->i_dev , block , 0)))
break;
offset = pos % BLOCK_SIZE;
chars = MIN(BLOCK_SIZE - offset , left);
pos += chars;
left -= chars;
p = bh->b_data + offset;
while (chars--)
*p++ = get_gs_byte(buf++);
bh->b_dirt = 1;
brelse(bh);
}
if (pos > m_inodep->i_size)
m_inodep->i_size = pos;
m_inodep->i_mtime = file_datetime;
m_inodep->i_dirt = 1;
if (!(filep->f_flag & O_APPEND))
filep->f_pos = pos;
return (count - left);
}
int sys_lseek(unsigned int fd, int offset, int origin){ struct file * filep;
struct m_inode * m_inodep; if ((fd >= NR_FILE) || (file_table[fd].f_count == 0))
return -1;
filep = &file_table[fd];
m_inodep = filep->f_inode;
if (!m_inodep)
return -1;
switch (origin) { case 0: if (offset < 0)
return -1; filep->f_pos = offset; break; case 1: if (filep->f_pos + offset < 0)
return -1; filep->f_pos = filep->f_pos + offset; break; case 2: if (m_inodep->i_size + offset < 0)
return -1; filep->f_pos = m_inodep->i_size + offset; break; default: return -1; } return filep->f_pos;}
int sys_rddir(char * buf,int length)
{
struct file * filep;
struct m_inode * m_inodep;
if (dir_file.f_count == 0)
return -1;
filep = &dir_file;
m_inodep = filep->f_inode;
if (!buf || !m_inodep || !S_ISDIR(m_inodep->i_mode))
return -1;
if ((length <= 0) || (filep->f_pos >= m_inodep->i_size) || (filep->f_pos % (NAME_LEN + 4)))
return 0;
return file_rddir(filep,buf,length);
}
int sys_read(unsigned int fd,char * buf,int count){ struct file * filep; struct m_inode * m_inodep; if ((fd >= NR_FILE) || (file_table[fd].f_count == 0))
return -1; filep = &file_table[fd];
m_inodep = filep->f_inode;
if (!buf || !m_inodep || !S_ISREG(m_inodep->i_mode))
return -1;
if (count <= 0)
return 0;
if (filep->f_pos + count > m_inodep->i_size) count = m_inodep->i_size - filep->f_pos; if (count <= 0) return 0; return file_read(filep,buf,count);}int sys_write(unsigned int fd,char * buf,int count){ struct file * filep; struct m_inode * m_inodep; if ((fd >= NR_FILE) || (file_table[fd].f_count == 0))
return -1;
filep = &file_table[fd];
m_inodep = filep->f_inode;
if (!buf || !m_inodep || !S_ISREG(m_inodep->i_mode))
return -1;
if (count <= 0)
return 0;
return file_write(filep,buf,count);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -