Repository navigation
Expand file tree
/
Copy pathBufferIO.c
More file actions
125 lines (105 loc) · 3.58 KB
/
Copy pathBufferIO.c
File metadata and controls
125 lines (105 loc) · 3.58 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**************************************************************
* Class:: CSC-415-02 Spring 2024
* Name::Chia-Tung Liu
* Student IDs::923408847
* GitHub-Name::Mob1001
* Group-Name::Alt+F4
* Project:: Basic File System
*
* File:: BufferIO.c
*
* Description:: Basic File System - Functions for bio files
*
**************************************************************/
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include "BufferIO.h"
#include "Directory.h"
#include "FreeSpaceManager.h"
#include "stdlib.h"
// Function to get file information given a file name
fileInfo* getFileInfo(char* fileName) {
// Get the absolute path of the file
char* concatinate = getAbsolutePath(fileName);
if (!concatinate) return NULL; // Return NULL if absolute path cannot be obtained
// Allocate memory for file information
fileInfo* theInfo = malloc(sizeof(fileInfo));
if (!theInfo) {
free(concatinate);
concatinate=NULL;
return NULL; // Return NULL if memory allocation fails
}
// Initialize Parsed Path of Entry (PPOE) structure
PPOE entryPPOE = {0};
// Parse the absolute path to obtain directory information
if (parsePath(concatinate, &entryPPOE) == -1) {
free(theInfo);
theInfo=NULL;
free(concatinate);
concatinate=NULL;
return NULL; // Return NULL if parsing fails
}
// Set file information fields
theInfo->index = entryPPOE.index;
theInfo->currDIR = entryPPOE.parentDIR;
// Free memory used for absolute path concatenation
free(concatinate);
concatinate=NULL;
return theInfo; // Return file information
}
// Function to create file information for a new file
fileInfo* newFileInfo(char* filename) {
// Attempt to create the file
if (makeFile(filename) != 0) {
return NULL; // Return NULL if file creation fails
}
// Return file information for the newly created file
return getFileInfo(filename);
}
// Function to delete file information and associated memory
int deleteFileInfo(fileInfo* fileinfo) {
if (!fileinfo) return -1; // Guard against NULL input
// Free memory used for directory entries and file information structure
free(fileinfo->currDIR);
free(fileinfo);
return 0; // Return success
}
// Function to truncate a file's content
int bufferTruncate(DIR_Entry* dir, int fileIndex) {
if (!dir) return -1; // Guard against NULL input
// Free all associated blocks
deAllocFAT(dir[fileIndex].location_of_first);
// Allocate a new block and reset the file size
dir[fileIndex].location_of_first = allocFAT(1);
dir[fileIndex].size = 0;
// Update timestamps
time_t timestamp;
time(×tamp);
dir[fileIndex].date_modified = timestamp;
dir[fileIndex].date_accessed = timestamp;
// Write updates to disk
writeToLBA(dir, dir->size, &dir->location_of_first);
return 0; // Return success
}
// Function to validate file opening flags for conflicts
int validFlags(int flags) {
// Define conflicting flag combinations
int conflicts[] = {
O_CREAT | O_RDONLY,
O_TRUNC | O_RDONLY,
O_APPEND | O_RDONLY,
O_RDWR | O_RDONLY,
O_RDWR | O_WRONLY,
O_WRONLY | O_RDONLY
};
// Check for conflicting permissions
for (int i = 0; i < sizeof(conflicts) / sizeof(conflicts[0]); i++) {
if ((flags & conflicts[i]) == conflicts[i]) {
printf("ERROR: conflicting permissions\n");
return -1; // Return -1 if conflicting permissions are found
}
}
return 0; // Return success
}