• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
4  * Author: David Fenner, Jon Hendrickson
5  * Copyright (C) 2024 Andrea Cervesato <andrea.cervesato@suse.com>
6  */
7 
8 /*\
9  * [Description]
10  *
11  * This test checks that stat() executed on file provide the same information
12  * of symlink linking to it.
13  */
14 
15 #include <stdlib.h>
16 #include "tst_test.h"
17 #include "tst_safe_stdio.h"
18 
19 #define FILENAME "file.txt"
20 #define MNTPOINT "mntpoint"
21 #define SYMBNAME MNTPOINT"/file_symlink"
22 
23 static char *symb_path;
24 static char *file_path;
25 static struct stat *file_stat;
26 static struct stat *symb_stat;
27 
run(void)28 static void run(void)
29 {
30 	SAFE_STAT(file_path, file_stat);
31 	SAFE_STAT(symb_path, symb_stat);
32 
33 	TST_EXP_EQ_LI(file_stat->st_dev, symb_stat->st_dev);
34 	TST_EXP_EQ_LI(file_stat->st_mode, symb_stat->st_mode);
35 	TST_EXP_EQ_LI(file_stat->st_nlink, symb_stat->st_nlink);
36 	TST_EXP_EQ_LI(file_stat->st_uid, symb_stat->st_uid);
37 	TST_EXP_EQ_LI(file_stat->st_gid, symb_stat->st_gid);
38 	TST_EXP_EQ_LI(file_stat->st_size, symb_stat->st_size);
39 	TST_EXP_EQ_LI(file_stat->st_atime, symb_stat->st_atime);
40 	TST_EXP_EQ_LI(file_stat->st_mtime, symb_stat->st_mtime);
41 	TST_EXP_EQ_LI(file_stat->st_ctime, symb_stat->st_ctime);
42 }
43 
setup(void)44 static void setup(void)
45 {
46 	char opt_bsize[32];
47 	const char *const fs_opts[] = {opt_bsize, NULL};
48 	struct stat sb;
49 	int pagesize;
50 	int fd;
51 
52 	file_path = tst_tmpdir_genpath(FILENAME);
53 	symb_path = tst_tmpdir_genpath(SYMBNAME);
54 
55 	/* change st_blksize / st_dev */
56 	SAFE_STAT(".", &sb);
57 	pagesize = sb.st_blksize == 4096 ? 1024 : 4096;
58 
59 	snprintf(opt_bsize, sizeof(opt_bsize), "-b %i", pagesize);
60 	SAFE_MKFS(tst_device->dev, tst_device->fs_type, fs_opts, NULL);
61 	SAFE_MOUNT(tst_device->dev, MNTPOINT, tst_device->fs_type, 0, 0);
62 
63 	SAFE_TOUCH(FILENAME, 0777, NULL);
64 
65 	/* change st_nlink */
66 	SAFE_LINK(FILENAME, "linked_file");
67 
68 	/* change st_uid and st_gid */
69 	SAFE_CHOWN(FILENAME, 1000, 1000);
70 
71 	/* change st_size */
72 	fd = SAFE_OPEN(FILENAME, O_WRONLY, 0777);
73 	tst_fill_fd(fd, 'a', TST_KB, 500);
74 	SAFE_CLOSE(fd);
75 
76 	/* change st_atime / st_mtime / st_ctime */
77 	usleep(1001000);
78 
79 	SAFE_SYMLINK(file_path, symb_path);
80 }
81 
cleanup(void)82 static void cleanup(void)
83 {
84 	if (tst_is_mounted(MNTPOINT))
85 		SAFE_UMOUNT(MNTPOINT);
86 }
87 
88 static struct tst_test test = {
89 	.setup = setup,
90 	.cleanup = cleanup,
91 	.test_all = run,
92 	.needs_root = 1,
93 	.needs_device = 1,
94 	.mntpoint = MNTPOINT,
95 	.bufs = (struct tst_buffers []) {
96 		{&file_stat, .size = sizeof(struct stat)},
97 		{&symb_stat, .size = sizeof(struct stat)},
98 		{}
99 	}
100 };
101