2008-03-30 04:31:32 +02:00
|
|
|
/*
|
|
|
|
|
filedescriptor.c - file handling
|
|
|
|
|
|
|
|
|
|
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/mutex.h"
|
|
|
|
|
#include "lib/safeclib.h"
|
|
|
|
|
#include "lib/cuckoo.h"
|
|
|
|
|
|
2008-04-07 06:47:57 +02:00
|
|
|
#include "backend/file.h"
|
2008-03-30 04:31:32 +02:00
|
|
|
#include "backend/filedescriptor.h"
|
2008-04-07 06:47:57 +02:00
|
|
|
#include "backend/filehandle.h"
|
|
|
|
|
#include "backend/filehandlecache.h"
|
2008-03-30 04:31:32 +02:00
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <limits.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
|
2008-04-07 05:51:32 +02:00
|
|
|
NOBUG_DEFINE_FLAG_PARENT (filedescriptor, file_all);
|
2008-03-30 04:31:32 +02:00
|
|
|
|
2008-04-01 22:32:35 +02:00
|
|
|
/*
|
|
|
|
|
Filedescriptor registry
|
|
|
|
|
|
|
|
|
|
This registry stores all acquired filedescriptors for lookup, they will be freed when not referenced anymore.
|
|
|
|
|
*/
|
2008-03-30 04:31:32 +02:00
|
|
|
static Cuckoo registry = NULL;
|
|
|
|
|
static lumiera_mutex registry_mutex = {PTHREAD_MUTEX_INITIALIZER};
|
|
|
|
|
|
2008-04-01 22:32:35 +02:00
|
|
|
/*
|
|
|
|
|
* setup hashing and compare functions for cuckoo hashing
|
|
|
|
|
*/
|
2008-03-30 04:31:32 +02:00
|
|
|
static size_t
|
|
|
|
|
h1 (const void* item, const uint32_t r)
|
|
|
|
|
{
|
2008-04-01 22:32:35 +02:00
|
|
|
const LumieraFiledescriptor i = *(const LumieraFiledescriptor*)item;
|
2008-03-30 04:31:32 +02:00
|
|
|
return i->stat.st_dev^i->stat.st_ino^i->flags^((i->stat.st_dev^i->stat.st_ino^i->flags)>>7)^r;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
|
h2 (const void* item, const uint32_t r)
|
|
|
|
|
{
|
2008-04-01 22:32:35 +02:00
|
|
|
const LumieraFiledescriptor i = *(const LumieraFiledescriptor*)item;
|
2008-03-30 04:31:32 +02:00
|
|
|
return i->stat.st_dev^i->stat.st_ino^i->flags^((i->stat.st_dev^i->stat.st_ino^i->flags)>>5)^r;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
|
h3 (const void* item, const uint32_t r)
|
|
|
|
|
{
|
2008-04-01 22:32:35 +02:00
|
|
|
const LumieraFiledescriptor i = *(const LumieraFiledescriptor*)item;
|
2008-03-30 04:31:32 +02:00
|
|
|
return i->stat.st_dev^i->stat.st_ino^i->flags^((i->stat.st_dev^i->stat.st_ino^i->flags)>>3)^r;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
cmp (const void* keya, const void* keyb)
|
|
|
|
|
{
|
2008-04-01 22:32:35 +02:00
|
|
|
const LumieraFiledescriptor a = *(const LumieraFiledescriptor*)keya;
|
|
|
|
|
const LumieraFiledescriptor b = *(const LumieraFiledescriptor*)keyb;
|
2008-03-30 04:31:32 +02:00
|
|
|
return a->stat.st_dev == b->stat.st_dev && a->stat.st_ino == b->stat.st_ino && a->flags == b->flags;
|
|
|
|
|
}
|
|
|
|
|
|
2008-04-07 05:51:32 +02:00
|
|
|
void
|
|
|
|
|
lumiera_filedescriptor_registry_init (void)
|
2008-03-30 04:31:32 +02:00
|
|
|
{
|
|
|
|
|
NOBUG_INIT_FLAG (filedescriptor);
|
2008-04-07 05:51:32 +02:00
|
|
|
TRACE (filedescriptor);
|
2008-03-30 04:31:32 +02:00
|
|
|
|
2008-04-07 05:51:32 +02:00
|
|
|
registry = cuckoo_new (h1, h2, h3, cmp,
|
|
|
|
|
sizeof (LumieraFiledescriptor),
|
|
|
|
|
3);
|
2008-03-30 04:31:32 +02:00
|
|
|
if (!registry)
|
|
|
|
|
LUMIERA_DIE (NO_MEMORY);
|
|
|
|
|
}
|
|
|
|
|
|
2008-04-07 05:51:32 +02:00
|
|
|
void
|
|
|
|
|
lumiera_filedescriptor_registry_destroy (void)
|
2008-04-01 22:32:35 +02:00
|
|
|
{
|
2008-04-07 05:51:32 +02:00
|
|
|
TRACE (filedescriptor);
|
|
|
|
|
REQUIRE (!cuckoo_nelements (registry));
|
2008-04-01 22:32:35 +02:00
|
|
|
cuckoo_free (registry);
|
|
|
|
|
}
|
|
|
|
|
|
2008-04-07 05:51:32 +02:00
|
|
|
|
2008-03-30 04:31:32 +02:00
|
|
|
/**
|
|
|
|
|
* Find existing filedescriptor or create one
|
|
|
|
|
* @param descriptor strong reference to be initialized with the filedescriptor,
|
|
|
|
|
* a filedescriptor will be deleted when its last string reference gets deleted.
|
|
|
|
|
* @param name name of the file
|
|
|
|
|
* @param flags opening flags for the filedescriptor
|
|
|
|
|
* @return descriptor on success or NULL on error
|
|
|
|
|
*/
|
2008-04-01 22:32:35 +02:00
|
|
|
LumieraFiledescriptor
|
|
|
|
|
lumiera_filedescriptor_acquire (const char* name, int flags)
|
2008-03-30 04:31:32 +02:00
|
|
|
{
|
2008-04-01 22:32:35 +02:00
|
|
|
TRACE (filedescriptor);
|
2008-04-07 05:51:32 +02:00
|
|
|
REQUIRE (registry, "not initialized");
|
2008-03-30 04:31:32 +02:00
|
|
|
UNCHECKED;
|
|
|
|
|
lumiera_mutexacquirer registry_lock;
|
|
|
|
|
lumiera_mutexacquirer_init_mutex (®istry_lock, ®istry_mutex, LUMIERA_LOCKED);
|
|
|
|
|
|
2008-04-01 22:32:35 +02:00
|
|
|
lumiera_filedescriptor fdesc;
|
2008-04-07 05:51:32 +02:00
|
|
|
fdesc.flags = flags&LUMIERA_FILE_MASK;
|
2008-03-30 04:31:32 +02:00
|
|
|
|
|
|
|
|
for (int retry = 0; !retry; ++retry)
|
|
|
|
|
{
|
2008-04-01 22:32:35 +02:00
|
|
|
if (stat (name, &fdesc.stat) == 0)
|
2008-03-30 04:31:32 +02:00
|
|
|
break;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (errno == ENOENT && flags&O_CREAT)
|
|
|
|
|
{
|
2008-04-01 22:32:35 +02:00
|
|
|
INFO (filedescriptor, "try creating file: %s %d", name, PATH_MAX);
|
|
|
|
|
char* dir = lumiera_tmpbuf_strndup (name, PATH_MAX);
|
|
|
|
|
char* slash = dir;
|
2008-03-30 04:31:32 +02:00
|
|
|
|
|
|
|
|
while ((slash = strchr (slash+1, '/')))
|
|
|
|
|
{
|
|
|
|
|
*slash = '\0';
|
2008-04-01 22:32:35 +02:00
|
|
|
INFO (filedescriptor, "try creating dir: %s", dir);
|
|
|
|
|
if (mkdir (dir, 0777) == -1)
|
2008-03-30 04:31:32 +02:00
|
|
|
{
|
|
|
|
|
LUMIERA_ERROR_SET (filedescriptor, ERRNO);
|
|
|
|
|
goto efile;
|
|
|
|
|
}
|
|
|
|
|
*slash = '/';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int fd;
|
2008-04-07 06:47:57 +02:00
|
|
|
INFO (filedescriptor, "try creating file: %s", name);
|
2008-03-30 04:31:32 +02:00
|
|
|
fd = creat (name, 0777);
|
|
|
|
|
if (fd == -1)
|
|
|
|
|
{
|
|
|
|
|
LUMIERA_ERROR_SET (filedescriptor, ERRNO);
|
|
|
|
|
goto efile;
|
|
|
|
|
}
|
|
|
|
|
close (fd);
|
2008-04-01 22:32:35 +02:00
|
|
|
continue; /* will now retry the fstat once */
|
2008-03-30 04:31:32 +02:00
|
|
|
}
|
|
|
|
|
}
|
2008-04-01 22:32:35 +02:00
|
|
|
/* finally, no luck */
|
|
|
|
|
LUMIERA_ERROR_SET (filedescriptor, ERRNO);
|
|
|
|
|
goto efile;
|
2008-03-30 04:31:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* lookup/create descriptor */
|
2008-04-01 22:32:35 +02:00
|
|
|
LumieraFiledescriptor dest = &fdesc;
|
|
|
|
|
LumieraFiledescriptor* found = cuckoo_find (registry, &dest);
|
2008-03-30 04:31:32 +02:00
|
|
|
|
2008-04-01 22:32:35 +02:00
|
|
|
if (!found)
|
2008-03-30 04:31:32 +02:00
|
|
|
{
|
|
|
|
|
TRACE (filedescriptor, "Descriptor not found");
|
|
|
|
|
|
2008-04-01 22:32:35 +02:00
|
|
|
dest = lumiera_filedescriptor_new (&fdesc);
|
|
|
|
|
if (!dest)
|
2008-03-30 04:31:32 +02:00
|
|
|
goto ecreate;
|
|
|
|
|
|
2008-04-01 22:32:35 +02:00
|
|
|
cuckoo_insert (registry, &dest);
|
2008-03-30 04:31:32 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2008-04-01 22:32:35 +02:00
|
|
|
TRACE (filedescriptor, "Descriptor already existing");
|
|
|
|
|
dest = *found;
|
|
|
|
|
++dest->refcount;
|
2008-03-30 04:31:32 +02:00
|
|
|
}
|
|
|
|
|
|
2008-04-01 22:32:35 +02:00
|
|
|
lumiera_mutexacquirer_unlock (®istry_lock);
|
|
|
|
|
return dest;
|
2008-03-30 04:31:32 +02:00
|
|
|
|
|
|
|
|
efile:
|
|
|
|
|
ecreate:
|
|
|
|
|
lumiera_mutexacquirer_unlock (®istry_lock);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2008-04-01 22:32:35 +02:00
|
|
|
void
|
|
|
|
|
lumiera_filedescriptor_release (LumieraFiledescriptor self)
|
|
|
|
|
{
|
|
|
|
|
TRACE (filedescriptor);
|
|
|
|
|
if (!--self->refcount)
|
|
|
|
|
lumiera_filedescriptor_delete (self);
|
|
|
|
|
}
|
2008-03-30 04:31:32 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Allocate a new filedescriptor cloned from a template
|
|
|
|
|
* @param template the source filedescriptor
|
|
|
|
|
* @return the constrccted filedescriptor
|
|
|
|
|
*/
|
|
|
|
|
LumieraFiledescriptor
|
|
|
|
|
lumiera_filedescriptor_new (LumieraFiledescriptor template)
|
|
|
|
|
{
|
2008-04-01 22:32:35 +02:00
|
|
|
LumieraFiledescriptor self = lumiera_malloc (sizeof (lumiera_filedescriptor));
|
|
|
|
|
TRACE (filedescriptor, "at %p", self);
|
|
|
|
|
|
|
|
|
|
self->stat = template->stat;
|
|
|
|
|
|
|
|
|
|
self->flags = template->flags;
|
|
|
|
|
lumiera_mutex_init (&self->lock);
|
|
|
|
|
self->reopened = 0;
|
|
|
|
|
self->refcount = 1;
|
2008-04-04 08:01:59 +02:00
|
|
|
self->handle = 0;
|
|
|
|
|
|
2008-04-07 05:51:32 +02:00
|
|
|
const char* type = "mutex";
|
|
|
|
|
const char* name = "filedescriptor";
|
|
|
|
|
|
|
|
|
|
RESOURCE_ANNOUNCE (filedescriptor, type, name, self, self->rh);
|
2008-04-01 22:32:35 +02:00
|
|
|
|
|
|
|
|
return self;
|
2008-03-30 04:31:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* delete a filedescriptor and free its resources
|
|
|
|
|
* @param descriptor filedescriptor to be freed
|
|
|
|
|
*/
|
|
|
|
|
void
|
2008-04-01 22:32:35 +02:00
|
|
|
lumiera_filedescriptor_delete (LumieraFiledescriptor self)
|
2008-03-30 04:31:32 +02:00
|
|
|
{
|
2008-04-01 22:32:35 +02:00
|
|
|
TRACE (filedescriptor, "%p", self);
|
|
|
|
|
lumiera_mutexacquirer registry_lock;
|
|
|
|
|
lumiera_mutexacquirer_init_mutex (®istry_lock, ®istry_mutex, LUMIERA_LOCKED);
|
|
|
|
|
|
|
|
|
|
REQUIRE (self->refcount == 0);
|
|
|
|
|
|
2008-04-04 08:01:59 +02:00
|
|
|
RESOURCE_FORGET (filedescriptor, self->rh);
|
|
|
|
|
|
2008-04-01 22:32:35 +02:00
|
|
|
cuckoo_remove (registry, cuckoo_find (registry, &self));
|
|
|
|
|
|
|
|
|
|
TODO ("destruct other members (WIP)");
|
|
|
|
|
|
2008-04-04 08:01:59 +02:00
|
|
|
|
|
|
|
|
TODO ("release filehandle");
|
|
|
|
|
|
2008-04-01 22:32:35 +02:00
|
|
|
lumiera_mutex_destroy (&self->lock);
|
|
|
|
|
free (self);
|
|
|
|
|
|
|
|
|
|
lumiera_mutexacquirer_unlock (®istry_lock);
|
2008-03-30 04:31:32 +02:00
|
|
|
}
|