All of lore.kernel.org
 help / color / mirror / Atom feed
From: Cyril Hrubis <chrubis@suse.cz>
To: Avinesh Kumar <akumar@suse.de>
Cc: ltp@lists.linux.it
Subject: Re: [LTP] [PATCH 4/6] Rewrite utime04.c using new LTP API
Date: Tue, 21 Jun 2022 14:38:43 +0200	[thread overview]
Message-ID: <YrG705WokoVxD4eM@yuki> (raw)
In-Reply-To: <20220617172025.23975-5-akumar@suse.de>

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

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

  reply	other threads:[~2022-06-21 12:36 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-17 17:20 [LTP] [PATCH 0/6] Convert utime tests to new LTP API Avinesh Kumar
2022-06-17 17:20 ` [LTP] [PATCH 1/6] Rewrite utime01.c using " Avinesh Kumar
2022-06-21 10:17   ` Cyril Hrubis
2022-06-17 17:20 ` [LTP] [PATCH 2/6] Rewrite utime02.c " Avinesh Kumar
2022-06-21 10:19   ` Cyril Hrubis
2022-06-17 17:20 ` [LTP] [PATCH 3/6] Remove unnecessary header includes Avinesh Kumar
2022-06-21 10:24   ` Cyril Hrubis
2022-06-17 17:20 ` [LTP] [PATCH 4/6] Rewrite utime04.c using new LTP API Avinesh Kumar
2022-06-21 12:38   ` Cyril Hrubis [this message]
2022-06-17 17:20 ` [LTP] [PATCH 5/6] Rewrite utime05.c " Avinesh Kumar
2022-06-21 12:40   ` Cyril Hrubis
2022-06-17 17:20 ` [LTP] [PATCH 6/6] Rewrite utime06.c " Avinesh Kumar
2022-06-21 12:59   ` Cyril Hrubis

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=YrG705WokoVxD4eM@yuki \
    --to=chrubis@suse.cz \
    --cc=akumar@suse.de \
    --cc=ltp@lists.linux.it \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.