1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2001 4f08c3bdfSopenharmony_ci * Copyright (c) 2019 FUJITSU LIMITED. All rights reserved. 5f08c3bdfSopenharmony_ci * Author: Wayne Boyer 6f08c3bdfSopenharmony_ci */ 7f08c3bdfSopenharmony_ci/* 8f08c3bdfSopenharmony_ci * Test Description: 9f08c3bdfSopenharmony_ci * Verify that, ftruncate() succeeds to truncate a file to a certain length, 10f08c3bdfSopenharmony_ci * if the file previously is smaller than the truncated size, ftruncate() 11f08c3bdfSopenharmony_ci * shall increase the size of the file. 12f08c3bdfSopenharmony_ci */ 13f08c3bdfSopenharmony_ci 14f08c3bdfSopenharmony_ci#include <sys/types.h> 15f08c3bdfSopenharmony_ci#include <sys/stat.h> 16f08c3bdfSopenharmony_ci#include <unistd.h> 17f08c3bdfSopenharmony_ci#include <fcntl.h> 18f08c3bdfSopenharmony_ci#include <errno.h> 19f08c3bdfSopenharmony_ci#include <string.h> 20f08c3bdfSopenharmony_ci 21f08c3bdfSopenharmony_ci#include "tst_test.h" 22f08c3bdfSopenharmony_ci 23f08c3bdfSopenharmony_ci#define TESTFILE "testfile" 24f08c3bdfSopenharmony_ci 25f08c3bdfSopenharmony_ci#define TRUNC_LEN1 256 26f08c3bdfSopenharmony_ci#define TRUNC_LEN2 512 27f08c3bdfSopenharmony_ci#define FILE_SIZE 1024 28f08c3bdfSopenharmony_ci 29f08c3bdfSopenharmony_cistatic int fd; 30f08c3bdfSopenharmony_ci 31f08c3bdfSopenharmony_cistatic void check_and_report(off_t offset, char data, off_t trunc_len) 32f08c3bdfSopenharmony_ci{ 33f08c3bdfSopenharmony_ci int i, file_length; 34f08c3bdfSopenharmony_ci char buf[FILE_SIZE]; 35f08c3bdfSopenharmony_ci struct stat stat_buf; 36f08c3bdfSopenharmony_ci 37f08c3bdfSopenharmony_ci memset(buf, '*', sizeof(buf)); 38f08c3bdfSopenharmony_ci 39f08c3bdfSopenharmony_ci SAFE_FSTAT(fd, &stat_buf); 40f08c3bdfSopenharmony_ci file_length = stat_buf.st_size; 41f08c3bdfSopenharmony_ci 42f08c3bdfSopenharmony_ci if (file_length != trunc_len) { 43f08c3bdfSopenharmony_ci tst_res(TFAIL, "ftruncate() got incorrected size: %d", 44f08c3bdfSopenharmony_ci file_length); 45f08c3bdfSopenharmony_ci return; 46f08c3bdfSopenharmony_ci } 47f08c3bdfSopenharmony_ci 48f08c3bdfSopenharmony_ci SAFE_LSEEK(fd, offset, SEEK_SET); 49f08c3bdfSopenharmony_ci SAFE_READ(0, fd, buf, sizeof(buf)); 50f08c3bdfSopenharmony_ci 51f08c3bdfSopenharmony_ci for (i = 0; i < TRUNC_LEN1; i++) { 52f08c3bdfSopenharmony_ci if (buf[i] != data) { 53f08c3bdfSopenharmony_ci tst_res(TFAIL, 54f08c3bdfSopenharmony_ci "ftruncate() got incorrect data %i, expected %i", 55f08c3bdfSopenharmony_ci buf[i], data); 56f08c3bdfSopenharmony_ci return; 57f08c3bdfSopenharmony_ci } 58f08c3bdfSopenharmony_ci } 59f08c3bdfSopenharmony_ci 60f08c3bdfSopenharmony_ci tst_res(TPASS, "ftruncate() succeeded"); 61f08c3bdfSopenharmony_ci} 62f08c3bdfSopenharmony_ci 63f08c3bdfSopenharmony_cistatic void verify_ftruncate(void) 64f08c3bdfSopenharmony_ci{ 65f08c3bdfSopenharmony_ci tst_res(TINFO, "Truncated length smaller than file size"); 66f08c3bdfSopenharmony_ci TEST(ftruncate(fd, TRUNC_LEN1)); 67f08c3bdfSopenharmony_ci if (TST_RET == -1) { 68f08c3bdfSopenharmony_ci tst_res(TFAIL | TTERRNO, "ftruncate() failed"); 69f08c3bdfSopenharmony_ci return; 70f08c3bdfSopenharmony_ci } 71f08c3bdfSopenharmony_ci 72f08c3bdfSopenharmony_ci check_and_report(0, 'a', TRUNC_LEN1); 73f08c3bdfSopenharmony_ci 74f08c3bdfSopenharmony_ci tst_res(TINFO, "Truncated length exceeds file size"); 75f08c3bdfSopenharmony_ci TEST(ftruncate(fd, TRUNC_LEN2)); 76f08c3bdfSopenharmony_ci if (TST_RET == -1) { 77f08c3bdfSopenharmony_ci tst_res(TFAIL | TTERRNO, "ftruncate() failed"); 78f08c3bdfSopenharmony_ci return; 79f08c3bdfSopenharmony_ci } 80f08c3bdfSopenharmony_ci 81f08c3bdfSopenharmony_ci check_and_report(TRUNC_LEN1, 0, TRUNC_LEN2); 82f08c3bdfSopenharmony_ci} 83f08c3bdfSopenharmony_ci 84f08c3bdfSopenharmony_cistatic void setup(void) 85f08c3bdfSopenharmony_ci{ 86f08c3bdfSopenharmony_ci if (tst_fill_file(TESTFILE, 'a', FILE_SIZE, 1)) 87f08c3bdfSopenharmony_ci tst_brk(TBROK, "Failed to create test file"); 88f08c3bdfSopenharmony_ci 89f08c3bdfSopenharmony_ci fd = SAFE_OPEN(TESTFILE, O_RDWR); 90f08c3bdfSopenharmony_ci} 91f08c3bdfSopenharmony_ci 92f08c3bdfSopenharmony_cistatic void cleanup(void) 93f08c3bdfSopenharmony_ci{ 94f08c3bdfSopenharmony_ci if (fd > 0) 95f08c3bdfSopenharmony_ci SAFE_CLOSE(fd); 96f08c3bdfSopenharmony_ci} 97f08c3bdfSopenharmony_ci 98f08c3bdfSopenharmony_cistatic struct tst_test test = { 99f08c3bdfSopenharmony_ci .test_all = verify_ftruncate, 100f08c3bdfSopenharmony_ci .setup = setup, 101f08c3bdfSopenharmony_ci .cleanup = cleanup, 102f08c3bdfSopenharmony_ci .needs_tmpdir = 1, 103f08c3bdfSopenharmony_ci}; 104