Sfoglia il codice sorgente

Fix directory paths with a trailing slash; report missing paths

`ls /ramfs/bin/` printed nothing: vfs_resolve() handed the filesystems the
path "bin/" and the ramfs prefix match found no entry, while `ls /ramfs/nope`
was silent as well (the listing answered "end of directory" for a path that
does not exist).  Tab completion inserts that trailing slash for directories,
so the ordinary `ls /ramfs/<TAB>` flow hit the first case.

- vfs_resolve() now copies a normalized path relative to the mount into the
  caller's buffer (skips slashes, drops "." components, trims a trailing one),
  so FAT and the ramfs both see a clean path and "/ramfs//bin", "/ramfs/./bin"
  work too.
- The ramfs listing reports a missing path (ENOENT) and a file (EINVAL)
  instead of an empty directory; the FAT listing maps "not found" to ENOENT
  (it used to say "not a directory"), and ucmd_ls gained a message for ENOENT
  (it printed nothing before).

Verified on QEMU: ls /ramfs/bin, /ramfs/bin/, /ramfs//bin, /ramfs/./bin and
/floppy1/ list correctly; /ramfs/nope and /floppy1/nope print "ls: no such
file or directory"; completing "ls /ramfs/bi<TAB>" yields "/ramfs/bin/" and
lists its contents; the shell/ramfs/FAT suite stays green (22/22).

AGENTS.md: the VFS bullet documents the normalization.
Johannes Zellner 1 mese fa
parent
commit
4e87a9dac2
6 ha cambiato i file con 81 aggiunte e 36 eliminazioni
  1. 4 1
      AGENTS.md
  2. 14 14
      sys_fs.c
  3. 2 2
      terminal.c
  4. 3 1
      ucmd_ls.c
  5. 52 15
      vfs.c
  6. 6 3
      vfs.h

+ 4 - 1
AGENTS.md

@@ -114,7 +114,10 @@ below the run area), so adding code no longer moves any address.
   legacy ports or the PCI IDE interface's native BARs — `ata_init_ports()`),
   `disk.c` (FDC + ISA DMA ch2, retries).  `vfs.c` exposes an `fs_ops` vtable
   (FAT and the ramfs implement it) so the file service and the shell never
-  touch `fat_*` directly; `sys_fs.c` is the file service for ring-3 commands:
+  touch `fat_*` directly; `vfs_resolve()` normalizes the path relative to the
+  mount (drops `.` components, collapses and trims slashes) so directory
+  paths from Tab completion ("/ramfs/bin/") work.  `sys_fs.c` is the file
+  service for ring-3 commands:
   whole-file read/write, index-based directory listing, mount listing,
   unlink/mkdir/rmdir (writes to the ramfs return a read-only error).  A FAT
   operation sets `sys_fs_busy`, which the scheduler honours (no process switch

+ 14 - 14
sys_fs.c

@@ -43,7 +43,7 @@ static int sys_err(int vfs_err)
 /* Resolve an absolute path to (mount, relative path); returns 0 or a SYS_E*
  * code (mirrors the old "no such device" vs "no filesystem" distinction). */
 static int fs_resolve(const char *path, struct vfs_mount **m,
-		      const char **rel)
+		      char *rel, unsigned int rel_size)
 {
 	char abs[VFS_PATH_MAX * 2];
 
@@ -59,7 +59,7 @@ static int fs_resolve(const char *path, struct vfs_mount **m,
 	}
 	if (path[0] == '/' && path[1] == '\0')
 		path = "/";     /* the root listing goes through vfs_resolve below */
-	if (vfs_resolve(path, m, rel) != 0)
+	if (vfs_resolve(path, m, rel, rel_size) != 0)
 		return vfs_device_known(path) ? SYS_ENOFS : SYS_ENODEV;
 	return 0;
 }
