-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrite_cp.c
More file actions
177 lines (152 loc) · 3.65 KB
/
write_cp.c
File metadata and controls
177 lines (152 loc) · 3.65 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
//Write to a file
int minimu(int one, int two)
{
if(one < two)
{
return one;
}
return two;
}
int my_write(int fd, char *buf, int nbytes)
{
// printf("%d %s %d\n",fd,buf,nbytes);
OFT* place = running->fd[fd];
if(place == 0)
{
printf("Nothing to write to...\n");
return;
}
int blk = 0, startbyte = 0, lbk = 0;
MINODE* mip = running->fd[fd]->minodePtr;
int indirect[256], temp[256];
char wbuf[BLKSIZE];
char *cq = buf;
while(nbytes > 0){
lbk = place->offset / BLKSIZE;
startbyte = place->offset % BLKSIZE;
if(lbk < 12)
{
if(mip->INODE.i_block[lbk] == 0)
{
mip->INODE.i_block[lbk] = balloc(dev);
}
blk = mip->INODE.i_block[lbk];
}
else if (lbk >= 12 && lbk < 256 + 12)
{
if(mip->INODE.i_block[12] == 0)
{
mip->INODE.i_block[12] = balloc(dev);
get_block(dev,mip->INODE.i_block[12],indirect);
bzero(indirect, BLKSIZE);
put_block(dev,mip->INODE.i_block[12],indirect);//with zero values
}
get_block(dev,mip->INODE.i_block[12],indirect);
blk = indirect[lbk - 12];
if(blk == 0)
{
blk = indirect[lbk - 12] = balloc(dev);
put_block(dev,mip->INODE.i_block[12],indirect);
}
}
else
{
if(mip->INODE.i_block[13] == 0)
{
mip->INODE.i_block[13] = balloc(dev);
get_block(dev,mip->INODE.i_block[13],indirect);
bzero(indirect, BLKSIZE);
put_block(dev,mip->INODE.i_block[13],indirect);//with zero values
}
get_block(dev,mip->INODE.i_block[13],indirect);
lbk -= (12 + 256);
int i = lbk / 256;
int j = lbk % 256;
int second = 0;
if(indirect[i] == 0)
{
indirect[i] = balloc(dev);
get_block(dev,indirect[i],temp);
bzero(temp,BLKSIZE);
put_block(dev,indirect[i],temp);//with zero values
put_block(dev,mip->INODE.i_block[13],indirect);
}
second = indirect[i];
get_block(dev,indirect[i],indirect);
if(indirect[j] == 0)
{
blk = indirect[j] = balloc(dev);
get_block(dev,blk,temp);
bzero(temp,BLKSIZE);
put_block(dev,second,indirect);
}
/*
get_block(dev,mip->INODE.i_block[13],indirect);//get the block of ints from 13
lbk -= (12 + 256);
int i = lbk / 256;
int j = lbk % 256;
get_block(dev,indirect[i],indirect);
blk = indirect[j];//get the block from the int block
*/
}
get_block(dev,blk,wbuf);
bzero(wbuf,BLKSIZE);
char *cp = wbuf + startbyte;
int remain = BLKSIZE - startbyte;
char copying[BLKSIZE];
while(remain > 0)
{
//*cp++ = *cq++;
//nbytes -= 1,
///remain -= 1;
//place->offset++;
int write_length = minimu(nbytes,remain);
strncpy(cp,cq,write_length);
cq += write_length;
cp += write_length;
place->offset += write_length;
nbytes -= write_length;
remain -= write_length;
if(place->offset > mip->INODE.i_size)
{
mip->INODE.i_size += write_length;
}
if(nbytes <= 0)
{
break;
}
}
put_block(dev,blk,wbuf);
//printf("%s this \n\n",wbuf);
}
mip->dirty = 1;
return nbytes;
}
//from <fcntl.h>:
//O_RDONLY = 0
//O_WRONLY = 1
//O_CREAT = 64
//O_WRONLY|O_CREAT = 65
int my_cp(char* src, char* dst) { // src is source, dst is destination
int sfile = getino(src);//get the src file
if(sfile == 0)//Doesn't exist
{
printf("Error, source file does not exist\n");
return;
}
int dfile = getino(dst);//get the dst
if(dfile == 0)//Doesn't exist
{
creats(dst);
}
int fd = my_open(src, 0); // fd = open src for READ
int gd = my_open(dst, 1);
int n;
char mybuf[BLKSIZE]; //went with 1024, unsure if another number would fit better
while( n = my_read(fd, mybuf, BLKSIZE)){
my_write(gd, mybuf, n);
}
my_close(fd);
my_close(gd);
return 0;
}