forked from sameenjalal/Linux-File-System
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_open_close.c
38 lines (32 loc) · 893 Bytes
/
file_open_close.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
// This file contains functions for opening and closing files
#include "api.h"
#include "private_api.h"
int
file_open(char *path)
{
// Check if file exists
int file_num;
int inumber = path_to_inumber(path);
printf("path_to_inumber returned %d\n", inumber);
if ( inumber <= 0 )
return ERR_FILE_EXISTS;
// Check if file or dir
inode *in = get_inode(inumber);
printf("get_inode returned %p\n", in);
if ( in->directory_flag )
return ERR_FILE_NOT_FOUND;
// make file_info and add to open_files arraylist
file_info *new_file = (file_info *) malloc(sizeof(file_info));
strcpy(new_file->filepath, path);
new_file->inumber = inumber;
new_file->cursor = 0;
file_num = arraylist_add(new_file,open_files);
return file_num;
}
void
file_close(int file_number)
{
// Delete file from arraylist "open_files"
if ( !arraylist_delete(file_number, open_files) )
return;
}