1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci
3f08c3bdfSopenharmony_ci/*
4f08c3bdfSopenharmony_ci * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
5f08c3bdfSopenharmony_ci * AUTHOR: Barrie Kletscher
6f08c3bdfSopenharmony_ci * CO-PILOT: Dave Baumgartner
7f08c3bdfSopenharmony_ci */
8f08c3bdfSopenharmony_ci
9f08c3bdfSopenharmony_ci#include <fcntl.h>
10f08c3bdfSopenharmony_ci#include <sys/types.h>
11f08c3bdfSopenharmony_ci#include <sys/stat.h>
12f08c3bdfSopenharmony_ci#include <sys/signal.h>
13f08c3bdfSopenharmony_ci#include <errno.h>
14f08c3bdfSopenharmony_ci#include <stdlib.h>
15f08c3bdfSopenharmony_ci#include "tst_test.h"
16f08c3bdfSopenharmony_ci
17f08c3bdfSopenharmony_ci/* Temporary file names */
18f08c3bdfSopenharmony_ci#define FNAME1	"stat02.1"
19f08c3bdfSopenharmony_ci#define FNAME2	"stat02.2"
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_ci/* Number of times each buffer is written */
22f08c3bdfSopenharmony_ci#define NUM_WRITES	(10)
23f08c3bdfSopenharmony_ci#define BUFSIZE		(4096)
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_cichar *buffer;
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_cistatic struct test_case {
28f08c3bdfSopenharmony_ci	const char *filename;
29f08c3bdfSopenharmony_ci	size_t bytes_to_write;
30f08c3bdfSopenharmony_ci	size_t decrement;
31f08c3bdfSopenharmony_ci} tcases[] = {
32f08c3bdfSopenharmony_ci	/* Write and verify BUFSIZE byte chunks */
33f08c3bdfSopenharmony_ci	{ FNAME1, BUFSIZE, BUFSIZE },
34f08c3bdfSopenharmony_ci	/* Write and verify decreasing chunk sizes */
35f08c3bdfSopenharmony_ci	{ FNAME2, BUFSIZE, 1000 }
36f08c3bdfSopenharmony_ci};
37f08c3bdfSopenharmony_ci
38f08c3bdfSopenharmony_civoid verify(const char *fname, size_t bytes, size_t decrement)
39f08c3bdfSopenharmony_ci{
40f08c3bdfSopenharmony_ci	struct stat s;
41f08c3bdfSopenharmony_ci	int fd, i;
42f08c3bdfSopenharmony_ci	size_t bytes_written = 0;
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_ci	fd = SAFE_OPEN(fname, O_CREAT | O_TRUNC | O_RDWR, 0777);
45f08c3bdfSopenharmony_ci	while (bytes > 0) {
46f08c3bdfSopenharmony_ci		for (i = 0; i < NUM_WRITES; i++) {
47f08c3bdfSopenharmony_ci			SAFE_WRITE(SAFE_WRITE_ALL, fd, buffer, bytes);
48f08c3bdfSopenharmony_ci			bytes_written += bytes;
49f08c3bdfSopenharmony_ci		}
50f08c3bdfSopenharmony_ci		bytes -= bytes > decrement ? decrement : bytes;
51f08c3bdfSopenharmony_ci	}
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_ci	SAFE_CLOSE(fd);
54f08c3bdfSopenharmony_ci	SAFE_STAT(fname, &s);
55f08c3bdfSopenharmony_ci	SAFE_UNLINK(fname);
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_ci	/*
58f08c3bdfSopenharmony_ci	 *  Now check to see if the number of bytes written was
59f08c3bdfSopenharmony_ci	 *  the same as the number of bytes in the file.
60f08c3bdfSopenharmony_ci	 */
61f08c3bdfSopenharmony_ci	if (s.st_size != (off_t) bytes_written) {
62f08c3bdfSopenharmony_ci		tst_res(TFAIL, "file size (%zu) not as expected (%zu) bytes",
63f08c3bdfSopenharmony_ci				s.st_size, bytes_written);
64f08c3bdfSopenharmony_ci		return;
65f08c3bdfSopenharmony_ci	}
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_ci	tst_res(TPASS, "File size reported as expected");
68f08c3bdfSopenharmony_ci}
69f08c3bdfSopenharmony_ci
70f08c3bdfSopenharmony_civoid verify_stat_size(unsigned int nr)
71f08c3bdfSopenharmony_ci{
72f08c3bdfSopenharmony_ci	struct test_case *tcase = &tcases[nr];
73f08c3bdfSopenharmony_ci
74f08c3bdfSopenharmony_ci	verify(tcase->filename, tcase->bytes_to_write, tcase->decrement);
75f08c3bdfSopenharmony_ci}
76f08c3bdfSopenharmony_ci
77f08c3bdfSopenharmony_civoid setup(void)
78f08c3bdfSopenharmony_ci{
79f08c3bdfSopenharmony_ci	buffer = SAFE_MALLOC(BUFSIZE);
80f08c3bdfSopenharmony_ci}
81f08c3bdfSopenharmony_ci
82f08c3bdfSopenharmony_civoid cleanup(void)
83f08c3bdfSopenharmony_ci{
84f08c3bdfSopenharmony_ci	free(buffer);
85f08c3bdfSopenharmony_ci}
86f08c3bdfSopenharmony_ci
87f08c3bdfSopenharmony_cistatic struct tst_test test = {
88f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tcases),
89f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
90f08c3bdfSopenharmony_ci	.test = verify_stat_size,
91f08c3bdfSopenharmony_ci	.setup = setup,
92f08c3bdfSopenharmony_ci	.cleanup = cleanup,
93f08c3bdfSopenharmony_ci};
94