-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_fs_04.c
More file actions
77 lines (63 loc) · 1.65 KB
/
basic_fs_04.c
File metadata and controls
77 lines (63 loc) · 1.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
https://powcoder.com
代写代考加微信 powcoder
Assignment Project Exam Help
Add WeChat powcoder
#include "myfs.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
int ret = 0;
char disk_name[] = "fs2";
char file_name[] = "file2";
// Mount filesystem from basic_fs_02.c
ret = mount_fs(disk_name);
if(ret != 0) {
printf("ERROR: mount_fs failed\n");
}
// open file from basic_fs_02.c
int fildes_file1 = fs_open(file_name);
if(fildes_file1 < 0) {
printf("ERROR: fs_open failed\n");
}
// create another file (there should be one from basic_fs_02.c)
char file2[] = "file3";
ret = fs_create(file2);
if(ret != 0) {
printf("ERROR: fs_create failed\n");
}
int fildes_file2 = fs_open(file2);
if(fildes_file2 < 0) {
printf("ERROR: fs_open failed\n");
}
// transfer contents of file2 to file3
int len = fs_get_filesize(fildes_file1);
char *buffer = (char*)malloc(len*sizeof(char));
ret = fs_read(fildes_file1,buffer,len);
if(ret != len) {
printf("ERROR: fs_read failed to read correct number of bytes\n");
}
ret = fs_write(fildes_file2,buffer,len);
if(ret != len) {
printf("ERROR: fs_write failed to write correct number of bytes\n");
}
// truncate file1
ret = fs_truncate(fildes_file1,0);
if(ret != 0) {
printf("ERROR: fs_truncate failed\n");
}
// make sure file sizes are correct
int len1 = fs_get_filesize(fildes_file1);
int len2 = fs_get_filesize(fildes_file2);
if(len1 != 0) {
printf("ERROR: file1 has incorrect size\n");
}
if(len2 != len) {
printf("ERROR: file2 has incorrect size\n");
}
ret = fs_close(fildes_file1);
ret = fs_close(fildes_file2);
ret = umount_fs(disk_name);
// done!
return 0;
}