Add comments and documentation, rework open mode logic
This commit is contained in:
@@ -3,6 +3,9 @@
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
static const bh_io_table_t bh_file_table = {
|
||||
(int (*)(struct bh_io_s *, int)) bh_file_open_base,
|
||||
@@ -88,23 +91,33 @@ int bh_file_open_base(bh_file_t *file,
|
||||
return BH_INVALID;
|
||||
|
||||
/* Determine open mode */
|
||||
switch (mode & BH_IO_MASK)
|
||||
switch (mode & (BH_IO_CREATE | BH_IO_OPEN))
|
||||
{
|
||||
case BH_IO_OPEN: break;
|
||||
case BH_IO_CREATE: open_flags |= O_CREAT | O_EXCL; break;
|
||||
case BH_IO_APPEND: open_flags |= O_CREAT | O_APPEND; break;
|
||||
case BH_IO_TRUNCATE: open_flags |= O_CREAT | O_TRUNC; break;
|
||||
|
||||
default:
|
||||
return BH_NO_IMPL;
|
||||
case 0: open_flags |= O_CREAT; break;
|
||||
case BH_IO_CREATE: open_flags |= O_CREAT | O_EXCL; break;
|
||||
case BH_IO_OPEN: /* Do nothing */ break;
|
||||
}
|
||||
|
||||
if (mode & BH_IO_APPEND)
|
||||
open_flags |= O_APPEND;
|
||||
|
||||
if (mode & BH_IO_TRUNCATE)
|
||||
open_flags |= O_TRUNC;
|
||||
|
||||
/* Open file */
|
||||
file->handle = open(file->path, open_flags, open_mode);
|
||||
|
||||
/* Check for errors */
|
||||
if (file->handle == -1)
|
||||
return BH_ERROR;
|
||||
{
|
||||
switch (errno)
|
||||
{
|
||||
case EEXIST: return BH_FOUND;
|
||||
case ENOENT: return BH_NOT_FOUND;
|
||||
default: return BH_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
return BH_OK;
|
||||
}
|
||||
@@ -210,7 +223,7 @@ int bh_file_seek_base(bh_file_t *file,
|
||||
return BH_ERROR;
|
||||
|
||||
/* Seek to desired location */
|
||||
if (lseek(file->handle, pos, dir) == -1);
|
||||
if (lseek(file->handle, pos, dir) == -1)
|
||||
return BH_ERROR;
|
||||
|
||||
return BH_OK;
|
||||
|
||||
Reference in New Issue
Block a user