int general_file_write(struct m_inode *inode,struct file *filp,char *buf,int count) { int i = 0,block_nr,c; off_t pos = 0; char *p = NULL; struct buffer_head *bh = NULL; int nr_start_sect = 0; printk("write 5"); if(!inode || !filp || !buf || count < 0) { printk("write parameters error."); return -EINVAL; } /*int nr_start_sect = inode->i_start_sect;*/ if((nr_start_sect = get_first_block(inode)) == 0) { set_block_nums(inode->i_dev ,(struct d_inode *)inode, NR_DEFAULT_SECTS); if((nr_start_sect = get_first_block(inode)) == 0) return -3; } printk("write num = %d.start_sect = %d.",inode->i_num, nr_start_sect); if(filp->f_flag & O_APPEND) pos = inode->i_size; else pos = filp->f_pos; while(i < count) { /*block = nr_start_sect + pos/SECTOR_SIZE;*/ block_nr = get_pos_block(inode, pos); bh = getblk(inode->i_dev,block_nr); c = pos % SECTOR_SIZE; p = bh->b_data + c; c = SECTOR_SIZE -c; if(c > count) c = count - i; pos += c; i += c; if(pos > inode->i_size) { inode->i_size = pos; } while(c-- > 0) *(p++) = *(buf++); hd_rw(inode->i_dev,block_nr,1,ATA_WRITE,bh); brelse(bh); } inode->i_dirty = 1; return 0; }
//读文件 int general_file_read(struct m_inode *inode,struct file *filp,char *buf,int count) { int i,c,m = 0; char *p; // char *ptr = buf; struct buffer_head *bh; struct dir_entry *de; int block_nr = 0; if(!inode || !filp) return -EINVAL; if(!buf || count <= 0) return 0; int nr_start_sect = get_first_block(inode); printk("read num = %d.start_sect = %d.",inode->i_num, nr_start_sect); // printk("inode->i_dev = %d inode->i_num = %d inode->i_start_sect = %d\n",inode->i_dev,inode->i_num,inode->i_start_sect); //文件所占的磁盘块总数 off_t pos = filp->f_pos; //开始读取的磁盘块 nr_start_sect += pos/SECTOR_SIZE; // nr_sects = (inode->i_size + SECTOR_SIZE -1)/SECTOR_SIZE; while(i < count) { block_nr = get_pos_block(inode, pos); c = pos % SECTOR_SIZE; c = SECTOR_SIZE -c ; if(c > count) { c = count - i; } // printk("file_read : inode->i_dev = %d nr_start_sect = %d count = %d\n",inode->i_dev,nr_start_sect + pos/SECTOR_SIZE,count); bh = getblk(inode->i_dev,block_nr); hd_rw(inode->i_dev,block_nr,1,ATA_READ,bh); p = bh->b_data + pos; de = (struct dir_entry*)bh->b_data; // printk("de->file_name = %s\n",(++de)->file_name); filp->f_pos += c; pos += c; i += c; m++; // printk("c = %d\n",c); while(c-- > 0) { if(*p == 0) { p++; continue; } else { *buf = *p; // printk("%d %c %c\t",512 - c,*p,*buf); buf++; p++; } } brelse(bh); } return 0; }
void read_thread(void *args) { transfer_session *session; file_desc *f_desc; file_block_desc *b_desc; frame_index *f_index, *last_index; FILE *fp = NULL; int err; int i = 0; session = (transfer_session *)args; f_desc = session->f_desc; for(;;) { if(session->state != STATE_TRANSFER) { /* if state = finish, close file and exit the thread */ if(fp) { fclose(fp); fp = NULL; } sleep(1); continue; } if(NULL == fp) { if(NULL == (fp = fopen(f_desc->file_name, "r"))) { t_log("open file error"); continue; } } b_desc = get_first_block(f_desc); if(b_desc == ERR_BLOCK_LIST_NULL) return; else if(b_desc == ERR_BLOCK_LIST_EMPTY) { sleep(1); continue; } else { if(b_desc->retry_flag == ORIGIN_FRAME) { f_index = b_desc->index; for(i = 1; i <= MAX_FRAME_COUNT; i++) { f_index->frame_index = i; if(read_file_to_msg_q(f_index, f_desc, fp, session->data_qid) < 0) break; } t_free(f_index); } else if(b_desc->retry_flag == RETRAN_FRAME) { printf("read_thread re tran\n"); f_index = b_desc->index; if(f_index->frame_index == 0XFFFF) { for(i = 1; i <= MAX_FRAME_COUNT; i++) { f_index->frame_index = i; read_file_to_msg_q(f_index, f_desc, fp, session->data_qid); } t_free(f_index); } else { do { read_file_to_msg_q(f_index, f_desc, fp, session->data_qid); last_index = f_index; f_index = f_index->next; t_free(last_index); }while(f_index); } } } t_free(b_desc); // printf("block:%d\n", b_desc->index->block_index); } return; }