-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathb_writeHelpers.c
More file actions
98 lines (76 loc) · 2.76 KB
/
Copy pathb_writeHelpers.c
File metadata and controls
98 lines (76 loc) · 2.76 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
/**************************************************************
* Class:: CSC-415-02 Spring 2024
* Name:: Tushin Kulshreshtha, Hayden Coke, Eric Khuong,
* Thanh Duong
* Student IDs:: 921741974
* GitHub-Name:: crowcode17
* Group-Name:: Something Simple
* Project:: Basic File System
*
* File:: b_writeHelpers.c
*
* Description:: The file contains functions to assist in writing files.
*
*
**************************************************************/
#include "b_io.h"
#include "fileSystem.h"
int writePreliminaryBufferCopy(b_fcb * fcb, int byteCount, char * buffer, int offset){
// read the block, add to it, and write it back
// read the current block
LBAread(fcb->buffer, 1, getBlockLocation(fcb, fcb->currentBlock));
// copy from caller's buffer to our buffer, using offset as buffer index
int bytesRemaining = (B_CHUNK_SIZE - offset);
int copyCount;
// int copyCount = (byteCount > bytesRemaining) ? bytesRemaining : byteCount;
if(byteCount > bytesRemaining){
copyCount = bytesRemaining;
}else{
copyCount = byteCount;
}
// if the user wants to write MORE than the space left in the buffer,
// ONLY copy to fill the buffer
// if the user wants to write LESS than or equal to the amount of space left,
// only copy their amount
memcpy(fcb->buffer + fcb->bufferIndex, buffer, copyCount);
// write buffer to file
LBAwrite(fcb->buffer, 1, getBlockLocation(fcb, fcb->currentBlock));
// update buffer index
fcb->bufferIndex = offset + copyCount;
if(fcb->bufferIndex == B_CHUNK_SIZE) {
fcb->bufferIndex = 0;
fcb->currentBlock += 1;
}
return copyCount;
}
// writes from caller buffer directly to file
// returns bytes read
// buffer parameter is the caller buffer
int writeDirectFileCopy(b_fcb * fcb, int byteCount, char * buffer, int callIndex){
if(byteCount == 0) return 0;
int blocks = bytesToBlocks(byteCount);
if(byteCount % BlockSize != 0) {
blocks = blocks - 1;
}
LBAwrite(buffer + callIndex, blocks, getBlockLocation(fcb, fcb->currentBlock));
fcb->currentBlock += blocks;
int bytesWritten = B_CHUNK_SIZE * blocks;
return bytesWritten;
}
// write from file to file buffer, then copies bytes to caller buffer
// returns bytes read
int writeSecondaryBufferCopy(b_fcb * fcb, int byteCount, char * buffer, int callIndex){
if(byteCount == 0) return 0;
// Copy from the caller buffer to the buffer
memcpy(fcb->buffer + fcb->bufferIndex, buffer + callIndex, byteCount);
// Write the number of bytes from the buffer to the file system
LBAwrite(fcb->buffer, 1, getBlockLocation(fcb, fcb->currentBlock));
// Update the buffer index
fcb->bufferIndex += byteCount;
// printf("(BWR - sec) buffer index: %d", fcb->bufferIndex);
if(fcb->bufferIndex == B_CHUNK_SIZE) {
fcb->bufferIndex = 0;
fcb->currentBlock += 1;
}
return byteCount;
}