Files
libaaruformat/src/close.c

163 lines
4.8 KiB
C
Raw Normal View History

2022-05-28 12:57:21 +01:00
/*
* This file is part of the Aaru Data Preservation Suite.
2025-08-01 21:19:45 +01:00
* Copyright (c) 2019-2025 Natalia Portillo.
2022-05-28 12:57:21 +01:00
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <errno.h>
2019-03-31 20:52:06 +01:00
#include <stdio.h>
2022-10-03 18:15:13 +01:00
#include <stdlib.h>
#ifdef __linux__
#include <sys/mman.h>
#endif
2022-05-28 12:10:04 +01:00
#include <aaruformat.h>
#include "internal.h"
2024-04-30 15:51:32 +01:00
int aaruf_close(void *context)
{
int i = 0;
mediaTagEntry *mediaTag = NULL;
mediaTagEntry *tmpMediaTag = NULL;
2019-08-03 02:11:36 +01:00
if(context == NULL)
{
errno = EINVAL;
return -1;
}
2024-04-30 15:51:32 +01:00
aaruformatContext *ctx = context;
2020-03-01 19:51:13 +00:00
// Not a libaaruformat context
2020-03-01 19:58:09 +00:00
if(ctx->magic != AARU_MAGIC)
{
errno = EINVAL;
return -1;
}
if(ctx->isWriting)
{
// Write the header at the beginning of the file
fseek(ctx->imageStream, 0, SEEK_SET);
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
if(ctx->writingBuffer != NULL)
{
int error = aaruf_close_current_block(ctx);
if(error != AARUF_STATUS_OK) return error;
}
}
// 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;
2022-10-04 20:32:26 +01:00
if(ctx->mediaTags != NULL)
2019-03-17 23:01:54 +00:00
{
2022-10-04 20:32:26 +01:00
HASH_ITER(hh, ctx->mediaTags, mediaTag, tmpMediaTag)
2019-03-17 23:01:54 +00:00
{
2022-10-04 20:32:26 +01:00
HASH_DEL(ctx->mediaTags, mediaTag);
2019-03-17 23:01:54 +00:00
free(mediaTag->data);
2022-10-04 20:32:26 +01:00
free(mediaTag);
2019-03-17 23:01:54 +00:00
}
}
2024-04-30 15:51:32 +01:00
#ifdef __linux__ // TODO: Implement
if(!ctx->inMemoryDdt)
{
munmap(ctx->userDataDdt, ctx->mappedMemoryDdtSize);
ctx->userDataDdt = NULL;
}
#endif
2019-03-17 23:25:45 +00:00
free(ctx->sectorPrefixDdt);
ctx->sectorPrefixDdt = NULL;
2019-03-17 23:25:45 +00:00
free(ctx->sectorSuffixDdt);
ctx->sectorSuffixDdt = NULL;
2019-03-17 23:25:45 +00:00
2019-03-17 23:41:07 +00:00
free(ctx->metadataBlock);
ctx->metadataBlock = NULL;
2019-03-18 00:10:24 +00:00
free(ctx->trackEntries);
ctx->trackEntries = NULL;
2019-03-18 22:06:10 +00:00
free(ctx->cicmBlock);
ctx->cicmBlock = NULL;
2019-03-17 23:41:07 +00:00
2019-03-20 00:23:30 +00:00
if(ctx->dumpHardwareEntriesWithData != NULL)
{
2019-08-03 02:11:36 +01:00
for(i = 0; i < ctx->dumpHardwareHeader.entries; i++)
2019-03-20 00:23:30 +00:00
{
free(ctx->dumpHardwareEntriesWithData[i].extents);
ctx->dumpHardwareEntriesWithData[i].extents = NULL;
2019-03-20 00:23:30 +00:00
free(ctx->dumpHardwareEntriesWithData[i].manufacturer);
ctx->dumpHardwareEntriesWithData[i].manufacturer = NULL;
2019-03-20 00:23:30 +00:00
free(ctx->dumpHardwareEntriesWithData[i].model);
ctx->dumpHardwareEntriesWithData[i].model = NULL;
2019-03-20 00:23:30 +00:00
free(ctx->dumpHardwareEntriesWithData[i].revision);
ctx->dumpHardwareEntriesWithData[i].revision = NULL;
2019-03-20 00:23:30 +00:00
free(ctx->dumpHardwareEntriesWithData[i].firmware);
ctx->dumpHardwareEntriesWithData[i].firmware = NULL;
2019-03-20 00:23:30 +00:00
free(ctx->dumpHardwareEntriesWithData[i].serial);
ctx->dumpHardwareEntriesWithData[i].serial = NULL;
2019-03-20 00:23:30 +00:00
free(ctx->dumpHardwareEntriesWithData[i].softwareName);
ctx->dumpHardwareEntriesWithData[i].softwareName = NULL;
2019-03-20 00:23:30 +00:00
free(ctx->dumpHardwareEntriesWithData[i].softwareVersion);
ctx->dumpHardwareEntriesWithData[i].softwareVersion = NULL;
2019-03-20 00:23:30 +00:00
free(ctx->dumpHardwareEntriesWithData[i].softwareOperatingSystem);
ctx->dumpHardwareEntriesWithData[i].softwareOperatingSystem = NULL;
2019-03-20 00:23:30 +00:00
}
ctx->dumpHardwareEntriesWithData = NULL;
2019-03-20 00:23:30 +00:00
}
2019-03-31 14:56:03 +01:00
free(ctx->readableSectorTags);
ctx->readableSectorTags = NULL;
2022-10-03 19:32:25 +01:00
free(ctx->eccCdContext);
ctx->eccCdContext = NULL;
2022-10-04 19:44:34 +01:00
free(ctx->checksums.spamsum);
ctx->checksums.spamsum = NULL;
// TODO: Free caches
2019-03-31 14:56:03 +01:00
free(context);
return 0;
}