summaryrefslogtreecommitdiff
path: root/nuttx/fs
diff options
context:
space:
mode:
authorpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2009-08-15 15:57:15 +0000
committerpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2009-08-15 15:57:15 +0000
commitcaa1841f02bb351aa368db8f1877db0812b47920 (patch)
tree4ab4836945bac0543dbd8894bb16f79690baf54e /nuttx/fs
parentc961699454ec2b7e1496e3feda83892b3ec0a509 (diff)
downloadpx4-nuttx-caa1841f02bb351aa368db8f1877db0812b47920.tar.gz
px4-nuttx-caa1841f02bb351aa368db8f1877db0812b47920.tar.bz2
px4-nuttx-caa1841f02bb351aa368db8f1877db0812b47920.zip
Read from socket is the same as recv with flags==0
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@2017 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'nuttx/fs')
-rw-r--r--nuttx/fs/Makefile2
-rw-r--r--nuttx/fs/fs_read.c43
-rw-r--r--nuttx/fs/fs_write.c131
3 files changed, 110 insertions, 66 deletions
diff --git a/nuttx/fs/Makefile b/nuttx/fs/Makefile
index 5c1f037af..d6301ab99 100644
--- a/nuttx/fs/Makefile
+++ b/nuttx/fs/Makefile
@@ -41,7 +41,7 @@ AOBJS = $(ASRCS:.S=$(OBJEXT))
CSRCS =
ifeq ($(CONFIG_NFILE_DESCRIPTORS),0)
ifneq ($(CONFIG_NSOCKET_DESCRIPTORS),0)
-CSRCS += fs_close.c fs_write.c fs_ioctl.c fs_poll.c fs_select.c
+CSRCS += fs_close.c fs_read.c fs_write.c fs_ioctl.c fs_poll.c fs_select.c
endif
else
CSRCS += fs_open.c fs_close.c fs_read.c fs_write.c fs_ioctl.c \
diff --git a/nuttx/fs/fs_read.c b/nuttx/fs/fs_read.c
index 6deb674b1..8afa65637 100644
--- a/nuttx/fs/fs_read.c
+++ b/nuttx/fs/fs_read.c
@@ -1,7 +1,7 @@
/****************************************************************************
* fs_read.c
*
- * Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved.
+ * Copyright (C) 2007-2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
@@ -39,17 +39,21 @@
#include <nuttx/config.h>
#include <sys/types.h>
+#include <sys/socket.h>
+
#include <unistd.h>
#include <fcntl.h>
#include <sched.h>
#include <errno.h>
+
#include "fs_internal.h"
/****************************************************************************
- * Global Functions
+ * Private Functions
****************************************************************************/
-ssize_t read(int fd, FAR void *buf, size_t nbytes)
+#if CONFIG_NFILE_DESCRIPTORS > 0
+static inline ssize_t file_read(int fd, FAR void *buf, size_t nbytes)
{
FAR struct filelist *list;
int ret = -EBADF;
@@ -103,4 +107,37 @@ ssize_t read(int fd, FAR void *buf, size_t nbytes)
return ret;
}
+#endif
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+ssize_t read(int fd, FAR void *buf, size_t nbytes)
+{
+ /* Did we get a valid file descriptor? */
+#if CONFIG_NFILE_DESCRIPTORS > 0
+ if ((unsigned int)fd >= CONFIG_NFILE_DESCRIPTORS)
+#endif
+ {
+ /* No.. If networking is enabled, read() is the same as recv() with
+ * the flags parameter set to zero.
+ */
+
+#if defined(CONFIG_NET) && CONFIG_NSOCKET_DESCRIPTORS > 0
+ return recv(fd, buf, nbytes, 0);
+#else
+ /* No networking... it is a bad descriptor in any event */
+
+ errno = EBADF;
+ return ERROR;
+#endif
+ }
+
+ /* The descriptor is in a valid range to file descriptor... do the read */
+
+#if CONFIG_NFILE_DESCRIPTORS > 0
+ return file_read(fd, buf, nbytes);
+#endif
+}
diff --git a/nuttx/fs/fs_write.c b/nuttx/fs/fs_write.c
index b7e57eb72..2912e8ed5 100644
--- a/nuttx/fs/fs_write.c
+++ b/nuttx/fs/fs_write.c
@@ -1,7 +1,7 @@
/****************************************************************************
* fs/fs_write.c
*
- * Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved.
+ * Copyright (C) 2007-2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
@@ -51,7 +51,64 @@
#include "fs_internal.h"
/****************************************************************************
- * Global Functions
+ * Private Functions
+ ****************************************************************************/
+
+#if CONFIG_NFILE_DESCRIPTORS > 0
+static inline ssize_t file_write(int fd, FAR const void *buf, size_t nbytes)
+{
+ FAR struct filelist *list;
+ FAR struct file *this_file;
+ FAR struct inode *inode;
+ int ret;
+ int err;
+
+ /* Get the thread-specific file list */
+
+ list = sched_getfiles();
+ if (!list)
+ {
+ err = EMFILE;
+ goto errout;
+ }
+
+ /* Was this file opened for write access? */
+
+ this_file = &list->fl_files[fd];
+ if ((this_file->f_oflags & O_WROK) == 0)
+ {
+ err = EBADF;
+ goto errout;
+ }
+
+ /* Is a driver registered? Does it support the write method? */
+
+ inode = this_file->f_inode;
+ if (!inode || !inode->u.i_ops || !inode->u.i_ops->write)
+ {
+ err = EBADF;
+ goto errout;
+ }
+
+ /* Yes, then let the driver perform the write */
+
+ ret = inode->u.i_ops->write(this_file, buf, nbytes);
+ if (ret < 0)
+ {
+ err = -ret;
+ goto errout;
+ }
+
+ return ret;
+
+errout:
+ *get_errno_ptr() = err;
+ return ERROR;
+}
+#endif
+
+/****************************************************************************
+ * Public Functions
****************************************************************************/
/***************************************************************************
@@ -68,7 +125,7 @@
*
* Returned Value:
* On success, the number of bytes written are returned (zero indicates
- * nothing was written). On error, -1 is returned, and errno is set appro‐
+ * nothing was written). On error, -1 is returned, and errno is set appro-
* priately:
*
* EAGAIN
@@ -106,76 +163,26 @@
ssize_t write(int fd, FAR const void *buf, size_t nbytes)
{
-#if CONFIG_NFILE_DESCRIPTORS > 0
- FAR struct filelist *list;
- FAR struct file *this_file;
- FAR struct inode *inode;
- int ret;
-#endif
- int err;
-
/* Did we get a valid file descriptor? */
+#if CONFIG_NFILE_DESCRIPTORS > 0
if ((unsigned int)fd >= CONFIG_NFILE_DESCRIPTORS)
+#endif
{
/* Write to a socket descriptor is equivalent to send with flags == 0 */
#if defined(CONFIG_NET_TCP) && CONFIG_NSOCKET_DESCRIPTORS > 0
- if ((unsigned int)fd < (CONFIG_NFILE_DESCRIPTORS+CONFIG_NSOCKET_DESCRIPTORS))
- {
- return send(fd, buf, nbytes, 0);
- }
- else
+ return send(fd, buf, nbytes, 0);
+#else
+ errno = EBADF;
+ return ERROR;
#endif
- {
- err = EBADF;
- goto errout;
- }
- }
-
-#if CONFIG_NFILE_DESCRIPTORS > 0
-
- /* Get the thread-specific file list */
-
- list = sched_getfiles();
- if (!list)
- {
- err = EMFILE;
- goto errout;
- }
-
- /* Was this file opened for write access? */
-
- this_file = &list->fl_files[fd];
- if ((this_file->f_oflags & O_WROK) == 0)
- {
- err = EBADF;
- goto errout;
- }
-
- /* Is a driver registered? Does it support the write method? */
-
- inode = this_file->f_inode;
- if (!inode || !inode->u.i_ops || !inode->u.i_ops->write)
- {
- err = EBADF;
- goto errout;
}
- /* Yes, then let the driver perform the write */
+ /* The descriptor is in the right range to be a file descriptor... write to the file */
- ret = inode->u.i_ops->write(this_file, buf, nbytes);
- if (ret < 0)
- {
- err = -ret;
- goto errout;
- }
-
- return ret;
+#if CONFIG_NFILE_DESCRIPTORS > 0
+ return file_write(fd, buf, nbytes);
#endif
-
-errout:
- *get_errno_ptr() = err;
- return ERROR;
}