-
Notifications
You must be signed in to change notification settings - Fork 0
/
retrieve.c
105 lines (81 loc) · 2.84 KB
/
retrieve.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
// retrieve.c
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "fs.h"
#include "libmemdrv.h"
// "reading from a file and storing on a device"
void read_inode(Inode* inode);
// read_inode method
void read_inode(Inode* inode) {
char buf[BLOCK_SIZE];
read_block(0, buf);
*inode = *(Inode*)buf;
}
void read_file(Inode* inode, FILE* file);
// main method
int main(int argc, char* argv[]){
// need to check for correct number of arguments if not print error
if (argc > 2){
fprintf(stderr, "Usage: %s [FILE]\n", argv[0]);
return EXIT_FAILURE;
}
FILE* file = stdout;
if (argc == 2){
file = fopen(argv[1], "r");
if (file != NULL){
// the file exists so we need to ask for overwrite confrimation.
fclose(file);
char confirm;
printf("The file alredy %s exists. Overwrite? (y/n): ", argv[1]);
scanf("%c", &confirm);
if (confirm != 'Y' && confirm != 'y'){
printf("Aborted.\n");
return EXIT_SUCCESS;
}
}
file = fopen(argv[1], "wb");
if (file == NULL){
perror("Error opening the file");
return EXIT_FAILURE;
}
}
open_device();
//declerations
Inode inode;
read_inode(&inode);
// need to loop through and read from memdrv to the file we want to write to
// calling the read file method which will read the file
read_file(&inode, file);
if (file != stdout){
fclose(file);
}
close_device();
return EXIT_SUCCESS;
}
// read_file method
void read_file(Inode* inode, FILE* file){
char buf[BLOCK_SIZE];
int remainder = inode->size;
// reading and writing to direct blocks
for(int i = 0; i < NDIRECT && remainder > 0; i++){
read_block(inode-> addrs[i], buf);
// for checking if we are at the last block (setting write size to be the remanider)
int8_t size_written = remainder < BLOCK_SIZE ? remainder : BLOCK_SIZE;
fwrite(buf, 1, size_written, file);
remainder -= BLOCK_SIZE;
}
//if there is no more data we should use read from.(when we reach the last index of the node 13th)
if(remainder> 0 && inode->addrs[NDIRECT] !=0){
int8_t indirect[BLOCK_SIZE/sizeof(int8_t)];
read_block(inode->addrs[NDIRECT], (char*)indirect);
// will loop through and write the reaminder of the file
for(int i = 0; i < (int8_t)(BLOCK_SIZE/sizeof(int8_t))&& remainder>0; i++){
read_block(indirect[i], buf);
int8_t size_written = remainder < BLOCK_SIZE ? remainder : BLOCK_SIZE;
fwrite (buf, 1, size_written, file);
remainder-= BLOCK_SIZE;
}
}
}