anchao commented on code in PR #10732:
URL: https://github.com/apache/nuttx/pull/10732#discussion_r1332366185


##########
drivers/mmcsd/mmcsd_procfs.c:
##########
@@ -0,0 +1,548 @@
+/****************************************************************************
+ * drivers/mmcsd/mmcsd_procfs.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <sys/stat.h>
+#include <sys/mount.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <string.h>
+#include <assert.h>
+#include <debug.h>
+#include <errno.h>
+
+#include <sys/param.h>
+
+#include <nuttx/nuttx.h>
+#include <nuttx/fs/fs.h>
+#include <nuttx/fs/procfs.h>
+#include <nuttx/kmalloc.h>
+
+#include "mmcsd.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Determines the size of an intermediate buffer that must be large enough
+ * to handle the longest line generated by this logic (plus a couple of
+ * bytes).
+ */
+
+#define MMCSD_LINELEN 512
+
+typedef CODE ssize_t (*mmcsd_read_t)(FAR struct file *filep,
+                                     FAR char *buffer, size_t buflen,
+                                     FAR struct mmcsd_state_s *priv);
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/* File system methods */
+
+static int     mmcsd_open(FAR struct file *filep, FAR const char *relpath,
+                          int oflags, mode_t mode);
+static int     mmcsd_close(FAR struct file *filep);
+static ssize_t mmcsd_read_cid(FAR struct file *filep, FAR char *buffer,
+                              size_t buflen, FAR struct mmcsd_state_s *priv);
+static ssize_t mmcsd_read_csd(FAR struct file *filep, FAR char *buffer,
+                              size_t buflen, FAR struct mmcsd_state_s *priv);
+static ssize_t mmcsd_read_type(FAR struct file *filep, FAR char *buffer,
+                              size_t buflen, FAR struct mmcsd_state_s *priv);
+static ssize_t mmcsd_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen);
+static int     mmcsd_dup(FAR const struct file *oldp,
+                         FAR struct file *newp);
+
+static int     mmcsd_opendir(FAR const char *relpath,
+                             FAR struct fs_dirent_s **dir);
+static int     mmcsd_closedir(FAR struct fs_dirent_s *dir);
+static int     mmcsd_readdir(FAR struct fs_dirent_s *dir,
+                             FAR struct dirent *entry);
+static int     mmcsd_rewinddir(FAR struct fs_dirent_s *dir);
+
+static int     mmcsd_stat(FAR const char *relpath, FAR struct stat *buf);
+
+static int     mmcsd_get_file_index(FAR const char *relpath);
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/* This structure describes one open "file" */
+
+struct mmcsd_file_s
+{
+  struct procfs_file_s base;      /* Base open file structure */
+  char line[MMCSD_LINELEN];       /* Pre-allocated buffer for formatted lines 
*/
+  int index;                      /* Device node index */
+  mmcsd_read_t read;              /* Read function */
+};
+
+struct mmcsd_file_ops_s
+{
+  FAR const char *name;
+  mmcsd_read_t read;
+};
+
+static const struct procfs_operations g_mmcsd_operations =
+{
+  mmcsd_open,       /* open */
+  mmcsd_close,      /* close */
+  mmcsd_read,       /* read */
+  NULL,             /* write */
+
+  mmcsd_dup,        /* dup */
+
+  mmcsd_opendir,    /* opendir */
+  mmcsd_closedir,   /* closedir */
+  mmcsd_readdir,    /* readdir */
+  mmcsd_rewinddir,  /* rewinddir */
+
+  mmcsd_stat        /* stat */
+};
+
+static const struct procfs_entry_s g_mmcsd_procfs1 =
+{
+  "mmcsd", &g_mmcsd_operations, PROCFS_DIR_TYPE
+};
+static const struct procfs_entry_s g_mmcsd_procfs2 =
+{
+  "mmcsd/**", &g_mmcsd_operations, PROCFS_UNKOWN_TYPE
+};
+
+static const struct mmcsd_file_ops_s g_mmcsd_files[] =
+{
+  {"cid",  mmcsd_read_cid},
+  {"csd",  mmcsd_read_csd},
+  {"type", mmcsd_read_type},
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: mmcsd_get_file_index
+ ****************************************************************************/
+
+static int mmcsd_get_file_index(FAR const char *relpath)
+{
+  int i;
+
+  for (i = 0; i < nitems(g_mmcsd_files); i++)
+    {
+      if (strncmp(relpath, g_mmcsd_files[i].name,
+                  strlen(g_mmcsd_files[i].name)) == 0)
+        {
+          return i;
+        }
+    }
+
+  return -1;
+}
+
+/****************************************************************************
+ * Name: mmcsd_open
+ ****************************************************************************/
+
+static int mmcsd_open(FAR struct file *filep, FAR const char *relpath,
+                      int oflags, mode_t mode)
+{
+  FAR struct mmcsd_file_s *mmcsdfile;
+  int ret;
+
+  /* This PROCFS file is read-only.  Any attempt to open with write access
+   * is not permitted.
+   */
+
+  if ((oflags & O_WRONLY) != 0 || (oflags & O_RDONLY) == 0)
+    {
+      finfo("ERROR: Only O_RDONLY supported\n");
+      return -EACCES;
+    }
+
+  relpath += strlen("mmcsd/");
+  ret = mmcsd_get_file_index(relpath);
+  if (ret < 0)
+    {
+      return -ENOENT;
+    }
+
+  /* Allocate a container to hold the file attributes */
+
+  mmcsdfile = kmm_zalloc(sizeof(struct mmcsd_file_s));
+  if (mmcsdfile == NULL)
+    {
+      finfo("ERROR: Failed to allocate file attributes\n");
+      return -ENOMEM;
+    }
+
+  mmcsdfile->read = g_mmcsd_files[ret].read;
+  mmcsdfile->index = atoi(relpath + strlen(g_mmcsd_files[ret].name));
+
+  /* Save the attributes as the open-specific state in filep->f_priv */
+
+  filep->f_priv = mmcsdfile;
+  return OK;
+}
+
+/****************************************************************************
+ * Name: mmcsd_close
+ ****************************************************************************/
+
+static int mmcsd_close(FAR struct file *filep)
+{
+  FAR struct mmcsd_file_s *mmcsdfile;
+
+  /* Recover our private data from the struct file instance */
+
+  mmcsdfile = filep->f_priv;
+  DEBUGASSERT(mmcsdfile);
+
+  /* Release the file attributes structure */
+
+  kmm_free(mmcsdfile);

Review Comment:
   ```suggestion
     DEBUGASSERT(filep->f_priv);
     
     /* Release the file attributes structure */
   
     kmm_free(filep->f_priv);
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to