[PATCH] commands: add new stat command

Sascha Hauer sha at pengutronix.de
Thu Oct 13 00:15:30 PDT 2022


On Mon, Oct 10, 2022 at 08:43:58AM +0200, Ahmad Fatoum wrote:
> +#include <common.h>
> +#include <command.h>
> +#include <fs.h>
> +#include <linux/stat.h>
> +#include <errno.h>
> +#include <malloc.h>
> +#include <getopt.h>
> +#include <stringlist.h>
> +
> +static int do_stat(int argc, char *argv[])
> +{
> +	int (*statfn)(const char *, struct stat *) = stat;
> +	int ret, opt, exitcode = 0;
> +	char **filename;
> +	struct stat st;
> +
> +	while((opt = getopt(argc, argv, "L")) > 0) {
> +		switch(opt) {
> +		case 'L':
> +			statfn = lstat;
> +			break;

That's the wrong way round. You have to use lstat() by default and
stat() when -L is given.

> +void stat_print(const char *filename, const struct stat *st)
> +{
> +	struct block_device *bdev = NULL;
> +	struct fs_device_d *fdev;
> +	struct cdev *cdev = NULL;
> +	const char *type = NULL;
> +	char modestr[11];
> +
> +	mkmodestr(st->st_mode, modestr);
> +
> +	switch (st->st_mode & S_IFMT) {
> +		case S_IFDIR:    type = "directory"; break;
> +		case S_IFBLK:    type = "block special file"; break;
> +		case S_IFCHR:    type = "character special file"; break;
> +		case S_IFIFO:    type = "fifo"; break;
> +		case S_IFLNK:    type = "symbolic link"; break;
> +		case S_IFSOCK:   type = "socket"; break;
> +		case S_IFREG:    type = "regular file"; break;
> +	}
> +
> +	printf("  File: %s\n", filename);
> +
> +	if (st->st_mode & S_IFCHR) {
> +		const char *devicefile;
> +
> +		devicefile = devpath_to_name(filename);

This doesn't work really well. I think it's ok this time because there's
enough prior art, but we really need something better. For example "stat
/dev/mmc0" works, but "stat dev/mmc0" does not. You could improve this a
bit by calling canonicalize_path() on filename.

To solve this issue we could add a struct cdev * to struct filep.

Sascha

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



More information about the barebox mailing list