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