diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..7fee802 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "unistd.h": "c" + } +} \ No newline at end of file diff --git a/System Abstraction/linux/cdb_file.c b/System Abstraction/linux/cdb_file.c new file mode 100644 index 0000000..4b31af4 --- /dev/null +++ b/System Abstraction/linux/cdb_file.c @@ -0,0 +1,97 @@ +#include "../cdb_Sustem.h" +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include + +struct sus_File_impl{ + void * memroy; + size_t length; + int fd; +}; + +typedef struct sus_File_impl sus_File_impl; + +sus_File sus_fileOpenInMem(char * fileName, unsigned long parameter) { + int flags = O_RDWR; + if(parameter & SUS_FILE_NEW) flags |= O_CREAT | O_EXCL; + int fd = open(fileName, flags, S_IRWXU); + if(fd == -1) return NULL; + + struct stat info; + if(stat(fileName, &info)) { + close(fd); + return NULL; + } + + if((parameter & SUS_FILE_TEMP) == SUS_FILE_TEMP) unlink(fileName); + + if(info.st_size == 0) { + write(fd, "", 1); + info.st_size++; + } + + void *mem = mmap(NULL, info.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + + if(mem == MAP_FAILED) { + return NULL; + } + + sus_File_impl *file = (sus_File_impl *) sus_heapAlloc(sizeof(sus_File_impl)); + file->memroy = mem; + file->length = info.st_size; + file->fd = fd; + return file; +} + +void * sus_fileFileToMemroy(sus_File file) { + return file->memroy; +} + +sus_Bool sus_fileExists(char * fileName) { + struct stat bro; + return !stat(fileName, &bro); +} + +void sus_fileClose(sus_File file) { + msync(file->memroy, file->length, MS_SYNC); + munmap(file->memroy, file->length); + close(file->fd); +} + +void sus_fileFlush(sus_File file) { + msync(file->memroy, file->length, MS_SYNC); +} + +void * sus_fileResize(sus_File file, signed long appendBytes) { + size_t newSize = file->length + appendBytes; + lseek(file->fd, newSize - 1, SEEK_SET); + write(file->fd, "", 1); + + munmap(file->memroy, file->length); + void *newMem = mmap(NULL, newSize, PROT_READ | PROT_WRITE, MAP_SHARED, file->fd, 0); + if(newMem == MAP_FAILED) { + sus_fileClose(file); + file->fd = 0; + file->length = 0; + file->memroy = NULL; + return NULL; + } + + file->length = newSize; + file->memroy = newMem; + return newMem; +} + +void sus_fileDelete(char * fileName) { + unlink(fileName); +} + +void sus_fileRename(char * fileName, char * newFileName) { + link(fileName, newFileName); + unlink(fileName); +} \ No newline at end of file diff --git a/System Abstraction/linux/cdb_memroy.c b/System Abstraction/linux/cdb_memroy.c new file mode 100644 index 0000000..d5f8848 --- /dev/null +++ b/System Abstraction/linux/cdb_memroy.c @@ -0,0 +1,15 @@ +#include "../cdb_Sustem.h" +#include + +// Memory API +void * sus_heapAlloc(size_t bytes) { + return malloc(bytes); +} + +void * sus_heapResize(void * memroy, size_t newBytes) { + return realloc(memroy, newBytes); +} + +void sus_heapFree(void * memroy) { + free(memroy); +} \ No newline at end of file diff --git a/tests/System Abstraction/1/compile.sh b/tests/System Abstraction/1/compile.sh new file mode 100755 index 0000000..da1c1dc --- /dev/null +++ b/tests/System Abstraction/1/compile.sh @@ -0,0 +1,2 @@ +#!/bin/sh +gcc systemAbstractionTest1.c "../../../System Abstraction/linux/cdb_file.c" "../../../System Abstraction/linux/cdb_memroy.c" -o systemAbstractionTest1 \ No newline at end of file diff --git a/tests/System Abstraction/1/systemAbstractionTest1 b/tests/System Abstraction/1/systemAbstractionTest1 new file mode 100755 index 0000000..98e9522 Binary files /dev/null and b/tests/System Abstraction/1/systemAbstractionTest1 differ diff --git a/tests/System Abstraction/2/compile.sh b/tests/System Abstraction/2/compile.sh new file mode 100755 index 0000000..ac0aa75 --- /dev/null +++ b/tests/System Abstraction/2/compile.sh @@ -0,0 +1,2 @@ +#!/bin/sh +gcc -g openNewFiletest.c "../../../System Abstraction/linux/cdb_file.c" "../../../System Abstraction/linux/cdb_memroy.c" -o openNewFiletest \ No newline at end of file diff --git a/tests/System Abstraction/2/openNewFiletest b/tests/System Abstraction/2/openNewFiletest new file mode 100755 index 0000000..5acadab Binary files /dev/null and b/tests/System Abstraction/2/openNewFiletest differ diff --git a/tests/System Abstraction/2/openNewFiletest.c b/tests/System Abstraction/2/openNewFiletest.c index f6cf4be..9a68f54 100644 --- a/tests/System Abstraction/2/openNewFiletest.c +++ b/tests/System Abstraction/2/openNewFiletest.c @@ -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("%c%c%c%c", memroy[0], memroy[1], memroy[2], memroy[3]); } sus_fileResize(file, 2); diff --git a/tests/System Abstraction/2/testfile.txt b/tests/System Abstraction/2/testfile.txt deleted file mode 100644 index 2fe1f90..0000000 --- a/tests/System Abstraction/2/testfile.txt +++ /dev/null @@ -1 +0,0 @@ -Obc \ No newline at end of file diff --git a/tests/System Abstraction/2/testfile2.txt b/tests/System Abstraction/2/testfile2.txt deleted file mode 100644 index e69de29..0000000 diff --git a/tests/System Abstraction/3/compile.sh b/tests/System Abstraction/3/compile.sh new file mode 100755 index 0000000..6fd23a2 --- /dev/null +++ b/tests/System Abstraction/3/compile.sh @@ -0,0 +1,2 @@ +#!/bin/sh +gcc -g openTempFiletest.c "../../../System Abstraction/linux/cdb_file.c" "../../../System Abstraction/linux/cdb_memroy.c" -o openTempFiletest \ No newline at end of file diff --git a/tests/System Abstraction/3/openTempFiletest b/tests/System Abstraction/3/openTempFiletest new file mode 100755 index 0000000..9e2b918 Binary files /dev/null and b/tests/System Abstraction/3/openTempFiletest differ diff --git a/tests/System Abstraction/3/openTempFiletest.c b/tests/System Abstraction/3/openTempFiletest.c index 2980bef..d3619f2 100644 --- a/tests/System Abstraction/3/openTempFiletest.c +++ b/tests/System Abstraction/3/openTempFiletest.c @@ -23,7 +23,7 @@ void main(){ memroy[2] = 'c'; memroy[3] = 'd'; memroy[4] = 'e'; - Sleep(10000); + //Sleep(10000); sus_fileFlush(file); sus_fileClose(file); file = sus_fileOpenInMem("testfile.txt", SUS_FILE_NOTHING); diff --git a/tests/System Abstraction/3/testfile2.txt b/tests/System Abstraction/3/testfile2.txt deleted file mode 100644 index 2fe1f90..0000000 --- a/tests/System Abstraction/3/testfile2.txt +++ /dev/null @@ -1 +0,0 @@ -Obc \ No newline at end of file