lumiera_/src/backend/filehandle.c
Christian Thaeter d37bb17566 Add a context string to the error system
lumiera_error_set() now takes an optional extra string which can be used
to pass context relevant data along. This string gets copied into the
error state so one can easily create it by the tmpbuf_snprintf() facility.

Also a lot of places which define errors get fixed according to this.
2009-01-13 21:23:37 +01:00

108 lines
2.8 KiB
C

/*
filehandle - filehandle management and caching
Copyright (C) Lumiera.org
2008, 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/llist.h"
#include "lib/safeclib.h"
#include "backend/file.h"
#include "backend/filehandle.h"
#include "backend/filedescriptor.h"
#include <unistd.h>
LumieraFilehandle
lumiera_filehandle_init (LumieraFilehandle self, LumieraFiledescriptor desc)
{
TRACE (filehandle, "%p", self);
if (self)
{
llist_init (&self->cachenode);
self->fd = -1;
self->use_cnt = 1;
self->descriptor = desc;
}
return self;
}
LumieraFilehandle
lumiera_filehandle_new (LumieraFiledescriptor desc)
{
LumieraFilehandle self = lumiera_malloc (sizeof (*self));
return lumiera_filehandle_init (self, desc);
}
void*
lumiera_filehandle_destroy_node (LList node)
{
TRACE (filehandle);
REQUIRE (llist_is_empty (node));
LumieraFilehandle self = (LumieraFilehandle)node;
REQUIRE (self->use_cnt == 0);
if (self->fd >= 0)
close (self->fd);
return self;
}
int
lumiera_filehandle_get (LumieraFilehandle self)
{
REQUIRE (self->descriptor);
return -1;
}
int
lumiera_filehandle_handle (LumieraFilehandle self)
{
TRACE (filehandle);
int fd = -1;
if (self->fd == -1)
{
fd = open (lumiera_filedescriptor_name (self->descriptor), lumiera_filedescriptor_flags (self->descriptor) & LUMIERA_FILE_MASK);
if (fd == -1)
{
LUMIERA_ERROR_SET (filehandle, ERRNO, lumiera_filedescriptor_name (self->descriptor));
}
else
{
struct stat st;
if (fstat (fd, &st) == -1)
{
close (fd);
LUMIERA_ERROR_SET (filehandle, ERRNO, lumiera_filedescriptor_name (self->descriptor));
}
else if (!lumiera_filedescriptor_samestat (self->descriptor, &st))
{
close (fd);
/* Woops this is not the file we expected to use */
LUMIERA_ERROR_SET (filehandle, FILE_CHANGED, lumiera_filedescriptor_name (self->descriptor));
}
}
self->fd = fd;
}
return self->fd;
}