You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by ma...@apache.org on 2015/12/02 21:23:10 UTC

[4/5] incubator-mynewt-larva git commit: Use error returs from nffs to determine whether things are files or directories.

Use error returs from nffs to determine whether things are files
or directories.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/commit/aea473a4
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/tree/aea473a4
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/diff/aea473a4

Branch: refs/heads/master
Commit: aea473a45b8231351dd9b3597981d56ace463058
Parents: fcb3de6
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Wed Dec 2 12:21:08 2015 -0800
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Wed Dec 2 12:21:08 2015 -0800

----------------------------------------------------------------------
 libs/fs/src/fs_cli.c | 50 ++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 41 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/aea473a4/libs/fs/src/fs_cli.c
----------------------------------------------------------------------
diff --git a/libs/fs/src/fs_cli.c b/libs/fs/src/fs_cli.c
index 1435110..d6cc182 100644
--- a/libs/fs/src/fs_cli.c
+++ b/libs/fs/src/fs_cli.c
@@ -16,14 +16,15 @@
 
 #ifdef SHELL_PRESENT
 
-#include <fs/fs.h>
+#include <string.h>
 
 #include <shell/shell.h>
 #include <console/console.h>
 
+#include "fs/fs.h"
+
 static struct shell_cmd fs_ls_struct;
 
-#if 0
 static void
 fs_ls_file(const char *name, struct fs_file *file)
 {
@@ -33,19 +34,28 @@ fs_ls_file(const char *name, struct fs_file *file)
     fs_filelen(file, &len);
     console_printf("\t%6d %s\n", len, name);
 }
-#endif
+
+static void
+fs_ls_dir(const char *name)
+{
+    console_printf("\t%6s %s\n", "dir", name);
+}
 
 static int
 fs_ls_cmd(int argc, char **argv)
 {
-    int rc;
+    int rc, file_cnt = 0;
     char *path;
     struct fs_file *file;
     struct fs_dir *dir;
+    struct fs_dirent *dirent;
+    char name[64];
+    int plen;
+    uint8_t namelen;
 
     switch (argc) {
     case 1:
-        path = NULL;
+        path = "/";
         break;
     case 2:
         path = argv[1];
@@ -55,20 +65,42 @@ fs_ls_cmd(int argc, char **argv)
         return 1;
     }
 
+    plen = strlen(path);
     rc = fs_open(path, FS_ACCESS_READ, &file);
     if (rc == 0) {
-#if 0
         fs_ls_file(path, file);
-#endif
         fs_close(file);
+        file_cnt = 1;
+        goto done;
     }
-    console_printf("fs_open() = %d\n", rc);
 
+    strncpy(name, path, sizeof(name) - 1);
     rc = fs_opendir(path, &dir);
-    console_printf("fs_opendir() = %d\n", rc);
     if (rc == 0) {
+        do {
+            rc = fs_readdir(dir, &dirent);
+            if (rc) {
+                break;
+            }
+            if (fs_dirent_name(dirent, sizeof(name) - plen, &name[plen],
+                &namelen)) {
+                break;
+            }
+            rc = fs_open(name, FS_ACCESS_READ, &file);
+            if (rc == 0) {
+                fs_ls_file(name, file);
+                fs_close(file);
+            } else {
+                fs_ls_dir(name);
+            }
+            file_cnt++;
+        } while (1);
         fs_closedir(dir);
+        goto done;
     }
+    console_printf("Error listing %s - %d\n", path, rc);
+done:
+    console_printf("%d files\n", file_cnt);
     return 0;
 }