Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ftruncate failed when the original file size = 0 and the extended part is not continuous clusters. #66

Open
Erichorng opened this issue Feb 7, 2024 · 3 comments

Comments

@Erichorng
Copy link

Erichorng commented Feb 7, 2024

I built the exfat module with the latest code on mainline.
I encountered a bug during the bellow experiment:

steps to reproduce:

  1. create 4G exfat filesystem.
  2. create a file and truncate to 3G.
  3. delete the file.
  4. repeat step 2 again.
  5. receive EIO, ftruncate failed.

Here is the script I used for testing:

#include <iostream>
#include <fcntl.h>
#include <unistd.h>

int main() {
    const char* filename = "example_file";
    off_t file_size = 3LL * 1024 * 1024 * 1024;

    // Open the file or create it if it doesn't exist
    int fd = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
    if (fd == -1) {
        std::cerr << "Error opening/creating the file" << std::endl;
        return 1;
    }

    // Set the file size using ftruncate
    if (ftruncate(fd, file_size) == -1) {
        std::cerr << "Error setting file size using ftruncate" << std::endl;
        close(fd);
        return 1;
    }

    // Cleanup
    close(fd);

    return 0;
}

and this is a patch I wrote to fix the bug:
0001-fix-bug-ftruncate-failed.patch
Please help check if this modification is correct and necessary

@namjaejeon
Copy link
Owner

@YuezhangMo There is one more bug in valid-size patch. Can you check it first ?

@Erichorng
Copy link
Author

By the way, I'd like to share the results of the experiment. If it's not a 4GB file system but a larger one, let's say 8GB, and you repeatedly create and delete 1GB files, you'll encounter issues on the eighth creation. The reason is that during the eighth ftruncate, the expanded portion consists of non-contiguous clusters.

@namjaejeon
Copy link
Owner

@Erichorng Right. Thanks for your check:) @YuezhangMo will hopefully check this issue next week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants