Linsus implementation

This commit is contained in:
MrLetsplay 2022-08-28 15:50:14 +02:00
parent 321fc20863
commit fa896b0c72
14 changed files with 125 additions and 4 deletions

5
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,5 @@
{
"files.associations": {
"unistd.h": "c"
}
}

View File

@ -0,0 +1,97 @@
#include "../cdb_Sustem.h"
#define _GNU_SOURCE
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/errno.h>
#include <fcntl.h>
#include <stdio.h>
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);
}

View File

@ -0,0 +1,15 @@
#include "../cdb_Sustem.h"
#include <stdlib.h>
// 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);
}

View File

@ -0,0 +1,2 @@
#!/bin/sh
gcc systemAbstractionTest1.c "../../../System Abstraction/linux/cdb_file.c" "../../../System Abstraction/linux/cdb_memroy.c" -o systemAbstractionTest1

Binary file not shown.

View File

@ -0,0 +1,2 @@
#!/bin/sh
gcc -g openNewFiletest.c "../../../System Abstraction/linux/cdb_file.c" "../../../System Abstraction/linux/cdb_memroy.c" -o openNewFiletest

Binary file not shown.

View File

@ -13,7 +13,7 @@ void main(){
printf("memroy was null\n"); printf("memroy was null\n");
return; return;
}else{ }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); sus_fileResize(file, 2);

View File

@ -1 +0,0 @@
Obc

View File

@ -0,0 +1,2 @@
#!/bin/sh
gcc -g openTempFiletest.c "../../../System Abstraction/linux/cdb_file.c" "../../../System Abstraction/linux/cdb_memroy.c" -o openTempFiletest

Binary file not shown.

View File

@ -23,7 +23,7 @@ void main(){
memroy[2] = 'c'; memroy[2] = 'c';
memroy[3] = 'd'; memroy[3] = 'd';
memroy[4] = 'e'; memroy[4] = 'e';
Sleep(10000); //Sleep(10000);
sus_fileFlush(file); sus_fileFlush(file);
sus_fileClose(file); sus_fileClose(file);
file = sus_fileOpenInMem("testfile.txt", SUS_FILE_NOTHING); file = sus_fileOpenInMem("testfile.txt", SUS_FILE_NOTHING);

View File

@ -1 +0,0 @@
Obc