fs: open: Add Truncate flag to zfs open

This commit has added new flag FS_O_TRUNC to support truncation
during file open. Modified fs_open to handle truncation based on
provided flags. Included unit tests for flag behavior with common
filesystems.

Signed-off-by: RAJAGOPALAN GANGADHARAN <g.raju2000@gmail.com>
This commit is contained in:
RAJAGOPALAN GANGADHARAN 2024-05-21 23:42:16 +05:30 committed by Henrik Brix Andersen
commit 65c4ca1f05
3 changed files with 121 additions and 1 deletions

View file

@ -149,9 +149,13 @@ void test_fs_open_flags(void)
ZOPEN(&ts, FS_O_READ, -ENOENT);
ZOPEN(&ts, FS_O_RDWR, -ENOENT);
ZOPEN(&ts, FS_O_APPEND, -ENOENT);
ZOPEN(&ts, FS_O_TRUNC, -EACCES);
ZOPEN(&ts, FS_O_APPEND | FS_O_READ, -ENOENT);
ZOPEN(&ts, FS_O_APPEND | FS_O_WRITE, -ENOENT);
ZOPEN(&ts, FS_O_APPEND | FS_O_RDWR, -ENOENT);
ZOPEN(&ts, FS_O_TRUNC | FS_O_RDWR, -ENOENT);
ZOPEN(&ts, FS_O_TRUNC | FS_O_APPEND, -EACCES);
ZOPEN(&ts, FS_O_TRUNC | FS_O_RDWR | FS_O_APPEND, -ENOENT);
ZEND();
@ -319,6 +323,73 @@ void test_fs_open_flags(void)
ZUNLINK(&ts);
ZEND();
/** FS_O_TRUNC tests */
ZBEGIN("Attempt truncate a new file without write access");
ZOPEN(&ts, FS_O_CREATE | FS_O_TRUNC, -EACCES);
ZCLOSE(&ts);
ZUNLINK(&ts);
ZEND();
ZBEGIN("Attempt truncate a new file with write access");
ZOPEN(&ts, FS_O_CREATE | FS_O_WRITE | FS_O_TRUNC, 0);
ZCLOSE(&ts);
ZUNLINK(&ts);
ZEND();
ZBEGIN("Attempt truncate existing with no write access");
ZMKEMPTY(&ts);
ZOPEN(&ts, FS_O_TRUNC, -EACCES);
ZCLOSE(&ts);
ZUNLINK(&ts);
ZEND();
ZBEGIN("Attempt truncate existing with write access");
ZMKEMPTY(&ts);
ZOPEN(&ts, FS_O_TRUNC | FS_O_WRITE, 0);
ZCLOSE(&ts);
ZUNLINK(&ts);
ZEND();
ZBEGIN("Attempt truncate existing with read access");
ZMKEMPTY(&ts);
ZOPEN(&ts, FS_O_READ | FS_O_TRUNC, -EACCES);
ZCLOSE(&ts);
ZUNLINK(&ts);
ZEND();
ZBEGIN("Attempt truncate existing with R/W access");
ZMKEMPTY(&ts);
ZOPEN(&ts, FS_O_RDWR | FS_O_TRUNC, 0);
ZCLOSE(&ts);
ZUNLINK(&ts);
ZEND();
ZBEGIN("Attempt read on truncated file but no read access");
ZMKEMPTY(&ts);
ZOPEN(&ts, FS_O_WRITE | FS_O_TRUNC, 0);
#ifndef BYPASS_FS_OPEN_FLAGS_LFS_ASSERT_CRASH
ZREAD(&ts, -EACCES);
#else
TC_PRINT("Read bypassed\n");
#endif
ZCLOSE(&ts);
ZUNLINK(&ts);
ZEND();
ZBEGIN("Attempt append existing with WRITE access truncated file");
ZMKEMPTY(&ts);
ZOPEN(&ts, FS_O_APPEND | FS_O_WRITE | FS_O_TRUNC, 0);
ZCHKPOS(&ts, 0);
ZWRITE(&ts, ts.write_size);
#ifndef BYPASS_FS_OPEN_FLAGS_LFS_ASSERT_CRASH
ZREAD(&ts, -EACCES);
#else
TC_PRINT("Read bypassed\n");
#endif
ZCLOSE(&ts);
ZUNLINK(&ts);
ZEND();
/* This is simple check by file position, not contents. Since writing
* same pattern twice, the position of file should be twice the
@ -355,4 +426,24 @@ void test_fs_open_flags(void)
ZCLOSE(&ts);
ZUNLINK(&ts);
ZEND();
ZBEGIN("Check if file is truncated with data");
/* Prepare file */
ZUNLINK(&ts);
ZOPEN(&ts, FS_O_CREATE | FS_O_WRITE, 0);
ZWRITE(&ts, ts.write_size);
ZCLOSE(&ts);
/* Make sure file has the content */
ZOPEN(&ts, FS_O_CREATE | FS_O_READ, 0);
ZREAD(&ts, ts.write_size);
ZCLOSE(&ts);
ZOPEN(&ts, FS_O_TRUNC | FS_O_RDWR, 0);
ZCHKPOS(&ts, 0);
ZREAD(&ts, 0);
ZCLOSE(&ts);
ZUNLINK(&ts);
ZEND();
}