diff --git a/fs/shm/shmfs.c b/fs/shm/shmfs.c index 8e634e0005..1adf884033 100644 --- a/fs/shm/shmfs.c +++ b/fs/shm/shmfs.c @@ -89,7 +89,45 @@ const struct file_operations g_shmfs_operations = static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer, 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, 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; } /****************************************************************************