[LTP] [PATCH v5] Add stat04 test

Cyril Hrubis chrubis@suse.cz
Mon Jun 24 14:22:50 CEST 2024


Hi!
> +/*\
> + * [Description]
> + *
> + * This test checks that stat() executed on file provide the same information
> + * of symlink linking to it.
> + */
> +
> +#include <stdlib.h>
> +#include "tst_test.h"
> +
> +static char *tmpdir;
> +
> +static void run(void)
> +{
> +	char *symname = "my_symlink0";
> +
> +	SAFE_SYMLINK(tmpdir, symname);
> +
> +	struct stat path;
> +	struct stat link;
> +
> +	TST_EXP_PASS(stat(tmpdir, &path));
> +	TST_EXP_PASS(stat(symname, &link));
> +
> +	TST_EXP_EQ_LI(path.st_dev, link.st_dev);
> +	TST_EXP_EQ_LI(path.st_mode, link.st_mode);
> +	TST_EXP_EQ_LI(path.st_nlink, link.st_nlink);
> +	TST_EXP_EQ_LI(path.st_uid, link.st_uid);
> +	TST_EXP_EQ_LI(path.st_gid, link.st_gid);
> +	TST_EXP_EQ_LI(path.st_size, link.st_size);
> +	TST_EXP_EQ_LI(path.st_atime, link.st_atime);
> +	TST_EXP_EQ_LI(path.st_mtime, link.st_mtime);
> +	TST_EXP_EQ_LI(path.st_ctime, link.st_ctime);

Looking at these, most of the would be the same both for the link and
the actual target. Maybe we should do some extra work in the test setup
to make sure they differ. I would create a file in the temporary
directory as a target for the symlink and use chown() to set a different
owner and group, we can use utime() to change access and modification
time to the beginning of the unix epoch. If we write a megabyte or so to
the file the size should differ as well. If we link() the file it's
nlink counter should increase. And I suppose that if we create either
the file or the symlink on a mounted loop device, it will have different
dev number as well.

I think that the only part that is not changed easily is the creation
time (also called birth time), the rest should be under our control.

> +	SAFE_UNLINK(symname);
> +}
> +
> +static void setup(void)
> +{
> +	tmpdir = tst_get_tmpdir();
> +}
> +
> +static void cleanup(void)
> +{
> +	free(tmpdir);
> +}
> +
> +static struct tst_test test = {
> +	.test_all = run,
> +	.setup = setup,
> +	.cleanup = cleanup,
> +	.needs_tmpdir = 1,
> +};
> -- 
> 2.35.3
> 
> 
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp

-- 
Cyril Hrubis
chrubis@suse.cz


More information about the ltp mailing list