FIX: tmpbuf.h includes in backend
This commit is contained in:
parent
0158c1b88d
commit
f967f9427b
4 changed files with 235 additions and 1 deletions
|
|
@ -21,6 +21,7 @@
|
||||||
|
|
||||||
#include "include/logging.h"
|
#include "include/logging.h"
|
||||||
#include "lib/safeclib.h"
|
#include "lib/safeclib.h"
|
||||||
|
#include "lib/tmpbuf.h"
|
||||||
#include "lib/mpool.h"
|
#include "lib/mpool.h"
|
||||||
|
|
||||||
#include "backend/backend.h"
|
#include "backend/backend.h"
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@
|
||||||
#include "include/logging.h"
|
#include "include/logging.h"
|
||||||
#include "lib/mutex.h"
|
#include "lib/mutex.h"
|
||||||
#include "lib/safeclib.h"
|
#include "lib/safeclib.h"
|
||||||
|
#include "lib/tmpbuf.h"
|
||||||
|
|
||||||
#include "backend/file.h"
|
#include "backend/file.h"
|
||||||
#include "backend/filedescriptor.h"
|
#include "backend/filedescriptor.h"
|
||||||
|
|
|
||||||
232
src/backend/fileheader.c
Normal file
232
src/backend/fileheader.c
Normal file
|
|
@ -0,0 +1,232 @@
|
||||||
|
/*
|
||||||
|
fileheader.c - Definitions of generic lumiera fileheaders and identification
|
||||||
|
|
||||||
|
Copyright (C) Lumiera.org
|
||||||
|
2010, Christian Thaeter <ct@pipapo.org>
|
||||||
|
|
||||||
|
This program is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU General Public License as
|
||||||
|
published by the Free Software Foundation; either version 2 of the
|
||||||
|
License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This program 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 General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "lib/tmpbuf.h"
|
||||||
|
|
||||||
|
#include "backend/fileheader.h"
|
||||||
|
#include "backend/file.h"
|
||||||
|
#include "include/logging.h"
|
||||||
|
|
||||||
|
//TODO: internal/static forward declarations//
|
||||||
|
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @file
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
//NOBUG_DEFINE_FLAG_PARENT (fileheader, /*TODO insert parent flag here */);
|
||||||
|
|
||||||
|
LUMIERA_ERROR_DEFINE (FILEHEADER_NOWRITE, "File is not writeable");
|
||||||
|
LUMIERA_ERROR_DEFINE (FILEHEADER_HEADER, "Error in header");
|
||||||
|
LUMIERA_ERROR_DEFINE (FILEHEADER_FLAGS, "Inconsistent Flags");
|
||||||
|
LUMIERA_ERROR_DEFINE (FILEHEADER_FLAGSPACE, "No more space for flags left");
|
||||||
|
LUMIERA_ERROR_DEFINE (FILEHEADER_ENDIANESS, "Unsupported Endianess");
|
||||||
|
|
||||||
|
|
||||||
|
lumiera_fileheader
|
||||||
|
lumiera_fileheader_create (LumieraFile file, char* fourcc, int version, size_t size, const char* flags)
|
||||||
|
{
|
||||||
|
lumiera_fileheader self = {NULL, NULL};
|
||||||
|
|
||||||
|
REQUIRE (fourcc && strlen (fourcc) == 4, "fourcc must be of size 4 and nothing else: %s", fourcc);
|
||||||
|
REQUIRE_IF (flags, strlen (flags) <= sizeof(self.header->flags), "to much flags given");
|
||||||
|
|
||||||
|
if (file)
|
||||||
|
{
|
||||||
|
if (lumiera_file_checkflags (file, O_RDWR))
|
||||||
|
{
|
||||||
|
WARN_IF (version == 0, fileheader, "Experimental version 0 given for file %s, %s", file->name, fourcc);
|
||||||
|
|
||||||
|
if ((self.map = lumiera_mmap_new_exact (file, 0, size)))
|
||||||
|
{
|
||||||
|
self.header = lumiera_mmap_address (self.map, 0);
|
||||||
|
|
||||||
|
memcpy (self.header->fourcc, fourcc, 4);
|
||||||
|
snprintf (self.header->version, 4, "%3u", version);
|
||||||
|
self.header->newline1 = '\n';
|
||||||
|
for (size_t i = 0; i < sizeof(self.header->meta); ++i)
|
||||||
|
self.header->meta[i] = ' ';
|
||||||
|
self.header->newline2 = '\n';
|
||||||
|
for (size_t i = 0; i < sizeof(self.header->flags); ++i)
|
||||||
|
self.header->flags[i] = ' ';
|
||||||
|
self.header->newline3 = '\n';
|
||||||
|
self.header->null = '\0';
|
||||||
|
self.header->endianess_mark = LUMIERA_FILEHEADER_ENDIANMAGIC;
|
||||||
|
|
||||||
|
lumiera_fileheader_flags_set (&self, flags);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
LUMIERA_ERROR_SET_CRITICAL(fileheader, FILEHEADER_NOWRITE, file->name);
|
||||||
|
}
|
||||||
|
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
lumiera_fileheader
|
||||||
|
lumiera_fileheader_open (LumieraFile file, char* fourcc, size_t size, const char* flags_expected, const char* flags_remove)
|
||||||
|
{
|
||||||
|
lumiera_fileheader self = {NULL, NULL};
|
||||||
|
|
||||||
|
REQUIRE (fourcc && strlen (fourcc) == 4, "fourcc must be of size 4 and nothing else: %s", fourcc);
|
||||||
|
|
||||||
|
if (file)
|
||||||
|
{
|
||||||
|
if ((self.map = lumiera_mmap_new_exact (file, 0, size)))
|
||||||
|
{
|
||||||
|
self.header = lumiera_mmap_address (self.map, 0);
|
||||||
|
|
||||||
|
if (memcmp (self.header->fourcc, fourcc, 4))
|
||||||
|
{
|
||||||
|
LUMIERA_ERROR_SET_WARNING (fileheader, FILEHEADER_HEADER, file->name);
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!lumiera_fileheader_flags_validate (&self, flags_expected, NULL))
|
||||||
|
{
|
||||||
|
LUMIERA_ERROR_SET_WARNING (fileheader, FILEHEADER_FLAGS, self.header->flags);
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
|
||||||
|
TODO("only clear flags when file is writeable");
|
||||||
|
lumiera_fileheader_flags_clear (&self, flags_remove);
|
||||||
|
|
||||||
|
static uint64_t endianess_mark = LUMIERA_FILEHEADER_ENDIANMAGIC;
|
||||||
|
|
||||||
|
if (lumiera_fileheader_flags_validate (&self, LUMIERA_FILEHEADER_FLAG_ENDIANESS, NULL) &&
|
||||||
|
memcmp (&self.header->endianess_mark, (char*)&endianess_mark, sizeof (endianess_mark)))
|
||||||
|
{
|
||||||
|
LUMIERA_ERROR_SET_CRITICAL (fileheader, FILEHEADER_ENDIANESS, NULL);
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return self;
|
||||||
|
|
||||||
|
err:
|
||||||
|
lumiera_mmap_delete (self.map);
|
||||||
|
self.map = NULL;
|
||||||
|
self.header = NULL;
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
lumiera_fileheader_close (LumieraFileheader self, const char* flags_add)
|
||||||
|
{
|
||||||
|
if (self && self->header)
|
||||||
|
{
|
||||||
|
lumiera_fileheader_flags_set (self, flags_add);
|
||||||
|
self->header = NULL;
|
||||||
|
lumiera_mmap_delete (self->map);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int
|
||||||
|
lumiera_fileheader_version (LumieraFileheader self)
|
||||||
|
{
|
||||||
|
int ret = -1;
|
||||||
|
if (self && self->header)
|
||||||
|
{
|
||||||
|
errno = 0;
|
||||||
|
ret = (int) strtol(self->header->version, NULL, 10);
|
||||||
|
if (errno)
|
||||||
|
{
|
||||||
|
ret = -1;
|
||||||
|
LUMIERA_ERROR_SET_WARNING (fileheader, FILEHEADER_HEADER, lumiera_tmpbuf_snprintf (32, "%s", self->header->fourcc));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* check if all flags given from some sets are either set or not.
|
||||||
|
*/
|
||||||
|
int
|
||||||
|
lumiera_fileheader_flags_validate (LumieraFileheader self, const char* expected, const char* unexpected)
|
||||||
|
{
|
||||||
|
for (; expected && *expected; ++expected)
|
||||||
|
if (!strchr (self->header->flags, *expected))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
for (; unexpected && *unexpected; ++unexpected)
|
||||||
|
if (strchr (self->header->flags, *unexpected))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets flags if not already set
|
||||||
|
*/
|
||||||
|
LumieraFileheader
|
||||||
|
lumiera_fileheader_flags_set (LumieraFileheader self, const char* flags)
|
||||||
|
{
|
||||||
|
for (; flags && *flags; ++flags)
|
||||||
|
if (!strchr(self->header->flags, *flags))
|
||||||
|
{
|
||||||
|
char* pos = strchr (self->header->flags, ' ');
|
||||||
|
if (!pos)
|
||||||
|
{
|
||||||
|
LUMIERA_ERROR_SET_CRITICAL (fileheader, FILEHEADER_FLAGSPACE, flags);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
*pos = *flags;
|
||||||
|
}
|
||||||
|
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clear flags if present
|
||||||
|
*/
|
||||||
|
LumieraFileheader
|
||||||
|
lumiera_fileheader_flags_clear (LumieraFileheader self, const char* flags)
|
||||||
|
{
|
||||||
|
char* pos;
|
||||||
|
|
||||||
|
for (; flags && *flags; ++flags)
|
||||||
|
if ((pos = strchr (self->header->flags, *flags)))
|
||||||
|
*pos = ' ';
|
||||||
|
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
// Local Variables:
|
||||||
|
// mode: C
|
||||||
|
// c-file-style: "gnu"
|
||||||
|
// indent-tabs-mode: nil
|
||||||
|
// End:
|
||||||
|
*/
|
||||||
|
|
@ -19,7 +19,7 @@
|
||||||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||||
*/
|
*/
|
||||||
#include "lib/llist.h"
|
#include "lib/llist.h"
|
||||||
#include "lib/safeclib.h"
|
#include "lib/tmpbuf.h"
|
||||||
#include "common/config.h"
|
#include "common/config.h"
|
||||||
|
|
||||||
#include "backend/backend.h"
|
#include "backend/backend.h"
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue