[LTP] [PATCH 4/6] Rewrite utime04.c using new LTP API

Cyril Hrubis chrubis@suse.cz
Tue Jun 21 14:38:43 CEST 2022


Hi!
> +#define FILE_MODE	0444
> +#define NEW_MODF_TIME	10000
> +#define NEW_ACCESS_TIME	20000
>  
> -struct utimbuf times;		/* struct. buffer for utime() */
> +static struct utimbuf times;
>  
> -void setup();			/* Main setup function of test */
> -void cleanup();			/* cleanup function for the test */
> -
> -int main(int ac, char **av)
> +static void setup(void)
>  {
> -	struct stat stat_buf;	/* struct buffer to hold file info. */
> -	int lc;
> -	time_t modf_time, access_time;
> -	/* file modification/access time */
> -
> -	tst_parse_opts(ac, av, NULL, NULL);
> -
> -	setup();
> -
> -	for (lc = 0; TEST_LOOPING(lc); lc++) {
> -
> -		tst_count = 0;
> -
> -		/*
> -		 * Invoke utime(2) to set TEMP_FILE access and
> -		 * modification times to that specified by
> -		 * times argument.
> -		 */
> -		TEST(utime(TEMP_FILE, &times));
> -
> -		if (TEST_RETURN == -1) {
> -			tst_resm(TFAIL|TTERRNO, "utime(%s) failed", TEMP_FILE);
> -		} else {
> -			/*
> -			 * Get the modification and access times of
> -			 * temporary file using stat(2).
> -			 */
> -			SAFE_STAT(cleanup, TEMP_FILE, &stat_buf);
> -			modf_time = stat_buf.st_mtime;
> -			access_time = stat_buf.st_atime;
> -
> -			/* Now do the actual verification */
> -			if ((modf_time != NEW_TIME) ||
> -			    (access_time != NEW_TIME)) {
> -				tst_resm(TFAIL, "%s access and "
> -					 "modification times not set",
> -					 TEMP_FILE);
> -			} else {
> -				tst_resm(TPASS, "Functionality of "
> -					 "utime(%s, &times) successful",
> -					 TEMP_FILE);
> -			}
> -		}
> -		tst_count++;	/* incr TEST_LOOP counter */
> -	}
> +	SAFE_TOUCH(TEMP_FILE, FILE_MODE, NULL);
>  
> -	cleanup();
> -	tst_exit();
> +	times.modtime = NEW_MODF_TIME;
> +	times.actime = NEW_ACCESS_TIME;

The times structure can be initialized inline as:

static struct utimbuf times = {
	.modtime = NEW_MOD_TIME,
	.actime = NEW_ACC_TIME,
};

>  }
>  
> -/*
> - * void
> - * setup() - performs all ONE TIME setup for this test.
> - *  Create a temporary directory and change directory to it.
> - *  Create a test file under temporary directory and close it
> - */
> -void setup(void)
> +static void run(void)
>  {
> -	int fildes;		/* file handle for temp file */
> -
> -	tst_require_root();
> +	struct stat stat_buf;
>  
> -	tst_sig(NOFORK, DEF_HANDLER, cleanup);
> +	TST_EXP_PASS(utime(TEMP_FILE, &times), "utime(%s, &times)", TEMP_FILE);
>  
> -	TEST_PAUSE;
> -
> -	tst_tmpdir();
> -
> -	/* Creat a temporary file under above directory */
> -	fildes = SAFE_CREAT(cleanup, TEMP_FILE, FILE_MODE);
> +	if (!TST_PASS) {
> +		tst_res(TFAIL | TTERRNO, "utime(%s, &times) failed", TEMP_FILE);

The PASS/FAIL message is already printed by the TST_EXP_PASS() macro,
all that you need to to do here is return.

> +		return;
> +	}
>  
> -	/* Close the temporary file created */
> -	SAFE_CLOSE(cleanup, fildes);
> +	SAFE_STAT(TEMP_FILE, &stat_buf);
>  
> -	/* Initialize the modification and access time in the times arg */
> -	times.actime = NEW_TIME;
> -	times.modtime = NEW_TIME;
> +	if (stat_buf.st_mtime != NEW_MODF_TIME)
> +		tst_res(TFAIL, "utime() did not set expected mtime");
>  
> +	if (stat_buf.st_atime != NEW_ACCESS_TIME)
> +		tst_res(TFAIL, "utime() did not set expected atime");

We do have macros for these kind of checks as well, please use them as:

	TST_EXP_EQ_LI(stat_buf.st_atime, NEW_ACC_TIME);

>  }
>  
> -/*
> - * void
> - * cleanup() - performs all ONE TIME cleanup for this test at
> - *             completion or premature exit.
> - *  Remove the test directory and testfile created in the setup.
> - */
> -void cleanup(void)
> -{
> -
> -	tst_rmdir();
> -
> -}
> +static struct tst_test test = {
> +	.test_all = run,
> +	.setup = setup,
> +	.needs_root = 1,
> +	.needs_tmpdir = 1
> +};
> -- 
> 2.36.1
> 
> 
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp

-- 
Cyril Hrubis
chrubis@suse.cz


More information about the ltp mailing list