mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <sha@pengutronix.de>
To: Ahmad Fatoum <a.fatoum@pengutronix.de>
Cc: barebox@lists.infradead.org
Subject: Re: [PATCH] commands: add new stat command
Date: Thu, 13 Oct 2022 09:15:30 +0200	[thread overview]
Message-ID: <20221013071530.GD6702@pengutronix.de> (raw)
In-Reply-To: <20221010064358.2119629-1-a.fatoum@pengutronix.de>

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 |



      reply	other threads:[~2022-10-13  7:17 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-10  6:43 Ahmad Fatoum
2022-10-13  7:15 ` Sascha Hauer [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221013071530.GD6702@pengutronix.de \
    --to=sha@pengutronix.de \
    --cc=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox