Proper flag and stat handling for files

Filedescriptors create files at construction, each later access becomes
a reopen on the now existing file.

file_handle_acquire() reopens the file to gain a handle when required
and checks that the file is really the one we expected (ino and dev num)
This commit is contained in:
Christian Thaeter 2008-04-10 06:03:21 +02:00
parent 00b9dff106
commit c4816c39fd
2 changed files with 28 additions and 4 deletions

View file

@ -27,9 +27,13 @@
#include "backend/filedescriptor.h"
#include <limits.h>
#include <unistd.h>
NOBUG_DEFINE_FLAG_PARENT (file, file_all);
LUMIERA_ERROR_DEFINE(FILE_CHANGED, "File changed unexpected");
LumieraFile
lumiera_file_init (LumieraFile self, const char* name, int flags)
{
@ -85,12 +89,31 @@ lumiera_file_handle_acquire (LumieraFile self)
if (self->descriptor->handle->fd == -1)
{
TODO ("stat handling/update");
self->descriptor->handle->fd = open (self->name, self->descriptor->flags);
if (self->descriptor->handle->fd == -1)
int fd;
fd = open (self->name, self->descriptor->flags & ~(O_EXCL|O_TRUNC|O_CREAT));
if (fd == -1)
{
LUMIERA_ERROR_SET (file, ERRNO);
return -1;
}
else
{
struct stat st;
if (fstat (fd, &st) == -1)
{
close (fd);
LUMIERA_ERROR_SET (file, ERRNO);
return -1;
}
else if (self->descriptor->stat.st_dev != st.st_dev || self->descriptor->stat.st_ino != st.st_ino)
{
close (fd);
/* Woops this is not the file we expected to use */
LUMIERA_ERROR_SET (file, FILE_CHANGED);
return -1;
}
}
self->descriptor->handle->fd = fd;
}
}

View file

@ -29,6 +29,8 @@
NOBUG_DECLARE_FLAG (file);
LUMIERA_ERROR_DECLARE(FILE_CHANGED);
/**
* @file
* File management
@ -48,7 +50,6 @@ typedef lumiera_file* LumieraFile;
#include "backend/filehandle.h"
#include "backend/filedescriptor.h"
#define LUMIERA_FILE_READONLY (O_RDONLY | O_LARGEFILE | O_NOATIME)
#define LUMIERA_FILE_READWRITE (O_RDWR | O_LARGEFILE | O_NOATIME)
#define LUMIERA_FILE_CREATE (O_RDWR | O_LARGEFILE | O_NOATIME | O_CREAT | O_EXCL)