fs/shmfs: implement read and write interface

To pass LTP posix case mmap/1-2.c

Signed-off-by: fangxinyong <fangxinyong@xiaomi.com>
This commit is contained in:
fangxinyong 2023-07-24 22:01:03 +08:00 committed by Xiang Xiao
parent ea52220509
commit 1adef07a79

View File

@ -89,7 +89,45 @@ const struct file_operations g_shmfs_operations =
static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer, static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
size_t buflen) size_t buflen)
{ {
return -ENOSYS; FAR struct shmfs_object_s *sho;
ssize_t nread;
off_t startpos;
off_t endpos;
DEBUGASSERT(filep->f_inode != NULL && filep->f_inode->i_private != NULL);
sho = filep->f_inode->i_private;
if (filep->f_pos > sho->length)
{
return 0;
}
/* Handle attempts to read beyond the end of the file. */
startpos = filep->f_pos;
nread = buflen;
endpos = startpos + buflen;
if (endpos > sho->length)
{
endpos = sho->length;
nread = endpos - startpos;
}
/* Copy data from the memory object to the user buffer */
if (sho->paddr != NULL)
{
memcpy(buffer, sho->paddr + startpos, nread);
filep->f_pos += nread;
}
else
{
DEBUGASSERT(sho->length == 0 && nread == 0);
}
return nread;
} }
/**************************************************************************** /****************************************************************************
@ -99,7 +137,41 @@ static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer, static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
size_t buflen) size_t buflen)
{ {
return -ENOSYS; FAR struct shmfs_object_s *sho;
ssize_t nwritten;
off_t startpos;
off_t endpos;
DEBUGASSERT(filep->f_inode != NULL && filep->f_inode->i_private != NULL);
sho = filep->f_inode->i_private;
/* Handle attempts to write beyond the end of the file */
startpos = filep->f_pos;
nwritten = buflen;
endpos = startpos + buflen;
/* Desn't support shm auto expand, truncate first */
if (endpos > sho->length)
{
return -EFBIG;
}
/* Copy data from the user buffer to the memory object */
if (sho->paddr != NULL)
{
memcpy(sho->paddr + startpos, buffer, nwritten);
filep->f_pos += nwritten;
}
else
{
DEBUGASSERT(sho->length == 0 && nwritten == 0);
}
return nwritten;
} }
/**************************************************************************** /****************************************************************************