-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_fs_01.c
More file actions
87 lines (74 loc) · 1.65 KB
/
basic_fs_01.c
File metadata and controls
87 lines (74 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
78
79
80
81
82
83
84
85
86
87
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[] = "fs1";
char file_name[] = "file1";
// 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
char data[] = "This is my data";
int len = strlen(data);
ret = fs_write(fildes,data,len);
if(ret != len) {
printf("ERROR: fs_write failed to write correct number of bytes\n");
}
// close file
ret = fs_close(fildes);
if(ret != 0) {
printf("ERROR: fs_close failed\n");
}
// re-open file
fildes = fs_open(file_name);
if(fildes < 0) {
printf("ERROR: fs_open failed\n");
}
// read what we just wrote into buffer
char buffer[4096];
ret = fs_read(fildes,buffer,len);
if(ret != len) {
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,buffer,len);
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;
}