-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy_os.c
216 lines (198 loc) · 7.39 KB
/
copy_os.c
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#include <sys/stat.h>
#include "oscafs.h"
extern str_t dentry[NUM_DENTRY];
extern int fat[NB_BLOCKS];
int file_size(char *filename)
{
struct stat statbuf;
stat(filename, &statbuf);
int size = statbuf.st_size;
return size;
}
void os_copy_host(FILE *fp)
{
FILE *file_host;
int fd; // dentry row number
int fb; //first block
int nb; // next block number
int data_length; // Count the number of characters
char file_name_copy[NAME_LEN];
char *pt;
bool same_name = false;
int count_alloc;
int count_free;
if ((return_first_entry()) >= 0)
{
printf(YELLOW "please input a host file name: " NONE);
while (s_gets(file_name_copy, NAME_LEN) != NULL && file_name_copy[0] != '\0') // check is it have same name file
{
if ((file_host = fopen(file_name_copy, "r+")) == NULL)
{
fprintf(stderr, "Cannot open or find the \"%s\" file, please try other file\n", file_name_copy);
//puts("please try other file");
continue;
}
for (int i = 0; i < NUM_DENTRY; i++)
{
if (strcmp(file_name_copy, return_entry(i)) == 0)
{
same_name = true;
break;
}
}
if (same_name)
{
printf("the file \"%s\" have been exist, cannot copy same name file, try again\n", file_name_copy);
//puts("try again");
same_name = false;
continue;
}
else
{
int num_block;
int size = file_size(file_name_copy);
//printf("for test size: %d\n", size); //for test
if (size % BLOCK_SIZE == 0)
num_block = size / BLOCK_SIZE;
else
num_block = (size / BLOCK_SIZE) + 1;
//printf("num_block:%d\n", num_block); //for test
//printf("spare block:%d\n", count_free_block()); // for test
if (num_block <= count_free_block())
{
char spdata[num_block * BLOCK_SIZE];
pt = spdata;
fread(spdata, 1, size, file_host);
spdata[size] = '\0';
//printf("spdata:%s\n", spdata); // for test
//printf("spdata strlen is %d\n", strlen(spdata)); //for test
fd = write_first_free(file_name_copy);
if (fd < 0)
{
printf("OSCAFS cannot contain more than %d file\n", NUM_DENTRY);
break;
}
fb = write_firstblock_num(fd);
count_alloc = 1;
//printf("%s is No.%d in dentry and ", file_name_copy, fd); //for test
//printf("begin in %d block\n", fb); //for test
//printf("spdata: %s\n", spdata); //fot test
//printf("spdata strlen is %d\n", strlen(spdata)); //for test
//printf("this file use %d block\n", num_block); //for test
char databuf[num_block][BLOCK_SIZE];
for (int count = 0; count < num_block; count++)
{
strncpy(databuf[count], pt + (sizeof(spdata) - (num_block - count) * BLOCK_SIZE), BLOCK_SIZE);
databuf[count][BLOCK_SIZE] = '\0';
//printf("databuf%d: %-8s | in block: %d\n", count + 1, databuf[count], fb); //for check data if correct
write_block(fb, databuf[count], fp);
//printf("%d ", fb); // for test
if (count_alloc < num_block)
{
nb = add_next_block(fb);
fb = nb;
count_alloc++;
}
}
fclose(file_host);
printf(YELLOW "Enter file name to copy new one to OSCAFS(empty to quit): " NONE);
}
else
{
puts("OSCAFS not enough spare sapce to save this file");
break;
//printf("need %d block\n", num_block); //for test
//printf("spare blocks:%d\n", count_free_block()); //for test
}
}
}
}
else
printf("OSCAFS cannot contain more than %d file\n", NUM_DENTRY);
}
void os_copy_OSCAFS(FILE *fp)
{
FILE *file_host;
int i; //for check empty dentry
int j; //for select file
int fb; // first block in file
bool have_file = false;
char file_name[NAME_LEN];
char data[BLOCK_SIZE];
int temp; //for change the value of fat table
for (i = 0; i < NUM_DENTRY; i++)
{
if (dentry[i].str[0] != '\0')
{
have_file = true;
}
}
if (have_file)
{
//printf("\n");
os_list();
printf(YELLOW "plese choice a file: " NONE);
bool true_name = false;
while (s_gets(file_name, NAME_LEN) != NULL && file_name[0] != '\0')
{
for (j = 0; j < NUM_DENTRY; j++)
{
if (strcmp(file_name, dentry[j].str) == 0)
{
true_name = true;
break;
}
}
if (true_name)
{
if ((file_host = fopen(file_name, "w+")) == NULL)
{
fprintf(stderr, "Cannot create the \"%s\" file", file_name);
printf("please try other file or make sure the authority(empty to quit): ");
continue;
}
else
{
rewind(file_host);
//printf("\n");
fb = dentry[j].num;
// printf("%d\n", fb); //for test
read_block(fb, data, fp);
data[BLOCK_SIZE] = '\0';
//printf("%s", data); // for test
fwrite(data, 1, BLOCK_SIZE, file_host);
int count_seek = 1;
while (fat[fb] != EOF_BLK)
{
char bufdata[BLOCK_SIZE];
temp = next_block(fb);
fb = temp;
read_block(fb, bufdata, fp);
bufdata[BLOCK_SIZE] = '\0';
//printf("%s", bufdata); for test
fseek(file_host, count_seek * BLOCK_SIZE, SEEK_SET);
fwrite(bufdata, 1, BLOCK_SIZE, file_host);
count_seek++;
}
//printf("done\n");
fclose(file_host);
printf(YELLOW "Enter file name to copy other one (empty to quit): " NONE);
}
}
else
{
printf("%s can not find in OSCAFS\n", file_name);
printf(YELLOW "please try again (empty to quit): " NONE);
continue;
}
true_name = false;
}
}
else
puts("No any file");
}