From 3b7471686210351071b191b43f7cd288e331d55b Mon Sep 17 00:00:00 2001 From: patacongo Date: Sat, 31 Mar 2012 15:13:12 +0000 Subject: [PATCH] Fix read() return value for the case of permissions problem git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@4545 42af7a65-404d-4744-a932-0658087f49c3 --- fs/fs_read.c | 44 ++++++++++++++++++++---------------- sched/sched_setupidlefiles.c | 2 +- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/fs/fs_read.c b/fs/fs_read.c index 8afa6563793..045c81f59f9 100644 --- a/fs/fs_read.c +++ b/fs/fs_read.c @@ -1,8 +1,8 @@ /**************************************************************************** * fs_read.c * - * Copyright (C) 2007-2009 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt + * Copyright (C) 2007-2009, 2012 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -63,35 +63,39 @@ static inline ssize_t file_read(int fd, FAR void *buf, size_t nbytes) list = sched_getfiles(); if (!list) { - errno = EMFILE; - return ERROR; + /* Failed to get the file list */ + + ret = -EMFILE; } /* Were we given a valid file descriptor? */ - if ((unsigned int)fd < CONFIG_NFILE_DESCRIPTORS) + else if ((unsigned int)fd < CONFIG_NFILE_DESCRIPTORS) { FAR struct file *this_file = &list->fl_files[fd]; + FAR struct inode *inode = this_file->f_inode; - /* Was this file opened for read access? */ + /* Yes.. Was this file opened for read access? */ - if ((this_file->f_oflags & O_RDOK) != 0) + if ((this_file->f_oflags & O_RDOK) == 0) { - struct inode *inode = this_file->f_inode; + /* No.. File is not read-able */ - /* Is a driver or mountpoint registered? If so, does it support - * the read method? + ret = -EACCES; + } + + /* Is a driver or mountpoint registered? If so, does it support + * the read method? + */ + + else if (inode && inode->u.i_ops && inode->u.i_ops->read) + { + /* Yes.. then let it perform the read. NOTE that for the case + * of the mountpoint, we depend on the read methods bing + * identical in signature and position in the operations vtable. */ - if (inode && inode->u.i_ops && inode->u.i_ops->read) - { - /* Yes, then let it perform the read. NOTE that for the case - * of the mountpoint, we depend on the read methods bing - * identical in signature and position in the operations vtable. - */ - - ret = (int)inode->u.i_ops->read(this_file, (char*)buf, (size_t)nbytes); - } + ret = (int)inode->u.i_ops->read(this_file, (char*)buf, (size_t)nbytes); } } @@ -99,7 +103,7 @@ static inline ssize_t file_read(int fd, FAR void *buf, size_t nbytes) if (ret < 0) { - errno = -ret; + set_errno(-ret); return ERROR; } diff --git a/sched/sched_setupidlefiles.c b/sched/sched_setupidlefiles.c index 7b283968525..f2bfb78d2be 100644 --- a/sched/sched_setupidlefiles.c +++ b/sched/sched_setupidlefiles.c @@ -123,7 +123,7 @@ int sched_setupidlefiles(FAR _TCB *tcb) * it and got some file descriptor other than 0. */ - if (fd >- 0) + if (fd > 0) { slldbg("Open /dev/console fd: %d\n", fd); (void)close(fd);