summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarkus Mittendrein <git@maxmitti.tk>2019-04-20 20:30:22 +0200
committerMarkus Mittendrein <git@maxmitti.tk>2019-04-20 20:30:22 +0200
commitd1202195a3d94d0333d735fb05ac72214bd6708d (patch)
tree21e791d56115fce3cdd242aa6bb681633d7c7e3c
parent1cb782c4ffbb6af576a6d1e265b300fa3fee253d (diff)
downloadcc4group-d1202195a3d94d0333d735fb05ac72214bd6708d.tar.gz
cc4group-d1202195a3d94d0333d735fb05ac72214bd6708d.zip
Replace all internal accesses to group headers with error checked cc4group_getHeader
-rw-r--r--src/cc4group.c96
1 files changed, 80 insertions, 16 deletions
diff --git a/src/cc4group.c b/src/cc4group.c
index 40b838c..a9ecb85 100644
--- a/src/cc4group.c
+++ b/src/cc4group.c
@@ -129,6 +129,7 @@ static const C4GroupEntryData* cc4group_getDirectoryByPath(CC4Group* const this,
static const C4GroupEntryData* cc4group_getFileByPath(CC4Group* const this, const char* const entryPath);
static const C4GroupEntryData* cc4group_getFileOrDirectoryByPath(CC4Group* const this, const char* const entryPath, bool allowRoot);
static GroupEntryList* cc4group_getChildren(CC4Group* const this, const C4GroupEntryData* entry);
+static C4GroupHeader* cc4group_getHeader(CC4Group* const this, const C4GroupEntryData* entry);
static char* cc4group_strerrorFormatter(int32_t const code, const char* const method, const char* const causer, void* const data)
{
@@ -1004,7 +1005,13 @@ static bool cc4group_setMakerRecursively(CC4Group* const this, const C4GroupEntr
{
if(entry->value.core.Directory)
{
- C4GroupHeader_setMaker(entry->value.header, maker);
+ C4GroupHeader* header = cc4group_getHeader(this, &entry->value);
+ if(header == NULL)
+ {
+ return false;
+ }
+
+ C4GroupHeader_setMaker(header, maker);
if(!cc4group_setMakerRecursively(this, &entry->value, maker))
{
return false;
@@ -1027,7 +1034,13 @@ static bool cc4group_setMaker(CC4Group* const this, const char* const maker, con
return false;
}
- C4GroupHeader_setMaker(entry->header, maker);
+ C4GroupHeader* header = cc4group_getHeader(this, entry);
+ if(header == NULL)
+ {
+ return false;
+ }
+
+ C4GroupHeader_setMaker(header, maker);
if(recursive)
{
return cc4group_setMakerRecursively(this, entry, maker);
@@ -1047,7 +1060,13 @@ static bool cc4group_setOfficialRecursively(CC4Group* const this, const C4GroupE
{
if(entry->value.core.Directory)
{
- C4GroupHeader_setOfficial(entry->value.header, official);
+ C4GroupHeader* header = cc4group_getHeader(this, &entry->value);
+ if(header == NULL)
+ {
+ return false;
+ }
+
+ C4GroupHeader_setOfficial(header, official);
if(!cc4group_setOfficialRecursively(this, &entry->value, official))
{
return false;
@@ -1069,7 +1088,13 @@ static bool cc4group_setOfficial(CC4Group* const this, bool const official, cons
return false;
}
- C4GroupHeader_setOfficial(entry->header, official);
+ C4GroupHeader* header = cc4group_getHeader(this, entry);
+ if(header == NULL)
+ {
+ return false;
+ }
+
+ C4GroupHeader_setOfficial(header, official);
if(recursive)
{
return cc4group_setOfficialRecursively(this, entry, official);
@@ -1090,7 +1115,13 @@ static bool cc4group_setCreationRecursively(CC4Group* const this, const C4GroupE
entry->value.core.Modified = creation;
if(entry->value.core.Directory)
{
- C4GroupHeader_setCreation(entry->value.header, creation);
+ C4GroupHeader* header = cc4group_getHeader(this, &entry->value);
+ if(header == NULL)
+ {
+ return false;
+ }
+
+ C4GroupHeader_setCreation(header, creation);
if(!cc4group_setCreationRecursively(this, &entry->value, creation))
{
return false;
@@ -1116,7 +1147,13 @@ static bool cc4group_setCreation(CC4Group* const this, int32_t const creation, c
if(entry->core.Directory)
{
- C4GroupHeader_setCreation(entry->header, creation);
+ C4GroupHeader* header = cc4group_getHeader(this, entry);
+ if(header == NULL)
+ {
+ return false;
+ }
+
+ C4GroupHeader_setCreation(header, creation);
if(recursive)
{
return cc4group_setCreationRecursively(this, entry, creation);
@@ -1509,6 +1546,16 @@ static GroupEntryList* cc4group_getChildren(CC4Group* const this, const C4GroupE
return entry->children;
}
+static C4GroupHeader* cc4group_getHeader(CC4Group* const this, const C4GroupEntryData* entry)
+{
+ // TODO: lazy
+ (void)this;
+
+ assert(entry->core.Directory);
+
+ return entry->header;
+}
+
static bool cc4group_getEntryData(CC4Group* const this, const char* const entryPath, const void** const data, size_t* size)
{
assert(this);
@@ -1975,18 +2022,30 @@ static bool cc4group_saveOverwrite(CC4Group* const this, const char* const path)
return cc4group_saveIt(this, path, true);
}
-static void cc4group_getEntryInfoForEntry(const C4GroupEntryData* const entry, CC4Group_EntryInfo* const info)
+static bool cc4group_getEntryInfoForEntry(CC4Group* const this, const C4GroupEntryData* const entry, CC4Group_EntryInfo* const info)
{
+ C4GroupHeader* header;
+ if(entry->core.Directory)
+ {
+ header = cc4group_getHeader(this, entry);
+ if(header == NULL)
+ {
+ return false;
+ }
+ }
+
*info = (CC4Group_EntryInfo){
.fileName = entry->core.FileName,
- .modified = entry->core.Directory ? entry->header->Creation : entry->core.Modified,
- .author = entry->core.Directory ? entry->header->Maker : entry->parent->header->Maker,
- .size = entry->core.Directory ? (sizeof(C4GroupHeader) + entry->header->Entries * sizeof(C4GroupEntryCore)) : (size_t)entry->core.Size,
+ .modified = entry->core.Directory ? header->Creation : entry->core.Modified,
+ .author = entry->core.Directory ? header->Maker : entry->parent->header->Maker,
+ .size = entry->core.Directory ? (sizeof(C4GroupHeader) + header->Entries * sizeof(C4GroupEntryCore)) : (size_t)entry->core.Size,
.totalSize = entry->core.Size,
.executable = entry->core.Executable ? true : false,
.directory = entry->core.Directory ? true : false,
- .official = C4GroupHeader_isOfficial(entry->core.Directory ? entry->header: entry->parent->header)
+ .official = C4GroupHeader_isOfficial(entry->core.Directory ? header : entry->parent->header) // parents header is already loaded to access this child
};
+
+ return true;
}
static bool cc4group_getEntryInfo(CC4Group* const this, const char* const path, CC4Group_EntryInfo* const info)
@@ -2000,8 +2059,7 @@ static bool cc4group_getEntryInfo(CC4Group* const this, const char* const path,
return false;
}
- cc4group_getEntryInfoForEntry(entry, info);
- return true;
+ return cc4group_getEntryInfoForEntry(this, entry, info);
}
static bool cc4group_getEntryInfos(CC4Group* const this, const char* const path, CC4Group_EntryInfo** const infos, size_t* size)
@@ -2040,13 +2098,19 @@ static bool cc4group_getEntryInfos(CC4Group* const this, const char* const path,
SET_ERRNO_ERROR("malloc: allocating memory for group entry infos");
return false;
}
+
*infos = myInfos;
ForeachGroupEntry(children)
{
- cc4group_getEntryInfoForEntry(&entry->value, myInfos++);
+ if(!cc4group_getEntryInfoForEntry(this, &entry->value, myInfos++))
+ {
+ free(myInfos);
+ return false;
+ }
}
+
return true;
}
@@ -2159,7 +2223,7 @@ static bool cc4group_deleteEntry(CC4Group* const this, const char* const path, b
assert(parentChildren); // if there was an error, it should have really been catched already in cc4group_getParentAndChildEntries
GroupEntryListRemove(parentChildren, deleteEntry);
- parent->header->Entries = GroupEntryListSize(parentChildren);
+ parent->header->Entries = GroupEntryListSize(parentChildren); // header is loaded already for the children
return true;
}
@@ -2177,7 +2241,7 @@ static C4GroupEntryData* cc4group_addEntryToDirectory(CC4Group* const this, C4Gr
C4GroupEntryData* newEntry = &GroupEntryListAppend(directoryChildren, *entry)->value;
newEntry->parent = directory;
- directory->header->Entries = GroupEntryListSize(directoryChildren);
+ directory->header->Entries = GroupEntryListSize(directoryChildren); // header is loaded already for the children
return newEntry;
}