Added a simple "cat" command. Fixed VFS overlay driver file open.

git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@15203 27425a3e-05d8-49a3-a47f-9c15f0e5edd8
This commit is contained in:
Giovanni Di Sirio 2021-12-05 12:46:08 +00:00
parent fe6602f75f
commit a07310d3cd
2 changed files with 30 additions and 16 deletions

View File

@ -238,7 +238,7 @@ static void scan_nodes(BaseSequentialStream *chp,
msg_t res; msg_t res;
vfs_directory_node_c *dirp; vfs_directory_node_c *dirp;
chprintf(chp, "%s\r\n", path); chprintf(chp, "%s" SHELL_NEWLINE_STR, path);
res = vfsOpenDirectory(path, &dirp); res = vfsOpenDirectory(path, &dirp);
if (res == VFS_RET_SUCCESS) { if (res == VFS_RET_SUCCESS) {
size_t i = strlen(path); size_t i = strlen(path);
@ -258,7 +258,7 @@ static void scan_nodes(BaseSequentialStream *chp,
path[i] = '\0'; path[i] = '\0';
} }
else { else {
chprintf(chp, "%s%s\r\n", path, fn); chprintf(chp, "%s%s" SHELL_NEWLINE_STR, path, fn);
} }
} }
@ -273,7 +273,7 @@ static void cmd_tree(BaseSequentialStream *chp, int argc, char *argv[]) {
(void)argv; (void)argv;
if (argc > 0) { if (argc > 0) {
chprintf(chp, "Usage: tree\r\n"); chprintf(chp, "Usage: tree" SHELL_NEWLINE_STR);
return; return;
} }
@ -281,7 +281,7 @@ static void cmd_tree(BaseSequentialStream *chp, int argc, char *argv[]) {
pathbuf = (char *)chHeapAlloc(NULL, 1024); pathbuf = (char *)chHeapAlloc(NULL, 1024);
nip = (vfs_node_info_t *)chHeapAlloc(NULL, 1024); nip = (vfs_node_info_t *)chHeapAlloc(NULL, 1024);
if ((pathbuf == NULL) || (nip == NULL)) { if ((pathbuf == NULL) || (nip == NULL)) {
chprintf(chp, "Out of memory\r\n"); chprintf(chp, "Out of memory" SHELL_NEWLINE_STR);
break; break;
} }
@ -303,7 +303,7 @@ static void cmd_cat(BaseSequentialStream *chp, int argc, char *argv[]) {
char *buf = NULL; char *buf = NULL;
if (argc != 1) { if (argc != 1) {
chprintf(chp, "Usage: cat <filename>\r\n"); chprintf(chp, "Usage: cat <filename>" SHELL_NEWLINE_STR);
return; return;
} }
@ -312,19 +312,20 @@ static void cmd_cat(BaseSequentialStream *chp, int argc, char *argv[]) {
buf= (char *)chHeapAlloc(NULL, 2048); buf= (char *)chHeapAlloc(NULL, 2048);
if (buf == NULL) { if (buf == NULL) {
chprintf(chp, "Out of memory\r\n"); chprintf(chp, "Out of memory" SHELL_NEWLINE_STR);
break; break;
} }
fd = open(argv[1], O_RDONLY); fd = open(argv[0], O_RDONLY);
if(fd == -1) { if(fd == -1) {
chprintf(chp, "Cannot open file\r\n"); chprintf(chp, "Cannot open file" SHELL_NEWLINE_STR);
break; break;
} }
while ((n = read(fd, buf, sizeof (buf))) > 0) { while ((n = read(fd, buf, sizeof (2048))) > 0) {
chprintf(chp, "%s", buf); streamWrite(chp, (const uint8_t *)buf, n);
} }
chprintf(chp, SHELL_NEWLINE_STR);
(void) close(fd); (void) close(fd);
} }

View File

@ -170,13 +170,14 @@ static msg_t drv_open_file(void *instance,
int oflag, int oflag,
vfs_file_node_c **vfnpp) { vfs_file_node_c **vfnpp) {
vfs_overlay_driver_c *drvp = (vfs_overlay_driver_c *)instance; vfs_overlay_driver_c *drvp = (vfs_overlay_driver_c *)instance;
const char *scanpath = path;
msg_t err; msg_t err;
do { do {
err = vfs_parse_match_separator(&path); err = vfs_parse_match_separator(&scanpath);
VFS_BREAK_ON_ERROR(err); VFS_BREAK_ON_ERROR(err);
if (*path == '\0') { if (*scanpath == '\0') {
(void)instance; (void)instance;
/* Always not found, there are no files in the root.*/ /* Always not found, there are no files in the root.*/
@ -185,11 +186,23 @@ static msg_t drv_open_file(void *instance,
else { else {
vfs_driver_c *dp; vfs_driver_c *dp;
/* Searching for a match among registered overlays.*/
err = match_driver(drvp, &scanpath, &dp);
if (err == VFS_RET_SUCCESS) {
/* Delegating node creation to a registered driver.*/ /* Delegating node creation to a registered driver.*/
err = match_driver(drvp, &path, &dp); err = dp->vmt->open_file((void *)dp, scanpath, oflag, vfnpp);
VFS_BREAK_ON_ERROR(err); }
else {
/* No matching overlay, the whole path is passed to the overlaid
driver, if defined, else returning the previous error.*/
if (drvp->overlaid_drv != NULL) {
err = drvp->overlaid_drv->vmt->open_file((void *)drvp->overlaid_drv,
path,
oflag,
vfnpp);
}
}
err = dp->vmt->open_file((void *)dp, path, oflag, vfnpp);
} }
} }
while (false); while (false);