[LTP] [PATCH v3 2/2] fstat: add test for multiple file types using fstat
Andrea Cervesato
andrea.cervesato@suse.com
Wed Feb 25 15:30:11 CET 2026
Hi!
> +
> +static struct tcase {
> + const char *desc;
> + const char *path;
> + const char *macro_name;
> + mode_t exp_type;
> +} tcases[] = {
> + { "regular file", REG_FILE, "S_IFREG", S_IFREG },
> + { "directory", DIR_FILE, "S_IFDIR", S_IFDIR },
> + { "FIFO (pipe)", FIFO_FILE, "S_IFIFO", S_IFIFO },
> + { "symbolic link", SYMLINK, "S_IFLNK", S_IFLNK },
> + { "character dev", CHR_DEV, "S_IFCHR", S_IFCHR },
> + { "block dev", BLK_DEV, "S_IFBLK", S_IFBLK },
Makes more sense to merge desc and macro_name together. Then see below:
> +};
> +
> +static void verify_fstat(unsigned int i)
> +{
> + struct tcase *tc = &tcases[i];
> + struct stat buf;
> +
> + int flags = O_RDONLY | O_NONBLOCK;
> +
> + if (tc->exp_type == S_IFDIR)
> + flags |= O_DIRECTORY;
> +
> + fd = SAFE_OPEN(tc->path, flags);
> +
> + SAFE_FSTAT(fd, &buf);
> + SAFE_CLOSE(fd);
You can just use:
SAFE_LSTAT(tc->path, &buf);
Otherwise test will fail with:
fstat04.c:74: TPASS: Expect: regular file: macro matches
fstat04.c:74: TPASS: Expect: directory: macro matches
fstat04.c:74: TPASS: Expect: FIFO (pipe): macro matches
fstat04.c:74: TFAIL: Expect: symbolic link: macro matches
fstat04.c:46: TBROK: open(chrdev,2048,0000) failed: EACCES (13)
> +
> + int is_correct = 0;
> +
> + switch (tc->exp_type) {
> + case S_IFREG:
> + is_correct = S_ISREG(buf.st_mode);
> + break;
> + case S_IFDIR:
> + is_correct = S_ISDIR(buf.st_mode);
> + break;
> + case S_IFIFO:
> + is_correct = S_ISFIFO(buf.st_mode);
> + break;
> + case S_IFLNK:
> + is_correct = S_ISLNK(buf.st_mode);
> + break;
> + case S_IFCHR:
> + is_correct = S_ISCHR(buf.st_mode);
> + break;
> + case S_IFBLK:
> + is_correct = S_ISBLK(buf.st_mode);
> + break;
> + }
> +
> + if (is_correct)
> + tst_res(TPASS, "%s: %s() macro matches", tc->desc, tc->macro_name);
> + else
> + tst_res(TFAIL, "%s: %s() macro does NOT match", tc->desc, tc->macro_name);
We don't need an if statement here. We can just:
TST_EXP_EXPR(is_correct, "%s macro matches", tc->desc);
> +static void setup(void)
> +{
> + fd = SAFE_OPEN(REG_FILE, O_WRONLY | O_CREAT | O_TRUNC, 0644);
> + SAFE_CLOSE(fd);
Just use SAFE_TOUCH() in here, so `fd` can be removed also from the
static variables.
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
andrea.cervesato@suse.com
More information about the ltp
mailing list