The return value of reading the hole part of a hole file

Insert image description here
In a hole file, the part that is not explicitly written is called a "hole". When reading the hole part, the system will return data with a byte value of 0.

This means that when you read data in a hole file at an offset that is in the hole part, the read operation will return a byte sequence of all zeros. This is a behavior of the file system that automatically fills parts that are not explicitly written with 0s.

Note that this behavior does not apply to all operating systems or file systems. For example, in some file systems, reads from hole sections may return incorrect or invalid data. Therefore, when writing applications, it is best to properly initialize files or use file locks to ensure correct handling of hole files.

Here is an example showing how to read the contents of a hole file and detect hole parts:

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

#define FILE_SIZE 1024 * 1024 * 1024  // 1 GB

int main() {
    
    
    int fd = open("holed_file.txt", O_RDONLY);
    if (fd == -1) {
    
    
        perror("open");
        return 1;
    }

    char buffer[4096] = {
    
    0};
    ssize_t bytes_read;

    off_t offset = lseek(fd, 0, SEEK_SET);
    while ((bytes_read = read(fd, buffer, sizeof(buffer))) > 0) {
    
    
        // 检查读取的数据是否全是0(空洞部分)
        int is_hole = 1;
        for (int i = 0; i < bytes_read; i++) {
    
    
            if (buffer[i] != 0) {
    
    
                is_hole = 0;
                break;
            }
        }

        if (is_hole) {
    
    
            printf("Hole found at offset %ld\n", offset);
        } else {
    
    
            // 处理非空洞数据
            // ...
        }

        offset += bytes_read;
    }

    if (bytes_read == -1) {
    
    
        perror("read");
        return 1;
    }

    close(fd);
    return 0;
}

In the above example, a hole file named "holed_file.txt" is opened and the file contents are read in blocks. For each block read, check whether the data in it is all zeros. If it is all 0, it means that the block is a hole part, and the corresponding offset is printed.

The handling of hole files may vary based on operating system and file system differences. Therefore, in real applications, it is best to conduct detailed testing and verification for the target operating system and file system.
Insert image description here

Guess you like

Origin blog.csdn.net/qq_33471732/article/details/135036524