/*
* This file is part of the Aaru Data Preservation Suite.
* Copyright (c) 2019-2025 Natalia Portillo.
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of the
* License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see .
*/
#include
#include
#include
#ifdef __linux__
#include
#endif
#include
#include "internal.h"
#include "log.h"
int aaruf_close(void *context)
{
TRACE("Entering aaruf_close(%p)", context);
int i = 0;
mediaTagEntry *mediaTag = NULL;
mediaTagEntry *tmpMediaTag = NULL;
if(context == NULL)
{
FATAL("Invalid context");
errno = EINVAL;
return -1;
}
aaruformatContext *ctx = context;
// Not a libaaruformat context
if(ctx->magic != AARU_MAGIC)
{
FATAL("Invalid context");
errno = EINVAL;
return -1;
}
if(ctx->isWriting)
{
TRACE("File is writing");
TRACE("Seeking to start of image");
// Write the header at the beginning of the file
fseek(ctx->imageStream, 0, SEEK_SET);
TRACE("Writing header at position 0");
if(fwrite(&ctx->header, sizeof(AaruHeaderV2), 1, ctx->imageStream) != 1)
{
fclose(ctx->imageStream);
ctx->imageStream = NULL;
errno = AARUF_ERROR_CANNOT_WRITE_HEADER;
return -1;
}
// Close current block first
TRACE("Closing current block if any");
if(ctx->writingBuffer != NULL)
{
int error = aaruf_close_current_block(ctx);
if(error != AARUF_STATUS_OK) return error;
}
}
TRACE("Freeing memory pointers");
// This may do nothing if imageStream is NULL, but as the behaviour is undefined, better sure than sorry
if(ctx->imageStream != NULL)
{
fclose(ctx->imageStream);
ctx->imageStream = NULL;
}
free(ctx->sectorPrefix);
ctx->sectorPrefix = NULL;
free(ctx->sectorPrefixCorrected);
ctx->sectorPrefixCorrected = NULL;
free(ctx->sectorSuffix);
ctx->sectorSuffix = NULL;
free(ctx->sectorSuffixCorrected);
ctx->sectorSuffixCorrected = NULL;
free(ctx->sectorSubchannel);
ctx->sectorSubchannel = NULL;
free(ctx->mode2Subheaders);
ctx->mode2Subheaders = NULL;
TRACE("Freeing media tags");
if(ctx->mediaTags != NULL) HASH_ITER(hh, ctx->mediaTags, mediaTag, tmpMediaTag)
{
HASH_DEL(ctx->mediaTags, mediaTag);
free(mediaTag->data);
free(mediaTag);
}
#ifdef __linux__ // TODO: Implement
TRACE("Unmapping user data DDT if it is not in memory");
if(!ctx->inMemoryDdt)
{
munmap(ctx->userDataDdt, ctx->mappedMemoryDdtSize);
ctx->userDataDdt = NULL;
}
#endif
free(ctx->sectorPrefixDdt);
ctx->sectorPrefixDdt = NULL;
free(ctx->sectorSuffixDdt);
ctx->sectorSuffixDdt = NULL;
free(ctx->metadataBlock);
ctx->metadataBlock = NULL;
free(ctx->trackEntries);
ctx->trackEntries = NULL;
free(ctx->cicmBlock);
ctx->cicmBlock = NULL;
if(ctx->dumpHardwareEntriesWithData != NULL)
{
for(i = 0; i < ctx->dumpHardwareHeader.entries; i++)
{
free(ctx->dumpHardwareEntriesWithData[i].extents);
ctx->dumpHardwareEntriesWithData[i].extents = NULL;
free(ctx->dumpHardwareEntriesWithData[i].manufacturer);
ctx->dumpHardwareEntriesWithData[i].manufacturer = NULL;
free(ctx->dumpHardwareEntriesWithData[i].model);
ctx->dumpHardwareEntriesWithData[i].model = NULL;
free(ctx->dumpHardwareEntriesWithData[i].revision);
ctx->dumpHardwareEntriesWithData[i].revision = NULL;
free(ctx->dumpHardwareEntriesWithData[i].firmware);
ctx->dumpHardwareEntriesWithData[i].firmware = NULL;
free(ctx->dumpHardwareEntriesWithData[i].serial);
ctx->dumpHardwareEntriesWithData[i].serial = NULL;
free(ctx->dumpHardwareEntriesWithData[i].softwareName);
ctx->dumpHardwareEntriesWithData[i].softwareName = NULL;
free(ctx->dumpHardwareEntriesWithData[i].softwareVersion);
ctx->dumpHardwareEntriesWithData[i].softwareVersion = NULL;
free(ctx->dumpHardwareEntriesWithData[i].softwareOperatingSystem);
ctx->dumpHardwareEntriesWithData[i].softwareOperatingSystem = NULL;
}
ctx->dumpHardwareEntriesWithData = NULL;
}
free(ctx->readableSectorTags);
ctx->readableSectorTags = NULL;
free(ctx->eccCdContext);
ctx->eccCdContext = NULL;
free(ctx->checksums.spamsum);
ctx->checksums.spamsum = NULL;
// TODO: Free caches
free(context);
TRACE("Exiting aaruf_close() = 0");
return 0;
}