@@ -67,11 +67,11 @@ static int fs_resolve(const char *path, struct vfs_mount **m,
 int sys_read_file(const char *path, void *buf, unsigned int max)
 {
 	struct vfs_mount *m;
-	const char *rel;
+	char rel[VFS_PATH_MAX];
 	int r;
 
 	sys_fs_enter();
-	r = fs_resolve(path, &m, &rel);
+	r = fs_resolve(path, &m, rel, sizeof(rel));
 	if (r == 0) {
 		r = m->ops->read_file(m, rel, buf, max);
 		if (r < 0)
@@ -84,11 +84,11 @@ int sys_read_file(const char *path, void *buf, unsigned int max)
 int sys_write_file(const char *path, const void *buf, unsigned int len)
 {
 	struct vfs_mount *m;
-	const char *rel;
+	char rel[VFS_PATH_MAX];
 	int r;
 
 	sys_fs_enter();
-	r = fs_resolve(path, &m, &rel);
+	r = fs_resolve(path, &m, rel, sizeof(rel));
 	if (r == 0) {
 		r = m->ops->write_file(m, rel, buf, len);
 		if (r < 0)
@@ -101,11 +101,11 @@ int sys_write_file(const char *path, const void *buf, unsigned int len)
 int sys_unlink(const char *path)
 {
 	struct vfs_mount *m;
-	const char *rel;
+	char rel[VFS_PATH_MAX];
 	int r;
 
 	sys_fs_enter();
-	r = fs_resolve(path, &m, &rel);
+	r = fs_resolve(path, &m, rel, sizeof(rel));
 	if (r == 0) {
 		r = m->ops->unlink(m, rel);
 		if (r < 0)
@@ -118,11 +118,11 @@ int sys_unlink(const char *path)
 int sys_mkdir(const char *path)
 {
 	struct vfs_mount *m;
-	const char *rel;
+	char rel[VFS_PATH_MAX];
 	int r;
 
 	sys_fs_enter();
-	r = fs_resolve(path, &m, &rel);
+	r = fs_resolve(path, &m, rel, sizeof(rel));
 	if (r == 0) {
 		r = m->ops->mkdir(m, rel);
 		if (r < 0)
@@ -135,11 +135,11 @@ int sys_mkdir(const char *path)
 int sys_rmdir(const char *path)
 {
 	struct vfs_mount *m;
-	const char *rel;
+	char rel[VFS_PATH_MAX];
 	int r;
 
 	sys_fs_enter();
-	r = fs_resolve(path, &m, &rel);
+	r = fs_resolve(path, &m, rel, sizeof(rel));
 	if (r == 0) {
 		r = m->ops->rmdir(m, rel);
 		if (r < 0)
@@ -152,11 +152,11 @@ int sys_rmdir(const char *path)
 int sys_list_dir(const char *path, unsigned int index, struct sys_dirent *out)
 {
 	struct vfs_mount *m;
-	const char *rel;
+	char rel[VFS_PATH_MAX];
 	int r;
 
 	sys_fs_enter();
-	r = fs_resolve(path, &m, &rel);
+	r = fs_resolve(path, &m, rel, sizeof(rel));
 	if (r == 0) {
 		struct vfs_dirent d;
 		unsigned int i;

+ 2 - 2
terminal.c

@@ -691,10 +691,10 @@ static void tab_print_cb(const char *name, int is_dir, unsigned int size)
 static int tab_scan_dir(const char *abs)
 {
 	struct vfs_mount *m;
-	const char *rel;
+	char rel[VFS_PATH_MAX];
 	unsigned int i;
 
-	if (vfs_resolve(abs, &m, &rel) != 0)
+	if (vfs_resolve(abs, &m, rel, sizeof(rel)) != 0)
 		return 0;
 	tab_raw_83 = m->ops->raw_83;
 	{

+ 3 - 1
ucmd_ls.c

@@ -105,7 +105,9 @@ void _start(const char *path)
 			print_entry(&d);
 			i++;
 		}
-		if (r == SYS_ENODEV)
+		if (r == SYS_ENOENT)
+			u_puts("ls: no such file or directory\n");
+		else if (r == SYS_ENODEV)
 			u_puts("ls: no such device\n");
 		else if (r == SYS_ENOFS)
 			u_puts("ls: no filesystem on device\n");

+ 52 - 15
vfs.c

@@ -252,7 +252,7 @@ static int fat_op_list_dir(struct vfs_mount *m, const char *path,
 	if (r == -2)
 		return VFS_EIO;
 	if (r != 0)
-		return VFS_EINVAL;
+		return VFS_ENOENT;   /* not found (or not a directory) */
 
 	fat_list_index = index;
 	fat_list_seen = 0;
@@ -384,8 +384,20 @@ static int ramfs_op_list_dir(struct vfs_mount *m, const char *path,
 	struct ramfs_dirent e;
 
 	(void)m;
-	if (ramfs_list(path, index, &e) != 1)
-		return 0;
+	if (ramfs_list(path, index, &e) != 1) {
+		if (index == 0 && path[0] != '\0') {
+			unsigned int size = 0;
+
+			/* Nothing matched: report a missing path instead of an
+			 * empty directory (the pack has no empty directories --
+			 * one exists exactly as a prefix of file paths), and a
+			 * file as "not a directory". */
+			if (ramfs_lookup(path, &size))
+				return VFS_EINVAL;
+			return VFS_ENOENT;
+		}
+		return 0;   /* end of directory */
+	}
 	copy_bounded(out->name, e.name, sizeof(out->name));
 	out->size = e.size;
 	out->is_dir = e.is_dir;
@@ -561,13 +573,13 @@ void vfs_list_root(void)
 }
 
 int vfs_resolve(const char *path, struct vfs_mount **m_out,
-		const char **rel_out)
+		char *rel_path, unsigned int rel_size)
 {
-	unsigned int i = 0, j = 0;
+	unsigned int i = 0, j = 0, o = 0;
 	char name[16];
 	vfs_mount_t *m;
 
-	if (!path || path[0] != '/')
+	if (!path || path[0] != '/' || rel_size == 0)
 		return -1;
 
 	i = 1;
@@ -582,11 +594,36 @@ int vfs_resolve(const char *path, struct vfs_mount **m_out,
 	if (!m)
 		return -1;
 
-	while (path[i] == '/')
-		i++;              /* the relative path has no leading '/' */
+	/* Copy the remainder as a normalized relative path: skip slashes, drop
+	 * "." components and never leave a trailing one. */
+	while (path[i]) {
+		unsigned int clen = 0;
+		const char *comp;
+
+		while (path[i] == '/')
+			i++;
+		if (!path[i])
+			break;
+		comp = path + i;
+		while (path[i] && path[i] != '/') {
+			i++;
+			clen++;
+		}
+		if (clen == 1 && comp[0] == '.')
+			continue;
+		if (o + clen + 2 > rel_size)
+			return -1;
+		if (o > 0)
+			rel_path[o++] = '/';
+		{
+			unsigned int k;
+			for (k = 0; k < clen; k++)
+				rel_path[o++] = comp[k];
+		}
+	}
+	rel_path[o] = '\0';
 
 	*m_out = m;
-	*rel_out = path + i;
 	return 0;
 }
 
@@ -594,10 +631,10 @@ int vfs_load_file(const char *path, struct vfs_file *out,
 		  struct vfs_mount **m_out)
 {
 	struct vfs_mount *m;
-	const char *rel;
+	char rel[VFS_PATH_MAX];
 	int r;
 
-	if (vfs_resolve(path, &m, &rel) != 0)
+	if (vfs_resolve(path, &m, rel, sizeof(rel)) != 0)
 		return VFS_ENOENT;
 	r = m->ops->load_file(m, rel, out);
 	if (r == 0 && m_out)
@@ -614,9 +651,9 @@ void vfs_release_file(struct vfs_mount *m, struct vfs_file *f)
 int vfs_stat(const char *path, unsigned int *size, int *is_dir)
 {
 	struct vfs_mount *m;
-	const char *rel;
+	char rel[VFS_PATH_MAX];
 
-	if (vfs_resolve(path, &m, &rel) != 0)
+	if (vfs_resolve(path, &m, rel, sizeof(rel)) != 0)
 		return VFS_ENOENT;
 	return m->ops->stat(m, rel, size, is_dir);
 }
@@ -624,9 +661,9 @@ int vfs_stat(const char *path, unsigned int *size, int *is_dir)
 int vfs_list_dir(const char *path, unsigned int index, struct vfs_dirent *out)
 {
 	struct vfs_mount *m;
-	const char *rel;
+	char rel[VFS_PATH_MAX];
 
-	if (vfs_resolve(path, &m, &rel) != 0)
+	if (vfs_resolve(path, &m, rel, sizeof(rel)) != 0)
 		return VFS_ENOENT;
 	return m->ops->list_dir(m, rel, index, out);
 }

+ 6 - 3
vfs.h

@@ -84,10 +84,13 @@ vfs_mount_t *vfs_find(const char *name);
 /* Print the mount list (for `ls /`). */
 void vfs_list_root(void);
 
-/* Resolve an absolute path "/<mount>/<path>" into the mount and a path
- * relative to it ("" for the mount root).  Returns 0 or -1. */
+/* Resolve an absolute path "/<mount>/<path>" into the mount and a normalized
+ * path relative to it ("" for the mount root) in the caller's buffer.  `.`
+ * components are dropped, repeated slashes collapsed and a trailing slash
+ * removed, so directory paths from Tab completion ("/ramfs/bin/") work.
+ * Returns 0 or -1 (bad path / unknown mount). */
 int vfs_resolve(const char *path, struct vfs_mount **m,
-		const char **rel_path);
+		char *rel_path, unsigned int rel_size);
 
 /* Load a whole file (borrowed pointer for ramfs, the static buffer for FAT);
  * release it with vfs_release_file() after use. */