Insert bytes into middle of a file (in windows filesystem) without reading entire file (using File Allocation Table)?

前端 未结 8 1113
盖世英雄少女心
盖世英雄少女心 2020-12-13 19:04

I need a way to insert some file clusters into the middle of a file to insert some data.

Normally, I would just read the entire file and write it back out again with

8条回答
  •  一整个雨季
    2020-12-13 19:20

    You don't need to (and probably can't) modify the file access table. You can achieve the same using a filter-driver or a stackable FS. Let us consider a cluster size of 4K. I am merely writing out the design for reasons I explain at the end.

    1. Creation of a new file will a layout-map of the file in a header. The header will mention the number of entries and a list of entries. The size of the header will be the same as the size of the cluster. For simplicity let the header be of fixed size with 4K entries. For example suppose there was a file of say 20KB the header may mention: [DWORD:5][DWORD:1][DWORD:2][DWORD:3][DWORD:4][DWORD:5]. This file currently has had no insertions.

    2. Suppose someone inserts a cluster after sector 3. You can add it to the end of the file and change the layout-map to: [5][1][2][3][5][6][4]

    3. Suppose someone needs to seek to cluster 4. You will need to access the layout-map and calculate the offset and then seek to it. It will be after the first 5 clusters so will start at 16K.

    4. Suppose someone reads or writes serially to the file. The reads and writes will have to map the same way.

    5. Suppose the header has only one more entry left: we will need to extend it by having a pointer to a new cluster at the end of the file using the same format as the other pointers above. To know that we have more than one cluster all we need to do is to look at the number of items and calculate the number of clusters that are needed to store it.

    You can implement all of the above using a filter driver on Windows or a stackable file-system (LKM) on Linux. Implementing the basic level of functionality is on the level of a grad-school mini project in difficulty. Getting this to work as a commercial filesystem can be quite challenging especially since you don't want to affect IO speeds.

    Note that the above filter will not be affected by any change in disk layout / defragmentation etc. You can also defragment your own file if you think it will be helpful.

提交回复
热议问题