-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_fs_02.c
More file actions
88 lines (75 loc) · 1.7 KB
/
basic_fs_02.c
File metadata and controls
88 lines (75 loc) · 1.7 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
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 fildes = 0;
int ret = 0;
char disk_name[] = "fs2";
char file_name[] = "file2";
// Make filesystem.
ret = make_fs(disk_name);
if(ret != 0) {
printf("ERROR: make_fs failed\n");
}
// Mount filesystem.
ret = mount_fs(disk_name);
if(ret != 0) {
printf("ERROR: mount_fs failed\n");
}
// create file
ret = fs_create(file_name);
if(ret != 0) {
printf("ERROR: fs_create failed\n");
}
// open file
fildes = fs_open(file_name);
if(fildes < 0) {
printf("ERROR: fs_open failed\n");
}
// write to file more than a block worth of data
char data[5000];
memset(data,0,5000);
data[4094] = 'm';
data[4095] = 'o';
data[4096] = 'o';
data[4097] = 's';
data[4098] = 'e';
ret = fs_write(fildes,data,5000);
if(ret != 5000) {
printf("ERROR: fs_write failed to write correct number of bytes\n");
}
// seek to position of 'moose'
ret = fs_lseek(fildes,4094);
if(ret != 0) {
printf("ERROR: fs_lseek failed\n");
}
// read bytes where 'moose' should be
char buffer[20];
ret = fs_read(fildes,buffer,5);
if(ret != 5) {
printf("%d\n",ret);
printf("ERROR: fs_read failed to read correct number of bytes\n");
}
// make sure what we read matches with what we wrote
ret = strncmp(data+4094,buffer,5);
if(ret != 0) {
printf("ERROR: data read does not match data written!\n");
}
// close file
ret = fs_close(fildes);
if(ret != 0) {
printf("ERROR: fs_close failed\n");
}
// unmount file system
ret = umount_fs(disk_name);
if(ret < 0) {
printf("ERROR: umount_fs failed\n");
}
// done!
return 0;
}