File address management method, terminal equipment and storage medium
By determining the partition type based on file attribute information and obtaining logical block addresses and building physical layout information, the problem of difficulty in obtaining the physical location of files on Android platform is solved, the acquisition efficiency and accuracy are improved, and the accuracy of file access is ensured.
Patent Information
- Application Number
- CN202510646258.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-05-20
- Publication Date
- 2025-06-24
- Estimated Expiration
- 2045-05-20
AI Technical Summary
The physical location of files on Android is difficult, complex, efficient and accurate, especially in scenarios where precise control of storage areas is required.
By determining the partition type to which the target file belongs based on the attribute information of the target file, and obtaining the logical block address of the partition where the target file is located on the physical storage device according to the partition type, a data structure is constructed to store the information to form physical layout information.
It realizes a method of effectively obtaining file storage locations in the Android system, improves the efficiency and accuracy of file storage locations, ensures the accuracy and reliability of file access, and is suitable for file location requirements in various scenarios.
Smart Images

Figure CN120196598A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the technical field of address management, and particularly to a file address management method, a terminal device, and a storage medium. Background Art
[0002] In the current architecture of the Android platform, the storage and access mechanisms of files mainly rely on the management strategies of the file system and block devices. However, in practical applications, especially in certain specific requirement scenarios, obtaining the start and end logical block addresses (LBAs) of files faces many challenges. Most existing file systems encapsulate the storage locations of files, making it difficult for ordinary application layers to directly obtain the logical block addresses of files on physical storage devices. This situation is particularly intractable in scenarios such as data recovery and file anti-tampering that require precise control of storage areas.
[0003] The management of the file system and the underlying storage hardware is usually decoupled. Although this design improves the flexibility and maintainability of the system, it also brings new problems. High-level file operating system calls cannot directly access or control the LBA information of the underlying storage device, which undoubtedly limits the developers' ability to perform refined management of storage. In the Android system, due to its special optimization for mobile devices, device mapping technologies, such as dm-verity, etc., have been widely used. These technologies aim to enhance the security and integrity of the system, but at the same time, they further abstract and hide the access to the physical storage of files, making the actual physical LBA address of files deeply buried under a more complex mapping layer and difficult to directly obtain.
[0004] Therefore, how to obtain the physical location of files on the Android system has become an important problem that needs to be solved urgently. Summary of the Invention
[0005] In view of this, embodiments of this application provide a file address management method, a terminal device, and a storage medium, which can effectively solve the problems of difficult, complex, inefficient, and inaccurate acquisition of file addresses on the Android platform.
[0006] In a first aspect, embodiments of this application provide a file address management method, including: Determine the partition type to which the target file belongs according to the attribute information of the target file; Obtain the logical block address of the partition to which the target file belongs on the physical storage device according to the partition type; Construct a data structure, and store the logical block address of the partition to which the target file belongs on the physical storage device into the data structure to form physical layout information; Obtain the logical block address of the target file on the physical storage device from the physical layout information according to the partition type.
[0007] In some embodiments, determining the partition type to which the target file belongs according to the attribute information of the target file includes: Obtaining the device number of the block device to which the target file belongs according to the attribute information of the target file; Determining the path information corresponding to the block device according to the device number; Obtaining the partition type to which the target file belongs according to the path information corresponding to the block device.
[0008] In some embodiments, obtaining the partition type to which the target file belongs according to the path information corresponding to the block device includes: When the path information of the block device contains a first field, determining that the partition type to which the target file belongs is a physical partition; When the path information of the block device contains a second field, determining that the partition type to which the target file belongs is a logical partition.
[0009] In some embodiments, when the path information of the block device contains a second field, it further includes: When there is only one second field in the path information of the logical partition, determining that the partition is a logically partitioned logical partition; When the path information of the logical partition contains multiple second fields, determining that the partition is a secondarily mapped logical partition.
[0010] In some embodiments, obtaining the logical block address of the partition to which the target file belongs on the physical storage device according to the partition type includes: When the partition type to which the target file belongs is the physical partition, reading the corresponding path information according to the device number of the block device to which the target file belongs, and obtaining the starting logical block address and the ending logical block address of the physical partition; When the partition type to which the target file belongs is the logical partition, determining the device path of the corresponding logical partition according to the device number of the block device to which the target file belongs, parsing the mapping relationship of the device according to the device path, and obtaining the logical block address of the partition to which the target file belongs according to the mapping relationship.
[0011] In some embodiments, obtaining the logical block address of the partition to which the file belongs according to the mapping relationship includes: When the logical partition is a once-mapped partition type, determine the corresponding logical partition device path according to the device number of the file; read the device path of the physical partition according to the device path; determine the corresponding node information according to the device path of the physical partition to obtain the starting logical block address and the ending logical block address of the physical partition; When the logical partition is a twice-mapped partition type, determine the corresponding logical partition device path according to the device number of the target file; read the device path of the upper-layer logical partition according to the device path of the twice-mapped logical partition, and read the device path of the physical partition according to the device path of the upper-layer logical partition; determine the corresponding node information according to the device path of the physical partition to obtain the starting logical block address and the ending logical block address of the physical partition.
[0012] In some embodiments, the constructing a data structure and storing the logical block addresses of the partition to which the target file belongs on the physical storage device into the data structure to form physical layout information includes: Define the data structure to store the logical block addresses of the partition to which the target file belongs on the physical storage device; Loop to call a preset interface to obtain all the logical block addresses of the partition to which the target file belongs on the physical storage device, and store the logical block address returned by each call into the data structure to form the physical layout information.
[0013] In some embodiments, the obtaining the logical block address of the target file on the physical storage device from the physical layout information according to the partition type includes: When the partition type is a physical partition, calculate the logical block address of the target file on the physical storage device according to the starting logical block address and the ending logical block address of the physical partition, in combination with the physical layout information; When the partition type is a once-mapped logical partition, calculate the logical block address of the target file on the physical storage device in segments according to the starting logical block address and the ending logical block address of the physical partition, based on the mapping table, in combination with the physical layout information; When the partition type is a twice-mapped logical partition and the logical partition is a proportional mapping, calculate the logical block address of the target file on the physical storage device according to the starting logical block address and the ending logical block address of the physical partition, using the mapping table, in combination with the physical layout information.
[0014] In some embodiments, when the partition type is a physical partition, the logical block address of the target file on the physical storage device is calculated by the following formula:
[0015] Among them, represents the nth logical block address pair of the target file in the physical storage device, represents the starting logical block address of the physical partition where the target file is located, represents the offset relative to the starting address ; + represents the logical block address of the target file on the physical storage device.
[0016] In some embodiments, when the partition type is a one-time mapped logical partition, the logical block address of the target file on the physical storage device is calculated by the following formula: Calculate the logical block address of each mapped area in the physical partition by the following formula:
[0017] Among them, represents the mth dm mapped segment area; represents the logical block address range of the mth mapped area in the physical partition; represents the mapping offset of the mth segment inside the logical partition; represents the logical block address range of the mth mapped segment area in the physical partition; represents the corresponding logical block address range of the same mapped segment area in the logical partition; Calculate the logical block address of the target file on the physical storage device by the following formula:
[0018] Among them, represents a section of the target file on the physical partition, that is, the nth logical block address interval; represents the starting logical block address of the physical partition where the target file is located; ... represents the mapping start offset of each dm mapped segment area in the physical partition; ... represents the mapping offset of the corresponding mapped segment area inside the logical partition; represents the starting logical block address of the 1st dm mapped segment area in the physical partition, corresponding to the logical interval , ; ... represents the offset of the target file in the logical partition.
[0019] In a second aspect, an embodiment of the present application provides a terminal device, which includes a processor and a memory. The memory stores a computer program, and the processor is configured to execute the computer program to implement the file address management method in the first aspect above.
[0020] In a third aspect, an embodiment of the present application provides a computer-readable storage medium. When the computer program is executed on a processor, the file address management method in the first aspect above is implemented.
[0021] The embodiments of the present application have the following beneficial effects: The file address management method, terminal device, and storage medium of the present application determine the partition type to which the target file belongs according to the attribute information of the target file, and obtain the logical block address of the partition where the target file is located on the physical storage device according to the partition type, and construct a data structure to store this information, thereby forming a physical layout. According to the partition type, the logical block address of the target file can be accurately obtained from the physical layout information. The method of the present application can not only effectively obtain the file storage location in the ordinary data partition of the Android system, but also handle read-only partitions and protected partitions in high-security scenarios, especially applicable to secondary mapping partitions involving protection mechanisms such as dm-verity. The present application significantly improves the efficiency and accuracy of obtaining the file storage location, ensures the accuracy and reliability of file access, and meets the file positioning requirements in various scenarios. BRIEF DESCRIPTION OF THE DRAWINGS
[0022] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the following will briefly introduce the drawings required for the embodiments. It should be understood that the following drawings only show some embodiments of the present application, and therefore should not be regarded as limiting the scope. For those of ordinary skill in the art, other related drawings can be obtained based on these drawings without creative efforts.
[0023] Figure 1 Shows a flowchart of a file address management method according to an embodiment of the present application; Figure 2 Shows a schematic diagram of the physical partition to which a file belongs in a file address management method according to an embodiment of the present application; Figure 3 Shows a schematic diagram of the logical partition to which a file belongs in a file address management method according to an embodiment of the present application; Figure 4 Shows another schematic diagram of the logical partition to which a file belongs in a file address management method according to an embodiment of the present application; Figure 5 Shows a schematic diagram of the distribution of a file within a physical partition in a file address management method according to an embodiment of the present application; Figure 6 It shows another distribution schematic diagram of a file within a physical partition in a file address management method according to an embodiment of the present application. Detailed implementation manners
[0024] Next, the technical solutions in the embodiments of the present application will be clearly and completely described with reference to the accompanying drawings in the embodiments of the present application. Obviously, the described embodiments are only a part of the embodiments of the present application, rather than all the embodiments.
[0025] Generally, the components of the embodiments of the present application described and illustrated herein can be arranged and designed in various different configurations. Therefore, the following detailed description of the embodiments of the present application provided in the drawings is not intended to limit the scope of the claimed present application, but merely represents selected embodiments of the present application. All other embodiments obtained by those skilled in the art based on the embodiments of the present application without creative efforts belong to the scope of protection of the present application.
[0026] In the following, the terms "including", "having" and their cognates that can be used in various embodiments of the present application are only intended to represent specific features, numbers, steps, operations, elements, components or combinations of the foregoing items, and should not be construed as first excluding the existence of one or more other features, numbers, steps, operations, elements, components or combinations of the foregoing items or increasing the possibility of one or more features, numbers, steps, operations, elements, components or combinations of the foregoing items. In addition, the terms "first", "second", "third", etc. are only used for distinguishing descriptions and cannot be construed as indicating or implying relative importance.
[0027] Unless otherwise defined, all terms (including technical terms and scientific terms) used herein have the same meaning as commonly understood by those of ordinary skill in the art to which the various embodiments of the present application belong. The terms (such as those defined in a general use dictionary) will be interpreted as having the same meaning as the contextual meaning in the relevant technical field and will not be interpreted as having an idealized meaning or being overly formal, unless clearly defined in the various embodiments of the present application.
[0028] Next, some implementation manners of the present application will be described in detail with reference to the accompanying drawings. Without conflict, the following embodiments and the features in the embodiments can be combined with each other.
[0029] Considering the difficulties, complexities, low efficiency, and low accuracy in obtaining file addresses on the Android platform, a file address management method is proposed. The file address management method of this application is a management method based on the Android platform. Specifically, this application determines the partition type to which the target file belongs according to the attribute information of the target file, obtains the logical block address of the partition where the target file is located on the physical storage device according to the partition type, constructs a data structure to store this information, thereby forming a physical layout, and then can accurately obtain the address of the target file from the physical layout information according to the partition type. The method of this application can not only effectively obtain the file storage location in the ordinary data partition of the Android system, but also handle read-only partitions and protected partitions in high-security scenarios, and is particularly suitable for secondary mapping partitions involving protection mechanisms such as dm-verity. Among them, dm-verity is a sub-module of Device Mapper used to verify the integrity of block device data. It significantly improves the efficiency and accuracy of obtaining the file storage location, ensures the accuracy and reliability of file access, can accurately obtain the logical block address of files in partitions without Device Mapper (dm) device mapping, and supports the logical block address of files in partitions with dm mapping, meets the file positioning requirements in various scenarios, and improves the efficiency and security of file access.
[0030] Figure 1 FIG. shows a flowchart of the file address management method according to an embodiment of this application. Exemplarily, the file address management method is based on the Android platform and includes the following steps: Step S100, determine the partition type to which the target file belongs according to the attribute information of the target file.
[0031] Exemplarily, the attribute information refers to the metadata related to the file. The attribute information describes the basic characteristics of the file, and the attribute information may include the following content: For example, file path, device ID, inode, file size, etc. Among them, the device ID represents the disk device where the file is located. The device ID indicates the unique identifier of the storage device or partition where the file is located, and is usually composed of the major device number and the minor device number.
[0032] The partition type refers to the type of the partition where the file system is located. For example, (1) Ordinary partition (file systems such as ext4, NTFS, FAT32, etc.): A partition created directly on a physical hard disk or virtual storage device without an additional mapping mechanism. The operations of the file system are directly mapped to the blocks of the underlying storage device. (2) Device Mapper (dm) device: Used to create virtual devices. The dm device itself is not directly mapped to physical storage, but rather maps logical devices to physical devices through a mapping layer. When using a dm device, data is usually not directly stored in a physical partition but in a virtual logical partition. (3) dm-verity device: dm-verity is a special type of device based on Device Mapper. It is used to provide read-only, tamper-proof data verification and checking functions for storage devices. The goal of dm-verity is to ensure data integrity and is typically applied to encrypted disks or systems that require data integrity assurance. Moreover, dm-verity is a two-layer mapping, that is, first map the file to a virtual partition and then to the actual physical device.
[0033] That is, based on the device number (st_dev) of the file, it can be determined whether the storage device where the file is located is an ordinary partition or a device that undergoes virtual mapping through Device Mapper (such as a dm device), or a device that uses dm-verity to provide integrity protection.
[0034] In an optional embodiment, in step S100, determining the partition type to which the target file belongs according to the attribute information of the target file includes: Step S110, obtaining the device number of the block device to which the target file belongs according to the attribute information of the target file.
[0035] Exemplarily, use the file operation functions provided by the operating system (such as open()) to open the target file and obtain the file handle (i.e., file descriptor). The file handle is an integer used to identify an opened file and can be used to reference the file in subsequent operations. Use the file handle to call the fstat() function to obtain the attribute information of the file. The fstat() function will return a stat structure containing file metadata. This structure contains multiple fields, and the most important ones are: Device number (st_dev): The device number of the device (partition) where the file is located (composed of the major device number and the minor device number).
[0036] inode number (st_ino): The unique identifier of the file, usually used for internal indexing in the file system.
[0037] File size (st_size): The byte size of the file.
[0038] File permissions, last access time, etc.
[0039] Extract the device number st_dev from the stat structure. The device number st_dev consists of the major device number and the minor device number. The major device number identifies the type of the device, and the minor device number identifies a specific partition on the device. For example, if the major device number in st_dev is 8 and the minor device number is 1, it may represent the partition / dev / sda1.
[0040] Step S120: Determine the path information corresponding to the block device according to the device number.
[0041] Exemplarily, according to the device number (st_dev) to which the file belongs, find the path of the device where the file is located. Block devices are usually located in the / dev / or / dev / block / directory. The specific path depends on the device type.
[0042] Step S130: Determine the partition type to which the file belongs according to the path information corresponding to the block device.
[0043] Exemplarily, if the block device path of the file belongs to a normal partition (for example, sd, nvme, mmcblk), then the partition where the file is located is a normal physical partition, where the physical partition is the embodiment of the Android system partition table.
[0044] If the block device path of the file belongs to a device of the dm type (for example, starting with dm-, dm-0, dm-1), then the partition where the file is located is a Device Mapper device. If the block device path belongs to a dm-verity device (for example, dm-verity, mapper / verity_device), it means it belongs to a two-layer mapping device, which is usually used to ensure the integrity of the file system.
[0045] That is, according to the device path, judge the device type (physical partition, dm device, dm-verity device, etc.), so as to determine the partition type to which the file belongs.
[0046] In an alternative embodiment, in step S130, obtaining the partition type to which the target file belongs according to the path information corresponding to the block device includes: When the path information of the block device contains a first field, which usually starts with fields such as sd, nvme, or mmcblk, it can be determined that the partition to which the file belongs is a physical partition. A physical partition refers to a partition directly mapped to a hard disk or a solid-state drive, without involving any logical volume management or virtualization layer, as Figure 2 shown.
[0047] When the path information of the block device contains a second field (usually starting with fields such as / dev / dm- or / dev / mapper / ), it can be determined that the partition to which the file belongs is a logical partition. A logical partition is a virtual device created through a Device Mapper or similar virtualization mechanism, and common applications include LVM (Logical Volume Management), RAID, encrypted partitions, etc.
[0048] In an alternative embodiment, a once-mapped logical partition means that the partition where the file is located is directly mapped to a physical storage device or partition, and there is no intermediate virtualization or additional mapping layer. In the path information of this type of partition, only one second field (dm- or mapper / etc. field) is included. That is to say, the device path is directly mapped to the virtual device of the physical storage device without going through multiple layers of mapping, as Figure 3 shown.
[0049] For example, dm-0: only one dm- field is included in this path, indicating a once-mapped logical partition.
[0050] A twice-mapped logical partition means that the partition where the file is located undergoes two layers of mapping, that is, a logical partition on the Device Mapper is further mapped to another virtual layer or physical device. In the path information of this type of partition, multiple second fields (for example, dm- or mapper) are included, which means that the device path goes through multiple mapping layers, thus forming a twice or multiple mapping, as Figure 4 shown.
[0051] For example, dm-0 and dm-1: if there are multiple dm-X fields in the device path, it means that the device has undergone multiple mappings.
[0052] / dev / mapper / verity_device: This is a dm-verity device, indicating a twice-mapped logical partition. It should be noted that the dm-verity type mapping belongs to the original ratio mapping, that is, there is no offset mapping.
[0053] Step S200, according to the partition type, obtain the logical block address of the partition to which the target file belongs on the physical storage device.
[0054] Exemplarily, the partition type is an important attribute of the storage device where the file is located, which determines how the file is stored on the disk. According to the different partition types, the method for obtaining the storage location of the file is also different.
[0055] For ordinary partitions (e.g., ordinary ext4 or NTFS, etc.), the location of a file on a physical storage device can be directly calculated through the starting LBA of the partition where the file is located. The logical block address (LBA) of the file plus the starting LBA of the partition is the physical location of the file on the disk.
[0056] For dm devices, the location (LBA) of a file in a logical partition is calculated through a mapping table. First, the mapping relationship of the dm device needs to be parsed to obtain the relative LBA of the file, and then the logical partition is mapped to the actual physical partition, and the starting LBA of the physical partition is combined to calculate the actual physical location of the file.
[0057] For dm-verity devices, since dm-verity is a read-only verification device, the storage location of a file may be a secondary mapping. First, the dm-verity mapping table needs to be parsed to obtain the logical block address (LBA) of the file on the dm-verity device. Then, continue to parse the mapping relationship of its next-level device (usually a physical device) to finally obtain the LBA of the physical storage device.
[0058] In one embodiment, in step S200, according to the partition type, obtaining the logical block address of the partition to which the target file belongs on the physical storage device includes: Step S210, when the partition type to which the file belongs is a physical partition, according to the device number of the block device to which the target file belongs, read the corresponding path information to obtain the starting logical block address and the ending logical block address of the physical partition; Exemplarily, when the partition type to which the file belongs is a physical partition (i.e., the file is directly stored in a partition of a hard disk or other physical storage device), first, according to the device number of the partition to which the file belongs, find the corresponding device information path in the / sys / block / directory. For example, the path / sys / block / sdx / sdxy / start refers to the starting logical block address (LBA) of a certain partition (e.g., / dev / sdxy) on a certain storage device (e.g., / dev / sdx). Among them, the start node stores the starting LBA value of the partition, indicating the starting position of the partition on the physical disk.
[0059] In addition, the ioctl function can also be used to obtain the hard disk geometry information. ioctl(fd, HDIO_GETGEO, &g) is a system call interface for device control and management. Here, fd is the file descriptor of the block device file, representing the hard disk device. HDIO_GETGEO is the command to obtain the hard disk geometry information. This command will return the hard disk geometry information through the ioctl call. &g: This is a pointer to the hd_geometry structure. The hd_geometry structure is used to store the hard disk geometry information. ioctl will fill the hard disk geometry information into this structure. g is a variable and is a declared and prepared variable of the hd_geometry type. In particular, it also includes the starting LBA information of the partition where the target file is located.
[0060] That is, when ioctl(fd, HDIO_GETGEO, &g) is called, the operating system will access the hard disk device through fd and send a HDIO_GETGEO request to the device. The hard disk device driver will respond to this request and fill the hard disk geometry information into the hd_geometry structure pointed to by g. These information include the number of heads of the hard disk and the number of sectors per head, etc., which are usually used to describe the physical structure of the hard disk.
[0061] Step S220, when the partition type to which the target file belongs is a logical partition, determine the device path of the corresponding logical partition according to the device number of the block device to which the target file belongs, parse the mapping relationship of the device according to the device path, and obtain the logical block address of the partition to which the file belongs according to the mapping relationship.
[0062] Exemplarily, when the partition type to which the file belongs is a logical partition (for example, virtualized partitions such as LVM, RAID, dm-verity, etc.), according to the device number of the file, the device path of the logical partition where the file is located can be determined. The device path is usually / dev / dm-X or / dev / mapper / lvm_volume, etc., pointing to a virtual device created through the Device Mapper or a similar mechanism. By parsing the device path, the mapping relationship of the device can be obtained, that is, how the logical partition is mapped to the physical storage device. The mapping relationship is usually obtained through a mapping table. Through these mapping relationships, the actual location of the file data on the physical device can be obtained. According to the mapping relationship, the logical block address (LBA) of the file in the logical partition can be obtained, representing the storage location of the file in the logical partition.
[0063] That is, for a physical partition, the partition path is obtained through the device number of the file, and the starting LBA and ending LBA of the physical partition are directly obtained. For a logical partition, the logical partition path is determined through the device number, and the logical block address of the file in the partition is obtained after parsing the mapping relationship.
[0064] In an optional embodiment, when the partition type to which the target file belongs is a once-mapped logical partition, the data of the file is stored in a logical partition mapped to a physical storage device through Device Mapper or a similar virtualization technology. In this case, first, according to the device number of the file, the corresponding logical partition device path is determined. For example, / sys / block / dm-x / slaves / . Then, based on this device path, the device path of the physical partition sdxy is further parsed. Usually, this path points to the path of the actual storage device. Then, from the device path of the physical partition, the start node value of the physical partition / sys / block / sdx / sdxy / start is obtained. This information contains the starting and ending logical block addresses of the partition on the physical storage device. Through these starting LBA and ending LBA, the storage range of the file in the physical partition can be determined, so as to locate the actual position of the file on the physical storage device.
[0065] When the partition type to which the target file belongs is a twice-mapped logical partition, the data of the file is stored on a logical partition that has been mapped twice. First, according to the device number of the file, the logical partition device path where the file is located is determined. For example, / sys / block / dm-x / slaves / . By accessing this path, the physical partition block device actually mapped by the dm device can be found. Among them, the slaves directory contains the path of the actually mapped physical storage device.
[0066] Then, use ioctl(fd, HDIO_GETGEO, &g) to obtain the geometric information of the hard disk, or by accessing the / sys / block / sdx / sdxy / start node, which stores the starting LBA value of the partition. These values represent the actual storage range of the file on the physical storage device. Through this information, the position of the file on the physical storage device can be accurately calculated.
[0067] Step S300, construct a data structure, store the logical block address of the partition to which the target file belongs on the physical storage device into the data structure, so as to form and use the physical layout information.
[0068] Exemplarily, a data structure is constructed to store the logical block address (LBA) of a file on a physical storage device and related information, and this information is organized into physical layout information. Through the data structure, the storage location of the file on the physical storage device can be effectively managed and traced, especially in the case of logical partitions and multi-layer mappings.
[0069] Specifically, by constructing a data structure to store the physical layout information of a file, that is, the logical block address of the file in the physical storage device and the storage information of the file at different mapping levels. That is, by constructing a data structure, it is possible to support the location and access of files, especially in complex storage architectures (such as logical partitions, virtual devices, multi-layer mappings, etc.), ensuring that the physical storage location of the file can be found quickly and accurately. And as the storage structure changes, the data structure can be flexibly modified and updated to support more device and partition types.
[0070] In one embodiment, in step S300, a data structure is constructed to store the logical block address of the partition to which the target file belongs on the physical storage device into the data structure to form and use physical layout information, including: Step S310, define a data structure to store the logical block address of the partition to which the target file belongs on the physical storage device.
[0071] Exemplarily, a data structure is defined to store the logical block address (LBA) of a file on a physical storage device. These logical block addresses represent the locations where the file data is stored on the physical storage device. The logical block address (LBA) is the specific location where the file data is stored on the disk, usually represented as a starting address and an ending address. The data structure may be a linked list or an array used to store multiple physical ranges of a file, and the starting LBA and ending LBA of each range are stored in the fields of the structure.
[0072] Step S320, loop to call a preset interface to obtain all the logical block addresses of the partition to which the target file belongs on the physical storage device, and store the logical block address returned by each call into the data structure to form physical layout information.
[0073] Use a predefined interface (for example, ioctl(fd, FS_IOC_FIEMAP, &fiemap)) to obtain the logical block address of a file on a physical storage device. Here, fd is the file handle of the target file, and fs is the structure struct fiemap used to describe the physical block mapping situation of the file on the storage device. Since the data of a file may not be continuously stored in multiple physical ranges and the mapping of the file on the physical storage device may be discontinuous, it is necessary to loop to call ioctl multiple times to obtain the complete file mapping.
[0074] Each time the interface is called, it returns the physical storage intervals of the file data, which are composed of multiple continuous physical blocks. Specifically, the physical layout of the file target_file is a continuous storage interval in n segments:
[0075] in, , … It is a multi-segment mapping area of the file in the partition. Due to the fragmentation management of the file system, the data blocks of the file will be stored in different blocks. Therefore, such a multi-segment mapping area is formed. The interval in the curly braces represents the start and end LBA of each mapping area relative to the partition to which it belongs, represented by uppercase X; and the interval in the square brackets represents the internal offset of the target file mapped by the mapping area, represented by lowercase x.
[0076] The logical block address returned by each interface call will be stored in the previously defined data structure. The data structure will gradually accumulate all LBA information to represent the physical layout of the file on the disk. If the file data is distributed in multiple physical intervals, these intervals will be stored one by one in the data structure. Usually, a linked list is used to save multiple consecutive physical interval information. When all logical block addresses are stored in the data structure, the physical layout information of the file on the physical storage device is formed. This layout information can be used for subsequent operations, such as file positioning, defragmentation, data recovery, etc.
[0077] Step S400: acquiring the logical block address of the target file on the physical storage device from the physical layout information according to the partition type.
[0078] Exemplarily, physical partition: If the target file is located in a physical partition (e.g., / dev / block / sdxX), then based on the starting LBA of the partition ( ) and end LBA ( ), combined with the multi-segment distribution of the file in the physical layout information, the actual LBA of the file on the physical storage device is calculated by directly accumulating the segment offset values.
[0079] Once-mapped logical partition: If the target file is located in a once-mapped dm logical partition (for example, / dev / block / dm-x), it is necessary to traverse the mapping area to which the file belongs, determine whether it is within the segment offset range of the dm mapping table, and calculate the file physical LBA segment by segment using the formula.
[0080] Logical Partition of Secondary Mapping: If the target file is located in the dm-verity logical partition of the secondary mapping, based on its non-offset mapping feature and the consistent mapping relationship between the parent and child devices, reuse the segmented calculation method of the primary mapping, and combine the consistency of the mapping relationship between the dm parent device and the child device to directly calculate the physical LBA of the file through the same formula.
[0081] In one embodiment, in step S400, according to the partition type, obtain the logical block address of the target file on the physical storage device from the physical layout information, including: Step S410, when the partition type is a physical partition, calculate the logical block address of the target file on the physical storage device according to the starting logical block address and the ending logical block address of the physical partition, in combination with the physical layout information.
[0082] For example, as Figure 5 shown, the mapped address of the physical disk corresponding to the physical partition sdxx to which the file belongs is set as , , is the starting point of the partition, is the end point of the partition, representing the ending logical block address of the physical partition, that is, the end address, which is used to limit the upper bound of the following offset values. That is , is the complete LBA range of the physical partition to which the file belongs in the entire disk. Then the actual distribution information of the target file on the physical disk is:
[0083] Among them, represents the nth logical block address interval of the target file on the physical storage device; represents the starting logical block address of the physical partition where the target file is located, that is, the starting point address; represents the offset relative to the starting address ; + represents the logical block address of the target file on the physical storage device.
[0084] The distribution information of the target file on the physical disk is represented by logical block address pairs, and each logical block address pair is composed of the starting logical block address plus the corresponding offset . For example, for represents the starting and ending addresses of the file on the first logical block address pair, that is, and Through these formulas, the specific storage location of the file on the physical disk can be determined, thereby achieving the correct reading and storage of the file.
[0085] Step S420, when the partition type is a once-mapped logical partition, based on the starting logical block address and the ending logical block address of the physical partition, and in combination with the mapping table and the physical layout information, calculate the logical block address of the target file on the physical storage device in segments.
[0086] As Figure 6 shown, when the partition is a once-mapped logical partition, first, obtain the starting LBA when the dm device maps the actual physical partition. Then, obtain the mapping table table of the dm device through the dmctl tool (or related source code tools), and obtain the starting LBA of the dm-mapped physical partition through the mapping table table. Assuming that there are multiple segments of mapping in the dm-mapped physical partition, let it be divided into m segments, and the mapped LBA of each segment is:
[0087] Among them, 、 … are the multiple segments of the dm partition actually mapping the physical partition. Each dm partition may also map multiple segments of the physical partition. The interval within the curly brackets represents the LBA range of each mapped area in the physical partition, denoted by the capital letter Y; while the interval within the square brackets represents the mapping offset within the dm partition, denoted by the lowercase letter y; is the LBA interval of this mapped segment in the physical partition sdxx; is the corresponding LBA interval in the dm logical partition for the same mapped segment; the two intervals correspond one by one.
[0088] Assume that the LBA range of the physical partition sdxx mapped by the dm device on the entire physical disk is , , where represents the starting logical block address (starting point) of the physical partition where the target file is located; represents the ending logical block address (ending point) of the physical partition. According to the offset of the target file in the dm device and the above two offsets, calculate the actual distribution on the physical disk: traverse ~ for each mapped area belonging to each segment on - whether the start and end LBAs (the interval within the curly brackets, capital X) relative to the belonging partition are within the offset range within each dm partition of the dm mapping ~ (the interval within the square brackets, lowercase y). If the requirements are met, then use the start and end LBAs of each segment within the curly brackets in this dm segment in the physical partition for calculation. The calculation formula is:
[0089] Among them, represents a distribution area of the target file on the physical partition, that is, the nth logical block address interval (starting LBA, ending LBA); represents the starting logical block address (starting point) of the physical partition where the target file is located; ... represents the mapping starting offset of each dm mapping segment in the physical partition (relative to the capital Y); that is, ... represents the identifier of the segment Y in the brackets where the interval where area A is located is included in the interval where area B is located; ... represents the mapping starting offset of the corresponding mapping segment inside the logical partition; Among them, , specifically corresponds to the start and end LBA offsets of the first mapping segment in the physical partition, paired with the logical interval , , and the subsequent { , } corresponds to +1, , …, { , } corresponds to -1+1, and so on; ... represents the offset of the target file in the logical partition.
[0090] Step S430, when the partition type is a logical partition with secondary mapping and the logical partition is a proportional mapping, calculate the logical block address of the target file on the physical storage device according to the starting logical block address and the ending logical block address of the physical partition, using the mapping table and combining with the physical layout information.
[0091] When the partition is a logical partition with secondary mapping, the dm-verity type mapping belongs to a proportional mapping, that is, a mapping without offset. The mapping status of the dm parent device is consistent with the mapping relationship of the child dm device (dm-verity device). Therefore, the method for calculating the LBA is the same as that when the partition is a logical partition with primary mapping. Refer to the explanation in step S420 above and will not be elaborated here.
[0092] The file address management method according to the embodiments of the present application can not only accurately obtain the LBA of files in the dm unmapped partition, but also support obtaining the LBA of files in the dm mapped partition, and is particularly applicable to the secondary mapped partition involving protection mechanisms such as dm-verity. The file address management method according to the embodiments of the present application can efficiently locate the file storage location in the ordinary data partition of the Android system, and meet the file access requirements in high-security scenarios (for example, read-only partitions and protected partitions), effectively improving the efficiency and accuracy of obtaining the file storage location, and ensuring the accuracy and reliability of file access.
[0093] The present application also provides a terminal device. Exemplarily, the terminal device includes a processor and a memory. Among them, the memory stores a computer program, and the processor executes the functions of each module in the above file address management method by running the computer program.
[0094] Among them, the processor can be an integrated circuit chip with signal processing capabilities. The processor can be a general-purpose processor, including at least one of a central processing unit (CPU), a graphics processing unit (GPU), a network processor (NP), a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field programmable gate array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, and discrete hardware components. The general-purpose processor can be a microprocessor or the processor can also be any conventional processor, etc., and can implement or execute the various methods, steps, and logic block diagrams disclosed in the embodiments of the present application.
[0095] The memory can be, but is not limited to, a random access memory (RAM), a read-only memory (ROM), a programmable read-only memory (PROM), an erasable programmable read-only memory (EPROM), an electrically erasable programmable read-only memory (EEPROM), etc. Among them, the memory is used to store the computer program, and after receiving the execution instruction, the processor can execute the computer program accordingly.
[0096] The present application also provides a computer-readable storage medium for storing the computer program used in the above terminal device. For example, the computer-readable storage medium may include, but is not limited to: various media such as USB flash drives, mobile hard disks, read-only memories (ROMs), random access memories (RAMs), magnetic disks, or optical discs, etc., which can store program codes.
[0097] In several embodiments provided by the present application, it should be understood that the disclosed apparatus and method can also be implemented in other ways. The apparatus embodiments described above are merely illustrative. For example, the flowcharts and structure diagrams in the accompanying drawings show the possible architectures, functions, and operations of the methods and computer program products according to multiple embodiments of the present application. In this regard, each block in the flowchart or block diagram may represent a module, a program segment, or a part of code, and the module, program segment, or part of code contains one or more executable instructions for implementing the specified logical function. It should also be noted that in an alternative implementation, the functions marked in the blocks may occur in a different order than marked in the accompanying drawings. For example, two consecutive blocks may actually be executed substantially in parallel, and they may sometimes be executed in the reverse order, depending on the functions involved. It should also be noted that each block in the structure diagram and / or flowchart, as well as the combination of blocks in the structure diagram and / or flowchart, can be implemented by a dedicated hardware-based system for performing the specified functions or actions, or can be implemented by a combination of dedicated hardware and computer instructions.
[0098] In addition, in each embodiment of the present application, each functional module or unit may be integrated together to form an independent part, or each module may exist alone, or two or more modules may be integrated to form an independent part.
[0099] If the above function is implemented in the form of a software functional module and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on such an understanding, the technical solution of the present application, in essence, or the part that contributes to the prior art, or a part of this technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions for causing a terminal device (which may be a smart phone, a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the methods described in each embodiment of the present application.
[0100] As described above, the above is only the specific implementation manner of the present application, but the protection scope of the present application is not limited thereto. Any person skilled in the art within the technical scope disclosed by the present application can easily think of changes or substitutions, which should all be covered within the protection scope of the present application.
Claims
1. A file address management method, characterized in that: The method comprises: Determine the partition type to which the target file belongs according to the attribute information of the target file; According to the partition type, obtaining the logical block address of the partition to which the target file belongs on the physical storage device; Constructing a data structure, storing the logical block address of the partition to which the target file belongs on the physical storage device into the data structure to form physical layout information; According to the partition type, the logical block address of the target file on the physical storage device is obtained from the physical layout information.
2. The file address management method according to claim 1, characterized in that: The step of determining the partition type to which the target file belongs according to the attribute information of the target file includes: According to the attribute information of the target file, obtaining the device number of the block device to which the target file belongs; Determining path information corresponding to the block device according to the device number; The partition type to which the target file belongs is obtained according to the path information corresponding to the block device.
3. The file address management method according to claim 2, characterized in that: The obtaining, according to the path information corresponding to the block device, the partition type to which the target file belongs, comprises: When the path information of the block device includes the first field, determining that the partition type to which the target file belongs is a physical partition; When the path information of the block device includes the second field, it is determined that the partition type to which the target file belongs is a logical partition.
4. The file address management method according to claim 3, characterized in that: When the path information of the block device includes the second field, it also includes: When there is one and only one second field in the path information of the logical partition, determining that the partition is a single-mapped logical partition; When the path information of the logical partition includes a plurality of the second fields, it is determined that the partition is a secondary mapped logical partition.
5. The file address management method according to claim 3, characterized in that: The obtaining, according to the partition type, a logical block address of the partition to which the target file belongs on the physical storage device comprises: When the partition type to which the target file belongs is the physical partition, reading the corresponding path information according to the device number of the block device to which the target file belongs, and obtaining the starting logical block address and the ending logical block address of the physical partition; When the partition type to which the target file belongs is the logical partition, the device path of the corresponding logical partition is determined according to the device number of the block device to which the target file belongs, the mapping relationship of the device is parsed according to the device path, and the logical block address of the partition to which the target file belongs is obtained according to the mapping relationship.
6. The file address management method according to claim 5, characterized in that: The obtaining the logical block address of the partition to which the target file belongs according to the mapping relationship includes: When the partition type is the logical partition mapped once, determining the corresponding logical partition device path according to the device number of the file; reading the device path of the physical partition according to the device path; determining the corresponding node information according to the device path of the physical partition to obtain the starting logical block address and the ending logical block address of the physical partition; When the partition type is the secondary mapped logical partition, the corresponding logical partition device path is determined according to the device number of the target file; the device path of the upper logical partition is read according to the device path of the secondary mapped logical partition, and the device path of the physical partition is read according to the device path of the upper logical partition; the corresponding node information is determined according to the device path of the physical partition to obtain the starting logical block address and the ending logical block address of the physical partition.
7. The file address management method according to claim 1, characterized in that: The constructing data structure stores the logical block address of the partition to which the target file belongs on the physical storage device into the data structure to form physical layout information, including: Defining the data structure to store the logical block address of the partition to which the target file belongs on the physical storage device; The preset interface is called cyclically to obtain all logical block addresses of the partition to which the target file belongs on the physical storage device, and the logical block addresses returned by each call are stored in the data structure to form the physical layout information.
8. The file address management method according to claim 4, characterized in that: The acquiring, according to the partition type, the logical block address of the target file on the physical storage device from the physical layout information comprises: When the partition type is a physical partition, the logical block address of the target file on the physical storage device is calculated according to the starting logical block address and the ending logical block address of the physical partition in combination with the physical layout information; When the partition type is a one-time mapped logical partition, according to the starting logical block address and the ending logical block address of the physical partition, based on the mapping table and in combination with the physical layout information, the logical block address of the target file on the physical storage device is calculated in segments; When the partition type is a secondary mapped logical partition and the logical partition is original proportional mapping, the logical block address of the target file on the physical storage device is calculated according to the starting logical block address and the ending logical block address of the physical partition, using the mapping table in combination with the physical layout information.
9. The file address management method according to claim 8, characterized in that: When the partition type is a physical partition, the logical block address of the target file on the physical storage device is calculated by the following formula: in, Indicates the nth logical block address pair of the target file in the physical storage device; Indicates the starting logical block address of the physical partition where the target file is located; Relative to the starting address The offset of + Indicates the logical block address of the target file on the physical storage device.
10. The file address management method according to claim 8, characterized in that: When the partition type is a single-mapped logical partition, the logical block address of the target file on the physical storage device is calculated using the following formula: The logical block address of each mapping area in the physical partition is calculated by the following formula: in, Indicates the mth dm mapping segment area; Indicates the logical block address range of the mth segment mapping area in the physical partition; Indicates the mapping offset of the mth segment within the logical partition; Indicates the logical block address range of the mth mapping segment area in the physical partition; Indicates the corresponding logical block address range of the same mapping segment area in the logical partition; The logical block address of the target file on the physical storage device is calculated by the following formula: in, Indicates a region of the target file on the physical partition, namely, the nth logical block address interval; Indicates the starting logical block address of the physical partition where the target file is located; ... Indicates the mapping start offset of each of the dm mapping segment areas in the physical partition; ... Indicates the mapping offset of the corresponding mapping segment area within the logical partition; Indicates the starting logical block address of the first dm mapping segment area in the physical partition, corresponding to the logical interval [ , ]; ... Indicates the offset of the target file in the logical partition.
11. A terminal device, characterized in that: The terminal device comprises a processor and a memory, wherein the memory stores a computer program, and the processor is used to execute the computer program to implement the file address management method according to any one of claims 1 to 10.
12. A computer-readable storage medium, characterized in that: It stores a computer program, which, when executed on a processor, implements the file address management method according to any one of claims 1-10.
Citation Information
Patent Citations
Firmware storage and reading method and device based on LVGL
CN113327639A
Method and device for reading data in storage medium, electronic equipment and medium
CN114968121A
Partition management method and device, terminal equipment and readable storage medium
CN118227231A
Memory data processing method and device, electronic equipment and storage medium
CN118819380A
Metadata grouping for un-map techniques
US20210065766A1