Major API change:

1. Introduced a cfs_offset_t type that designates file offsets. unsigned int
was insufficient on several platforms with non-volatile storage that exceed
the capacity of unsigned int.

2. Switched cfs_seek signature to POSIX style with a "whence" parameter.
This commit is contained in:
nvt-se 2009-02-27 14:25:37 +00:00
parent 5e038640e2
commit 3191a2568c
6 changed files with 78 additions and 44 deletions

View File

@ -67,7 +67,7 @@
#define COFFEE_FILE_MODIFIED 0x1
#define INVALID_PAGE ((coffee_page_t)-1)
#define UNKNOWN_OFFSET ((coffee_offset_t)-1)
#define UNKNOWN_OFFSET ((cfs_offset_t)-1)
#define FD_VALID(fd) \
((fd) >= 0 && (fd) < COFFEE_FD_SET_SIZE && \
@ -111,7 +111,7 @@ struct sector_stats {
};
struct file {
coffee_offset_t end;
cfs_offset_t end;
coffee_page_t page;
coffee_page_t max_pages;
int16_t next_log_record;
@ -120,7 +120,7 @@ struct file {
};
struct file_desc {
coffee_offset_t offset;
cfs_offset_t offset;
struct file *file;
uint8_t flags;
};
@ -137,7 +137,7 @@ struct file_header {
/* This is needed because of a buggy compiler. */
struct log_param {
coffee_offset_t offset;
cfs_offset_t offset;
const char *buf;
uint16_t size;
};
@ -170,8 +170,8 @@ read_header(struct file_header *hdr, coffee_page_t page)
#endif
}
/*---------------------------------------------------------------------------*/
static coffee_offset_t
absolute_offset(coffee_page_t page, coffee_offset_t offset)
static cfs_offset_t
absolute_offset(coffee_page_t page, cfs_offset_t offset)
{
return page * COFFEE_PAGE_SIZE + sizeof(struct file_header) + offset;
}
@ -390,7 +390,7 @@ refresh_eof_hint(struct file *file)
}
#endif /* COFFEE_CONF_EOF_HINT */
/*---------------------------------------------------------------------------*/
static coffee_offset_t
static cfs_offset_t
file_end(coffee_page_t start)
{
struct file_header hdr;
@ -519,7 +519,7 @@ remove_by_page(coffee_page_t page, int remove_log, int close_fds)
}
/*---------------------------------------------------------------------------*/
static coffee_page_t
page_count(coffee_offset_t size)
page_count(cfs_offset_t size)
{
return (size + sizeof(struct file_header) + COFFEE_PAGE_SIZE - 1) /
COFFEE_PAGE_SIZE;
@ -579,7 +579,7 @@ adjust_log_config(struct file_header *hdr,
/*---------------------------------------------------------------------------*/
static uint16_t
modify_log_buffer(uint16_t log_record_size,
coffee_offset_t *offset, uint16_t *size)
cfs_offset_t *offset, uint16_t *size)
{
uint16_t region;
@ -595,7 +595,7 @@ static int
get_record_index(coffee_page_t log_page, uint16_t search_records,
uint16_t region)
{
coffee_offset_t base;
cfs_offset_t base;
uint16_t processed;
uint16_t batch_size;
int16_t match_index, i;
@ -641,7 +641,7 @@ read_log_page(struct file_header *hdr, int16_t last_record, struct log_param *lp
int16_t match_index;
uint16_t log_record_size;
uint16_t log_records;
coffee_offset_t base;
cfs_offset_t base;
uint16_t search_records;
adjust_log_config(hdr, &log_record_size, &log_records);
@ -654,7 +654,7 @@ read_log_page(struct file_header *hdr, int16_t last_record, struct log_param *lp
}
base = absolute_offset(hdr->log_page, log_records * sizeof(region));
base += (coffee_offset_t)match_index * log_record_size;
base += (cfs_offset_t)match_index * log_record_size;
base += lp->offset;
COFFEE_READ(lp->buf, lp->size, base);
@ -683,7 +683,7 @@ create_log(struct file *file, struct file_header *hdr)
coffee_page_t log_page;
unsigned char log_name[sizeof(hdr->name)];
uint16_t log_record_size, log_records;
coffee_offset_t size;
cfs_offset_t size;
struct file *log_file;
adjust_log_config(hdr, &log_record_size, &log_records);
@ -715,7 +715,7 @@ merge_log(coffee_page_t file_page, int extend)
coffee_page_t log_page;
struct file_header hdr, hdr2;
int fd, n;
coffee_offset_t offset;
cfs_offset_t offset;
coffee_page_t max_pages;
struct file *new_file;
int i;
@ -834,7 +834,7 @@ write_log_page(struct file *file, struct log_param *lp)
int16_t log_record;
uint16_t log_record_size;
uint16_t log_records;
coffee_offset_t table_base, record_base;
cfs_offset_t table_base, record_base;
struct log_param lp_out;
read_header(&hdr, file->page);
@ -958,7 +958,7 @@ cfs_close(int fd)
}
/*---------------------------------------------------------------------------*/
unsigned
cfs_seek(int fd, unsigned offset)
cfs_seek(int fd, unsigned offset, int whence)
{
struct file_header hdr;
struct file_desc *fdp;
@ -975,11 +975,21 @@ cfs_seek(int fd, unsigned offset)
return -1;
}
if(whence == CFS_SEEK_SET) {
fdp->offset = offset;
} else if(whence == CFS_SEEK_END) {
fdp->offset = fdp->file->end + offset;
} else if(whence == CFS_SEEK_CUR) {
fdp->offset += offset;
} else {
return (cfs_offset_t)-1;
}
if(fdp->file->end < offset) {
fdp->file->end = offset;
}
return fdp->offset = offset;
return fdp->offset;
}
/*---------------------------------------------------------------------------*/
int
@ -1010,7 +1020,7 @@ cfs_read(int fd, void *buf, unsigned size)
struct file *file;
unsigned remains, read_chunk;
int r;
coffee_offset_t base, offset;
cfs_offset_t base, offset;
struct log_param lp;
if(!(FD_VALID(fd) && FD_READABLE(fd))) {
@ -1067,7 +1077,7 @@ cfs_write(int fd, const void *buf, unsigned size)
struct file *file;
int i;
struct log_param lp;
coffee_offset_t remains;
cfs_offset_t remains;
if(!(FD_VALID(fd) && FD_WRITABLE(fd))) {
return -1;

View File

@ -30,7 +30,7 @@
*
* Author: Adam Dunkels <adam@sics.se>
*
* $Id: cfs-eeprom.c,v 1.8 2008/11/24 10:56:55 nvt-se Exp $
* $Id: cfs-eeprom.c,v 1.9 2009/02/27 14:25:38 nvt-se Exp $
*/
#include "cfs/cfs.h"
@ -106,9 +106,9 @@ cfs_write(int f, const void *buf, unsigned int len)
}
/*---------------------------------------------------------------------------*/
unsigned int
cfs_seek(int f, unsigned int o)
cfs_seek(int f, unsigned int o, int w)
{
if(f == 1) {
if(w == CFS_SEEK_SET && f == 1) {
file.fileptr = o;
return o;
} else {

View File

@ -30,7 +30,7 @@
*
* Author: Adam Dunkels <adam@sics.se>
*
* $Id: cfs-posix.c,v 1.12 2008/11/24 10:56:55 nvt-se Exp $
* $Id: cfs-posix.c,v 1.13 2009/02/27 14:25:38 nvt-se Exp $
*/
#include <stdio.h>
@ -84,10 +84,19 @@ cfs_write(int f, const void *b, unsigned int l)
return write(f, b, l);
}
/*---------------------------------------------------------------------------*/
unsigned int
cfs_seek(int f, unsigned int o)
cfs_offset_t
cfs_seek(int f, unsigned int o, int w)
{
return lseek(f, o, SEEK_SET);
if(w == CFS_SEEK_SET) {
w = SEEK_SET;
} else if(w == CFS_SEEK_CUR) {
w = SEEK_CUR;
} else if(w == CFS_SEEK_END) {
w = SEEK_END;
} else {
return (cfs_offset_t)-1;
}
return lseek(f, o, w);
}
/*---------------------------------------------------------------------------*/
int

View File

@ -30,7 +30,7 @@
*
* Author: Adam Dunkels <adam@sics.se>
*
* $Id: cfs-ram.c,v 1.9 2008/11/24 10:56:55 nvt-se Exp $
* $Id: cfs-ram.c,v 1.10 2009/02/27 14:25:38 nvt-se Exp $
*/
#include <string.h>
@ -127,18 +127,17 @@ cfs_write(int f, const void *buf, unsigned int len)
}
}
/*---------------------------------------------------------------------------*/
unsigned int
cfs_seek(int f, unsigned int o)
cfs_offset_t
cfs_seek(int f, cfs_offset_t o, int w)
{
if(f == 1) {
if(w == CFS_SEEK_SET && f == 1) {
if(o > file.filesize) {
o = file.filesize;
}
file.fileptr = o;
return o;
} else {
return -1;
}
return (cfs_offset_t)-1;
}
/*---------------------------------------------------------------------------*/
int

View File

@ -30,7 +30,7 @@
*
* Author: Adam Dunkels <adam@sics.se>
*
* $Id: cfs-xmem.c,v 1.10 2008/11/30 22:42:40 nvt-se Exp $
* $Id: cfs-xmem.c,v 1.11 2009/02/27 14:25:38 nvt-se Exp $
*/
#include "cfs/cfs.h"
@ -133,18 +133,17 @@ cfs_write(int f, const void *buf, unsigned int len)
}
}
/*---------------------------------------------------------------------------*/
unsigned int
cfs_seek(int f, unsigned int o)
cfs_offset_t
cfs_seek(int f, cfs_offset_t o, int w)
{
if(f == 1) {
if(w == CFS_SEEK_SET && f == 1) {
if(o > file.filesize) {
o = file.filesize;
}
file.fileptr = o;
return o;
} else {
return -1;
}
return -1;
}
/*---------------------------------------------------------------------------*/
int

View File

@ -54,7 +54,7 @@
*
* Author: Adam Dunkels <adam@sics.se>
*
* $Id: cfs.h,v 1.12 2008/11/24 10:56:55 nvt-se Exp $
* $Id: cfs.h,v 1.13 2009/02/27 14:25:38 nvt-se Exp $
*/
#ifndef __CFS_H__
#define __CFS_H__
@ -70,6 +70,12 @@ struct cfs_dirent {
unsigned int size;
};
#ifndef CFS_OFFSET_TYPE
typedef unsigned cfs_offset_t;
#else
typedef CFS_OFFSET_TYPE cfs_offset_t;
#endif
/**
* Specify that cfs_open() should open a file for reading.
*
@ -178,16 +184,27 @@ CCIF int cfs_write(int fd, const void *buf, unsigned int len);
/**
* \brief Seek to a specified position in an open file.
* \param fd The file descriptor of the open file.
* \param offset The position in the file.
* \return The new position in the file.
* \param offset A position, either relative or absolute, in the file.
* \param whence Determines how to interpret the offset parameter.
* \return The new position in the file, or (cfs_offset_t)-1 if the seek failed.
*
* This function moves the file position to the specified
* position in the file. The next byte that is read from
* or written to the file will be at the position given by
* the offset parameter.
* or written to the file will be at the position given
* determined by the combination of the offset parameter
* and the whence parameter.
*
* If whence is CFS_SEEK_SET, the current position is set
* to the offset value. CFS_SEEK_CUR moves the position
* forward the number of bytes specified by offset. CFS_SEEK
* end moves the position offset bytes past the end of the file.
*/
#ifndef cfs_seek
CCIF unsigned int cfs_seek(int fd, unsigned int offset);
#define CFS_SEEK_SET 0
#define CFS_SEEK_CUR 1
#define CFS_SEEK_END 2
CCIF cfs_offset_t cfs_seek(int fd, cfs_offset_t offset, int whence);
#endif
/**