@@ -8,7 +8,103 @@ typedef struct
8
8
#define MAX ( a , b ) ( ( a ) > ( b ) ? ( a ) : ( b ) )
9
9
int * get_data_block_at_inode (EXT2_FILESYSTEM * fs , INODE * inode );
10
10
int ext2_write (EXT2_NODE * file , unsigned long offset , unsigned long length , const char * buffer )
11
- {
11
+ {
12
+ int writeEnd ;
13
+ int currentOffset ,currentBlock , blockSeq ,copyLength ;
14
+ int fileSize ,i = 0 ;
15
+ INODE * inodeBuffer ;
16
+ char sector [MAX_SECTOR_SIZE ];
17
+ ZeroMemory (sector ,sizeof (sector ));
18
+ inodeBuffer = (INODE * )sector ;
19
+ int * dataBlocks ;
20
+ get_inode (file -> fs ,file -> entry .inode ,& inodeBuffer );
21
+ // 새로만든 파일이라서 쓸 블럭이 없으면 추가시켜준다
22
+ printf ("hello \n" );
23
+ if (inodeBuffer -> blocks == 0 )
24
+ {
25
+ expand_block (file -> fs ,file -> entry .inode );
26
+ printf ("블럭이 없어서 추가시켜줬어영\n" );
27
+ }
28
+ get_inode (file -> fs ,file -> entry .inode ,inodeBuffer );
29
+ currentOffset = offset ; // 어디부터 쓸건지
30
+ fileSize = inodeBuffer -> blocks ; // 파일 사이즈를 기록한다
31
+
32
+ printf ("%d \n" ,fileSize ) ;
33
+ while ((offset ) > (inodeBuffer -> blocks * MAX_BLOCK_SIZE - 1 )) // offset이 가지고 있는 블록보다 클경우
34
+ {
35
+
36
+ expand_block (file -> fs ,file -> entry .inode );
37
+ }
38
+
39
+
40
+ dataBlocks = get_data_block_at_inode (file -> fs ,& inodeBuffer ); // 블럭 받는다
41
+ printf (" aaa %d\n" ,dataBlocks [0 ]);
42
+ writeEnd = MIN (offset + length , fileSize * MAX_BLOCK_SIZE ); // 어디까지 쓸건지
43
+ int blockOffset = currentOffset % MAX_BLOCK_SIZE ; // 해당 블록 내에서의 위치
44
+ printf ("hello \n" );
45
+ while ((offset ) > (inodeBuffer -> blocks * MAX_BLOCK_SIZE - 1 ))
46
+ {
47
+ dataBlocks [++ i ];
48
+ } // 해당 블럭을 찾는 일
49
+
50
+ currentBlock = dataBlocks [i ]; // 몇번째 블록 부터 읽을지 정한다
51
+
52
+ ZeroMemory (sector ,sizeof (sector ));
53
+ printf ("hello \n" );
54
+ while (currentOffset < writeEnd ) // 어디까지 읽을지 정하는 것
55
+ { printf ("여기까진함 \n" );
56
+ data_read (file -> fs ,file -> location .group ,currentBlock ,sector ); // 일단 해당 그룹의 블록을 읽어온다
57
+ printf ("여기까진함 \n" );
58
+ copyLength = MIN (MAX_BLOCK_SIZE - blockOffset , writeEnd - currentOffset );
59
+ memcpy (& sector [blockOffset ],buffer ,copyLength );
60
+ data_write (file -> fs ,file -> location .group ,currentBlock ,sector );
61
+ buffer += copyLength ; // 읽은만큼 더해준다
62
+ currentOffset += offset ;
63
+ }
64
+
65
+ return currentOffset - offset ; // 얼마나 읽었는지 알려준다
66
+ }
67
+ int ext2_read (EXT2_NODE * file , unsigned long offset , unsigned long length , const char * buffer )
68
+ {
69
+
70
+ printf ("hello \n" );
71
+ int readEnd ;
72
+ int currentOffset ,currentBlock , blockSeq ,copyLength ;
73
+ int fileSize ,i = 0 ;
74
+ INODE * inodeBuffer ;
75
+ char sector [MAX_SECTOR_SIZE ];
76
+ ZeroMemory (sector ,sizeof (sector ));
77
+ inodeBuffer = (INODE * )sector ;
78
+ int * dataBlocks ;
79
+ get_inode (file -> fs ,file -> entry .inode ,inodeBuffer );
80
+ if (inodeBuffer -> blocks == 0 ) // 파일에 해당되는 블록이 아예없을경우
81
+ {
82
+ return EXT2_ERROR ;
83
+ }
84
+ dataBlocks = get_data_block_at_inode (file -> fs ,inodeBuffer );
85
+ currentOffset = offset ;
86
+ fileSize = inodeBuffer -> blocks ;
87
+ readEnd = MIN (offset + length , fileSize * MAX_BLOCK_SIZE );
88
+ int blockOffset = currentOffset % MAX_BLOCK_SIZE ; // 해당 블록 내에서 몇번째 섹터인지
89
+ printf ("hello 2\n" );
90
+ printf (" %d" ,dataBlocks [i ]);
91
+ while ((offset /MAX_BLOCK_SIZE ) > i )
92
+ {
93
+ dataBlocks [++ i ];
94
+ } // 해당 블럭을 찾는 일
95
+ currentBlock = dataBlocks [i ]; // 몇번째 블록 부터 읽을지 정한다
96
+ ZeroMemory (sector ,sizeof (sector ));
97
+ printf ("hello 3\n" );
98
+ while (currentOffset < readEnd ) // 어디까지 읽을지 정하는 것
99
+ {
100
+ data_read (file -> fs ,file -> location .group ,currentBlock ,sector ); // 일단 해당 그룹의 블록을 읽어온다
101
+ copyLength = MIN (MAX_BLOCK_SIZE - blockOffset , readEnd - currentOffset );
102
+ memcpy (buffer ,& sector [blockOffset ],copyLength );
103
+ buffer += copyLength ; // 읽은만큼 더해준다
104
+ currentOffset += offset ;
105
+ }
106
+ return currentOffset - offset ; // 얼마나 읽었는지 알려준다
107
+
12
108
}
13
109
14
110
UINT32 get_free_inode_number (EXT2_FILESYSTEM * fs , int group );
@@ -630,7 +726,12 @@ UINT32 expand_block(EXT2_FILESYSTEM * fs, UINT32 inode_num)
630
726
blocks ++ ;
631
727
632
728
}
633
-
729
+ inodeBuffer -> blocks = blocks ;
730
+ printf ("used blocks %d \n" ,used_blocks );
731
+ write_bitmap ( used_blocks ,1 ,sector );
732
+ fs -> disk -> write_sector (fs -> disk , 1 + groupNum * fs -> sb .block_per_group + 2 , sector ); // 블록 비트맵 수정
733
+ printf ("ddd %d" , inodeBuffer -> blocks );
734
+ set_inode_onto_inode_table (fs ,inode_num ,inodeBuffer );
634
735
return EXT2_SUCCESS ;
635
736
636
737
0 commit comments