-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrite_cp.c
More file actions
219 lines (192 loc) · 6.02 KB
/
Copy pathwrite_cp.c
File metadata and controls
219 lines (192 loc) · 6.02 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#ifndef WRITECP
#define WRITECP
#include "mkdir_creat.c"
#include "read_cat.c"
#include "open_close.c"
int my_write_str(char *fd_string, char *content)
{
// (1) convert fd to integer
int fd = atoi(fd_string);
// (2) verify fd is opened and for correct mode
if (fd < 0 || fd >= NFD)
{
printf("write> invalid fd\n");
return 0;
}
OFT *oftp = running->fd[fd];
if (!oftp)
{
printf("write> fd not an instance\n");
return 0;
}
if (oftp->mode < 1 || oftp->mode > 3)
{
printf("write> fd not correct mode\n");
return 0;
}
// (3) make my_write call
char buf[BLKSIZE];
strcpy(buf, content);
int nbytes = strlen(buf);
return my_write(fd, buf, nbytes);
}
int my_write(int fd, char buf[ ], int nbytes)
{
OFT *oftp = running->fd[fd];
MINODE *mip = oftp->minodePtr;
INODE *ip = &mip->INODE;
int total_wrote = 0;
int first = 0;
while (nbytes > 0)
{
// compute logical block
int lbk = oftp->offset / BLKSIZE;
int startByte = oftp->offset % BLKSIZE;
// get block
int blk = -1;
if (lbk < 12)
{
// direct block
if (ip->i_block[lbk] == 0)
{
// allocate new block
ip->i_block[lbk] = balloc(mip->dev);
}
blk = ip->i_block[lbk];
}
else if (lbk >= 12 && lbk < 256 + 12)
{
// indirect block
int ibuf[256];
if (ip->i_block[12] == 0)
{
ip->i_block[12] = balloc(mip->dev);
get_block(mip->dev, ip->i_block[12], ibuf);
bzero(ibuf, BLKSIZE);
put_block(mip->dev, ip->i_block[12], ibuf);
}
get_block(mip->dev, ip->i_block[12], ibuf);
blk = ibuf[lbk - 12];
if (blk == 0)
{
blk = ibuf[lbk - 12] = balloc(mip->dev);
put_block(mip->dev, ip->i_block[12], ibuf);
}
}
else {
// double indirect block
// check if [13] is allocated
int ibuf[256];
if (ip->i_block[13] == 0)
{
// if not, allocate new block
ip->i_block[13] = balloc(mip->dev);
// load block
get_block(mip->dev, ip->i_block[13], ibuf);
// bzero block
bzero(ibuf, BLKSIZE);
// changed: [13], save changed blocks
put_block(mip->dev, ip->i_block[13], ibuf);
}
// get indirect block
get_block(mip->dev, ip->i_block[13], ibuf);
// get block of indirect block
int first_index = (lbk - 256 - 12) / 256;
first = ibuf[first_index];
// check if allocated
int iibuf[256];
if (first == 0)
{
// if not, allocate new block
first = balloc(mip->dev);
ibuf[first_index] = first;
// load block
get_block(mip->dev, first, iibuf);
// bzero block
bzero(iibuf, BLKSIZE);
// changed: [13] and allocated block, save changed blocks
put_block(mip->dev, first, iibuf);
put_block(mip->dev, ip->i_block[13], ibuf);
}
// get indirect block of indirect block
get_block(mip->dev, first, iibuf);
int second_index = (lbk - 256 - 12) % 256;
int second = iibuf[second_index];
// check if allocated
int iiibuf[256]; //
if (second == 0)
{
// if not, allocate new block
second = balloc(mip->dev);
iibuf[second_index] = second;
// load block
get_block(mip->dev, second, iiibuf);
// bzero block
bzero(iiibuf, BLKSIZE);
// changed: [1st indirect block] and allocated block, save changed blocks
put_block(mip->dev, second, iiibuf);
put_block(mip->dev, first, iibuf);
}
// get blk
blk = second;
}
// write to block the lesser of remain and nbytes
// printf("first: %d blk: %d\n", first, blk);
char wbuf[BLKSIZE];
bzero(wbuf, BLKSIZE);
get_block(mip->dev, blk, wbuf);
char *cp = wbuf + startByte;
int remain = BLKSIZE - startByte;
int write_amount = remain < nbytes ? remain : nbytes;
memcpy(cp, buf, write_amount);
oftp->offset += write_amount;
nbytes -= write_amount;
total_wrote += write_amount;
if (oftp->offset > ip->i_size)
{
ip->i_size = oftp->offset;
}
put_block(mip->dev, blk, wbuf);
}
mip->dirty = 1;
// printf("wrote %d char into file descriptor fd=%d\n", total_wrote, fd);
return total_wrote;
}
int my_cp(char* source, char* destination)
{
printf("Attempting to copy %s into %s\n", source, destination);
int n = 0;
int fd = open_file(source, "0");
// check if destination exists
int ino = getino(destination);
if (ino == 0)
{
// does not exist, create file
my_creat(destination);
}
int gd = open_file(destination, "1");
if (fd == -1 || gd == -1)
{
if (fd == -1)
{
printf("Source file wasn't opened properly and now aborting from command.\n");
close_file(fd);
}
if (gd == -1)
{
printf("Destination file wasn't opened properly and now aborting from command.\n");
close_file(gd);
}
return -1;
}
char mybuff[BLKSIZE] = {0};
memset(mybuff, '\0', BLKSIZE);
while(n = my_read(fd, mybuff, BLKSIZE)){
mybuff[n] = 0;
my_write(gd, mybuff, n); // notice the n in write()
memset(mybuff, '\0', BLKSIZE);
}
close_file(fd);
close_file(gd);
}
#endif