#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); }