-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.c
More file actions
69 lines (56 loc) · 1.51 KB
/
file.c
File metadata and controls
69 lines (56 loc) · 1.51 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
#include "file.h"
#include <stdlib.h>
#include <string.h>
#define BLOCK_SIZE 4096 //(32*1024*1024)
#define ROUND_BLOCK(x) (((x/BLOCK_SIZE)+1)*BLOCK_SIZE)
File* fileCreate() {
File* newFile = malloc(sizeof(File));
if(!newFile) return NULL;
newFile->contents = NULL;
newFile->size = 0;
return newFile;
}
void fileDestroy(File* file) {
if (!file) return;
fileResize(file, 0);
free(file);
}
int fileRead(File* file, void* buffer, size_t size, off_t offset) {
int readLength;
if (!file) return 0;
if (!file->contents) return 0;
if (offset < file->size) {
readLength = size;
if (readLength + offset > file->size)
readLength = file->size - offset;
memcpy(buffer, file->contents + offset, readLength);
return readLength;
} else {
return 0;
}
}
int fileWrite(File* file, const void* buffer, size_t size, off_t offset) {
if (!file) return 0;
if (offset + size > file->size)
fileResize(file, offset+size);
memcpy(file->contents + offset, buffer, size);
return size;
}
int fileResize(File* file, size_t size) {
if (!file) return 0;
if(file->size > size)
memset((file->contents)+size, 0, file->size - size);
if (size > 0 && (ROUND_BLOCK(size) != ROUND_BLOCK(file->size) || file->size == 0)) {
//printf("realloc: %d\n", ROUND_BLOCK(size));
file->contents = realloc(file->contents, ROUND_BLOCK(size));
} else if (size == 0 && file->size > 0) {
free(file->contents);
file->contents = NULL;
}
file->size = size;
return 1;
}
size_t fileGetSize(File* file) {
if (!file) return 0;
return file->size;
}