file page test 1

This commit is contained in:
JDobeshow 2022-08-29 17:27:24 +02:00
parent fe4260e8f0
commit 4e3fb0cb65
17 changed files with 197 additions and 27 deletions

View File

@ -13,11 +13,11 @@ fp_File fp_fileNew(char * fileName, unsigned long pageSize){ //there shouldn't b
return NULL;
}
struct fp_File_impl * returnable = sus_heapAlloc(sizeof(struct fp_File_impl));
fp_File returnable = sus_heapAlloc(sizeof(struct fp_File_impl));
returnable->pageSize = pageSize;
returnable->file = currentFile;
//The new File will be initialized with one memory Unit (Byte), so this code resizes it to the pageSize
//The new File will be initialized with one memroy Unit (Byte), so this code resizes it to the pageSize
if(sus_fileResize(returnable->file, pageSize - 1) == NULL){
sus_heapFree(returnable);
return NULL;
@ -32,11 +32,11 @@ fp_File fp_fileTemp(char * fileName, unsigned long pageSize){
return NULL;
}
struct fp_File_impl * returnable = sus_heapAlloc(sizeof(struct fp_File_impl));
fp_File returnable = sus_heapAlloc(sizeof(struct fp_File_impl));
returnable->pageSize = pageSize;
returnable->file = currentFile;
//The new File will be initialized with one memory Unit (Byte), so this code resizes it to the pageSize
//The new File will be initialized with one memroy Unit (Byte), so this code resizes it to the pageSize
if(sus_fileResize(returnable->file, pageSize - 1) == NULL){
sus_heapFree(returnable);
return NULL;
@ -44,13 +44,14 @@ fp_File fp_fileTemp(char * fileName, unsigned long pageSize){
return returnable;
}
fp_File fp_fileOpen(char * fileName){
sus_File currentFile = sus_fileOpenInMem(fileName, SUS_FILE_NOTHING);
if(currentFile == NULL){
return NULL;
}
struct fp_File_impl * returnable = sus_heapAlloc(sizeof(struct fp_File_impl));
fp_File returnable = sus_heapAlloc(sizeof(struct fp_File_impl));
//returnable->pageSize = pageSize;
returnable->file = currentFile;
@ -66,14 +67,29 @@ void fp_fileInit(fp_File file, unsigned long pageSize){
//negative values indicate the removable of the latest pages; so this library doesn't need more functions
void * fp_fileAppendPages(fp_File file, long numberOfPages){
sus_fileResize(file->file, file->pageSize * numberOfPages);
void *newMem = sus_fileResize(file->file, file->pageSize * numberOfPages);
if(newMem == NULL) {
sus_fileClose(file->file);
file->file = NULL;
file->pageSize = 0;
return NULL;
}
return newMem;
}
void * fp_fileToMemroy(fp_File file){
return sus_fileFileToMemroy(file->file);
}
void fp_fileFlush(fp_File file) {
sus_fileFlush(file->file);
}
void fp_fileClose(fp_File file){
if(file->file != NULL) {
sus_fileClose(file->file);
}
sus_heapFree(file);
}

View File

@ -4,6 +4,7 @@ typedef struct fp_File_impl * fp_File;
fp_File fp_fileNew(char * fileName, unsigned long pageSize); //there shouldn't be a page, longer than 4gb
fp_File fp_fileTemp(char * fileName, unsigned long pageSize);
fp_File fp_fileOpen(char * fileName);
void fp_fileInit(fp_File file, unsigned long pageSize);
//Since someone else has to read the header, this function sets the read page size after fp_fileOpen has been called
void fp_fileSetPageSize(fp_File file, unsigned long pageSize);
@ -13,4 +14,6 @@ void * fp_fileAppendPages(fp_File file, long numberOfPages);
void * fp_fileToMemroy(fp_File file);
void fp_fileFlush(fp_File file);
void fp_fileClose(fp_File file);

View File

@ -20,7 +20,10 @@ sus_File sus_fileOpenInMem(char * fileName, unsigned long parameter) {
int fd = open(fileName, flags, S_IRWXU);
if(fd == -1) return NULL;
flock(fd, LOCK_EX);
if(flock(fd, LOCK_EX | LOCK_NB)) {
close(fd);
return NULL;
}
struct stat info;
if(stat(fileName, &info)) {
@ -59,11 +62,15 @@ sus_Bool sus_fileExists(char * fileName) {
}
void sus_fileClose(sus_File file) {
if(file->memroy != NULL) {
msync(file->memroy, file->length, MS_SYNC);
munmap(file->memroy, file->length);
close(file->fd);
}
sus_heapFree(file);
}
void sus_fileFlush(sus_File file) {
msync(file->memroy, file->length, MS_SYNC);
}

View File

@ -0,0 +1 @@
run

View File

@ -0,0 +1,5 @@
#!/bin/sh
if [ ! -d run ]; then
mkdir run
fi
gcc -g filePageTest1.c "../../../System Abstraction/linux/cdb_file.c" "../../../System Abstraction/linux/cdb_memroy.c" "../../../File-Page Abstraction/cdb_file-page.c" -o run/filePageTest1

View File

@ -0,0 +1,41 @@
#include "../../../File-Page Abstraction/cdb_file-page.h"
#include <stdio.h>
int main() {
fp_File file = fp_fileNew("created", 512);
if(file == NULL) {
printf("file is null\n");
return 1;
}
char * memory = fp_fileToMemroy(file);
memory[0] = 'a';
memory[511] = 'b';
printf("Append pages\n");
memory = fp_fileAppendPages(file, 1);
if(memory == NULL) {
printf("memory is null\n");
return 1;
}
memory[1023] = 'z';
printf("Close\n");
fp_fileClose(file);
printf("Open\n");
file = fp_fileOpen("created");
if(file == NULL) {
printf("second file is null\n");
return 1;
}
printf("Init\n");
fp_fileInit(file, 512);
memory = fp_fileToMemroy(file);
printf("%c%c%c\n", memory[0],memory[511],memory[1023]);
fp_fileClose(file);
return 0;
}

View File

@ -0,0 +1,20 @@
#!/bin/sh
./compile.sh
cd run
# Test 1
echo "-- Test 1"
echo "Expected output: abz"
rm created
./filePageTest1
rm created
# Test 2
echo "-- Test 2"
echo "Expected output: file is null"
touch created
./filePageTest1
rm created
cd ..

View File

@ -0,0 +1,19 @@
#!/bin/sh
./compile.sh
cd run
# Test 1
echo "-- Test 1"
echo "Expected output: file is null"
rm testfile.txt
./systemAbstractionTest1
# Test 2
echo "-- Test 2"
echo "Expected output: abc"
echo "Expected file contents: Obcde"
echo -n "abc" > testfile.txt
./systemAbstractionTest1
cd ..

View File

@ -4,7 +4,7 @@
void main(){
sus_File file = sus_fileOpenInMem("testfile.txt", SUS_FILE_NOTHING);
if(file == NULL){
printf("file is null");
printf("file is null\n");
return;
}
@ -13,7 +13,7 @@ void main(){
printf("memroy was null\n");
return;
}else{
printf("%c%c%c%c", memroy[0], memroy[1], memroy[2], memroy[3]);
printf("Read chars: %c%c%c\n", memroy[0], memroy[1], memroy[2]);
}
sus_fileResize(file, 2);
@ -23,11 +23,10 @@ void main(){
memroy[2] = 'c';
memroy[3] = 'd';
memroy[4] = 'e';
sus_fileFlush(file);
sus_fileClose(file);
file = sus_fileOpenInMem("testfile.txt", SUS_FILE_NOTHING);
memroy = sus_fileFileToMemroy(file);
memroy[0] = 'O';
sus_fileFlush(file);
sus_fileClose(file);
}

View File

@ -4,7 +4,7 @@
void main(){
sus_File file = sus_fileOpenInMem("testfile.txt", SUS_FILE_NEW);
if(file == NULL){
printf("file is null");
printf("file is null\n");
return;
}
@ -13,10 +13,10 @@ void main(){
printf("memroy was null\n");
return;
}else{
printf("%c%c%c%c", memroy[0], memroy[1], memroy[2], memroy[3]);
printf("%c\n", memroy[0] == 0 ? 'Y' : 'N');
}
sus_fileResize(file, 2);
sus_fileResize(file, 4);
memroy = sus_fileFileToMemroy(file);
memroy[0] = 'a';
memroy[1] = 'b';

View File

@ -0,0 +1,17 @@
#!/bin/sh
./compile.sh
cd run
# Test 1
echo "-- Test 1"
echo "Expected output: Y"
echo "Expected file contents: Obcde"
rm testfile.txt
./openNewFiletest
echo "-- Test 2"
echo "Expected output: file is null"
./openNewFiletest
cd ..

View File

@ -4,7 +4,7 @@
void main(){
sus_File file = sus_fileOpenInMem("testfile.txt", SUS_FILE_TEMP);
if(file == NULL){
printf("file is null");
printf("file is null\n");
return;
}
@ -13,22 +13,23 @@ void main(){
printf("memroy was null\n");
return;
}else{
printf("%c%c%c%c", memroy[0], memroy[1], memroy[2], memroy[3]);
printf("%c\n", memroy[0] == 0 ? 'Y' : 'N');
}
sus_fileResize(file, 2);
sus_fileResize(file, 4);
memroy = sus_fileFileToMemroy(file);
memroy[0] = 'a';
memroy[1] = 'b';
memroy[2] = 'c';
memroy[3] = 'd';
memroy[4] = 'e';
Sleep(10000);
sus_fileFlush(file);
sus_fileClose(file);
file = sus_fileOpenInMem("testfile.txt", SUS_FILE_NOTHING);
memroy = sus_fileFileToMemroy(file); //crash expected here
memroy[0] = 'O';
sus_fileFlush(file);
if(file == NULL) {
printf("Second file is null\n");
}else {
printf("Second file is NOT null\n");
sus_fileClose(file);
}
}

View File

@ -0,0 +1,17 @@
#!/bin/sh
./compile.sh
cd run
# Test 1
echo "-- Test 1"
echo "Expected output: file is null"
touch testfile.txt
./openTempFiletest
rm testfile.txt
echo "-- Test 2"
echo "Expected output: Y, Second file is null"
./openTempFiletest
cd ..

1
tests/System Abstraction/4/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
run

View File

@ -0,0 +1,5 @@
#!/bin/sh
if [ ! -d run ]; then
mkdir run
fi
gcc -g renameFileTest.c "../../../System Abstraction/linux/cdb_file.c" "../../../System Abstraction/linux/cdb_memroy.c" -o run/renameFileTest

View File

@ -0,0 +1,6 @@
#include "../../../System Abstraction/cdb_sustem.h"
#include "stdio.h"
int main() {
sus_fileRename("testfile.txt", "renamedtestfile.txt");
}

View File

@ -0,0 +1,12 @@
#!/bin/sh
./compile.sh
cd run
# Test 1
echo "-- Test 1"
echo "Expected state: testfile.txt was renamed to renamedtestfile.txt"
touch testfile.txt
./renameFiletest
cd ..