You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by je...@apache.org on 2020/09/22 08:30:46 UTC

[incubator-nuttx] branch master updated: libc: Implement access function correctly

This is an automated email from the ASF dual-hosted git repository.

jerpelea pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new f4794f0  libc: Implement access function correctly
f4794f0 is described below

commit f4794f0b484ea7f0f680fafa2cdb649e11065a43
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Thu Sep 10 12:10:52 2020 +0800

    libc: Implement access function correctly
    
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
    Change-Id: I6ae3abf79bd9aa8cfb54b8bbe302d69c4d9cb8ff
---
 libs/libc/unistd/lib_access.c | 30 +++++++++++++++++++++++++++---
 1 file changed, 27 insertions(+), 3 deletions(-)

diff --git a/libs/libc/unistd/lib_access.c b/libs/libc/unistd/lib_access.c
index e06f29b..42b6bd2 100644
--- a/libs/libc/unistd/lib_access.c
+++ b/libs/libc/unistd/lib_access.c
@@ -39,6 +39,7 @@
 
 #include <nuttx/config.h>
 
+#include <sys/stat.h>
 #include <unistd.h>
 
 /****************************************************************************
@@ -71,8 +72,8 @@
  * Description:
  *   The access() function shall check the file named by the pathname pointed
  *   to by the path argument for accessibility according to the bit pattern
- *   contained in amode, using the real user ID in place of the effective user
- *   ID and the real group ID in place of the effective group ID.
+ *   contained in amode, using the real user ID in place of the effective
+ *   user ID and the real group ID in place of the effective group ID.
  *   As there are no users in NuttX, the function always succeeds.
  *
  * Input Parameters:
@@ -80,7 +81,8 @@
  *   amode - the access mode
  *
  * Returned Value:
- *   Always OK (zero)
+ *   Return OK (zero) if the caller can access the file with the required
+ *   permission, otherwise return -1.
  *
  * Assumptions:
  *
@@ -88,5 +90,27 @@
 
 int access(FAR const char *path, int amode)
 {
+  struct stat s;
+
+  if (stat(path, &s))
+    {
+      return -1;
+    }
+
+  if (s.st_mode & S_IFDIR)
+    {
+      return 0;
+    }
+
+  if (amode & W_OK)
+    {
+      if (s.st_mode & S_IWUSR)
+        {
+          return 0;
+        }
+
+      return -1;
+    }
+
   return 0;
 }