From 8a51a268190b39bce1e3b8a40f69b26111385894 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Sat, 24 Apr 2021 12:27:39 +0900 Subject: [PATCH] Allow truncation of open and modified files Regression introduced in f5bf41cf117e6dfc0d510348cdc1cc11817662ed. Fixes #1575. --- src/fdcache.cpp | 7 +++++-- test/integration-test-main.sh | 2 +- test/ut_test.py | 12 ++++++++++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/fdcache.cpp b/src/fdcache.cpp index b218a4c..29ed467 100644 --- a/src/fdcache.cpp +++ b/src/fdcache.cpp @@ -474,8 +474,11 @@ FdEntity* FdManager::Open(const char* path, headers_t* pmeta, off_t size, time_t ent = (*iter).second; ent->Dup(); if(ent->IsModified()){ - // If the file is being modified, it will not be resized. - size = -1; + // If the file is being modified and it's size is larger than size parameter, it will not be resized. + off_t cur_size = 0; + if(ent->GetSize(cur_size) && size <= cur_size){ + size = -1; + } } close = true; diff --git a/test/integration-test-main.sh b/test/integration-test-main.sh index b2b2c4b..277731e 100755 --- a/test/integration-test-main.sh +++ b/test/integration-test-main.sh @@ -1404,7 +1404,7 @@ function test_ensurespace_move_file() { return 1 fi if [ ${MOVED_FILE_LENGTH} -ne ${BIG_FILE_LENGTH} ]; then - echo "Failed to move file with file length" + echo "Failed to move file with file length: ${MOVED_FILE_LENGTH} ${BIG_FILE_LENGTH}" return 1 fi diff --git a/test/ut_test.py b/test/ut_test.py index a3abcd6..fa227b2 100755 --- a/test/ut_test.py +++ b/test/ut_test.py @@ -94,6 +94,18 @@ class OssfsUnitTest(unittest.TestCase): self.assertEqual(len(data1), len(data2)) self.assertEqual(data1, data2) + def test_truncate_open_file(self): + filename = "%s" % (self.random_string(10)) + fd = os.open(filename, os.O_CREAT|os.O_RDWR) + try: + os.write(fd, 'a' * 42) + self.assertEqual(os.fstat(fd).st_size, 42) + os.ftruncate(fd, 100) + self.assertEqual(os.fstat(fd).st_size, 100) + finally: + os.close(fd) + self.assertEqual(100, os.stat(filename).st_size) + if __name__ == '__main__': unittest.main()