-
Notifications
You must be signed in to change notification settings - Fork 0
/
rdwt.cpp
97 lines (86 loc) · 2.82 KB
/
rdwt.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <stdio.h>
#include "filesys.h"
unsigned int read(int fd, char *buf, unsigned int size){
unsigned long off;
int block, block_off, i, j;
struct inode *inode;
char *temp_buf;
inode = sys_ofile[user[user_id].u_ofile[fd]].f_inode;
if (!(sys_ofile[user[user_id].u_ofile[fd]].f_flag & FREAD)){
printf("\nthe file is not opened for read\n");
return 0;
}
temp_buf = buf;
off = sys_ofile[user[user_id].u_ofile[fd]].f_off;
if ((off+size) > inode->di_size){
size = inode->di_size - off;
}
block_off = off % BLOCKSIZ;
block = off / BLOCKSIZ;
if (block_off+size<BLOCKSIZ){
memcpy(buf, disk+DATASTART+inode->di_addr[block]*BLOCKSIZ+block_off, size);
return size;
}
memcpy(temp_buf, disk+DATASTART+inode->di_addr[block]*BLOCKSIZ+block_off,BLOCKSIZ-block_off);
temp_buf += BLOCKSIZ - block_off;
j = (inode->di_size-off-block_off) / BLOCKSIZ;
for (i=0; i<(size-(BLOCKSIZ-block_off))/BLOCKSIZ; i++){
memcpy(temp_buf, disk+DATASTART+inode->di_addr[j+i]*BLOCKSIZ, BLOCKSIZ);
temp_buf += BLOCKSIZ;
}
block_off = (size-(BLOCKSIZ-block_off))% BLOCKSIZ;
memcpy(temp_buf, disk+DATASTART+i*BLOCKSIZ, block_off);
sys_ofile[user[user_id].u_ofile[fd]].f_off += size;
return size;
}
unsigned int write(int fd, char *buf, unsigned int size){
unsigned long off;
int block, block_off, i, j;
struct inode *inode;
char *temp_buf;
inode = sys_ofile[user[user_id].u_ofile[fd]].f_inode;
if (!(sys_ofile[user[user_id].u_ofile[fd]].f_flag & FWRITE)){
printf("\nthe file is not opened for write\n");
return 0;
}
//add by liwen to check the filesize and alloc the BLOCK
off = sys_ofile[user[user_id].u_ofile[fd]].f_off;
block = ((off+size)-inode->di_size)/BLOCKSIZ;//ÉÐÐè¸öÊý
if(((off+size)-inode->di_size)%BLOCKSIZ)
block++;
if(filsys.s_nfree < block){
printf("Not enough space to write so much bytes!\n");
return 0;
}
j = inode->di_size/BLOCKSIZ;
if(inode->di_size % BLOCKSIZ){
j ++;
}
if(j+block>NADDR){
printf("To write so much bytes will exceed the file limit!!\n");
return 0;
}
for(i=j;i<j+block;i++){
inode->di_addr[i]=balloc();
}
inode->di_size += size;
//end add
temp_buf = buf;
off = sys_ofile[user[user_id].u_ofile[fd]].f_off;
block_off = off % BLOCKSIZ;
block = off/BLOCKSIZ;
if (block_off+size<BLOCKSIZ){
memcpy(disk+DATASTART+inode->di_addr[block]*BLOCKSIZ+block_off, buf, size);
return size;
}
memcpy(disk+DATASTART+inode->di_addr[block]*BLOCKSIZ+block_off,temp_buf,BLOCKSIZ-block_off);
temp_buf += BLOCKSIZ-block_off;
for (i=0; i<(size-(BLOCKSIZ-block_off))/BLOCKSIZ; i++){
memcpy(disk+DATASTART+inode->di_addr[block+1+i]*BLOCKSIZ, temp_buf, BLOCKSIZ);
temp_buf += BLOCKSIZ;
}
block_off = (size-(BLOCKSIZ-block_off)) % BLOCKSIZ;
memcpy(disk + DATASTART + inode->di_addr[block + 1 + i] * BLOCKSIZ, temp_buf, block_off);
sys_ofile[user[user_id].u_ofile[fd]].f_off += size;
return size;
}