From 4e3fb0cb65911035261e4119819359f4abda2faf Mon Sep 17 00:00:00 2001 From: JDobeshow Date: Mon, 29 Aug 2022 17:27:24 +0200 Subject: [PATCH] file page test 1 --- File-Page Abstraction/cdb_file-page.c | 30 ++++++++++---- File-Page Abstraction/cdb_file-page.h | 3 ++ System Abstraction/linux/cdb_file.c | 15 +++++-- tests/File-Page Abstraction/1/.gitignore | 1 + tests/File-Page Abstraction/1/compile.sh | 5 +++ tests/File-Page Abstraction/1/filePageTest1.c | 41 +++++++++++++++++++ tests/File-Page Abstraction/1/run.sh | 20 +++++++++ tests/System Abstraction/1/run.sh | 19 +++++++++ .../1/systemAbstractionTest1.c | 7 ++-- tests/System Abstraction/2/openNewFiletest.c | 6 +-- tests/System Abstraction/2/run.sh | 17 ++++++++ tests/System Abstraction/3/openTempFiletest.c | 19 +++++---- tests/System Abstraction/3/run.sh | 17 ++++++++ tests/System Abstraction/4/.gitignore | 1 + tests/System Abstraction/4/compile.sh | 5 +++ tests/System Abstraction/4/renameFiletest.c | 6 +++ tests/System Abstraction/4/run.sh | 12 ++++++ 17 files changed, 197 insertions(+), 27 deletions(-) create mode 100644 tests/File-Page Abstraction/1/.gitignore create mode 100755 tests/File-Page Abstraction/1/compile.sh create mode 100644 tests/File-Page Abstraction/1/filePageTest1.c create mode 100755 tests/File-Page Abstraction/1/run.sh create mode 100755 tests/System Abstraction/1/run.sh create mode 100755 tests/System Abstraction/2/run.sh create mode 100755 tests/System Abstraction/3/run.sh create mode 100644 tests/System Abstraction/4/.gitignore create mode 100644 tests/System Abstraction/4/compile.sh create mode 100644 tests/System Abstraction/4/renameFiletest.c create mode 100644 tests/System Abstraction/4/run.sh diff --git a/File-Page Abstraction/cdb_file-page.c b/File-Page Abstraction/cdb_file-page.c index 6eb3064..b16b6f6 100644 --- a/File-Page Abstraction/cdb_file-page.c +++ b/File-Page Abstraction/cdb_file-page.c @@ -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){ - sus_fileClose(file->file); + if(file->file != NULL) { + sus_fileClose(file->file); + } + sus_heapFree(file); } \ No newline at end of file diff --git a/File-Page Abstraction/cdb_file-page.h b/File-Page Abstraction/cdb_file-page.h index 5ea7352..8afc7aa 100644 --- a/File-Page Abstraction/cdb_file-page.h +++ b/File-Page Abstraction/cdb_file-page.h @@ -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); \ No newline at end of file diff --git a/System Abstraction/linux/cdb_file.c b/System Abstraction/linux/cdb_file.c index 1b8dc87..e7e04bb 100644 --- a/System Abstraction/linux/cdb_file.c +++ b/System Abstraction/linux/cdb_file.c @@ -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,9 +62,13 @@ sus_Bool sus_fileExists(char * fileName) { } void sus_fileClose(sus_File file) { - msync(file->memroy, file->length, MS_SYNC); - munmap(file->memroy, file->length); - close(file->fd); + 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) { diff --git a/tests/File-Page Abstraction/1/.gitignore b/tests/File-Page Abstraction/1/.gitignore new file mode 100644 index 0000000..f5bdd21 --- /dev/null +++ b/tests/File-Page Abstraction/1/.gitignore @@ -0,0 +1 @@ +run diff --git a/tests/File-Page Abstraction/1/compile.sh b/tests/File-Page Abstraction/1/compile.sh new file mode 100755 index 0000000..bd92195 --- /dev/null +++ b/tests/File-Page Abstraction/1/compile.sh @@ -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 \ No newline at end of file diff --git a/tests/File-Page Abstraction/1/filePageTest1.c b/tests/File-Page Abstraction/1/filePageTest1.c new file mode 100644 index 0000000..0ca757b --- /dev/null +++ b/tests/File-Page Abstraction/1/filePageTest1.c @@ -0,0 +1,41 @@ +#include "../../../File-Page Abstraction/cdb_file-page.h" +#include + +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; +} \ No newline at end of file diff --git a/tests/File-Page Abstraction/1/run.sh b/tests/File-Page Abstraction/1/run.sh new file mode 100755 index 0000000..1af433d --- /dev/null +++ b/tests/File-Page Abstraction/1/run.sh @@ -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 .. \ No newline at end of file diff --git a/tests/System Abstraction/1/run.sh b/tests/System Abstraction/1/run.sh new file mode 100755 index 0000000..c7f57f6 --- /dev/null +++ b/tests/System Abstraction/1/run.sh @@ -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 .. \ No newline at end of file diff --git a/tests/System Abstraction/1/systemAbstractionTest1.c b/tests/System Abstraction/1/systemAbstractionTest1.c index 8a2c811..668cd15 100644 --- a/tests/System Abstraction/1/systemAbstractionTest1.c +++ b/tests/System Abstraction/1/systemAbstractionTest1.c @@ -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); } \ No newline at end of file diff --git a/tests/System Abstraction/2/openNewFiletest.c b/tests/System Abstraction/2/openNewFiletest.c index 09a6cad..ceec6ff 100644 --- a/tests/System Abstraction/2/openNewFiletest.c +++ b/tests/System Abstraction/2/openNewFiletest.c @@ -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'; diff --git a/tests/System Abstraction/2/run.sh b/tests/System Abstraction/2/run.sh new file mode 100755 index 0000000..9370ed8 --- /dev/null +++ b/tests/System Abstraction/2/run.sh @@ -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 .. \ No newline at end of file diff --git a/tests/System Abstraction/3/openTempFiletest.c b/tests/System Abstraction/3/openTempFiletest.c index d19aee4..59eff70 100644 --- a/tests/System Abstraction/3/openTempFiletest.c +++ b/tests/System Abstraction/3/openTempFiletest.c @@ -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); - sus_fileClose(file); + if(file == NULL) { + printf("Second file is null\n"); + }else { + printf("Second file is NOT null\n"); + sus_fileClose(file); + } } \ No newline at end of file diff --git a/tests/System Abstraction/3/run.sh b/tests/System Abstraction/3/run.sh new file mode 100755 index 0000000..91960e2 --- /dev/null +++ b/tests/System Abstraction/3/run.sh @@ -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 .. \ No newline at end of file diff --git a/tests/System Abstraction/4/.gitignore b/tests/System Abstraction/4/.gitignore new file mode 100644 index 0000000..f5bdd21 --- /dev/null +++ b/tests/System Abstraction/4/.gitignore @@ -0,0 +1 @@ +run diff --git a/tests/System Abstraction/4/compile.sh b/tests/System Abstraction/4/compile.sh new file mode 100644 index 0000000..6c64d24 --- /dev/null +++ b/tests/System Abstraction/4/compile.sh @@ -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 \ No newline at end of file diff --git a/tests/System Abstraction/4/renameFiletest.c b/tests/System Abstraction/4/renameFiletest.c new file mode 100644 index 0000000..6fcfe24 --- /dev/null +++ b/tests/System Abstraction/4/renameFiletest.c @@ -0,0 +1,6 @@ +#include "../../../System Abstraction/cdb_sustem.h" +#include "stdio.h" + +int main() { + sus_fileRename("testfile.txt", "renamedtestfile.txt"); +} \ No newline at end of file diff --git a/tests/System Abstraction/4/run.sh b/tests/System Abstraction/4/run.sh new file mode 100644 index 0000000..9fcb278 --- /dev/null +++ b/tests/System Abstraction/4/run.sh @@ -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 .. \ No newline at end of file