-
Notifications
You must be signed in to change notification settings - Fork 0
/
vfs_save.c
52 lines (43 loc) · 1.19 KB
/
vfs_save.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "vfs.h"
int save_vfs(char *name){
//Pre-condition: VFS is loaded
if(vfs.vfs_status==VFS_CLOSE){
return -1;
}
FILE *fp;
int offset,vfs_offset,size,count=0;
char ch;
//Open file to be saved
if( (fp=fopen(name,"rb+"))==NULL){
printf("Unable to open the file %s\n",name);
exit(0);
}
else{
//Calculate offset where file needs to be stored
fseek(fp,0,SEEK_END);
size=ftell(fp);
rewind(fp);
fseek(vfs.vfs_fp,0,SEEK_END);
vfs_offset=ftell(vfs.vfs_fp);
//Initialize vfs_file_info
vfs.header.vfs_files[vfs.header.vfs_info.num_files].offset=vfs_offset; //offset of file in VFS
strcpy(vfs.header.vfs_files[vfs.header.vfs_info.num_files].fname,name);
vfs.header.vfs_files[vfs.header.vfs_info.num_files].size=size;
vfs.header.vfs_info.num_files+=1;
//Write external file contents at offset location
while(count!=size){
fread(&ch,sizeof(char),1,fp);
//printf("%c",ch);
fwrite(&ch,sizeof(char),1,vfs.vfs_fp);
++count;
}
rewind(vfs.vfs_fp);
fclose(fp);
//sort the vfs_files
qsort(vfs.header.vfs_files,vfs.header.vfs_info.num_files, sizeof(struct Vfs_File_Info), cmp_vfsfile);
}
return 0;
}