cinera.c: Gracefully handle database non-creation

This commit is contained in:
Matt Mascarenhas 2020-06-24 17:34:41 +01:00
parent b3470e0f48
commit 6eeb588adf
1 changed files with 111 additions and 97 deletions

View File

@ -23,7 +23,7 @@ typedef struct
version CINERA_APP_VERSION = {
.Major = 0,
.Minor = 7,
.Patch = 17
.Patch = 18
};
#include <stdarg.h> // NOTE(matt): varargs
@ -4735,7 +4735,6 @@ void *
LocateBlock(block_id BlockID)
{
void *Result = 0;
db_header *Header = (db_header *)DB.Metadata.File.Buffer.Location;
char *Ptr = (char *)Header;
Ptr += sizeof(db_header);
@ -14133,9 +14132,10 @@ WriteEntireDatabase()
fclose(DB.Metadata.File.Handle);
}
int
rc
InitDB(void)
{
rc Result = RC_SUCCESS;
// TODO(matt): InitDB() is called once on startup. This is correct for the .metadata file, because we only want one of
// those to house the info for all projects. However, we will want to create a .index file for each project, so
// need a separate InitIndex() function that we can call when looping over the projects after ParseConfig()
@ -14261,7 +14261,7 @@ InitDB(void)
LogError(LOG_ERROR, "Unable to create directory %.*s: %s", (int)Config->DatabaseLocation.Length, Config->DatabaseLocation.Base, strerror(errno));
fprintf(stderr, "Unable to create directory %.*s: %s\n", (int)Config->DatabaseLocation.Length, Config->DatabaseLocation.Base, strerror(errno));
Free(DatabaseLocation0);
return RC_ERROR_DIRECTORY;
Result = RC_ERROR_DIRECTORY;
};
}
else
@ -14271,6 +14271,9 @@ InitDB(void)
}
Free(DatabaseLocation0);
if(Result == RC_SUCCESS)
{
DB.Metadata.File.Handle = fopen(DB.Metadata.File.Path, "w");
if(DB.Metadata.File.Handle)
{
@ -14303,7 +14306,8 @@ InitDB(void)
ReadFileIntoBuffer(&DB.File);
#endif
}
return RC_SUCCESS;
}
return Result;
}
void
@ -15340,15 +15344,18 @@ PrintEvent(struct inotify_event *Event, int EventIndex, int Indentation)
}
#endif
void
rc
InitAll(neighbourhood *Neighbourhood, buffers *CollationBuffers, template *BespokeTemplate)
{
rc Result = RC_SUCCESS;
MEM_TEST_TOP("InitAll()");
RewindCollationBuffers(CollationBuffers);
/* */ MEM_TEST_MID("InitAll()");
/* +MEM */ InitDB();
/* +MEM */ Result = InitDB();
/* */ MEM_TEST_MID("InitAll()");
if(Result == RC_SUCCESS)
{
// TODO(matt): Straight up remove these PrintAssetsBlock() calls
//PrintAssetsBlock(0);
@ -15392,6 +15399,8 @@ InitAll(neighbourhood *Neighbourhood, buffers *CollationBuffers, template *Bespo
ColourStrings[EditTypes[EDIT_ADDITION].Colour], ColourStrings[CS_END],
ColourStrings[EditTypes[EDIT_REINSERTION].Colour], ColourStrings[CS_END],
ColourStrings[EditTypes[EDIT_DELETION].Colour], ColourStrings[CS_END]);
}
return Result;
}
void
@ -15672,6 +15681,7 @@ void
Exit(void)
{
Free(MemoryArena.Location);
fprintf(stderr, "Exiting\n");
_exit(0);
}
@ -15812,6 +15822,7 @@ main(int ArgC, char **Args)
PushWatchHandle(ConfigPathL, EXT_NULL, WT_CONFIG, 0, 0);
Config = ParseConfig(ConfigPathL, &TokensList);
rc Succeeding = RC_SUCCESS;
if(Config)
{
if(Mode & MODE_EXAMINE)
@ -15828,7 +15839,7 @@ main(int ArgC, char **Args)
else
{
/* */ MEM_TEST_MID("main()");
/* +MEM */ InitAll(&Neighbourhood, &CollationBuffers, &BespokeTemplate);
/* +MEM */ Succeeding = InitAll(&Neighbourhood, &CollationBuffers, &BespokeTemplate);
/* */ MEM_TEST_MID("main()");
}
}
@ -15844,6 +15855,8 @@ main(int ArgC, char **Args)
}
}
if(Succeeding == RC_SUCCESS)
{
if(inotifyInstance)
{
while(MonitorFilesystem(&Neighbourhood, &CollationBuffers, &BespokeTemplate, ConfigPathL, &TokensList) != RC_ARENA_FULL)
@ -15875,5 +15888,6 @@ main(int ArgC, char **Args)
RemoveAndFreeWatchHandles(&WatchHandles);
MEM_TEST_END("main()");
}
}
Exit();
}