void UDB_open(UDB *self) { self->committedFile = fopen(self->committedPath, "r+"); if (!self->committedFile) { self->committedFile = fopen(self->committedPath, "w"); fclose(self->committedFile); self->committedFile = fopen(self->committedPath, "r+"); } if (UDB_readCommitted(self) == 0) { JFile_clipLog(self->index->file); JFile_clipLog(self->records->file); } UDBIndex_open(self->index); UDBRecords_open(self->records); self->isOpen = 1; }
void JFile_writeLogToFile(JFile *self) { size_t writeCount = 0; int logNotEmpty = 0; int lastTerminator = 0; fseek(self->log, 0, SEEK_SET); while (1) { long pos; size_t total; if (fread(&pos, sizeof(long), 1, self->log) != 1) { if (feof(self->log)) break; printf("JFile: error reading pos\n"); goto fatalError; } if (fread(&total, sizeof(size_t), 1, self->log) != 1) { printf("JFile: error reading total\n"); goto fatalError; } self->buf = realloc(self->buf, total); if (fread(self->buf, total, 1, self->log) != 1) { printf("JFile: error reading buf\n"); goto fatalError; } lastTerminator = fgetc(self->log); if (lastTerminator != JFILE_END_TRANSACTION_ITEM && lastTerminator != JFILE_END_TRANSACTION) { printf("JFile: invalid log terminator\n"); goto fatalError; } fseek(self->file, pos, SEEK_SET); fwrite(self->buf, total, 1, self->file); writeCount ++; logNotEmpty = 1; } if (logNotEmpty) { //printf("writing log\n"); JFile_syncFileWritesToDisk(self); JFile_clipLog(self); } return; fatalError: printf("JFile: FATAL ERROR: invalid log file '%s' - may result in inconsistency\n", self->logPath); }