1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved. 4f08c3bdfSopenharmony_ci * Author: Dai Shili <daisl.fnst@fujitsu.com> 5f08c3bdfSopenharmony_ci */ 6f08c3bdfSopenharmony_ci 7f08c3bdfSopenharmony_ci/*\ 8f08c3bdfSopenharmony_ci * [Description] 9f08c3bdfSopenharmony_ci * 10f08c3bdfSopenharmony_ci * Test the write() system call with O_APPEND. 11f08c3bdfSopenharmony_ci * 12f08c3bdfSopenharmony_ci * The full description of O_APPEND is in open(2) man-pages: 13f08c3bdfSopenharmony_ci * The file is opened in append mode. Before each write(2), the 14f08c3bdfSopenharmony_ci * file offset is positioned at the end of the file, as if with lseek(2). 15f08c3bdfSopenharmony_ci * The modification of the file offset and the write operation are 16f08c3bdfSopenharmony_ci * performed as a single atomic step. 17f08c3bdfSopenharmony_ci * 18f08c3bdfSopenharmony_ci * Writing 2k data to the file, close it and reopen it with O_APPEND. 19f08c3bdfSopenharmony_ci * Verify that the file size is 3k and offset is moved to the end of the file. 20f08c3bdfSopenharmony_ci */ 21f08c3bdfSopenharmony_ci 22f08c3bdfSopenharmony_ci#include <stdlib.h> 23f08c3bdfSopenharmony_ci#include <inttypes.h> 24f08c3bdfSopenharmony_ci#include "tst_test.h" 25f08c3bdfSopenharmony_ci#include "tst_safe_prw.h" 26f08c3bdfSopenharmony_ci 27f08c3bdfSopenharmony_ci#define K1 1024 28f08c3bdfSopenharmony_ci#define K2 (K1 * 2) 29f08c3bdfSopenharmony_ci#define K3 (K1 * 3) 30f08c3bdfSopenharmony_ci#define DATA_FILE "write06_file" 31f08c3bdfSopenharmony_ci 32f08c3bdfSopenharmony_cistatic int fd = -1; 33f08c3bdfSopenharmony_cistatic char *write_buf[2]; 34f08c3bdfSopenharmony_ci 35f08c3bdfSopenharmony_cistatic void verify_write(void) 36f08c3bdfSopenharmony_ci{ 37f08c3bdfSopenharmony_ci off_t off; 38f08c3bdfSopenharmony_ci struct stat statbuf; 39f08c3bdfSopenharmony_ci 40f08c3bdfSopenharmony_ci fd = SAFE_OPEN(DATA_FILE, O_RDWR | O_CREAT | O_TRUNC, 0666); 41f08c3bdfSopenharmony_ci SAFE_WRITE(SAFE_WRITE_ALL, fd, write_buf[0], K2); 42f08c3bdfSopenharmony_ci SAFE_CLOSE(fd); 43f08c3bdfSopenharmony_ci 44f08c3bdfSopenharmony_ci fd = SAFE_OPEN(DATA_FILE, O_RDWR | O_APPEND); 45f08c3bdfSopenharmony_ci SAFE_FSTAT(fd, &statbuf); 46f08c3bdfSopenharmony_ci if (statbuf.st_size != K2) 47f08c3bdfSopenharmony_ci tst_res(TFAIL, "file size is %ld != K2", statbuf.st_size); 48f08c3bdfSopenharmony_ci 49f08c3bdfSopenharmony_ci off = SAFE_LSEEK(fd, K1, SEEK_SET); 50f08c3bdfSopenharmony_ci if (off != K1) 51f08c3bdfSopenharmony_ci tst_brk(TBROK, "Failed to seek to K1"); 52f08c3bdfSopenharmony_ci 53f08c3bdfSopenharmony_ci SAFE_WRITE(SAFE_WRITE_ALL, fd, write_buf[1], K1); 54f08c3bdfSopenharmony_ci 55f08c3bdfSopenharmony_ci off = SAFE_LSEEK(fd, 0, SEEK_CUR); 56f08c3bdfSopenharmony_ci if (off != K3) 57f08c3bdfSopenharmony_ci tst_res(TFAIL, "Wrong offset after write %zu expected %u", off, K3); 58f08c3bdfSopenharmony_ci else 59f08c3bdfSopenharmony_ci tst_res(TPASS, "Offset is correct after write %zu", off); 60f08c3bdfSopenharmony_ci 61f08c3bdfSopenharmony_ci SAFE_FSTAT(fd, &statbuf); 62f08c3bdfSopenharmony_ci if (statbuf.st_size != K3) 63f08c3bdfSopenharmony_ci tst_res(TFAIL, "Wrong file size after append %zu expected %u", statbuf.st_size, K3); 64f08c3bdfSopenharmony_ci else 65f08c3bdfSopenharmony_ci tst_res(TPASS, "Correct file size after append %u", K3); 66f08c3bdfSopenharmony_ci 67f08c3bdfSopenharmony_ci SAFE_CLOSE(fd); 68f08c3bdfSopenharmony_ci} 69f08c3bdfSopenharmony_ci 70f08c3bdfSopenharmony_cistatic void setup(void) 71f08c3bdfSopenharmony_ci{ 72f08c3bdfSopenharmony_ci memset(write_buf[0], 0, K2); 73f08c3bdfSopenharmony_ci memset(write_buf[1], 1, K1); 74f08c3bdfSopenharmony_ci} 75f08c3bdfSopenharmony_ci 76f08c3bdfSopenharmony_cistatic void cleanup(void) 77f08c3bdfSopenharmony_ci{ 78f08c3bdfSopenharmony_ci if (fd != -1) 79f08c3bdfSopenharmony_ci SAFE_CLOSE(fd); 80f08c3bdfSopenharmony_ci 81f08c3bdfSopenharmony_ci SAFE_UNLINK(DATA_FILE); 82f08c3bdfSopenharmony_ci} 83f08c3bdfSopenharmony_ci 84f08c3bdfSopenharmony_cistatic struct tst_test test = { 85f08c3bdfSopenharmony_ci .needs_tmpdir = 1, 86f08c3bdfSopenharmony_ci .setup = setup, 87f08c3bdfSopenharmony_ci .cleanup = cleanup, 88f08c3bdfSopenharmony_ci .test_all = verify_write, 89f08c3bdfSopenharmony_ci .bufs = (struct tst_buffers[]) { 90f08c3bdfSopenharmony_ci {&write_buf[0], .size = K2}, 91f08c3bdfSopenharmony_ci {&write_buf[1], .size = K1}, 92f08c3bdfSopenharmony_ci {} 93f08c3bdfSopenharmony_ci } 94f08c3bdfSopenharmony_ci}; 95